Created
April 29, 2010 21:39
-
-
Save Gozala/384296 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Mutates noun to the adjective | |
* Adds dependency resolver | |
* Adds suggestion history cache | |
* Adds delay support for suggestions | |
*/ | |
CmdUtils.CreateAdjective = function CreateAdjective (noun) { | |
/** | |
* Checks all the dependencies and returns true if all of them are | |
* satisfied | |
* @returns {boolean} | |
*/ | |
noun.__defineGetter__('reliable', function relible(){ | |
for each (var dependency in this.dependencies) | |
if (!dependency.reliable) return false | |
return true; | |
}), | |
/** | |
* Delay which will happen before parser will satrt parseing input | |
* If input will change before suggestions will be canceled | |
* @type Integer number of miliseconds | |
*/ | |
noun.delay = noun.delay || 0; | |
/** | |
* Length length | |
*/ | |
noun.memory = noun.memory || 10; | |
/** | |
* This property is used to store timer id for the delayed suggestions | |
* @type {string} | |
*/ | |
noun._timerId = null; | |
/** | |
* Saving original suggest in order to be able to use it after mutation | |
*/ | |
noun._suggest = noun.suggest; | |
/** | |
* returns suggestions | |
*/ | |
noun.suggest = function suggest() { | |
if (this.reliable) { | |
Utils.clearTimeout(this._timerId); | |
var args = arguments; | |
if (this.delay == 0) | |
return noun._suggest.apply(this, args); | |
// If mutate | |
this._timerId = Utils.setTimeout(function(){ | |
noun._suggest.apply(noun, args); | |
}, this.delay); | |
// Workaround for async suggestions bug | |
// https://bugzilla.mozilla.org/show_bug.cgi?id=484615 | |
return [{ | |
text: 'BUG 484615', | |
summary: 'Workaround for bug 484615', | |
html: '<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=484615">BUG 484615</a>', | |
data: { | |
summary: "Ubiquity Asynchronous Noun Suggestions are not working", | |
internals: { | |
cf_blocking_fennec: "---", | |
priority: "--", | |
bug_id: 484615, | |
_multi_selects: [], | |
bug_file_loc: "http://groups.google.com/group/ubiquity-firefox/browse_thread/thread/d556c431e40ff9aa", | |
cclist_accessible: 1, | |
rep_platform: "x86", | |
product_id: 40, | |
creation_ts: "2009.03.21 17:46", | |
assigned_to: 298253, | |
short_desc: "Ubiquity Asynchronous Noun Suggestions are not working", | |
qa_contact: 247525, | |
everconfirmed: 0, | |
status_whiteboard: "", | |
bug_severity: "major", | |
bug_status: "UNCONFIRMED", | |
delta_ts: "2009-03-21 17:46:06", | |
version: "unspecified", | |
reporter_id: 295373, | |
component_id: 757, | |
resolution: "", | |
reporter_accessible: 1, | |
target_milestone: "--", | |
alias: {}, | |
op_sys: "Mac OS X" | |
}, | |
id: 484615, | |
last_change_time: new Date(1240328766122), | |
creation_time: new Date(1240328760122), | |
alias: "" | |
} | |
}]; | |
} | |
return []; | |
}; | |
/** | |
* Saving original default in order to be able to use it after mutation | |
*/ | |
noun._default = noun.default; | |
/** | |
* Proxy function for noun default that is enabled only in case if noun | |
* has default | |
* Used to prevent any actions if object is not reliable | |
* @returns {Array} | |
*/ | |
noun.default = !noun.default ? noun.default : function() { | |
if (this.reliable) { | |
return this._default.apply(this, arguments); | |
} | |
else | |
return []; | |
}; | |
/** | |
* If true suggestion data will be cached. | |
*/ | |
noun.cache = noun.cache || false; | |
/** | |
* History of suggestion | |
*/ | |
noun.history = function history(limit, callback, self) { | |
self = self || arguments.callee.caller; | |
limit = limit || this.memory; | |
var suggestions = []; | |
try { | |
suggestions = Utils.decodeJson(Application.prefs.getValue( | |
'ubiquity.adjectives.' + this._name + '.history', '[]' | |
)).slice(0, limit); | |
} catch(e) {} | |
if (this.cache) { | |
if (callback) | |
suggestions.forEach(function(suggestion) { | |
callback.call(self, suggestion); | |
}); | |
} else { | |
if (callback) | |
suggestions.forEach(function(suggestion) { | |
this.suggest(suggestion.text, suggestion.html, | |
function(data) { | |
callback.call(self, data); | |
} | |
); | |
}, this); | |
else | |
return suggestions.map(function(suggestion) { | |
return this._suggest(suggestion.text, suggestion.html)[0]; | |
}, this); | |
} | |
return suggestions; | |
}; | |
/** | |
* Adds suggestion to the history | |
*/ | |
noun.addHistory = function addHistory(suggestion) { | |
if (suggestion && suggestion.text) { | |
try { | |
var suggestions = Utils.decodeJson(Application.prefs.getValue( | |
'ubiquity.adjectives.' + this._name + '.history', '[]' | |
)).filter(function(element){ | |
return (element.text != suggestion.text); | |
}).slice(0, this.memory - 1); | |
suggestions.unshift(suggestion); | |
Application.prefs.setValue( | |
'ubiquity.adjectives.' + this._name + '.history', | |
Utils.encodeJson(suggestions) | |
); | |
} catch (e) { | |
Logger.log(e.toSource()); | |
} | |
} | |
}; | |
/** | |
* Removes suggestion from history | |
*/ | |
noun.removeHistory = function removeHistory(suggestion) { | |
if (suggestion) { | |
try { | |
var suggestions = Utils.decodeJson(Application.prefs.getValue( | |
'ubiquity.adjectives.' + this._name + '.history', '[]' | |
)).filter(function(element) { | |
return (element.text != suggestion.text); | |
}); | |
Application.prefs.setValue( | |
'ubiquity.adjectives.' + this._name + '.history', | |
Utils.encodeJson(suggestions) | |
); | |
} catch(e) { | |
Logger.log(e.toSource()); | |
} | |
} | |
}; | |
return noun; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment