PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/lib/util.js.in

https://bitbucket.org/portix/dwb
Autoconf | 365 lines | 363 code | 2 blank | 0 comment | 29 complexity | fe2e69ec78d4f4e95bf252da7ff44098 MD5 | raw file
  1. // See COPYING for copyright and license details
  2. (function () {
  3. var util = namespace("util");
  4. var data = namespace("data");
  5. var tabs = namespace("tabs");
  6. Object.defineProperties(util,
  7. {
  8. /**
  9. * Clones an object, object with circular references are not supported.
  10. *
  11. * @name clone
  12. * @memberOf util
  13. * @function
  14. *
  15. * @param {Object} object The object to clone
  16. *
  17. * @returns {Object} A copy of the object
  18. * */
  19. "clone" : {
  20. value : function(o) {
  21. if (o instanceof Array) {
  22. return o.slice(0);
  23. }
  24. else {
  25. return JSON.parse(JSON.stringify(o));
  26. }
  27. }
  28. },
  29. /**
  30. * Get the selected text in a webview
  31. * @name getSelection
  32. * @memberOf util
  33. * @function
  34. *
  35. * @returns {String} The selected text or null if no text was selected
  36. * */
  37. "getSelection" :
  38. {
  39. value : function()
  40. {
  41. var frames = tabs.current.allFrames;
  42. for (var i=frames.length-1; i>=0; --i) {
  43. var selection = JSON.parse(frames[i].inject("return document.getSelection().toString()"));
  44. if (selection && selection.length > 0)
  45. return selection;
  46. }
  47. return null;
  48. }
  49. },
  50. /**
  51. * Converts camel-case string for usage with GObject properties to a
  52. * non-camel-case String
  53. * @name uncamelize
  54. * @memberOf util
  55. * @function
  56. * @example
  57. * util.uncamelize("fooBarBaz"); // "foo-bar-baz"
  58. *
  59. * @param {String} text The text to uncamelize
  60. *
  61. * @returns {String} The uncamelized String
  62. *
  63. * */
  64. "uncamelize" :
  65. {
  66. value : function(text)
  67. {
  68. if (! text || text.length === 0)
  69. return text;
  70. return text.replace(/(.)?([A-Z])/g, function(x, s, m) {
  71. return s ? s + "-" + m.toLowerCase() : m.toLowerCase();
  72. });
  73. }
  74. },
  75. /**
  76. * Converts a non-camel-case string to a camel-case string
  77. *
  78. * @name camelize
  79. * @memberOf util
  80. * @function
  81. * @example
  82. * util.camelize("foo-bar-baz"); // "fooBarBaz"
  83. *
  84. * @param {String} text The text to camelize
  85. *
  86. * @returns {String} A camelcase String
  87. * */
  88. "camelize" :
  89. {
  90. value : function(text)
  91. {
  92. if (! text || text.length === 0)
  93. return text;
  94. return text.replace(/[-_]+(.)?/g, function(a, b) {
  95. return b ? b.toUpperCase() : "";
  96. });
  97. }
  98. },
  99. /**
  100. * Mixes properties of objects into an object. Properties are mixed in
  101. * from left to right, so properties will not be overwritten in the
  102. * leftmost object if they are already defined.
  103. *
  104. * @name mixin
  105. * @memberOf util
  106. * @function
  107. *
  108. * @param {Object} self
  109. * The object to mixin the properties
  110. * @param {...Object} varargs
  111. * Variable number of objects to mix in.
  112. *
  113. * @returns {Object}
  114. * <i>self</i> or a new object if <i>self</i> is null or undefined.
  115. *
  116. * @example
  117. * var a = { a : 1, b : 2, c : 3 };
  118. * var b = { b : 1, d : 2, e : 3 };
  119. * var c = { e : 1, f : 2, g : 3 };
  120. *
  121. * a = util.mixin(a, b, c); // a = { a : 1, b : 2, c : 3, d : 2, e : 3, f : 2, g : 3}
  122. * */
  123. "mixin" :
  124. {
  125. value : function(arg)
  126. {
  127. var i, l, key, o;
  128. var self = arg || {};
  129. for (i=1, l=arguments.length; i<l; i++)
  130. {
  131. o = arguments[i];
  132. for (key in o)
  133. {
  134. if (!self.hasOwnProperty(key))
  135. {
  136. self[key] = o[key];
  137. }
  138. }
  139. }
  140. return self;
  141. }
  142. },
  143. /**
  144. * Encodes a UTF8 string
  145. * @name encodeUTF8
  146. * @memberOf util
  147. * @function
  148. * @param {String} string
  149. * The string to encode
  150. *
  151. * @returns {String}
  152. * The encoded string
  153. */
  154. "encodeUTF8" :
  155. {
  156. value : function(string)
  157. {
  158. return unescape(encodeURIComponent(string));
  159. }
  160. },
  161. /**
  162. * Decode a string to UTF8
  163. * @name decodeUTF8
  164. * @memberOf util
  165. * @function
  166. * @param {String} string
  167. * The string to decode
  168. *
  169. * @returns {String}
  170. * The decoded string
  171. */
  172. "decodeUTF8" :
  173. {
  174. value : function(string)
  175. {
  176. return decodeURIComponent(escape(string));
  177. }
  178. },
  179. /**
  180. * Encodes a string to base64
  181. * @name base64Encode
  182. * @memberOf util
  183. * @function
  184. * @param {String} string
  185. * The string to encode
  186. *
  187. * @returns {String}
  188. * The base64-representation of the string
  189. */
  190. "base64Encode" :
  191. {
  192. value : function(string)
  193. {
  194. return util._base64Encode(util.encodeUTF8(string));
  195. }
  196. },
  197. /**
  198. * Decodes a base64 string to a string
  199. * @name base64Decode
  200. * @memberOf util
  201. * @function
  202. * @param {String} string
  203. * The string to decode
  204. *
  205. * @returns {String}
  206. * The decoded string
  207. */
  208. "base64Decode" :
  209. {
  210. value : function(string)
  211. {
  212. return util.decodeUTF8(util._base64Decode(string));
  213. }
  214. },
  215. /**
  216. * Shorthand for <i>util.changeMode(Modes.NormalMode)</i>
  217. * @name normalMode
  218. * @memberOf util
  219. * @function
  220. */
  221. "normalMode" :
  222. {
  223. value : function(string)
  224. {
  225. return util.changeMode(Modes.NormalMode);
  226. }
  227. },
  228. /**
  229. * Shorthand for <i>util.changeMode(Modes.InsertMode)</i>
  230. *
  231. * @name insertMode
  232. * @memberOf util
  233. * @function
  234. */
  235. "insertMode" :
  236. {
  237. value : function(string)
  238. {
  239. return util.changeMode(Modes.InsertMode);
  240. }
  241. },
  242. /**
  243. * Shorthand for <i>util.changeMode(Modes.CaretMode)</i>
  244. * @name caretMode
  245. * @memberOf util
  246. * @function
  247. */
  248. "caretMode" :
  249. {
  250. value : function(string)
  251. {
  252. return util.changeMode(Modes.CaretMode);
  253. }
  254. },
  255. /**
  256. * Function that creates a multiline string
  257. *
  258. * @name hereDoc
  259. * @memberOf util
  260. * @function
  261. *
  262. * @param {Function} doc
  263. * A function, the body of the function must contain the here
  264. * document. The content must be wrapped in &#47;&#42;*HEREDOC ...HEREDOC*&#42;&#47;
  265. *
  266. * @returns {String}
  267. * The here document.
  268. * @example
  269. * function myDocument() {
  270. * &#47;&#42;HEREDOC
  271. * &lt;body&gt;
  272. * &lt;input&gt;
  273. * &lt;/input&gt;
  274. * &lt;/body&gt;
  275. * HEREDOC&#42;&#47;
  276. * }
  277. *
  278. * tabs.current.loadString(util.hereDoc(myDocument));
  279. */
  280. "hereDoc" :
  281. {
  282. value : function(func)
  283. {
  284. return this.getBody(func).replace(/\s*\/\*HEREDOC([\s\S]*)\s*HEREDOC\*\/\s*;?/, "$1");
  285. }
  286. },
  287. /**
  288. * Flattens an array, this function modifies the original array.
  289. *
  290. * @name flatten
  291. * @memberOf util
  292. * @function
  293. * @example
  294. * var a = [ 1, [ 2, 3, [4, 5, [ 6, 7 ] ] ], 8 ];
  295. * var b = util.flatten(a) // -> [ 1, 2, 3, [ 4, 5, [ 6, 7 ] ], 8 ]
  296. * var c = util.flatten(a, true) // -> [ 1, 2, 3, 4, 5, 6, 7, 8 ]
  297. *
  298. * @param {Array} array
  299. * The array to flatten
  300. * @param {Boolean} [deep]
  301. * Whether to deep flatten the array
  302. *
  303. * @returns
  304. * The a new array that is flat..
  305. * */
  306. "flatten" : {
  307. value : function(arr, deep) {
  308. if (deep) {
  309. arr = arr.map(function(o, i, a) {
  310. if (o instanceof Array)
  311. return this.flatten(o, true);
  312. else
  313. return o;
  314. }, this);
  315. }
  316. return Array.prototype.concat.apply([], arr);
  317. }
  318. },
  319. /**
  320. * Searches for a string in the current or a new tab using dwb's
  321. * searchengines
  322. *
  323. * @name search
  324. * @memberOf util
  325. * @function
  326. *
  327. * @param {String} keyword
  328. * The search keyword
  329. * @param {String} searchString
  330. * The string to search for
  331. * @param {Boolean} [newTab]
  332. * Whether to open the result in a new tab, default false
  333. * */
  334. search : {
  335. value : function(keyword, searchString, newTab) {
  336. var command = newTab ? "tabopen" : "open";
  337. data.parse("searchEngines").some(function(engine) {
  338. if (engine.keyword == keyword) {
  339. execute(command + " " + engine.keyword + " " + searchString);
  340. return true;
  341. }
  342. return false;
  343. });
  344. }
  345. }
  346. });
  347. Object.freeze(util);
  348. if (Object.prototype.forEach === undefined)
  349. {
  350. Object.defineProperty(Object.prototype, "forEach",
  351. {
  352. value : function (callback)
  353. {
  354. var key;
  355. for (key in this)
  356. {
  357. if (this.hasOwnProperty(key))
  358. callback(key, this[key], this);
  359. }
  360. }
  361. });
  362. }
  363. })();