events.js

来自「在线编辑器」· JavaScript 代码 · 共 384 行

JS
384
字号
/* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.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 Bespin. * * 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): *   Bespin Team (bespin@mozilla.com) * * ***** END LICENSE BLOCK ***** */ dojo.provide("bespin.events");dojo.require("bespin.util.util");// = Event Bus =//// Global home for event watching where it doesn't fit using the pattern// of custom events tied to components themselves such as://// * {{{bespin.cmd.commandline.Events}}}// * {{{bespin.client.settings.Events}}}// ** {{{ Event: bespin:editor:newfile }}} **// // Observe a request for a new file to be createdbespin.subscribe("bespin:editor:newfile", function(event) {    var project = event.project || _editSession.project;     var newfilename = event.newfilename || "new.txt";        if (typeof _files != "undefined") { // We got the file system object baybee        _files.newFile(project, newfilename, function() {            bespin.publish("bespin:editor:openfile:opensuccess", { file: {                name: newfilename,                content: " ",                timestamp: new Date().getTime()            }});        });            } else { // Shoot over to the editor        bespin.util.navigate.editor(project, newfilename);    }});// ** {{{ Event: bespin:editor:titlechange }}} **// // Observe a title change event and then... change the document.title!bespin.subscribe("bespin:editor:titlechange", function(event) {    var title;    if (event.filename) title = event.filename + ' - editing with Bespin';    else if (event.title) title = event.title;    else title = 'Bespin &raquo; Code in the Cloud';    document.title = title;});// ** {{{ Event: bespin:cmdline:executed }}} **// // Set the last command in the status windowbespin.subscribe("bespin:cmdline:executed", function(event) {    var commandname = event.command.name;    var args        = event.args;    dojo.byId('message').innerHTML = "last cmd: <span title='" + commandname + " " + args + "'>" + commandname + "</span>"; // set the status message area});// ** {{{ Event: bespin:editor:config:run }}} **// // Load the users config filebespin.subscribe("bespin:editor:config:run", function(event) {    // 1. load the file    //   project: _editSession.userproject,    //   filename: "config.js"    // 2. Take the contents and eval the code with a nice scope    _files.loadFile(bespin.userSettingsProject, "config.js", function(file) {        var scope = {            bespin: bespin,            include: function(file) {                dojo.require(file);            },            publish: function(topic, args) {                bespin.publish(topic, args);            },            subscribe: function(topic, args) {                bespin.subscribe(topic, args);            }        };        if (typeof _commandLine != "undefined") {            scope.commandLine = _commandLine;            scope.execute = function(cmd) {                _commandLine.executeCommand(cmd);            }        }        if (typeof _editor != "undefined")      scope.editor = _editor;        if (typeof _editSession != "undefined") scope.editSession = _editSession;        if (typeof _files != "undefined")       scope.files = _files;                if (typeof _server != "undefined")      scope.server = _server;        if (typeof _toolbar != "undefined")     scope.toolbar = _toolbar;                with (scope) { // wow, using with. crazy.            try {                eval(file.content);            } catch (e) {                _commandLine.showInfo("There is a error in your config.js:<br><br>" + e);            }        }    }, true);});// ** {{{ Event: bespin:editor:config:edit }}} **// // Open the users special config filebespin.subscribe("bespin:editor:config:edit", function(event) {    if (!bespin.userSettingsProject) {        bespin.publish("bespin:cmdline:showinfo", { msg: "You don't seem to have a user project. Sorry." });        return;    }    bespin.publish("bespin:editor:openfile", {        project: bespin.userSettingsProject,        filename: "config.js"    });});// ** {{{ Event: bespin:commands:load }}} **// // Create a new command in your special command directorybespin.subscribe("bespin:commands:load", function(event) {    var commandname = event.commandname;        if (!commandname) {        bespin.publish("bespin:cmdline:showinfo", { msg: "Please pass me a command name to load." });        return;    }    _files.loadFile(bespin.userSettingsProject, "commands/" + commandname + ".js", function(file) {        try {            eval('_commandLine.addCommands([' + file.content.replace(/\n/g, "") + '])');        } catch (e) {            bespin.publish("bespin:cmdline:showinfo", { msg: "Something is wrong about the command:<br><br>" + e });        }    }, true);});// ** {{{ Event: bespin:commands:edit }}} **// // Edit the given commandbespin.subscribe("bespin:commands:edit", function(event) {    var commandname = event.commandname;        if (!bespin.userSettingsProject) {        bespin.publish("bespin:cmdline:showinfo", { msg: "You don't seem to have a user project. Sorry." });        return;    }    if (!commandname) {        bespin.publish("bespin:cmdline:showinfo", { msg: "Please pass me a command name to edit." });        return;    }        bespin.publish("bespin:editor:forceopenfile", {        project: bespin.userSettingsProject,        filename: "commands/" + commandname + ".js",        content: "{\n    name: '" + commandname + "',\n    takes: [YOUR_ARGUMENTS_HERE],\n    preview: 'execute any editor action',\n    execute: function(self, args) {\n\n    }\n}"    });});// ** {{{ Event: bespin:commands:list }}} **// // List the custom commands that a user hasbespin.subscribe("bespin:commands:list", function(event) {    if (!bespin.userSettingsProject) {        bespin.publish("bespin:cmdline:showinfo", { msg: "You don't seem to have a user project. Sorry." });        return;    }    _server.list(bespin.userSettingsProject, 'commands/', function(commands) {        var output;                if (!commands || commands.length < 1) {            output = "You haven't installed any custom commands.<br>Want to <a href='https://wiki.mozilla.org/Labs/Bespin/Roadmap/Commands'>learn how?</a>";        } else {            output = "<u>Your Custom Commands</u><br/><br/>";                        output += dojo.map(dojo.filter(commands, function(file) {                return bespin.util.endsWith(file.name, '\\.js');            }), function(c) { return c.name.replace(/\.js$/, '') }).join("<br>");        }                bespin.publish("bespin:cmdline:showinfo", { msg: output });    });});// ** {{{ Event: bespin:commands:delete }}} **// // List the custom commands that a user hasbespin.subscribe("bespin:commands:delete", function(event) {    var commandname = event.commandname;    if (!bespin.userSettingsProject) {        bespin.publish("bespin:cmdline:showinfo", { msg: "You don't seem to have a user project. Sorry." });        return;    }    if (!commandname) {        bespin.publish("bespin:cmdline:showinfo", { msg: "Please pass me a command name to delete." });        return;    }    var commandpath = "commands/" + commandname + ".js";        _files.removeFile(bespin.userSettingsProject, commandpath, function() {        if (_editSession.checkSameFile(bespin.userSettingsProject, commandpath)) _editor.model.clear(); // only clear if deleting the same file        bespin.publish("bespin:cmdline:showinfo", { msg: 'Removed command: ' + commandname, autohide: true });    }, function(xhr) {        bespin.publish("bespin:cmdline:showinfo", {             msg: "Wasn't able to remove the command <b>" + commandname + "</b><br/><em>Error</em> (probably doesn't exist): " + xhr.responseText,             autohide: true         });    });});// ** {{{ Event: bespin:editor:preview }}} **// // Load the users config filebespin.subscribe("bespin:editor:preview", function(event) {    var filename = event.filename || _editSession.path;  // default to current page    var project  = event.project  || _editSession.project;     // Make sure to save the file first    bespin.publish("bespin:editor:savefile", {        filename: filename    });    if (filename)        window.open(bespin.util.path.combine("preview/at", project, filename));});// ** {{{ Event: bespin:editor:closefile }}} **// // Load the users config filebespin.subscribe("bespin:editor:closefile", function(event) {    var filename = event.filename || _editSession.path;  // default to current page    var project  = event.project  || _editSession.project;           _files.closeFile(project, filename, function() {        bespin.publish("bespin:editor:closedfile", { filename: filename });                 // if the current file, move on to a new one        if (filename == _editSession.path) bespin.publish("bespin:editor:newfile");            bespin.publish("bespin:cmdline:showinfo", { msg: 'Closed file: ' + filename });    });});// ** {{{ Event: bespin:directory:create }}} **// // Create a new directorybespin.subscribe("bespin:directory:create", function(event) {    var project = event.project || _editSession.project;    var path = event.path || '';         _files.makeDirectory(project, path, function() {        if (path == '') bespin.publish("bespin:project:set", { project: project });        bespin.publish("bespin:cmdline:showinfo", {             msg: 'Successfully created directory: [project=' + project + ', path=' + path + ']', autohide: true });    }, function() {        bespin.publish("bespin:cmdline:showinfo", {            msg: 'Unable to delete directory: [project=' + project + ', path=' + path + ']' + project, autohide: true });    });});bespin.subscribe("bespin:directory:delete", function(event) {    var project = event.project || _editSession.project;    var path = event.path || '';        if (project == bespin.userSettingsProject && path == '/') return; // don't delete the settings project        _files.removeDirectory(project, path, function() {        if (path == '/') bespin.publish("bespin:project:set", { project: '' }); // reset        bespin.publish("bespin:cmdline:showinfo", {             msg: 'Successfully deleted directory: [project=' + project + ', path=' + path + ']', autohide: true });    }, function() {        bespin.publish("bespin:cmdline:showinfo", {            msg: 'Unable to delete directory: [project=' + project + ', path=' + path + ']', autohide: true });    });});// ** {{{ Event: bespin:project:create }}} **// // Create a new projectbespin.subscribe("bespin:project:create", function(event) {    var project = event.project || _editSession.project;        bespin.publish("bespin:directory:create", { project: project });});// ** {{{ Event: bespin:project:delete }}} **// // Create a new projectbespin.subscribe("bespin:project:delete", function(event) {    var project = event.project;    if (!project || project == bespin.userSettingsProject) return; // don't delete the settings project        bespin.publish("bespin:directory:delete", { project: project });});// ** {{{ Event: bespin:project:delete }}} **// // Create a new projectbespin.subscribe("bespin:project:rename", function(event) {    var currentProject = event.currentProject;    var newProject = event.newProject;    if ( (!currentProject || !newProject) || (currentProject == newProject) ) return;        _server.renameProject(currentProject, newProject, {        call: function() {            bespin.publish("bespin:project:set", { project: newProject });        },        onFailure: function(xhr) {            bespin.publish("bespin:cmdline:showinfo", { msg: 'Unable to rename project from ' + currentProject + " to " + newProject + "<br><br><em>Are you sure that the " + currentProject + " project exists?</em>", autohide: true });        }    });});// ** {{{ Event: bespin:project:import }}} **// // Create a new projectbespin.subscribe("bespin:project:import", function(event) {    var project = event.project;    var url = event.url;    _server.importProject(project, url, { call: function() {        bespin.publish("bespin:cmdline:showinfo", { msg: "Project " + project + " imported from:<br><br>" + url, autohide: true });    }, onFailure: function(xhr) {        bespin.publish("bespin:cmdline:showinfo", { msg: "Unable to import " + project + " from:<br><br>" + url + ".<br><br>Maybe due to: " + xhr.responseText });    }});});// == Events// // ** {{{ bespin.events }}} **//// Helpers for the event subsystem// ** {{{ bespin.events.toFire }}} **//// Given an {{{eventString}}} parse out the arguments and configure an event object//// Example events://// * {{{bespin:cmdline:execute;name=ls,args=bespin}}}// * {{{bespin:cmdline:execute}}}     dojo.mixin(bespin.events, {    toFire: function(eventString) {        var event = {};        if (!eventString.indexOf(';')) { // just a plain command with no args            event.name = eventString;        } else { // split up the args            var pieces = eventString.split(';');            event.name = pieces[0];            event.args = bespin.util.queryToObject(pieces[1], ',');        }        return event;    }});

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?