⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.js

📁 在线编辑器
💻 JS
📖 第 1 页 / 共 2 页
字号:
dojo.provide("bespin.client.server");/* ***** 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 ***** */// = Server =//// The Server object implements the [[https://wiki.mozilla.org/BespinServerAPI|Bespin Server API]]// giving the client access to the backend store. The {{{FileSystem}}} object uses this to talk back.dojo.declare("bespin.client.Server", null, {    // ** {{{ initialize(base) }}}    //    // Object creation initialization    //    // * {{{base}}} is the base server URL to access    constructor: function(base) {        this.SERVER_BASE_URL = base || '';    },    // == Helpers ==    // ** {{{ request(method, url, payload, callbackOptions) }}}    //    // The core way to access the backend system.    // Similar to the Prototype Ajax.Request wrapper     //    // * {{{method}}} is the HTTP method (GET|POST|PUT|DELETE)    // * {{{url}}} is the sub url to hit (after the base url)    // * {{{payload}}} is what to send up for POST requests    // * {{{options}}} is how you pass in various callbacks.    //   options['evalJSON'] = true or false to auto eval    //   options['call'] = the main callback    //   options['log'] = just log the following    //   options['onFailure'] = call for general failures    //   options['on' + STATUS CODE] = call for specific failures    request: function(method, url, payload, options) {        var xhr = new XMLHttpRequest();        if (location.href.indexOf("file:") == 0){ // if developing and using this locally only!           try {               if (netscape.security.PrivilegeManager.enablePrivilege) {                   netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');               }           } catch (ex) {           }        }        if (options) { // do it async (best)            xhr.onreadystatechange = function() {                if (xhr.readyState == 4) {                    if (xhr.status && xhr.status != 0 && (xhr.status >= 200 && xhr.status < 300)) {                        var response = xhr.responseText;                                                if (options['evalJSON'] && response) {                            try {                                response = dojo.fromJson(response);                            } catch (syntaxException) {                                console.log("Couldn't eval the JSON: " + response + " (SyntaxError: " + syntaxException + ")");                            }                        }                                                if (dojo.isFunction(options['call'])) {                            options['call'](response, xhr);                        } else if (options['log']) {                            console.log(options['log']);                        }                    } else {                                                var onStatus = 'on' + xhr.status;                        if (options[onStatus]) {                            options[onStatus](xhr);                        } else if (options['onFailure']) {                            options['onFailure'](xhr);                        }                    }                }            }            xhr.open(method, this.SERVER_BASE_URL + url, true); // url must have leading /            xhr.setRequestHeader("Content-Type", 'application/x-www-form-urlencoded');            if (options.headers) {                for (var key in options.headers) {                    if (options.headers.hasOwnProperty(key)) {                        xhr.setRequestHeader(key, options.headers[key]);                    }                }            }            xhr.send(payload);        } else {            var fullUrl = this.SERVER_BASE_URL + url;            console.log("Are you sure you want to do a synchronous Ajax call? Really? " + fullUrl);            xhr.open(method, fullUrl, false);            xhr.send(payload);            return xhr.responseText;        }    },    // == USER ==    // ** {{{ login(user, pass, token, callback, notloggedin) }}}    //    // Try to login to the backend system.    //     // * {{{user}}} is the username    // * {{{pass}}} is the password    // * {{{onSuccess}}} fires when the user is logged in    // * {{{onFailure}}} fires when the user failed to login    login: function(user, pass, token, onSuccess, onFailure) {        var url = "/register/login/" + user;        this.request('POST', url, "password=" + escape(pass), {             call: onSuccess,            on401: onFailure,            log: 'Login complete.',            headers: { 'DoubleSubmitCookie':token }        });    },    // ** {{{ signup(user, pass, email, callback, notloggedin, userconflict) }}}    //    // Signup / Register the user to the backend system    //     // * {{{user}}} is the username    // * {{{pass}}} is the password    // * {{{email}}} is the email    // * {{{onSuccess}}} fires when the user is logged in    // * {{{notloggedin}}} fires when not logged in    // * {{{userconflict}}} fires when the username exists	signup: function(user, pass, email, onSuccess, notloggedin, userconflict) {        var url = "/register/new/" + user;        this.request('POST', url, 			"password=" + escape(pass) + "&email=" + escape(email), { 			call: onSuccess, on401: notloggedin, on409: userconflict,			log: 'Login complete.' 		});	},    // ** {{{ logout(callback) }}}    //    // Logout from the backend    //     // * {{{callback}}} fires after the logout attempt    logout: function(callback) {        var url = "/register/logout/";        this.request('POST', url, null, { log: 'Logout complete.', call: callback });    },    // ** {{{ currentuser(callback, notloggedin) }}}    //    // Return info on the current logged in user    //     // * {{{callback}}} fires after the user attempt    // * {{{notloggedin}}} fires if the user isn't logged in    currentuser: function(callback, notloggedin) {        var url = "/register/userinfo/";        return this.request('GET', url, null,                 { call: callback, on401: notloggedin, evalJSON: true });    },    // == FILES ==    // ** {{{ list(project, path, onSuccess, onFailure) }}}    //    // List the path in the given project    //     // * {{{project}}} is the project to list    // * {{{path}}} is the path to list out    // * {{{onSuccess}}} fires if the list returns something    // * {{{onFailure}}} fires if there is an error getting a list from the server    list: function(project, path, onSuccess, onFailure) {        var project = project || '';        var url = bespin.util.path.combine('/file/list/', project, path || '/');        var opts = { call: onSuccess, evalJSON: true, log: "Listing files in: " + url };        if (dojo.isFunction(onFailure)) opts.onFailure = onFailure;        this.request('GET', url, null, opts);    },    // ** {{{ projects(callback) }}}    //    // Return the list of projects that you have access too    //     // * {{{callback}}} gets fired with the project list    projects: function(callback) {        this.request('GET', '/file/list/', null, { call: callback, evalJSON: true });    },    // ** {{{ saveFile(project, path, contents, lastOp) }}}    //    // Save the given file    //     // * {{{project}}} is the project to save    // * {{{path}}} is the path to save to    // * {{{callback}}} fires after the save returns    // * {{{lastOp}}} contains the last edit operation    saveFile: function(project, path, contents, lastOp) {        if (!project || !path) return;        var url = bespin.util.path.combine('/file/at', project, (path || ''));        if (lastOp) url += "?lastEdit=" + lastOp;        this.request('PUT', url, contents, { log: 'Saved file.' });    },    // ** {{{ loadFile(project, path, contents) }}}    //    // Load the given file    //     // * {{{project}}} is the project to load from    // * {{{path}}} is the path to load    // * {{{callback}}} fires after the file is loaded    loadFile: function(project, path, callback) {

⌨️ 快捷键说明

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