From 15f260742e01c8a923cd3f4b48a416e13d41cec9 Mon Sep 17 00:00:00 2001 From: Felix Fung Date: Thu, 26 Jan 2012 16:10:00 -0500 Subject: [PATCH] Bug 275223 - Enable Tree View Columns to be Sorted in Page Info. r=db48x --- browser/base/content/pageinfo/pageInfo.js | 85 +++++++++++++++---- browser/base/content/pageinfo/pageInfo.xul | 22 +++-- browser/components/preferences/cookies.xul | 2 +- browser/components/preferences/jar.mn | 1 - browser/components/preferences/permissions.js | 14 ++- .../components/preferences/permissions.xul | 2 +- toolkit/content/jar.mn | 1 + .../content/treeUtils.js | 27 ++++-- 8 files changed, 116 insertions(+), 38 deletions(-) rename browser/components/preferences/permissionsutils.js => toolkit/content/treeUtils.js (88%) diff --git a/browser/base/content/pageinfo/pageInfo.js b/browser/base/content/pageinfo/pageInfo.js index 30c7f5accd5..0b9ef978723 100644 --- a/browser/base/content/pageinfo/pageInfo.js +++ b/browser/base/content/pageinfo/pageInfo.js @@ -42,17 +42,18 @@ # ***** END LICENSE BLOCK ***** //******** define a js object to implement nsITreeView -function pageInfoTreeView(copycol) +function pageInfoTreeView(treeid, copycol) { // copycol is the index number for the column that we want to add to // the copy-n-paste buffer when the user hits accel-c + this.treeid = treeid; this.copycol = copycol; this.rows = 0; this.tree = null; this.data = [ ]; this.selection = null; - this.sortcol = null; - this.sortdir = 0; + this.sortcol = -1; + this.sortdir = false; } pageInfoTreeView.prototype = { @@ -121,6 +122,25 @@ pageInfoTreeView.prototype = { } }, + onPageMediaSort : function(columnname) + { + var tree = document.getElementById(this.treeid); + var treecol = tree.columns.getNamedColumn(columnname); + + this.sortdir = + gTreeUtils.sort( + tree, + this, + this.data, + treecol.index, + function textComparator(a, b) { return a.toLowerCase().localeCompare(b.toLowerCase()); }, + this.sortcol, + this.sortdir + ); + + this.sortcol = treecol.index; + }, + getRowProperties: function(row, prop) { }, getCellProperties: function(row, column, prop) { }, getColumnProperties: function(column, prop) { }, @@ -166,9 +186,8 @@ const COPYCOL_META_CONTENT = 1; const COPYCOL_IMAGE = COL_IMAGE_ADDRESS; // one nsITreeView for each tree in the window -var gMetaView = new pageInfoTreeView(COPYCOL_META_CONTENT); -var gImageView = new pageInfoTreeView(COPYCOL_IMAGE); - +var gMetaView = new pageInfoTreeView('metatree', COPYCOL_META_CONTENT); +var gImageView = new pageInfoTreeView('imagetree', COPYCOL_IMAGE); var atomSvc = Components.classes["@mozilla.org/atom-service;1"] .getService(Components.interfaces.nsIAtomService); @@ -187,6 +206,44 @@ gImageView.getCellProperties = function(row, col, props) { props.AppendElement(this._ltrAtom); }; +gImageView.getCellText = function(row, column) { + var value = this.data[row][column.index]; + if (column.index == COL_IMAGE_SIZE) { + if (value == -1) { + return gStrings.unknown; + } else { + var kbSize = Number(Math.round(value / 1024 * 100) / 100); + return gBundle.getFormattedString("mediaFileSize", [kbSize]); + } + } + return value || ""; +}; + +gImageView.onPageMediaSort = function(columnname) { + var tree = document.getElementById(this.treeid); + var treecol = tree.columns.getNamedColumn(columnname); + + var comparator; + if (treecol.index == COL_IMAGE_SIZE) { + comparator = function numComparator(a, b) { return a - b; }; + } else { + comparator = function textComparator(a, b) { return a.toLowerCase().localeCompare(b.toLowerCase()); }; + } + + this.sortdir = + gTreeUtils.sort( + tree, + this, + this.data, + treecol.index, + comparator, + this.sortcol, + this.sortdir + ); + + this.sortcol = treecol.index; +}; + var gImageHash = { }; // localized strings (will be filled in when the document is loaded) @@ -585,15 +642,8 @@ function addImage(url, type, alt, elem, isBg) catch(ex2) { } } - var sizeText; - if (cacheEntryDescriptor) { - var pageSize = cacheEntryDescriptor.dataSize; - var kbSize = formatNumber(Math.round(pageSize / 1024 * 100) / 100); - sizeText = gBundle.getFormattedString("mediaFileSize", [kbSize]); - } - else - sizeText = gStrings.unknown; - gImageView.addRow([url, type, sizeText, alt, 1, elem, isBg]); + var dataSize = (cacheEntryDescriptor) ? cacheEntryDescriptor.dataSize : -1; + gImageView.addRow([url, type, dataSize, alt, 1, elem, isBg]); // Add the observer, only once. if (gImageView.data.length == 1) { @@ -700,7 +750,10 @@ function getSelectedImage(tree) return null; // Only works if only one item is selected - var clickedRow = tree.currentIndex; + var clickedRow = tree.view.selection.currentIndex; + if (clickedRow == -1) + return null; + // image-node return gImageView.data[clickedRow][COL_IMAGE_NODE]; } diff --git a/browser/base/content/pageinfo/pageInfo.xul b/browser/base/content/pageinfo/pageInfo.xul index 3cb71a5ce85..e1f9bb122a6 100644 --- a/browser/base/content/pageinfo/pageInfo.xul +++ b/browser/base/content/pageinfo/pageInfo.xul @@ -66,6 +66,7 @@