Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/autonome/Tweez. A Firefox add-on for quick-tagging via the keyboard: https://addons.mozilla.org/en- autonome.wordpress.com
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273/* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Tag Autocomplete Search. * * The Initial Developer of the Original Code is * Adam Souzis <adam@souzis.com> * Portions created by the Initial Developer are Copyright (C) 2007 * the Initial Developer. All Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */
const Ci = Components.interfaces;const Cc = Components.classes;const Cr = Components.results;const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
// Implements nsIAutoCompleteResultfunction TagAutoCompleteResult(searchString, searchResult, defaultIndex, errorDescription, results, comments) { this._searchString = searchString; this._searchResult = searchResult; this._defaultIndex = defaultIndex; this._errorDescription = errorDescription; this._results = results; this._comments = comments;}
TagAutoCompleteResult.prototype = { /** * The original search string */ get searchString() { return this._searchString; },
/** * The result code of this result object, either: * RESULT_IGNORED (invalid searchString) * RESULT_FAILURE (failure) * RESULT_NOMATCH (no matches found) * RESULT_SUCCESS (matches found) */ get searchResult() { return this._searchResult; },
/** * Index of the default item that should be entered if none is selected */ get defaultIndex() { return this._defaultIndex; },
/** * A string describing the cause of a search failure */ get errorDescription() { return this._errorDescription; },
/** * The number of matches */ get matchCount() { return this._results.length; },
/** * Get the value of the result at the given index */ getValueAt: function PTACR_getValueAt(index) { return this._results[index]; },
/** * Get the comment of the result at the given index */ getCommentAt: function PTACR_getCommentAt(index) { return this._comments[index]; },
/** * Get the style hint for the result at the given index */ getStyleAt: function PTACR_getStyleAt(index) { if (!this._comments[index]) return null; // not a category label, so no special styling
if (index == 0) return "suggestfirst"; // category label on first line of results
return "suggesthint"; // category label on any other line of results },
/** * Get the image for the result at the given index */ getImageAt: function PTACR_getImageAt(index) { return null; },
/** * Remove the value at the given index from the autocomplete results. * If removeFromDb is set to true, the value should be removed from * persistent storage as well. */ removeValueAt: function PTACR_removeValueAt(index, removeFromDb) { this._results.splice(index, 1); this._comments.splice(index, 1); },
QueryInterface: function(aIID) { if (!aIID.equals(Ci.nsIAutoCompleteResult) && !aIID.equals(Ci.nsISupports)) throw Components.results.NS_ERROR_NO_INTERFACE; return this; }};
// Implements nsIAutoCompleteSearchfunction TagAutoCompleteSearch() {}
TagAutoCompleteSearch.prototype = { _stopped : false,
get tagging() { let svc = Cc["@mozilla.org/browser/tagging-service;1"]. getService(Ci.nsITaggingService); this.__defineGetter__("tagging", function() svc); return this.tagging; },
/* * Search for a given string and notify a listener (either synchronously * or asynchronously) of the result * * @param searchString - The string to search for * @param searchParam - An extra parameter * @param previousResult - A previous result to use for faster searching * @param listener - A listener to notify when the search is complete */ startSearch: function PTACS_startSearch(searchString, searchParam, result, listener) { var searchResults = this.tagging.allTags; var results = []; var comments = []; this._stopped = false;
// only search on characters for the last tag var index = Math.max(searchString.lastIndexOf(","), searchString.lastIndexOf(";")); var before = ''; if (index != -1) { before = searchString.slice(0, index+1); searchString = searchString.slice(index+1); // skip past whitespace var m = searchString.match(/\s+/); if (m) { before += m[0]; searchString = searchString.slice(m[0].length); } }
if (!searchString.length) { var newResult = new TagAutoCompleteResult(searchString, Ci.nsIAutoCompleteResult.RESULT_NOMATCH, 0, "", results, comments); listener.onSearchResult(self, newResult); return; } var self = this; // generator: if yields true, not done function doSearch() { var i = 0; while (i < searchResults.length) { if (self._stopped) yield false; // for each match, prepend what the user has typed so far if (searchResults[i].indexOf(searchString) == 0 && comments.indexOf(searchResults[i]) == -1) { results.push(before + searchResults[i]); comments.push(searchResults[i]); } ++i; // 100 loops per yield if ((i % 100) == 0) { var newResult = new TagAutoCompleteResult(searchString, Ci.nsIAutoCompleteResult.RESULT_SUCCESS_ONGOING, 0, "", results, comments); listener.onSearchResult(self, newResult); yield true; } }
var newResult = new TagAutoCompleteResult(searchString, Ci.nsIAutoCompleteResult.RESULT_SUCCESS, 0, "", results, comments); listener.onSearchResult(self, newResult); yield false; } // chunk the search results via a generator var gen = doSearch(); function driveGenerator() { if (gen.next()) { var timer = Cc["@mozilla.org/timer;1"] .createInstance(Components.interfaces.nsITimer); self._callback = driveGenerator; timer.initWithCallback(self, 0, timer.TYPE_ONE_SHOT); } else { gen.close(); } } driveGenerator(); },
/** * nsITimer callback */ notify: function PTACS_notify(timer) { if (this._callback) this._callback(); },
/** * Stop an asynchronous search that is in progress */ stopSearch: function PTACS_stopSearch() { this._stopped = true; },
// nsISupports QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteSearch, Ci.nsITimerCallback]),
classDescription: "Tag AutoComplete", contractID: "@mozilla.org/autocomplete/search;1?name=tag-autocomplete", classID: Components.ID("{1dcc23b0-d4cb-11dc-9ad6-479d56d89593}"),};
var component = [TagAutoCompleteSearch];function NSGetModule(compMgr, fileSpec) { return XPCOMUtils.generateModule(component);}