From 6508cb8c65eb81eaf88920efaed0b251c9b3a51b Mon Sep 17 00:00:00 2001 From: Doug Kearns Date: Mon, 6 Aug 2007 12:52:33 +0000 Subject: [PATCH] move Option constructor to Options, move Options.{getPref,setPref,getFirefoxPref,setFirefoxPref} to a slot in vimperator.options and move all option initialisation to Options() --- chrome/content/vimperator/bookmarks.js | 4 +- chrome/content/vimperator/options.js | 172 ++++++++++++++---------- chrome/content/vimperator/ui.js | 4 +- chrome/content/vimperator/vimperator.js | 30 +---- 4 files changed, 108 insertions(+), 102 deletions(-) diff --git a/chrome/content/vimperator/bookmarks.js b/chrome/content/vimperator/bookmarks.js index 7320b7ea..7798bc9a 100644 --- a/chrome/content/vimperator/bookmarks.js +++ b/chrome/content/vimperator/bookmarks.js @@ -722,7 +722,7 @@ function QuickMarks() //{{{ var marks = {}; // load the saved quickmarks -- TODO: change to sqlite - var saved_marks = Options.getPref("quickmarks", "").split("\n"); + var saved_marks = vimperator.options.getPref("quickmarks", "").split("\n"); for (var i = 0; i < saved_marks.length - 1; i += 2) { marks[saved_marks[i]] = saved_marks[i + 1]; @@ -783,7 +783,7 @@ function QuickMarks() //{{{ saved_marks += i + "\n"; saved_marks += marks[i] + "\n"; } - Options.setPref("quickmarks", saved_marks); + vimperator.options.setPref("quickmarks", saved_marks); } //}}} } //}}} diff --git a/chrome/content/vimperator/options.js b/chrome/content/vimperator/options.js index b249af80..4cd3b6bf 100644 --- a/chrome/content/vimperator/options.js +++ b/chrome/content/vimperator/options.js @@ -26,72 +26,6 @@ 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 *****/ -function Option(names, type, extra_info) //{{{ -{ - if (!names || !type) - return null; - - this.name = names[0]; - this.names = names; - this.type = type; - - this.setter = function(value) { Options.setPref(this.name, value); }; - this.getter = function() { return Options.getPref(this.name); }; - - if (extra_info) - { - if (extra_info.usage) - this.usage = extra_info.usage; - else - this.usage = this.names; - - this.help = extra_info.help || null; - this.short_help = extra_info.short_help || null; - - // "", 0 are valid default values - if (extra_info.default_value !== undefined) - this.default_value = extra_info.default_value; - else - this.default_value = null; - - if (extra_info.setter) - this.setter = extra_info.setter; - if (extra_info.getter) - this.getter = extra_info.getter; - - this.completer = extra_info.completer || null; - this.validator = extra_info.validator || null; - } - - // add noOPTION variant of boolean OPTION to this.names - if (this.type == "boolean") - { - this.names = []; - for (var i = 0; i < names.length; i++) - { - this.names.push(names[i]); - this.names.push("no" + names[i]); - } - } - - // NOTE: forced defaults need to use Options.getPref - Option.prototype.__defineGetter__("value", function() { return this.getter.call(this); }); - Option.prototype.__defineSetter__("value", function(value) { this.setter.call(this, value); }); - - // TODO: add is[Type]() queries for use in set()? - // : add isValid() or just throw an exception? - - this.hasName = function(name) - { - for (var i = 0; i < this.names.length; i++) - { - if (this.names[i] == name) - return true; - } - return false; - } -} //}}} - function Options() //{{{ { //////////////////////////////////////////////////////////////////////////////// @@ -182,9 +116,9 @@ function Options() //{{{ } catch (e) { - //alert("error: " + e); pref = default_value; } + return pref; } @@ -227,6 +161,22 @@ function Options() //{{{ document.title = window.content.document.title + " - " + value; // not perfect fix, but good enough } + // + // firefox preferences which we need to be changed to work well with vimperator + // + + // work around firefox popup blocker + var popup_allowed_events = loadPreference('dom.popup_allowed_events', 'change click dblclick mouseup reset submit'); + if (!popup_allowed_events.match("keypress")) + storePreference('dom.popup_allowed_events', popup_allowed_events + " keypress"); + + // TODO: should we be resetting these in destroy too? + // we have our own typeahead find implementation + storePreference('accessibility.typeaheadfind.autostart', false); + storePreference('accessibility.typeaheadfind', false); // actually the above setting should do it, but has no effect in firefox + + storePreference("browser.startup.page", 3); // start with saved session + /////////////////////////////////////////////////////////////////////////////}}} ////////////////////// PUBLIC SECTION ////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////{{{ @@ -248,30 +198,104 @@ function Options() //{{{ // TODO: separate Preferences from Options? Would these utility functions // be better placed in the 'core' vimperator namespace somewhere? - Options.setPref = function(name, value) + this.setPref = function(name, value) { return storePreference(name, value, true); } - Options.getPref = function(name, forced_default) + this.getPref = function(name, forced_default) { return loadPreference(name, forced_default, true); } - Options.setFirefoxPref = function(name, value) + this.setFirefoxPref = function(name, value) { return storePreference(name, value); } - Options.getFirefoxPref = function(name, forced_default) + this.getFirefoxPref = function(name, forced_default) { return loadPreference(name, forced_default); } + this.destroy = function() + { + // reset some modified firefox prefs + if (this.getFirefoxPref('dom.popup_allowed_events', 'change click dblclick mouseup reset submit') + == popup_allowed_events + " keypress") + this.setFirefoxPref('dom.popup_allowed_events', popup_allowed_events); + } + /////////////////////////////////////////////////////////////////////////////}}} ////////////////////// DEFAULT OPTIONS ///////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////{{{ + function Option(names, type, extra_info) //{{{ + { + if (!names || !type) + return null; + + this.name = names[0]; + this.names = names; + this.type = type; + + this.setter = function(value) { storePreference(this.name, value, true); }; + this.getter = function() { return loadPreference(this.name, true); }; + + if (extra_info) + { + if (extra_info.usage) + this.usage = extra_info.usage; + else + this.usage = this.names; + + this.help = extra_info.help || null; + this.short_help = extra_info.short_help || null; + + // "", 0 are valid default values + if (extra_info.default_value !== undefined) + this.default_value = extra_info.default_value; + else + this.default_value = null; + + if (extra_info.setter) + this.setter = extra_info.setter; + if (extra_info.getter) + this.getter = extra_info.getter; + + this.completer = extra_info.completer || null; + this.validator = extra_info.validator || null; + } + + // add noOPTION variant of boolean OPTION to this.names + if (this.type == "boolean") + { + this.names = []; + for (var i = 0; i < names.length; i++) + { + this.names.push(names[i]); + this.names.push("no" + names[i]); + } + } + + // NOTE: forced defaults need to use vimperator.options.getPref + this.__defineGetter__("value", function() { return this.getter.call(this); }); + this.__defineSetter__("value", function(value) { this.setter.call(this, value); }); + + // TODO: add is[Type]() queries for use in set()? + // : add isValid() or just throw an exception? + + this.hasName = function(name) + { + for (var i = 0; i < this.names.length; i++) + { + if (this.names[i] == name) + return true; + } + return false; + } + } //}}} + const DEFAULT_HINTTAGS = "//*[@onclick or @onmouseover or @onmousedown or @onmouseup or @oncommand or @class='lk' or @class='s'] | " + "//input[not(@type='hidden')] | //a | //area | //iframe | //textarea | //button | //select | " + "//xhtml:*[@onclick or @onmouseover or @onmousedown or @onmouseup or @oncommand or @class='lk' or @class='s'] | " + @@ -347,7 +371,7 @@ function Options() //{{{ "
  • T: toolbar
  • " + "
  • b: bookmark bar
  • " + "
  • s: original Firefox statusbar
  • ", - setter: function(value) { Options.setPref("guioptions", value); setGuiOptions(value); }, + setter: function(value) { this.setPref("guioptions", value); setGuiOptions(value); }, default_value: "", validator: function (value) { if (/[^mTbs]/.test(value)) return false; else return true; } } @@ -422,7 +446,7 @@ function Options() //{{{ "
  • 1: Show tab bar only if more than one tab is open
  • " + "
  • 2: Always show tab bar
  • " + "NOTE: Not fully implemented yet and buggy with stal=0", - setter: function(value) { Options.setPref("showtabline", value); setShowTabline(value); }, + setter: function(value) { this.setPref("showtabline", value); setShowTabline(value); }, default_value: 2, validator: function (value) { if (value>=0 && value <=2) return true; else return false; } } @@ -433,7 +457,7 @@ function Options() //{{{ help: "Vimperator changes the browser title from \"Title of web page - Mozilla Firefox\" to " + "\"Title of web page - Vimperator\".
    If you don't like that, you can restore it with: " + ":set titlestring=Mozilla Firefox.", - setter: function(value) { Options.setPref("titlestring", value); setTitleString(value); }, + setter: function(value) { this.setPref("titlestring", value); setTitleString(value); }, default_value: "Vimperator" } )); diff --git a/chrome/content/vimperator/ui.js b/chrome/content/vimperator/ui.js index bfa2303a..04a4c1a1 100644 --- a/chrome/content/vimperator/ui.js +++ b/chrome/content/vimperator/ui.js @@ -84,7 +84,7 @@ function CommandLine() //{{{ var multiline_callback = null; // load the commandline history - var hist = Options.getPref("commandline_history", ""); + var hist = vimperator.options.getPref("commandline_history", ""); history = hist.split("\n"); // TODO: these styles should be moved to the .css file @@ -564,7 +564,7 @@ function CommandLine() //{{{ // it would be better if we had a destructor in javascript ... this.destroy = function() { - Options.setPref("commandline_history", history.join("\n")); + vimperator.options.setPref("commandline_history", history.join("\n")); } //}}} } //}}} diff --git a/chrome/content/vimperator/vimperator.js b/chrome/content/vimperator/vimperator.js index ea6edea5..5c328e2e 100644 --- a/chrome/content/vimperator/vimperator.js +++ b/chrome/content/vimperator/vimperator.js @@ -266,7 +266,7 @@ const vimperator = (function() //{{{ */ log: function(msg, level) { - // if (Options.getPref("verbose") >= level) // FIXME: hangs vimperator, probably timing issue --mst + // if (vimperator.options.getPref("verbose") >= level) // FIXME: hangs vimperator, probably timing issue --mst console_service.logStringMessage('vimperator: ' + msg); }, @@ -365,9 +365,9 @@ const vimperator = (function() //{{{ quit: function(save_session) { if (save_session) - Options.setFirefoxPref("browser.startup.page", 3); // start with saved session + vimperator.options.setFirefoxPref("browser.startup.page", 3); // start with saved session else - Options.setFirefoxPref("browser.startup.page", 1); // start with default homepage session + vimperator.options.setFirefoxPref("browser.startup.page", 1); // start with default homepage session goQuitApplication(); }, @@ -626,32 +626,18 @@ const vimperator = (function() //{{{ vimperator.registerCallback("submit", vimperator.modes.EX, function(command) { vimperator.execute(command); } ); vimperator.registerCallback("complete", vimperator.modes.EX, function(str) { return exTabCompletion(str); } ); - // TODO: move most of the following code to Options constructor - - // work around firefox popup blocker - popup_allowed_events = Options.getFirefoxPref('dom.popup_allowed_events', 'change click dblclick mouseup reset submit'); - if (!popup_allowed_events.match("keypress")) - Options.setFirefoxPref('dom.popup_allowed_events', popup_allowed_events + " keypress"); - - // we have our own typeahead find implementation - Options.setFirefoxPref('accessibility.typeaheadfind.autostart', false); - Options.setFirefoxPref('accessibility.typeaheadfind', false); // actually the above setting should do it, but has no effect in firefox - // first time intro message - if (Options.getPref("firsttime", true)) + if (vimperator.options.getPref("firsttime", true)) { setTimeout(function() { vimperator.help(null, null, null, { inTab: true }); - Options.setPref("firsttime", false); + vimperator.options.setPref("firsttime", false); }, 1000); } //gURLBar.blur(); // TODO: needed anymore? vimperator.focusContent(); - // firefox preferences which we need to be changed to work well with vimperator - Options.setFirefoxPref("browser.startup.page", 3); // start with saved session - // finally, read a ~/.vimperatorrc // make sourcing asynchronous, otherwise commands that open new tabs won't work setTimeout(function() { @@ -683,12 +669,8 @@ const vimperator = (function() //{{{ // save our preferences vimperator.commandline.destroy(); vimperator.events.destroy(); + vimperator.options.destroy(); vimperator.quickmarks.destroy(); - - // reset some modified firefox prefs - if (Options.getFirefoxPref('dom.popup_allowed_events', 'change click dblclick mouseup reset submit') - == popup_allowed_events + " keypress") - Options.setFirefoxPref('dom.popup_allowed_events', popup_allowed_events); }, // @param value MUST be a string, it can have the following form: "+20%", "200%", "-30"