/* ***** 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 Mozilla Skywriter. * * The Initial Developer of the Original Code is * Mozilla. * Portions created by the Initial Developer are Copyright (C) 2009 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Joe Walker (jwalker@mozilla.com) * * 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 ***** */ define(function(require, exports, module) { var console = require('pilot/console'); var Trace = require('pilot/stacktrace').Trace; var oop = require('pilot/oop').oop; var EventEmitter = require('pilot/event_emitter').EventEmitter; var catalog = require('pilot/catalog'); var Status = require('pilot/types').Status; var types = require('pilot/types'); /* // TODO: this doesn't belong here - or maybe anywhere? var dimensionsChangedExtensionSpec = { name: 'dimensionsChanged', description: 'A dimensionsChanged is a way to be notified of ' + 'changes to the dimension of Skywriter' }; exports.startup = function(data, reason) { catalog.addExtensionSpec(commandExtensionSpec); }; exports.shutdown = function(data, reason) { catalog.removeExtensionSpec(commandExtensionSpec); }; */ var commandExtensionSpec = { name: 'command', description: 'A command is a bit of functionality with optional ' + 'typed arguments which can do something small like moving ' + 'the cursor around the screen, or large like cloning a ' + 'project from VCS.', indexOn: 'name' }; var env; exports.startup = function(data, reason) { // TODO: this is probably all kinds of evil, but we need something working env = data.env; catalog.addExtensionSpec(commandExtensionSpec); }; exports.shutdown = function(data, reason) { catalog.removeExtensionSpec(commandExtensionSpec); }; /** * Manage a list of commands in the current canon */ /** * A Command is a discrete action optionally with a set of ways to customize * how it happens. This is here for documentation purposes. * TODO: Document better */ var thingCommand = { name: 'thing', description: 'thing is an example command', params: [{ name: 'param1', description: 'an example parameter', type: 'text', defaultValue: null }], exec: function(env, args, request) { thing(); } }; var commands = {}; /** * This registration method isn't like other Ace registration methods because * it doesn't return a decorated command because there is no functional * decoration to be done. * TODO: Are we sure that in the future there will be no such decoration? */ function addCommand(command) { if (!command.name) { throw new Error('All registered commands must have a name'); } if (command.params == null) { command.params = []; } if (!Array.isArray(command.params)) { throw new Error('command.params must be an array in ' + command.name); } // Replace the type command.params.forEach(function(param) { if (!param.name) { throw new Error('In ' + command.name + ': all params must have a name'); } upgradeType(param); }, this); commands[command.name] = command; }; function upgradeType(param) { var lookup = param.type; param.type = types.getType(lookup); if (param.type == null) { throw new Error('In ' + command.name + '/' + param.name + ': can\'t find type for: ' + JSON.stringify(lookup)); } } function removeCommand(command) { if (typeof command === 'string') { delete commands[command]; } else { delete commands[command.name]; } }; function getCommand(name) { return commands[name]; }; function getCommandNames() { return Object.keys(commands); }; /** * Entry point for keyboard accelerators or anything else that knows * everything it needs to about the command params * @param command Either a command, or the name of one */ function exec(command, args, typed) { // TODO: Use requisition.toString rather than 'typed' if (typeof command === 'string') { command = commands[command]; } if (!command) { // TODO: Should we complain more than returning false? return false; } // TODO: Ugg. really? env.selection = env.editor.getSelection(); var request = new Request({ command: command, args: args, typed: typed }); command.exec(env, args || {}, request); return true; }; exports.removeCommand = removeCommand; exports.addCommand = addCommand; exports.getCommand = getCommand; exports.getCommandNames = getCommandNames; exports.exec = exec; exports.upgradeType = upgradeType; /** * We publish a 'output' event whenever new command begins output * TODO: make this more obvious */ oop.implement(exports, EventEmitter); /** * Current requirements are around displaying the command line, and provision * of a 'history' command and cursor up|down navigation of history. *

Future requirements could include: *

*

The execute() command doesn't really live here, except as part of that * last future requirement, and because it doesn't really have anywhere else to * live. */ /** * The array of requests that wish to announce their presence */ var requests = []; /** * How many requests do we store? */ var maxRequestLength = 100; /** * To create an invocation, you need to do something like this (all the ctor * args are optional): *

 * var request = new Request({
 *     command: command,
 *     args: args,
 *     typed: typed
 * });
 * 
* @constructor */ function Request(options) { options = options || {}; // Will be used in the keyboard case and the cli case this.command = options.command; // Will be used only in the cli case this.args = options.args; this.typed = options.typed; // Have we been initialized? this._begunOutput = false; this.start = new Date(); this.end = null; this.completed = false; this.error = false; }; oop.implement(Request.prototype, EventEmitter); /** * Lazy init to register with the history should only be done on output. * init() is expensive, and won't be used in the majority of cases */ Request.prototype._beginOutput = function() { this._begunOutput = true; this.outputs = []; requests.push(this); // This could probably be optimized with some maths, but 99.99% of the // time we will only be off by one, and I'm feeling lazy. while (requests.length > maxRequestLength) { requests.shiftObject(); } exports._dispatchEvent('output', { requests: requests, request: this }); }; /** * Sugar for: *
request.error = true; request.done(output);
*/ Request.prototype.doneWithError = function(content) { this.error = true; this.done(content); }; /** * Declares that this function will not be automatically done when * the command exits */ Request.prototype.async = function() { if (!this._begunOutput) { this._beginOutput(); } }; /** * Complete the currently executing command with successful output. * @param output Either DOM node, an SproutCore element or something that * can be used in the content of a DIV to create a DOM node. */ Request.prototype.output = function(content) { if (!this._begunOutput) { this._beginOutput(); } if (typeof content !== 'string' && !(content instanceof Node)) { content = content.toString(); } this.outputs.push(content); this._dispatchEvent('output', {}); return this; }; /** * All commands that do output must call this to indicate that the command * has finished execution. */ Request.prototype.done = function(content) { this.completed = true; this.end = new Date(); this.duration = this.end.getTime() - this.start.getTime(); if (content) { this.output(content); } this._dispatchEvent('output', {}); }; exports.Request = Request; });