Skip to content

Commit

Permalink
Merge remote branch 'arantius/addons'
Browse files Browse the repository at this point in the history
  • Loading branch information
arantius committed May 11, 2010
2 parents 681cc8a + ca5a1ed commit 5398a1e
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 49 deletions.
174 changes: 128 additions & 46 deletions content/addons.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/*
TODO: Add observer to put scripts installed, while the addons window is open,
into the list.
*/
// Globals.
var GM_config = GM_getConfig();

(function() {
// Override some built-in functions, with a closure reference to the original
// function, to either handle or delegate the call.
(function() {
var _origShowView = showView;
showView = function(aView) {
if ('userscripts' == aView) {
Expand All @@ -23,26 +21,76 @@ buildContextMenu = function(aEvent) {
_origBuildContextMenu(aEvent);
}
};

// Set up an "observer" on the config, to keep the displayed items up to date
// with their actual state.
var config = GM_config;
window.addEventListener("load", function() {
config.addObserver(observer);
}, false);
window.addEventListener("unload", function() {
config.removeObserver(observer);
}, false);

var observer = {
notifyEvent: function(script, event, data) {
if (event == "install") {
var item = greasemonkeyAddons.addScriptToList(script);
gExtensionsView.selectedItem = item;
return;
}

var listbox = gExtensionsView;
var node;
var scriptId = script.namespace + script.name;
for (var i = 0; node = listbox.childNodes[i]; i++) {
if (node.getAttribute('addonId') == scriptId) {
break;
}
}
if (!node) return;

switch (event) {
case "edit-enabled":
node.setAttribute('isDisabled', !data);
break;
case "uninstall":
listbox.removeChild(node);
break;
case "move":
listbox.removeChild(node);
listbox.insertBefore(node, listbox.childNodes[data]);
break;
}
}
};
})();

// Set event listeners.
window.addEventListener('load', function() {
greasemonkeyAddons.onAddonSelect();
gExtensionsView.addEventListener('select', greasemonkeyAddons.onAddonSelect, false);
}, false);
gExtensionsView.addEventListener(
'select', greasemonkeyAddons.onAddonSelect, false);

var greasemonkeyAddons={
gmSvc: Components.classes["@greasemonkey.mozdev.org/greasemonkey-service;1"]
.getService(Components.interfaces.gmIGreasemonkeyService)
.wrappedJSObject,
// Work-around for Stylish compatibility, which does not update gView in
// its overridden showView() function.
var stylishRadio = document.getElementById('userstyles-view');
if (stylishRadio) {
stylishRadio.addEventListener(
'command',
function() { gView = 'userstyles' },
false);
}
}, false);

var greasemonkeyAddons = {
showView: function() {
if ('userscripts' == gView) return;
updateLastSelected('userscripts');
gView='userscripts';

// Update any possibly modified scripts.
this.gmSvc.config.updateModifiedScripts();
GM_config.updateModifiedScripts();

// Hide the native controls that don't work in the user scripts view.
function $(id) { return document.getElementById(id); }
Expand All @@ -51,7 +99,9 @@ var greasemonkeyAddons={
'searchPanel', 'installFileButton', 'checkUpdatesAllButton',
'skipDialogButton', 'themePreviewArea', 'themeSplitter',
'showUpdateInfoButton', 'hideUpdateInfoButton',
'installUpdatesAllButton'];
'installUpdatesAllButton',
// Stylish injects these elements.
'copy-style-info', 'new-style'];
elementIds.forEach(hide);

var getMore = document.getElementById('getMore');
Expand All @@ -67,9 +117,7 @@ var greasemonkeyAddons={
},

fillList: function() {
// I'd much prefer to use the inbuilt templates/rules mechanism that the
// native FFX bits of this dialog use, but this works as P.O.C.
var config = GM_getConfig();
var config = GM_config;
var listbox = gExtensionsView;

// Remove any pre-existing contents.
Expand All @@ -79,36 +127,45 @@ var greasemonkeyAddons={

// Add a list item for each script.
for (var i = 0, script = null; script = config.scripts[i]; i++) {
var item = document.createElement('richlistitem');
item.setAttribute('class', 'userscript');
// Fake this for now.
var id = script.namespace + script.name;
// Setting these attributes inherits the values into the same place they
// would go for extensions.
item.setAttribute('addonId', id);
item.setAttribute('name', script.name);
item.setAttribute('description', script.description);
item.setAttribute('id', 'urn:greasemonkey:item:'+id);
item.setAttribute('isDisabled', !script.enabled);
// These hide extension-specific bits we don't want to display.
item.setAttribute('blocklisted', 'false');
item.setAttribute('blocklistedsoft', 'false');
item.setAttribute('compatible', 'true');
item.setAttribute('locked', 'false');
item.setAttribute('providesUpdatesSecurely', 'true');
item.setAttribute('satisfiesDependencies', 'true');
item.setAttribute('type', nsIUpdateItem.TYPE_EXTENSION);

// Place this item in the container.
listbox.appendChild(item);
greasemonkeyAddons.addScriptToList(script);
}
},

listitemForScript: function(script) {
var item = document.createElement('richlistitem');
item.setAttribute('class', 'userscript');
// Fake this for now.
var id = script.namespace + script.name;
// Setting these attributes inherits the values into the same place they
// would go for extensions.
item.setAttribute('addonId', id);
item.setAttribute('name', script.name);
item.setAttribute('description', script.description);
item.setAttribute('id', 'urn:greasemonkey:item:'+id);
item.setAttribute('isDisabled', !script.enabled);
// These hide extension-specific bits we don't want to display.
item.setAttribute('blocklisted', 'false');
item.setAttribute('blocklistedsoft', 'false');
item.setAttribute('compatible', 'true');
item.setAttribute('locked', 'false');
item.setAttribute('providesUpdatesSecurely', 'true');
item.setAttribute('satisfiesDependencies', 'true');
item.setAttribute('type', nsIUpdateItem.TYPE_EXTENSION);
return item;
},

addScriptToList: function(script, beforeNode) {
var item = greasemonkeyAddons.listitemForScript(script);
gExtensionsView.insertBefore(item, beforeNode || null);
return item;
},

findSelectedScript: function() {
var scripts = GM_getConfig().scripts;
if (!gExtensionsView.selectedItem) return null;
var scripts = GM_config.scripts;
var selectedScriptId = gExtensionsView.selectedItem.getAttribute('addonId');
for (var i = 0, script = null; script=scripts[i]; i++) {
if (selectedScriptId == script.namespace+script.name) {
for (var i = 0, script = null; script = scripts[i]; i++) {
if (selectedScriptId == script.namespace + script.name) {
return script;
}
}
Expand Down Expand Up @@ -172,17 +229,31 @@ var greasemonkeyAddons={
GM_openInEditor(script);
break;
case 'cmd_userscript_enable':
selectedListitem.setAttribute('isDisabled', 'false');
script.enabled = true;
break;
case 'cmd_userscript_disable':
selectedListitem.setAttribute('isDisabled', 'true');
script.enabled = false;
break;
case 'cmd_userscript_move_down':
GM_config.move(script, 1);
break;
case 'cmd_userscript_move_bottom':
GM_config.move(script, GM_config.scripts.length);
break;
case 'cmd_userscript_move_up':
GM_config.move(script, -1);
break;
case 'cmd_userscript_move_top':
GM_config.move(script, -1 * GM_config.scripts.length);
break;
case 'cmd_userscript_sort':
function scriptCmp(a, b) { return a.name < b.name ? -1 : 1; }
GM_config._scripts.sort(scriptCmp);
GM_config._save();
greasemonkeyAddons.fillList();
break;
case 'cmd_userscript_uninstall':
// todo, make this "true" configurable?
GM_getConfig().uninstall(script, true);
selectedListitem.parentNode.removeChild(selectedListitem);
GM_config.uninstall(script);
break;
}
},
Expand Down Expand Up @@ -213,5 +284,16 @@ var greasemonkeyAddons={
addMenuItem('Enable', 'cmd_userscript_enable');
}
addMenuItem('Uninstall', 'cmd_userscript_uninstall');

popup.appendChild(document.createElement('menuseparator'));

addMenuItem('Move Up', 'cmd_userscript_move_up');
addMenuItem('Move Down', 'cmd_userscript_move_down');
addMenuItem('Move To Top', 'cmd_userscript_move_top');
addMenuItem('Move To Bottom', 'cmd_userscript_move_bottom');

popup.appendChild(document.createElement('menuseparator'));

addMenuItem('Sort Scripts', 'cmd_userscript_sort');
}
};
7 changes: 7 additions & 0 deletions content/addons.xul
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
<command id="cmd_userscript_enable"/>
<command id="cmd_userscript_disable"/>
<command id="cmd_userscript_uninstall"/>

<command id="cmd_userscript_move_up"/>
<command id="cmd_userscript_move_down"/>
<command id="cmd_userscript_move_top"/>
<command id="cmd_userscript_move_bottom"/>

<command id="cmd_userscript_sort"/>
</commandset>
</window>

Expand Down
6 changes: 3 additions & 3 deletions content/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ Config.prototype = {

var existingIndex = this._find(script);
if (existingIndex > -1) {
this.uninstall(this._scripts[existingIndex], false);
this.uninstall(this._scripts[existingIndex]);
}

script._initFile(script._tempFile);
Expand Down Expand Up @@ -392,8 +392,8 @@ Config.prototype = {
*
* @param script The script to be moved.
* @param destination Can be either (a) a numeric offset for the script to be
* moved or (b) another installet script to which position
* the script will be moved.
* moved by, or (b) another installed script to which
* position the script will be moved.
*/
move: function(script, destination) {
var from = this._scripts.indexOf(script);
Expand Down

0 comments on commit 5398a1e

Please sign in to comment.