Mercurial > snowl
changeset 176:0e5d80b88d1b
collection object rearch: genericize the setting of conditions on collections, and distinguish between conditions that define the initial state of the collection (called constraints) and those that temporarily further refine the results returned by the collection (called filters)
author | Myk Melez <myk@mozilla.org> |
---|---|
date | Thu, 17 Jul 2008 17:53:58 -0700 |
parents | 5409a8759186 |
children | 06a3197f71b1 |
files | extension/content/sidebar.js extension/content/snowl.js extension/modules/collection.js |
diffstat | 3 files changed, 48 insertions(+), 158 deletions(-) [+] |
line wrap: on
line diff
--- a/extension/content/sidebar.js Thu Jul 17 16:25:18 2008 -0700 +++ b/extension/content/sidebar.js Thu Jul 17 17:53:58 2008 -0700 @@ -236,7 +236,7 @@ nameColumn: "sources.name", uriColumn: "sources.humanURI" } - let collection = new SnowlCollection(null, null, null, null, null, null, grouping); + let collection = new SnowlCollection(null, null, grouping); collection.name = "Sources"; collection.faviconURI = URI.get("chrome://snowl/content/icons/rainbow.png"); this._collections = [collection];
--- a/extension/content/snowl.js Thu Jul 17 16:25:18 2008 -0700 +++ b/extension/content/snowl.js Thu Jul 17 17:53:58 2008 -0700 @@ -175,38 +175,37 @@ }, onFilter: function() { - this._collection.filter = this._filter.value; - this._rebuildView(); + this._applyFilters(); }, onCommandCurrentButton: function(aEvent) { - this._collection.current = this._currentButton.checked ? true : undefined; - this._rebuildView(); + this._applyFilters(); }, onCommandUnreadButton: function(aEvent) { - // FIXME: instead of rebuilding from scratch each time, when going from + // XXX Instead of rebuilding from scratch each time, when going from // all to unread, simply hide the ones that are read (f.e. by setting a CSS - // class on read items and then using a CSS rule to hide them). - this._collection.read = this._unreadButton.checked ? false : undefined; - this._rebuildView(); + // class on read items and then using a CSS rule to hide them)? + this._applyFilters(); }, - _group: "source", - setGroup: function(group) { - this._group = group; - }, + _applyFilters: function() { + let filters = []; + + if (this._currentButton.checked) + filters.push({ expression: "current = 1", parameters: {} }); + + if (this._unreadButton.checked) + filters.push({ expression: "read = 0", parameters: {} }); - setGroupID: function(aGroupID) { - if (this._group == "source") { - this._collection.sourceID = aGroupID; - this._collection.authorID = null; - } - else if (this._group == "person") { - this._collection.authorID = aGroupID; - this._collection.sourceID = null; - } + // FIXME: use a left join here once the SQLite bug breaking left joins to + // virtual tables has been fixed (i.e. after we upgrade to SQLite 3.5.7+). + if (this._filter.value) + filters.push({ expression: "messages.id IN (SELECT messageID FROM parts WHERE content MATCH :filter)", + parameters: { filter: this._filter.value } }); + this._collection.filters = filters; + this._collection.invalidate(); this._rebuildView(); },
--- a/extension/modules/collection.js Thu Jul 17 16:25:18 2008 -0700 +++ b/extension/modules/collection.js Thu Jul 17 17:53:58 2008 -0700 @@ -26,13 +26,9 @@ /** * A group of messages. */ -function SnowlCollection(aSourceID, aFilter, aCurrent, aRead, aAuthorID, conditions, grouping) { - this._sourceID = aSourceID; - this._authorID = aAuthorID; - this._filter = aFilter; - this._current = aCurrent; - this._read = aRead; - this.conditions = conditions || []; +function SnowlCollection(constraints, filters, grouping) { + this.constraints = constraints || []; + this._filters = filters || []; this.grouping = grouping; } @@ -43,80 +39,17 @@ return this._log; }, - _sourceID: null, - - get sourceID() { - return this._sourceID; - }, - - set sourceID(newVal) { - if (this._sourceID == newVal) - return; - - this._sourceID = newVal; - this.invalidate(); - }, - - _authorID: null, + _filters: null, - get authorID() { - return this._authorID; - }, - - set authorID(newVal) { - if (this._authorID == newVal) - return; - - this._authorID = newVal; - this.invalidate(); - }, - - _filter: null, - - get filter() { - return this._filter; + get filters() { + return this._filters; }, - set filter(newVal) { - if (this._filter == newVal) - return; - - this._filter = newVal; + set filters(newVal) { + this._filters = newVal; this.invalidate(); }, - _current: undefined, - - get current() { - return this._current; - }, - - set current(newValue) { - if (this._current === newValue) - return; - - this._current = (typeof newValue == "undefined") ? undefined : newValue ? true : false; - - // Invalidate the message cache. - this._messages = null; - }, - - _read: undefined, - - get read() { - return this._read; - }, - - set read(newValue) { - if (this._read === newValue) - return; - - this._read = (typeof newValue == "undefined") ? undefined : newValue ? true : false; - - // Invalidate the message cache. - this._messages = null; - }, - //**************************************************************************// // Grouping @@ -137,9 +70,10 @@ try { while (statement.step()) { // FIXME: base this on the current collection's conditions array. - let conditions = [ { expression: this.grouping.nameColumn + " = :groupValue", - parameters: { groupValue: statement.row.name } } ]; - let group = new SnowlCollection(this.sourceID, this.filter, this.current, this.read, this.authorID, conditions); + let constraints = [constraint for each (constraint in this.constraints)]; + constraints.push({ expression: this.grouping.nameColumn + " = :groupValue", + parameters: { groupValue: statement.row.name } }); + let group = new SnowlCollection(constraints); group.name = statement.row.name; group.uri = URI.get(statement.row.uri); groups.push(group); @@ -161,7 +95,11 @@ "FROM sources JOIN messages ON sources.id = messages.sourceID " + "LEFT JOIN people AS authors ON messages.authorID = authors.id"; - let conditions = this._generateConditions(); + let conditions = []; + + for each (let condition in this.constraints) + conditions.push(condition.expression); + if (conditions.length > 0) query += " WHERE " + conditions.join(" AND "); @@ -171,41 +109,13 @@ let statement = SnowlDatastore.createStatement(query); - if (this.sourceID) - statement.params.sourceID = this.sourceID; - - if (this.authorID) - statement.params.authorID = this.authorID; - - if (this.filter) - statement.params.filter = this.filter; + for each (let condition in this.constraints) + for (let [name, value] in Iterator(condition.parameters)) + statement.params[name] = value; return statement; }, - _generateConditions: function() { - let conditions = []; - - if (this.sourceID) - conditions.push("messages.sourceID = :sourceID"); - - if (this.authorID) - conditions.push("messages.authorID = :authorID"); - - // FIXME: use a left join here once the SQLite bug breaking left joins to - // virtual tables has been fixed (i.e. after we upgrade to SQLite 3.5.7+). - if (this.filter) - conditions.push("messages.id IN (SELECT messageID FROM parts WHERE content MATCH :filter)"); - - if (typeof this.current != "undefined") - conditions.push("current = " + (this.current ? "1" : "0")); - - if (typeof this.read != "undefined") - conditions.push("read = " + (this.read ? "1" : "0")); - - return conditions; - }, - //**************************************************************************// // Retrieval @@ -291,24 +201,10 @@ let conditions = []; - if (this.sourceID) - conditions.push("messages.sourceID = :sourceID"); - - if (this.authorID) - conditions.push("messages.authorID = :authorID"); + for each (let condition in this.constraints) + conditions.push(condition.expression); - // FIXME: use a left join here once the SQLite bug breaking left joins to - // virtual tables has been fixed (i.e. after we upgrade to SQLite 3.5.7+). - if (this.filter) - conditions.push("messages.id IN (SELECT messageID FROM parts WHERE content MATCH :filter)"); - - if (typeof this.current != "undefined") - conditions.push("current = " + (this.current ? "1" : "0")); - - if (typeof this.read != "undefined") - conditions.push("read = " + (this.read ? "1" : "0")); - - for each (let condition in this.conditions) + for each (let condition in this.filters) conditions.push(condition.expression); if (conditions.length > 0) @@ -318,16 +214,11 @@ let statement = SnowlDatastore.createStatement(query); - if (this.sourceID) - statement.params.sourceID = this.sourceID; + for each (let condition in this.constraints) + for (let [name, value] in Iterator(condition.parameters)) + statement.params[name] = value; - if (this.authorID) - statement.params.authorID = this.authorID; - - if (this.filter) - statement.params.filter = this.filter; - - for each (let condition in this.conditions) + for each (let condition in this.filters) for (let [name, value] in Iterator(condition.parameters)) statement.params[name] = value;