From 98a711fa5107a1214ec92726e25415a787147950 Mon Sep 17 00:00:00 2001 From: Joe Walker Date: Wed, 24 Nov 2010 14:14:31 +0000 Subject: [PATCH] rationalize set|unset commands into a commandSpec and startup|shutdown config --- plugins/pilot/lib/commands/settings.js | 176 +++++++++++++++++-------- plugins/pilot/lib/settings.js | 71 ++-------- 2 files changed, 135 insertions(+), 112 deletions(-) diff --git a/plugins/pilot/lib/commands/settings.js b/plugins/pilot/lib/commands/settings.js index 3f781839..cfc5edd1 100644 --- a/plugins/pilot/lib/commands/settings.js +++ b/plugins/pilot/lib/commands/settings.js @@ -1,13 +1,3 @@ -require.def(['require', 'exports', 'module', - 'skywriter/plugins', - 'settings/environment', - 'settings/settings' -], function(require, exports, module, - plugins, - environment, - settingsMod -) { - /* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * @@ -45,60 +35,136 @@ require.def(['require', 'exports', 'module', * * ***** END LICENSE BLOCK ***** */ -var catalog = plugins.catalog; -var env = environment.env; - -var settings = settingsMod.settings; +define(function(require, exports, module) { /** - * 'set' command + * Something of a hack to allow the set command to give a clearer definition + * of the type to the command line. */ -exports.setCommand = function(args, request) { - var html; +var valueDeferredType = { + name: "deferred", + undeferType: function(typeSpec, env) { + var assignments = typeSpec.assignments; + var replacement = 'text'; - if (!args.setting) { - var settingsList = settings._list(); - html = ''; - // first sort the settingsList based on the key - settingsList.sort(function(a, b) { - if (a.key < b.key) { - return -1; - } else if (a.key == b.key) { - return 0; - } else { - return 1; + if (assignments) { + // Find the assignment for 'setting' so we can get it's value + var settingAssignment = null; + assignments.forEach(function(assignment) { + if (assignment.param.name === 'setting') { + settingAssignment = assignment; + } + }); + + if (settingAssignment) { + var settingName = settingAssignment.value; + if (settingName && settingName !== '') { + var settingExt = settings[settingName]; + if (settingExt) { + replacement = settingExt.type; + } + } } - }); - - settingsList.forEach(function(setting) { - html += '' + - setting.key + - ' = ' + - setting.value + - '
'; - }); - } else { - if (args.value === undefined) { - html = '' + args.setting + ' = ' + settings.get(args.setting); - } else { - html = 'Setting: ' + args.setting + ' = ' + args.value; - settings.set(args.setting, args.value); } + + return replacement; } - - request.done(html); }; -/** - * 'unset' command - */ -exports.unsetCommand = function(args, request) { - settings.resetValue(args.setting); - request.done('Reset ' + args.setting + ' to default: ' + settings.get(args.setting)); +var setCommandSpec = { + name: "set", + params: [ + { + name: "setting", + type: { + name: "selection", + getOptions: function(env) { + return env.settings.getSettingNames(); + } + }, + description: "The name of the setting to display or alter", + defaultValue: null + }, + { + name: "value", + type: valueDeferredType, + description: "The new value for the chosen setting", + defaultValue: null + } + ], + description: "define and show settings", + exec: function(env, args, request) { + var html; + if (!args.setting) { + var settingsList = env.settings._list(); + html = ''; + // first sort the settingsList based on the key + settingsList.sort(function(a, b) { + if (a.key < b.key) { + return -1; + } else if (a.key == b.key) { + return 0; + } else { + return 1; + } + }); + var url = "https://wiki.mozilla.org/Labs/Skywriter/Settings#" + + setting.key; + settingsList.forEach(function(setting) { + html += '' + + setting.key + + ' = ' + + setting.value + + '
'; + }); + } else { + if (args.value === undefined) { + html = '' + args.setting + ' = ' + + env.settings.get(args.setting); + } else { + html = 'Setting: ' + args.setting + ' = ' + + args.value; + env.settings.set(args.setting, args.value); + } + } + request.done(html); + } }; +var unsetCommandSpec = { + name: "unset", + params: [ + { + name: "setting", + type: { + name: "selection", + pointer: "settings:index#getSettings" + }, + description: "The name of the setting to return to defaults" + } + ], + description: "unset a setting entirely", + exec: function(env, args, request) { + env.settings.resetValue(args.setting); + request.done('Reset ' + args.setting + ' to default: ' + + env.settings.get(args.setting)); + } +}; + +var canon = require('pilot/canon'); + +exports.startup = function(data, reason) { + canon.addCommand(setCommandSpec); + canon.addCommand(unsetCommandSpec); +}; + +exports.shutdown = function(data, reason) { + canon.removeCommand(setCommandSpec); + canon.removeCommand(unsetCommandSpec); +}; + + }); diff --git a/plugins/pilot/lib/settings.js b/plugins/pilot/lib/settings.js index d0d0210b..1f7ed2ea 100644 --- a/plugins/pilot/lib/settings.js +++ b/plugins/pilot/lib/settings.js @@ -37,77 +37,34 @@ * * ***** END LICENSE BLOCK ***** */ +define(function(require, exports, module) { + /** * This plug-in manages settings. */ -define(function(require, exports, module) { - var console = require("pilot/console"); var oop = require("pilot/oop").oop; var types = require("pilot/types"); var EventEmitter = require("pilot/event_emitter").EventEmitter; +var catalog = require("pilot/catalog"); + +var settingExtensionSpec = { + name: "setting", + description: "A setting is something that the application offers as a " + + "way to customize how it works", + register: "env.settings.addSetting", + indexOn: "name" +}; exports.startup = function(data, reason) { - // TODO add extension point in new style - // catalog.addExtensionPoint("setting", { - // "description": - // "A setting is something that the application offers as a way to customize how it works", - // "register": "index#addSetting", - // "indexOn": "name" - // }); - // catalog.addExtensionPoint("settingChange", { - // "description": - // "A settingChange is a way to be notified of changes to a setting" - // }); - - // TODO add commands in new style - // catalog.connect("command", module.id, { - // "name": "set", - // "params": [ - // { - // "name": "setting", - // "type": { - // "name": "selection", - // "pointer": "settings:index#getSettings" - // }, - // "description": "The name of the setting to display or alter", - // "defaultValue": null - // }, - // { - // "name": "value", - // "type": { - // "name": "deferred", - // "pointer": "settings:index#getTypeSpecFromAssignment" - // }, - // "description": "The new value for the chosen setting", - // "defaultValue": null - // } - // ], - // "description": "define and show settings", - // "pointer": "commands#setCommand" - // }); - // catalog.connect("command", module.id, { - // "name": "unset", - // "params": [ - // { - // "name": "setting", - // "type": { - // "name": "selection", - // "pointer": "settings:index#getSettings" - // }, - // "description": "The name of the setting to return to defaults" - // } - // ], - // "description": "unset a setting entirely", - // "pointer": "commands#unsetCommand" - // }); + catalog.addExtensionSpec(settingExtensionSpec); }; exports.shutdown = function(data, reason) { + catalog.removeExtensionSpec(settingExtensionSpec); }; - /** * A base class for all the various methods of storing settings. *

Usage: @@ -313,7 +270,7 @@ Settings.prototype = { oop.implement(Settings.prototype, EventEmitter); -exports.settings = new Settings(new MemoryPersister()); +exports.settings = new Settings(); /** * Save the settings in a cookie