settings.js

来自「在线编辑器」· JavaScript 代码 · 共 556 行 · 第 1/2 页

JS
556
字号
/* ***** 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.client.settings");// = Settings =//// This settings module provides a base implementation to store settings for users.// It also contains various "stores" to save that data, including://// * {{{bespin.client.settings.Core}}} : Core interface to settings. User code always goes through here.// * {{{bespin.client.settings.Server}}} : The main store. Saves back to the Bespin Server API// * {{{bespin.client.settings.InMemory}}} : In memory settings that are used primarily for debugging// * {{{bespin.client.settings.Cookie}}} : Store in a cookie using cookie-jar// * {{{bespin.client.settings.URL}}} : Intercept settings in the URL. Often used to override// * {{{bespin.client.settings.DB}}} : Commented out for now, but a way to store settings locally// * {{{bespin.client.settings.Events}}} : Custom events that the settings store can intercept and send// ** {{{ bespin.client.settings.Core }}} **//// Handles load/save of user settings.// TODO: tie into the sessions servlet; eliminate Gears dependencydojo.declare("bespin.client.settings.Core", null, {    constructor: function() {        this.browserOverrides = {};        this.fromURL = new bespin.client.settings.URL();        this.customEvents = new bespin.client.settings.Events(this);        this.loadStore();    // Load up the correct settings store    },    loadSession: function() {        var path    = this.fromURL.get('path') || this.get('_path');        var project = this.fromURL.get('project') || this.get('_project');                bespin.publish("bespin:settings:init", { // -- time to init my friends            path: path,            project: project        });    },    defaultSettings: function() {        return {            'tabsize': '2',            'fontsize': '10',            'autocomplete': 'off',            'collaborate': 'off',            'syntax': 'auto'        };    },    // TODO: Make sure I can nuke the default function    initSettings: function() {        var self = this;        bespin.publish("bespin:settings:set:collaborate", {            value: self.get("collaborate")        });    },    isOn: function(value) {        return value == 'on' || value == 'true';    },    isOff: function(value) {        return value == 'off' || value == 'false';    },    // ** {{{ Settings.loadStore() }}} **    //    // This is where we choose which store to load    loadStore: function() {        this.store = new bespin.client.settings.Server(this);//        this.store = new Bespin.Settings.Cookie(this);// TODO: ignore gears for now:// this.store = (window['google']) ? new Bespin.Settings.DB : new Bespin.Settings.InMemory;// this.store = new Bespin.Settings.InMemory;    },    toList: function() {        var settings = [];        var storeSettings = this.store.settings;        for (var prop in storeSettings) {            if (storeSettings.hasOwnProperty(prop)) {                settings.push({ 'key': prop, 'value': storeSettings[prop] });            }        }        return settings;    },    set: function(key, value) {        this.store.set(key, value);        bespin.publish("bespin:settings:set:" + key, { value: value });    },    get: function(key) {        var fromURL = this.fromURL.get(key); // short circuit        if (fromURL) return fromURL;        return this.store.get(key);    },    unset: function(key) {        this.store.unset(key);    },    list: function() {        if (typeof this.store['list'] == "function") {            return this.store.list();        } else {            return this.toList();        }    }});// ** {{{ bespin.client.settings.InMemory }}} **//// Debugging in memory settings (die when browser is closed)dojo.declare("bespin.client.settings.InMemory", null, {    constructor: function(parent) {        this.parent = parent;        this.settings = this.parent.defaultSettings();        bespin.publish("bespin:settings:loaded");    },    set: function(key, value) {        this.settings[key] = value;    },    get: function(key) {        return this.settings[key];    },    unset: function(key) {        delete this.settings[key];    }});// ** {{{ bespin.client.settings.Cookie }}} **//// Save the settings in a cookiedojo.declare("bespin.client.settings.Cookie", null, {    constructor: function(parent) {        var expirationInHours = 1;        this.cookieSettings = {            expires: expirationInHours / 24,            path: '/'        };        var settings = dojo.fromJson(dojo.cookie("settings"));        if (settings) {            this.settings = settings;        } else {            this.settings = {                'tabsize': '2',                'fontsize': '10',                'autocomplete': 'off',                'collaborate': 'off',                '_username': 'dion'            };            dojo.cookie("settings", dojo.toJson(this.settings), this.cookieSettings);        }        bespin.publish("bespin:settings:loaded");    },    set: function(key, value) {        this.settings[key] = value;        dojo.cookie("settings", dojo.toJson(this.settings), this.cookieSettings);    },    get: function(key) {        return this.settings[key];    },    unset: function(key) {        delete this.settings[key];        dojo.cookie("settings", dojo.toJson(this.settings), this.cookieSettings);    }});    // ** {{{ bespin.client.settings.Server }}} **//// The real grand-daddy that implements uses {{{Server}}} to access the backenddojo.declare("bespin.client.settings.Server", null, {    constructor: function(parent) {        this.parent = parent;        this.server = _server;        // TODO: seed the settings          this.server.listSettings(dojo.hitch(this, function(settings) {            this.settings = settings;            if (settings['tabsize'] == undefined) {                this.settings = this.parent.defaultSettings();                this.server.setSettings(this.settings);            }            bespin.publish("bespin:settings:loaded");        }));    },    set: function(key, value) {        this.settings[key] = value;        this.server.setSetting(key, value);    },    get: function(key) {        return this.settings[key];    },    unset: function(key) {        delete this.settings[key];        this.unsetSetting(key);    }});// ** {{{ bespin.client.settings.DB }}} **//// Taken out for now to allow us to not require gears_db.js (and Gears itself).// Experimental ability to save locally in the SQLite database.// The plan is to migrate to ActiveRecord.js or something like it to abstract on top// of various stores (HTML5, Gears, globalStorage, etc.)/*// turn off for now so we can take gears_db.js outBespin.Settings.DB = Class.create({    initialize: function(parent) {        this.parent = parent;        this.db = new GearsDB('wideboy');        //this.db.run('drop table settings');        this.db.run('create table if not exists settings (' +               'id integer primary key,' +               'key varchar(255) unique not null,' +               'value varchar(255) not null,' +               'timestamp int not null)');        this.db.run('CREATE INDEX IF NOT EXISTS settings_id_index ON settings (id)');        bespin.publish("bespin:settings:loaded");    },    set: function(key, value) {        this.db.forceRow('settings', { 'key': key, 'value': value, timestamp: new Date().getTime() }, 'key');    },    get: function(key) {

⌨️ 快捷键说明

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