/scripts/lib/util.js.in
Autoconf | 365 lines | 363 code | 2 blank | 0 comment | 29 complexity | fe2e69ec78d4f4e95bf252da7ff44098 MD5 | raw file
- // See COPYING for copyright and license details
- (function () {
- var util = namespace("util");
- var data = namespace("data");
- var tabs = namespace("tabs");
- Object.defineProperties(util,
- {
- /**
- * Clones an object, object with circular references are not supported.
- *
- * @name clone
- * @memberOf util
- * @function
- *
- * @param {Object} object The object to clone
- *
- * @returns {Object} A copy of the object
- * */
- "clone" : {
- value : function(o) {
- if (o instanceof Array) {
- return o.slice(0);
- }
- else {
- return JSON.parse(JSON.stringify(o));
- }
- }
- },
- /**
- * Get the selected text in a webview
- * @name getSelection
- * @memberOf util
- * @function
- *
- * @returns {String} The selected text or null if no text was selected
- * */
- "getSelection" :
- {
- value : function()
- {
- var frames = tabs.current.allFrames;
- for (var i=frames.length-1; i>=0; --i) {
- var selection = JSON.parse(frames[i].inject("return document.getSelection().toString()"));
- if (selection && selection.length > 0)
- return selection;
- }
- return null;
- }
- },
- /**
- * Converts camel-case string for usage with GObject properties to a
- * non-camel-case String
- * @name uncamelize
- * @memberOf util
- * @function
- * @example
- * util.uncamelize("fooBarBaz"); // "foo-bar-baz"
- *
- * @param {String} text The text to uncamelize
- *
- * @returns {String} The uncamelized String
- *
- * */
- "uncamelize" :
- {
- value : function(text)
- {
- if (! text || text.length === 0)
- return text;
- return text.replace(/(.)?([A-Z])/g, function(x, s, m) {
- return s ? s + "-" + m.toLowerCase() : m.toLowerCase();
- });
- }
- },
- /**
- * Converts a non-camel-case string to a camel-case string
- *
- * @name camelize
- * @memberOf util
- * @function
- * @example
- * util.camelize("foo-bar-baz"); // "fooBarBaz"
- *
- * @param {String} text The text to camelize
- *
- * @returns {String} A camelcase String
- * */
- "camelize" :
- {
- value : function(text)
- {
- if (! text || text.length === 0)
- return text;
- return text.replace(/[-_]+(.)?/g, function(a, b) {
- return b ? b.toUpperCase() : "";
- });
- }
- },
- /**
- * Mixes properties of objects into an object. Properties are mixed in
- * from left to right, so properties will not be overwritten in the
- * leftmost object if they are already defined.
- *
- * @name mixin
- * @memberOf util
- * @function
- *
- * @param {Object} self
- * The object to mixin the properties
- * @param {...Object} varargs
- * Variable number of objects to mix in.
- *
- * @returns {Object}
- * <i>self</i> or a new object if <i>self</i> is null or undefined.
- *
- * @example
- * var a = { a : 1, b : 2, c : 3 };
- * var b = { b : 1, d : 2, e : 3 };
- * var c = { e : 1, f : 2, g : 3 };
- *
- * a = util.mixin(a, b, c); // a = { a : 1, b : 2, c : 3, d : 2, e : 3, f : 2, g : 3}
- * */
- "mixin" :
- {
- value : function(arg)
- {
- var i, l, key, o;
- var self = arg || {};
- for (i=1, l=arguments.length; i<l; i++)
- {
- o = arguments[i];
- for (key in o)
- {
- if (!self.hasOwnProperty(key))
- {
- self[key] = o[key];
- }
- }
- }
- return self;
- }
- },
- /**
- * Encodes a UTF8 string
- * @name encodeUTF8
- * @memberOf util
- * @function
- * @param {String} string
- * The string to encode
- *
- * @returns {String}
- * The encoded string
- */
- "encodeUTF8" :
- {
- value : function(string)
- {
- return unescape(encodeURIComponent(string));
- }
- },
- /**
- * Decode a string to UTF8
- * @name decodeUTF8
- * @memberOf util
- * @function
- * @param {String} string
- * The string to decode
- *
- * @returns {String}
- * The decoded string
- */
- "decodeUTF8" :
- {
- value : function(string)
- {
- return decodeURIComponent(escape(string));
- }
- },
- /**
- * Encodes a string to base64
- * @name base64Encode
- * @memberOf util
- * @function
- * @param {String} string
- * The string to encode
- *
- * @returns {String}
- * The base64-representation of the string
- */
- "base64Encode" :
- {
- value : function(string)
- {
- return util._base64Encode(util.encodeUTF8(string));
- }
- },
- /**
- * Decodes a base64 string to a string
- * @name base64Decode
- * @memberOf util
- * @function
- * @param {String} string
- * The string to decode
- *
- * @returns {String}
- * The decoded string
- */
- "base64Decode" :
- {
- value : function(string)
- {
- return util.decodeUTF8(util._base64Decode(string));
- }
- },
- /**
- * Shorthand for <i>util.changeMode(Modes.NormalMode)</i>
- * @name normalMode
- * @memberOf util
- * @function
- */
- "normalMode" :
- {
- value : function(string)
- {
- return util.changeMode(Modes.NormalMode);
- }
- },
- /**
- * Shorthand for <i>util.changeMode(Modes.InsertMode)</i>
- *
- * @name insertMode
- * @memberOf util
- * @function
- */
- "insertMode" :
- {
- value : function(string)
- {
- return util.changeMode(Modes.InsertMode);
- }
- },
- /**
- * Shorthand for <i>util.changeMode(Modes.CaretMode)</i>
- * @name caretMode
- * @memberOf util
- * @function
- */
- "caretMode" :
- {
- value : function(string)
- {
- return util.changeMode(Modes.CaretMode);
- }
- },
- /**
- * Function that creates a multiline string
- *
- * @name hereDoc
- * @memberOf util
- * @function
- *
- * @param {Function} doc
- * A function, the body of the function must contain the here
- * document. The content must be wrapped in /**HEREDOC ...HEREDOC**/
- *
- * @returns {String}
- * The here document.
- * @example
- * function myDocument() {
- * /*HEREDOC
- * <body>
- * <input>
- * </input>
- * </body>
- * HEREDOC*/
- * }
- *
- * tabs.current.loadString(util.hereDoc(myDocument));
- */
- "hereDoc" :
- {
- value : function(func)
- {
- return this.getBody(func).replace(/\s*\/\*HEREDOC([\s\S]*)\s*HEREDOC\*\/\s*;?/, "$1");
- }
- },
- /**
- * Flattens an array, this function modifies the original array.
- *
- * @name flatten
- * @memberOf util
- * @function
- * @example
- * var a = [ 1, [ 2, 3, [4, 5, [ 6, 7 ] ] ], 8 ];
- * var b = util.flatten(a) // -> [ 1, 2, 3, [ 4, 5, [ 6, 7 ] ], 8 ]
- * var c = util.flatten(a, true) // -> [ 1, 2, 3, 4, 5, 6, 7, 8 ]
- *
- * @param {Array} array
- * The array to flatten
- * @param {Boolean} [deep]
- * Whether to deep flatten the array
- *
- * @returns
- * The a new array that is flat..
- * */
- "flatten" : {
- value : function(arr, deep) {
- if (deep) {
- arr = arr.map(function(o, i, a) {
- if (o instanceof Array)
- return this.flatten(o, true);
- else
- return o;
- }, this);
- }
- return Array.prototype.concat.apply([], arr);
- }
- },
- /**
- * Searches for a string in the current or a new tab using dwb's
- * searchengines
- *
- * @name search
- * @memberOf util
- * @function
- *
- * @param {String} keyword
- * The search keyword
- * @param {String} searchString
- * The string to search for
- * @param {Boolean} [newTab]
- * Whether to open the result in a new tab, default false
- * */
- search : {
- value : function(keyword, searchString, newTab) {
- var command = newTab ? "tabopen" : "open";
- data.parse("searchEngines").some(function(engine) {
- if (engine.keyword == keyword) {
- execute(command + " " + engine.keyword + " " + searchString);
- return true;
- }
- return false;
- });
- }
- }
- });
- Object.freeze(util);
- if (Object.prototype.forEach === undefined)
- {
- Object.defineProperty(Object.prototype, "forEach",
- {
- value : function (callback)
- {
- var key;
- for (key in this)
- {
- if (this.hasOwnProperty(key))
- callback(key, this[key], this);
- }
- }
- });
- }
- })();