Bug 752841 - [New Tab Page] Make the number of tabs adjustable; r=ttaubert

This commit is contained in:
Bellindira Castillo [:bellindira] 2012-08-16 23:01:03 -06:00
parent 749bba8724
commit 055134613f
7 changed files with 176 additions and 34 deletions

View File

@ -1128,6 +1128,12 @@ pref("browser.newtab.preload", false);
// Toggles the content of 'about:newtab'. Shows the grid when enabled.
pref("browser.newtabpage.enabled", true);
// number of rows of newtab grid
pref("browser.newtabpage.rows", 3);
// number of columns of newtab grid
pref("browser.newtabpage.columns", 3);
// Enable the DOM fullscreen API.
pref("full-screen-api.enabled", true);

View File

@ -22,17 +22,8 @@ let gGrid = {
/**
* All cells contained in the grid.
*/
get cells() {
let cells = [];
let children = this.node.querySelectorAll(".newtab-cell");
for (let i = 0; i < children.length; i++)
cells.push(new Cell(this, children[i]));
// Replace the getter with our cached value.
Object.defineProperty(this, "cells", {value: cells, enumerable: true});
return cells;
},
_cells: null,
get cells() this._cells,
/**
* All sites contained in the grid's cells. Sites may be empty.
@ -46,7 +37,7 @@ let gGrid = {
init: function Grid_init() {
this._node = document.getElementById("newtab-grid");
this._createSiteFragment();
this._draw();
this._render();
},
/**
@ -74,8 +65,8 @@ let gGrid = {
node.removeChild(child);
}, this);
// Draw the grid again.
this._draw();
// Render the grid again.
this._render();
},
/**
@ -92,6 +83,32 @@ let gGrid = {
this.node.removeAttribute("locked");
},
/**
* Creates the newtab grid.
*/
_renderGrid: function Grid_renderGrid() {
let row = document.createElementNS(HTML_NAMESPACE, "div");
let cell = document.createElementNS(HTML_NAMESPACE, "div");
row.classList.add("newtab-row");
cell.classList.add("newtab-cell");
// Clear the grid
this._node.innerHTML = "";
// Creates the structure of one row
for (let i = 0; i < gGridPrefs.gridColumns; i++) {
row.appendChild(cell.cloneNode(true));
}
// Creates the grid
for (let j = 0; j < gGridPrefs.gridRows; j++) {
this._node.appendChild(row.cloneNode(true));
}
// (Re-)initialize all cells.
let cellElements = this.node.querySelectorAll(".newtab-cell");
this._cells = [new Cell(this, cell) for (cell of cellElements)];
},
/**
* Creates the DOM fragment that is re-used when creating sites.
*/
@ -116,11 +133,10 @@ let gGrid = {
},
/**
* Draws the grid, creates all sites and puts them into their cells.
* Renders the sites, creates all sites and puts them into their cells.
*/
_draw: function Grid_draw() {
_renderSites: function Grid_renderSites() {
let cells = this.cells;
// Put sites into the cells.
let links = gLinks.getLinks();
let length = Math.min(links.length, cells.length);
@ -129,5 +145,24 @@ let gGrid = {
if (links[i])
this.createSite(links[i], cells[i]);
}
},
/**
* Renders the grid.
*/
_render: function Grid_render() {
if (this._shouldRenderGrid()) {
this._renderGrid();
}
this._renderSites();
},
_shouldRenderGrid : function Grid_shouldRenderGrid() {
let rowsLength = this._node.querySelectorAll(".newtab-row").length;
let cellsLength = this._node.querySelectorAll(".newtab-cell").length;
return (rowsLength != gGridPrefs.gridRows ||
cellsLength != (gGridPrefs.gridRows * gGridPrefs.gridColumns));
}
};

View File

@ -20,7 +20,8 @@ let {
allPages: gAllPages,
linkChecker: gLinkChecker,
pinnedLinks: gPinnedLinks,
blockedLinks: gBlockedLinks
blockedLinks: gBlockedLinks,
gridPrefs: gGridPrefs
} = NewTabUtils;
XPCOMUtils.defineLazyGetter(this, "gStringBundle", function() {

View File

@ -26,21 +26,6 @@
<div class="newtab-side-margin"/>
<div id="newtab-grid">
<div class="newtab-row">
<div class="newtab-cell"/>
<div class="newtab-cell"/>
<div class="newtab-cell"/>
</div>
<div class="newtab-row">
<div class="newtab-cell"/>
<div class="newtab-cell"/>
<div class="newtab-cell"/>
</div>
<div class="newtab-row">
<div class="newtab-cell"/>
<div class="newtab-cell"/>
<div class="newtab-cell"/>
</div>
</div>
<div class="newtab-side-margin"/>

View File

@ -28,6 +28,7 @@ _BROWSER_FILES = \
browser_newtab_bug725996.js \
browser_newtab_bug734043.js \
browser_newtab_bug735987.js \
browser_newtab_bug752841.js \
browser_newtab_bug765628.js \
head.js \
$(NULL)

View File

@ -0,0 +1,53 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
const PREF_NEWTAB_ROWS = "browser.newtabpage.rows";
const PREF_NEWTAB_COLUMNS = "browser.newtabpage.columns";
function runTests() {
let testValues = [
{row: 0, column: 0},
{row: -1, column: -1},
{row: -1, column: 0},
{row: 0, column: -1},
{row: 2, column: 4},
{row: 2, column: 5},
];
// Expected length of grid
let expectedValues = [1, 1, 1, 1, 8, 10];
// Values before setting new pref values (9 is the default value -> 3 x 3)
let previousValues = [9, 1, 1, 1, 1, 8];
let existingTab, existingTabGridLength, newTab, newTabGridLength;
yield addNewTabPageTab();
existingTab = gBrowser.selectedTab;
for (let i = 0; i < expectedValues.length; i++) {
gBrowser.selectedTab = existingTab;
existingTabGridLength = getGrid().cells.length;
is(existingTabGridLength, previousValues[i],
"Grid length of existing page before update is correctly.");
Services.prefs.setIntPref(PREF_NEWTAB_ROWS, testValues[i].row);
Services.prefs.setIntPref(PREF_NEWTAB_COLUMNS, testValues[i].column);
existingTabGridLength = getGrid().cells.length;
is(existingTabGridLength, expectedValues[i],
"Existing page grid is updated correctly.");
yield addNewTabPageTab();
newTab = gBrowser.selectedTab;
newTabGridLength = getGrid().cells.length;
is(newTabGridLength, expectedValues[i],
"New page grid is updated correctly.");
gBrowser.removeTab(newTab);
}
gBrowser.removeTab(existingTab);
Services.prefs.clearUserPref(PREF_NEWTAB_ROWS);
Services.prefs.clearUserPref(PREF_NEWTAB_COLUMNS);
}

View File

@ -24,6 +24,12 @@ XPCOMUtils.defineLazyGetter(this, "gPrincipal", function () {
// The preference that tells whether this feature is enabled.
const PREF_NEWTAB_ENABLED = "browser.newtabpage.enabled";
// The preference that tells the number of rows of the newtab grid.
const PREF_NEWTAB_ROWS = "browser.newtabpage.rows";
// The preference that tells the number of columns of the newtab grid.
const PREF_NEWTAB_COLUMNS = "browser.newtabpage.columns";
// The maximum number of results we want to retrieve from history.
const HISTORY_RESULTS_LIMIT = 100;
@ -183,6 +189,60 @@ let AllPages = {
Ci.nsISupportsWeakReference])
};
/**
* Singleton that keeps Grid preferences
*/
let GridPrefs = {
/**
* Cached value that tells the number of rows of newtab grid.
*/
_gridRows: null,
get gridRows() {
if (!this._gridRows) {
this._gridRows = Math.max(1, Services.prefs.getIntPref(PREF_NEWTAB_ROWS));
}
return this._gridRows;
},
/**
* Cached value that tells the number of columns of newtab grid.
*/
_gridColumns: null,
get gridColumns() {
if (!this._gridColumns) {
this._gridColumns = Math.max(1, Services.prefs.getIntPref(PREF_NEWTAB_COLUMNS));
}
return this._gridColumns;
},
/**
* Initializes object. Adds a preference observer
*/
init: function GridPrefs_init() {
Services.prefs.addObserver(PREF_NEWTAB_ROWS, this, false);
Services.prefs.addObserver(PREF_NEWTAB_COLUMNS, this, false);
},
/**
* Implements the nsIObserver interface to get notified when the preference
* value changes.
*/
observe: function GridPrefs_observe(aSubject, aTopic, aData) {
if (aData == PREF_NEWTAB_ROWS) {
this._gridRows = null;
} else {
this._gridColumns = null;
}
AllPages.update();
}
};
GridPrefs.init();
/**
* Singleton that keeps track of all pinned links and their positions in the
* grid.
@ -606,5 +666,6 @@ let NewTabUtils = {
allPages: AllPages,
linkChecker: LinkChecker,
pinnedLinks: PinnedLinks,
blockedLinks: BlockedLinks
blockedLinks: BlockedLinks,
gridPrefs: GridPrefs
};