diff options
author | Trevor Elliott <trevor@galois.com> | 2013-06-12 16:41:00 -0700 |
---|---|---|
committer | Trevor Elliott <trevor@galois.com> | 2013-06-12 16:41:00 -0700 |
commit | c7b493dcc46ac800ace472a580d5430a1b0c2e41 (patch) | |
tree | 1ae2ea9ad7b84b07387d41c2b6ca6c4eccc4366a /src/js/fiveui/js/settings.js | |
parent | 0decef2093c676d91b0b590a9b94f201c4995716 (diff) |
Group url patterns with rule set definitions
Diffstat (limited to 'src/js/fiveui/js/settings.js')
-rw-r--r-- | src/js/fiveui/js/settings.js | 283 |
1 files changed, 89 insertions, 194 deletions
diff --git a/src/js/fiveui/js/settings.js b/src/js/fiveui/js/settings.js index a17a9e0..96ec864 100644 --- a/src/js/fiveui/js/settings.js +++ b/src/js/fiveui/js/settings.js @@ -70,6 +70,43 @@ _.extend(fiveui.Settings.prototype, { }, /** + * @param {!number} id The nuber of the element to retrieve. + * @param {!string} listName The name of the portion of the localstorage hierarchy to search for id. + * @param {!function(number, !Object): *} fromJSON A deserialization function. + * + * @return {*} Either null, or the result of fromJSON. + */ + getById: function(id, listName, fromJSON) { + var obj = this.get(listName + '.' + id); + if (!obj) { + return null; + } + + return fromJSON(id, obj); + }, + + /** + * @param {!number} id The nuber of the element to retrieve. + * @param {!string} listName The name of the portion of the + * localstorage hierarchy to search for id. + */ + remById: function(id, listName) { + // remove it from the list of ids: + var ids = this.get(listName) || []; + + for (var i = 0; i < ids.length; ++i) { + if (ids[i] == id) { + ids.splice(i, 1); + this.set(listName, ids); + this.remove(listName + '.' + id); + break; + } + } + }, + + /** General Config **********************************************************/ + + /** * Set the display default. * * @param {!boolean} def Whether or not to display the FiveUI Window @@ -92,106 +129,39 @@ _.extend(fiveui.Settings.prototype, { return !!def; }, - /** - * Add a mapping from url patterns to RuleSets (via rule set ids) - * - * @param {!string} url_pat A regular expression (actually, a glob) to match URLs against. - * @param {!number} rule_id The id of the RuleSet to use with any matching URL. - * @return {!number} The id of the new url pattern. - */ - addUrl: function(url_pat, rule_id) { - var pats = this.getUrls(); - - var new_id = fiveui.utils.getNewId(pats); - - this.updateUrl(new_id, url_pat, rule_id); - - // add it to the patterns list - pats.push(new_id); - this.set('urls', pats); - - return new_id; - }, - - updateUrl: function(id, url_pat, rule_id) { - this.set('urls.' + id, new fiveui.UrlPat(id, url_pat, rule_id)); - return id; - }, - - /** - * Retrieve the list of url patterns. - * - * @return {Array.<number>} An ordered list of the currently active - * url patterns. - */ - getUrls: function() { - return (/** @type {Array.<number>} */ this.get('urls')) || []; - }, - /** - * Retrieve a specific url pattern. - * - * @param {!number} url_id The id of the url pattern to retrieve. - * @return {?fiveui.UrlPat} The matching UrlPat or null, - * if no pattern exists for url_id. - */ - getUrlPat: function(url_id) { - return /** @type {?fiveui.UrlPat} */ this.getById(url_id, 'urls', fiveui.UrlPat.fromJSON); - }, + /** Rule Sets ***************************************************************/ /** - * Remove a UrlPat from the persistent storage. + * Retrieve the list of rule set ids. * - * @param {!number} pat_id The id of the UrlPat to remove. - * @return {void} - */ - remUrlPat: function(pat_id) { - this.remById(pat_id, 'urls'); - }, - - /** - * @param {!string} url A url to compare against the list of ordered - * url patterns in local storage. - * @return {?fiveui.UrlPat} The matching pattern, or null, if no - * mapping was found. + * @return {!Array.<number>} An ordered list of the configured rule sets. */ - checkUrl: function(url) { - var pats = this.getUrls(); - - // check for a possible match - for (var i = 0; i < pats.length; ++i) { - var pat = this.getUrlPat(pats[i]); - if (pat.match(url)) { - return pat; - } - } - - return null; + getRuleSetIds: function() { + return (/** @type {!Array.<number>} */ this.get('ruleSet')) || []; }, /** - * Retrieve the list of rule sets. - * - * @return {!Array.<number>} An ordered list of the configured rule sets. + * Retrieve all rule set ids. */ - getRuleSets: function() { - return (/** @type {!Array.<number>} */ this.get('ruleSet')) || []; + getRuleSets:function() { + return _.map(this.getRuleSetIds(), _.bind(this.getRuleSet, this)); }, - /** * @param {!Object} ruleSet The new rule set, as an anonymous JSON object. - * @return {!fiveui.RuleSet} The new RuleSet object. + * @return {!fiveui.RuleSet} The id of the new rule set. */ addRuleSet: function(ruleSet) { - var ids = this.getRuleSets(); - var id = fiveui.utils.getNewId(ids); + var ids = this.getRuleSetIds(); + var id = fiveui.utils.getNewId(ids); + + this.updateRuleSet(id, ruleSet); - var newRS = this.updateRuleSet(id, ruleSet); ids.push(id); this.set('ruleSet', ids); - return newRS; + return id; }, /** @@ -199,12 +169,10 @@ _.extend(fiveui.Settings.prototype, { * * @param {!number} ruleSetId The id of the ruleset that is being modified. * @param {!Object} ruleSet The rule set, as an anonymous JSON object. - * @return {!fiveui.RuleSet} The new RuleSet object. */ updateRuleSet: function(ruleSetId, ruleSet) { var newRS = fiveui.RuleSet.fromJSON(ruleSetId, ruleSet); this.set('ruleSet.'+ruleSetId, newRS); - return newRS; }, /** @@ -212,79 +180,35 @@ _.extend(fiveui.Settings.prototype, { * @return {?fiveui.RuleSet} The RuleSet, or null, if no RuleSet was found. */ getRuleSet: function(id) { - return /** @type {?fiveui.RuleSet} */ this.getById(id, 'ruleSet', fiveui.RuleSet.fromJSON); + return this.getById(id, 'ruleSet', fiveui.RuleSet.fromJSON); }, /** * @param {!number} id The id of the rule set to remove. - * @return {!Array.<fiveui.UrlPat>} null if the remove succeeded, otherwise, - * returns the list of UrlPats that use this - * rule set, if any. */ remRuleSet: function(id) { - var matches = _.map(this.getRuleSetUrlPats(id), function(id) { - return this.getUrlPat(id); - }, this); - - if (0 == matches.length) { - this.remById(id, 'ruleSet'); - } - - return matches; + this.remById(id, 'ruleSet'); }, + /** URL Pattern Management **************************************************/ + /** - * @param {!number} ruleSetId The rule set to retrieve url patterns for. - * @return {Array.<number>} Url pattern ids associated with this rule set. + * Test a url agains the rule set database. Return the first rule set that + * matches, or null if none do. */ - getRuleSetUrlPats: function(ruleSetId) { - var urls = this.getUrls(); - var patIds = []; - - _.each(urls, function(patId) { - var pat = this.getUrlPat(patId); - if(pat.rule_id == ruleSetId) { - patIds.push(patId); - } - }, this); + checkUrl: function(url) { + return _.find(this.getRuleSets(), function(rs) { - return patIds; - }, + var pat = _.find(rs.patterns, function(pat) { + var regex = fiveui.UrlPat.compile(pat); + return regex.test(url); + }); - /** - * @param {!number} id The nuber of the element to retrieve. - * @param {!string} listName The name of the portion of the localstorage hierarchy to search for id. - * @param {!function(number, !Object): *} fromJSON A deserialization function. - * - * @return {*} Either null, or the result of fromJSON. - */ - getById: function(id, listName, fromJSON) { - var obj = this.get(listName + '.' + id); - if (!obj) { - return null; - } + return pat != null; - return fromJSON(id, obj); + }); }, - /** - * @param {!number} id The nuber of the element to retrieve. - * @param {!string} listName The name of the portion of the - * localstorage hierarchy to search for id. - */ - remById: function(id, listName) { - // remove it from the list of ids: - var ids = this.get(listName) || []; - - for (var i = 0; i < ids.length; ++i) { - if (ids[i] == id) { - ids.splice(i, 1); - this.set(listName, ids); - this.remove(listName + '.' + id); - break; - } - } - } }); @@ -297,41 +221,29 @@ fiveui.Settings.manager = function(chan, settings) { var msg = new fiveui.Messenger(chan); + // create a new rule set, and call the response continuation with the created + // object. msg.register('addRuleSet', function(ruleSet,respond){ - var newRS = settings.addRuleSet(ruleSet); - respond(newRS); + var id = settings.addRuleSet(ruleSet) + respond(settings.getRuleSet(id)); }); + // update a rule set, and call the response continuation with the updated + // object. msg.register('updateRuleSet', function(updatedRS,respond){ - var newRS = settings.updateRuleSet(updatedRS.id, updatedRS); - respond(newRS); + settings.updateRuleSet(updatedRS.id, updatedRS); + respond(settings.getRuleSet(updatedRS.id)); }); + // remove a rule set by id. the response continuation is called with no + // argument. msg.register('remRuleSet', function(ruleSetId, respond) { - var pats = settings.remRuleSet(ruleSetId); - respond({ - id: ruleSetId, - pats: pats, - removed: pats.length == 0 - }); - }); - - msg.register('getRuleSetUrlPats', function(ruleSetId, respond) { - var pats = settings.getUrls(); - var patIds = []; - - _.each(pats, function(patId) { - var pat = settings.getUrlPat(patId); - if(pat.rule_id == ruleSetId) { - patIds.push(patId); - } - }); - - respond(patIds); + settings.remRuleSet(ruleSetId); + respond(); }); // Retrieve the manifest, and return the object to the caller. Invokes the - // caller with `null` when the manifest fails to load. + // response continuation with an error object when rule set fails to load. msg.register('loadRuleSet', function(url, respond) { fiveui.RuleSet.load(url, { success:respond, @@ -342,44 +254,27 @@ fiveui.Settings.manager = function(chan, settings) { }); }); + // get a rule set structure by id. invoke the respond continuation with the + // rule set, if it exists, and null if it does not. msg.register('getRuleSet', function(ruleSetId, respond){ respond(settings.getRuleSet(ruleSetId)); }); + // Retrieve the list of all rule sets. invoke the respond continuation with + // the list of rule sets. msg.register('getRuleSets', function(unused, respond) { - var ruleSets = _.map(settings.getRuleSets(), - _.bind(settings.getRuleSet, settings)); - respond(ruleSets); - }); - - msg.register('getUrlPats', function(unused, respond){ - respond(_.map(settings.getUrls(), _.bind(settings.getUrlPat, settings))); - }); - - msg.register('addUrlPat', function(url, respond){ - var urlId = settings.addUrl(url.regex, url.rule_id); - respond(settings.getUrlPat(urlId)); - }); - - msg.register('updateUrlPat', function(pat, respond) { - var obj = settings.getUrlPat(pat.id); - settings.updateUrl(pat.id, pat.regex, pat.rule_id); - respond(pat); - }); - - msg.register('getUrlPat', function(urlPatId, respond){ - respond(settings.getUrlPat(urlPatId)); - }); - - msg.register('remUrlPat', function(urlPatId, respond){ - settings.remUrlPat(urlPatId); - respond(true); + respond(settings.getRuleSets()); }); - msg.register('setDisplayDefault', function(def) { + // sets the value of the 'display default' config option. invokes the respond + // callback with no argument. + msg.register('setDisplayDefault', function(def, respond) { settings.setDisplayDefault(def); + respond(); }); + // get the value of the 'display default' config option. invokes the respond + // callback with the value. msg.register('getDisplayDefault', function(ignored, respond) { respond(settings.getDisplayDefault()); }); |