Extensions.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // Array Remove - By John Resig (MIT Licensed)
  2. Array.prototype.remove = function (from, to) {
  3. var rest = this.slice((to || from) + 1 || this.length);
  4. this.length = from < 0 ? this.length + from : from;
  5. return this.push.apply(this, rest);
  6. };
  7. String.prototype.endsWith = function (suffix) {
  8. return this.indexOf(suffix, this.length - suffix.length) !== -1;
  9. };
  10. $.fn.checked = function (value) {
  11. if (value === true || value === false) {
  12. // Set the value of the checkbox
  13. return $(this).each(function () {
  14. this.checked = value;
  15. });
  16. } else {
  17. // Return check state
  18. return $(this).is(':checked');
  19. }
  20. };
  21. var WebNotifications = {
  22. show: function (data) {
  23. if (window.webkitNotifications) {
  24. if (!webkitNotifications.checkPermission()) {
  25. var notif = webkitNotifications.createNotification(data.icon, data.title, data.body);
  26. notif.show();
  27. if (data.timeout) {
  28. setTimeout(function () {
  29. notif.cancel();
  30. }, data.timeout);
  31. }
  32. return notif;
  33. } else {
  34. webkitNotifications.requestPermission(function () {
  35. return WebNotifications.show(data);
  36. });
  37. }
  38. }
  39. else if (window.Notification) {
  40. if (Notification.permissionLevel() === "granted") {
  41. var notif = new Notification(data.title, data);
  42. notif.show();
  43. if (data.timeout) {
  44. setTimeout(function () {
  45. notif.cancel();
  46. }, data.timeout);
  47. }
  48. return notif;
  49. } else if (Notification.permissionLevel() === "default") {
  50. Notification.requestPermission(function () {
  51. return WebNotifications.show(data);
  52. });
  53. }
  54. }
  55. },
  56. requestPermission: function () {
  57. if (window.webkitNotifications) {
  58. if (!webkitNotifications.checkPermission()) {
  59. } else {
  60. webkitNotifications.requestPermission(function () {
  61. });
  62. }
  63. }
  64. else if (window.Notification) {
  65. if (Notification.permissionLevel() === "granted") {
  66. } else if (Notification.permissionLevel() === "default") {
  67. Notification.requestPermission(function () {
  68. });
  69. }
  70. }
  71. }
  72. };
  73. /*
  74. * Javascript Humane Dates
  75. * Copyright (c) 2008 Dean Landolt (deanlandolt.com)
  76. * Re-write by Zach Leatherman (zachleat.com)
  77. *
  78. * Adopted from the John Resig's pretty.js
  79. * at http://ejohn.org/blog/javascript-pretty-date
  80. * and henrah's proposed modification
  81. * at http://ejohn.org/blog/javascript-pretty-date/#comment-297458
  82. *
  83. * Licensed under the MIT license.
  84. */
  85. function humane_date(date_str) {
  86. var time_formats = [[90, 'a minute'], // 60*1.5
  87. [3600, 'minutes', 60], // 60*60, 60
  88. [5400, 'an hour'], // 60*60*1.5
  89. [86400, 'hours', 3600], // 60*60*24, 60*60
  90. [129600, 'a day'], // 60*60*24*1.5
  91. [604800, 'days', 86400], // 60*60*24*7, 60*60*24
  92. [907200, 'a week'], // 60*60*24*7*1.5
  93. [2628000, 'weeks', 604800], // 60*60*24*(365/12), 60*60*24*7
  94. [3942000, 'a month'], // 60*60*24*(365/12)*1.5
  95. [31536000, 'months', 2628000], // 60*60*24*365, 60*60*24*(365/12)
  96. [47304000, 'a year'], // 60*60*24*365*1.5
  97. [3153600000, 'years', 31536000] // 60*60*24*365*100, 60*60*24*365
  98. ];
  99. var dt = new Date;
  100. var date = parseISO8601Date(date_str, true);
  101. var seconds = ((dt - date) / 1000);
  102. var token = ' ago';
  103. var i = 0;
  104. var format;
  105. if (seconds < 0) {
  106. seconds = Math.abs(seconds);
  107. token = '';
  108. }
  109. while (format = time_formats[i++]) {
  110. if (seconds < format[0]) {
  111. if (format.length == 2) {
  112. return format[1] + token;
  113. } else {
  114. return Math.round(seconds / format[2]) + ' ' + format[1] + token;
  115. }
  116. }
  117. }
  118. // overflow for centuries
  119. if (seconds > 4730400000)
  120. return Math.round(seconds / 4730400000) + ' centuries' + token;
  121. return date_str;
  122. };
  123. function humane_elapsed(firstDateStr, secondDateStr) {
  124. var dt1 = new Date(firstDateStr);
  125. var dt2 = new Date(secondDateStr);
  126. var seconds = (dt2.getTime() - dt1.getTime()) / 1000;
  127. var numdays = Math.floor((seconds % 31536000) / 86400);
  128. var numhours = Math.floor(((seconds % 31536000) % 86400) / 3600);
  129. var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
  130. var numseconds = Math.round((((seconds % 31536000) % 86400) % 3600) % 60);
  131. var elapsedStr = '';
  132. elapsedStr += numdays == 1 ? numdays + ' day ' : '';
  133. elapsedStr += numdays > 1 ? numdays + ' days ' : '';
  134. elapsedStr += numhours == 1 ? numhours + ' hour ' : '';
  135. elapsedStr += numhours > 1 ? numhours + ' hours ' : '';
  136. elapsedStr += numminutes == 1 ? numminutes + ' minute ' : '';
  137. elapsedStr += numminutes > 1 ? numminutes + ' minutes ' : '';
  138. elapsedStr += elapsedStr.length > 0 ? 'and ' : '';
  139. elapsedStr += numseconds == 1 ? numseconds + ' second' : '';
  140. elapsedStr += numseconds == 0 || numseconds > 1 ? numseconds + ' seconds' : '';
  141. return elapsedStr;
  142. }
  143. function getParameterByName(name) {
  144. name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  145. var regexS = "[\\?&]" + name + "=([^&#]*)";
  146. var regex = new RegExp(regexS);
  147. var results = regex.exec(window.location.search);
  148. if (results == null)
  149. return "";
  150. else
  151. return decodeURIComponent(results[1].replace(/\+/g, " "));
  152. }
  153. function parseISO8601Date(s, toLocal) {
  154. // parenthese matches:
  155. // year month day hours minutes seconds
  156. // dotmilliseconds
  157. // tzstring plusminus hours minutes
  158. var re = /(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)(\.\d+)?(Z|([+-])(\d\d):(\d\d))/;
  159. var d = [];
  160. d = s.match(re);
  161. // "2010-12-07T11:00:00.000-09:00" parses to:
  162. // ["2010-12-07T11:00:00.000-09:00", "2010", "12", "07", "11",
  163. // "00", "00", ".000", "-09:00", "-", "09", "00"]
  164. // "2010-12-07T11:00:00.000Z" parses to:
  165. // ["2010-12-07T11:00:00.000Z", "2010", "12", "07", "11",
  166. // "00", "00", ".000", "Z", undefined, undefined, undefined]
  167. if (!d) {
  168. throw "Couldn't parse ISO 8601 date string '" + s + "'";
  169. }
  170. // parse strings, leading zeros into proper ints
  171. var a = [1, 2, 3, 4, 5, 6, 10, 11];
  172. for (var i in a) {
  173. d[a[i]] = parseInt(d[a[i]], 10);
  174. }
  175. d[7] = parseFloat(d[7]);
  176. // Date.UTC(year, month[, date[, hrs[, min[, sec[, ms]]]]])
  177. // note that month is 0-11, not 1-12
  178. // see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/UTC
  179. var ms = Date.UTC(d[1], d[2] - 1, d[3], d[4], d[5], d[6]);
  180. // if there are milliseconds, add them
  181. if (d[7] > 0) {
  182. ms += Math.round(d[7] * 1000);
  183. }
  184. // if there's a timezone, calculate it
  185. if (d[8] != "Z" && d[10]) {
  186. var offset = d[10] * 60 * 60 * 1000;
  187. if (d[11]) {
  188. offset += d[11] * 60 * 1000;
  189. }
  190. if (d[9] == "-") {
  191. ms -= offset;
  192. } else {
  193. ms += offset;
  194. }
  195. } else if (!toLocal) {
  196. ms += new Date().getTimezoneOffset() * 60000;
  197. }
  198. return new Date(ms);
  199. };
  200. /**
  201. *
  202. * Secure Hash Algorithm (SHA1)
  203. * http://www.webtoolkit.info/
  204. *
  205. **/
  206. function SHA1(msg) {
  207. function rotate_left(n, s) {
  208. var t4 = (n << s) | (n >>> (32 - s));
  209. return t4;
  210. };
  211. function lsb_hex(val) {
  212. var str = "";
  213. var i;
  214. var vh;
  215. var vl;
  216. for (i = 0; i <= 6; i += 2) {
  217. vh = (val >>> (i * 4 + 4)) & 0x0f;
  218. vl = (val >>> (i * 4)) & 0x0f;
  219. str += vh.toString(16) + vl.toString(16);
  220. }
  221. return str;
  222. };
  223. function cvt_hex(val) {
  224. var str = "";
  225. var i;
  226. var v;
  227. for (i = 7; i >= 0; i--) {
  228. v = (val >>> (i * 4)) & 0x0f;
  229. str += v.toString(16);
  230. }
  231. return str;
  232. };
  233. function Utf8Encode(string) {
  234. string = string.replace(/\r\n/g, "\n");
  235. var utftext = "";
  236. for (var n = 0; n < string.length; n++) {
  237. var c = string.charCodeAt(n);
  238. if (c < 128) {
  239. utftext += String.fromCharCode(c);
  240. }
  241. else if ((c > 127) && (c < 2048)) {
  242. utftext += String.fromCharCode((c >> 6) | 192);
  243. utftext += String.fromCharCode((c & 63) | 128);
  244. }
  245. else {
  246. utftext += String.fromCharCode((c >> 12) | 224);
  247. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  248. utftext += String.fromCharCode((c & 63) | 128);
  249. }
  250. }
  251. return utftext;
  252. };
  253. var blockstart;
  254. var i, j;
  255. var W = new Array(80);
  256. var H0 = 0x67452301;
  257. var H1 = 0xEFCDAB89;
  258. var H2 = 0x98BADCFE;
  259. var H3 = 0x10325476;
  260. var H4 = 0xC3D2E1F0;
  261. var A, B, C, D, E;
  262. var temp;
  263. msg = Utf8Encode(msg);
  264. var msg_len = msg.length;
  265. var word_array = new Array();
  266. for (i = 0; i < msg_len - 3; i += 4) {
  267. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  268. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  269. word_array.push(j);
  270. }
  271. switch (msg_len % 4) {
  272. case 0:
  273. i = 0x080000000;
  274. break;
  275. case 1:
  276. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  277. break;
  278. case 2:
  279. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  280. break;
  281. case 3:
  282. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  283. break;
  284. }
  285. word_array.push(i);
  286. while ((word_array.length % 16) != 14) word_array.push(0);
  287. word_array.push(msg_len >>> 29);
  288. word_array.push((msg_len << 3) & 0x0ffffffff);
  289. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  290. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  291. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  292. A = H0;
  293. B = H1;
  294. C = H2;
  295. D = H3;
  296. E = H4;
  297. for (i = 0; i <= 19; i++) {
  298. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  299. E = D;
  300. D = C;
  301. C = rotate_left(B, 30);
  302. B = A;
  303. A = temp;
  304. }
  305. for (i = 20; i <= 39; i++) {
  306. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  307. E = D;
  308. D = C;
  309. C = rotate_left(B, 30);
  310. B = A;
  311. A = temp;
  312. }
  313. for (i = 40; i <= 59; i++) {
  314. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  315. E = D;
  316. D = C;
  317. C = rotate_left(B, 30);
  318. B = A;
  319. A = temp;
  320. }
  321. for (i = 60; i <= 79; i++) {
  322. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  323. E = D;
  324. D = C;
  325. C = rotate_left(B, 30);
  326. B = A;
  327. A = temp;
  328. }
  329. H0 = (H0 + A) & 0x0ffffffff;
  330. H1 = (H1 + B) & 0x0ffffffff;
  331. H2 = (H2 + C) & 0x0ffffffff;
  332. H3 = (H3 + D) & 0x0ffffffff;
  333. H4 = (H4 + E) & 0x0ffffffff;
  334. }
  335. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  336. return temp.toLowerCase();
  337. }
  338. // jqm.page.params.js - version 0.1
  339. // Copyright (c) 2011, Kin Blas
  340. // All rights reserved.
  341. //
  342. // Redistribution and use in source and binary forms, with or without
  343. // modification, are permitted provided that the following conditions are met:
  344. // * Redistributions of source code must retain the above copyright
  345. // notice, this list of conditions and the following disclaimer.
  346. // * Redistributions in binary form must reproduce the above copyright
  347. // notice, this list of conditions and the following disclaimer in the
  348. // documentation and/or other materials provided with the distribution.
  349. // * Neither the name of the <organization> nor the
  350. // names of its contributors may be used to endorse or promote products
  351. // derived from this software without specific prior written permission.
  352. //
  353. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  354. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  355. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  356. // DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  357. // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  358. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  359. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  360. // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  361. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  362. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  363. (function ($, window, undefined) {
  364. // Given a query string, convert all the name/value pairs
  365. // into a property/value object. If a name appears more than
  366. // once in a query string, the value is automatically turned
  367. // into an array.
  368. function queryStringToObject(qstr) {
  369. var result = {}, nvPairs = ((qstr || "").replace(/^\?/, "").split(/&/)), i, pair, n, v;
  370. for (i = 0; i < nvPairs.length; i++) {
  371. var pstr = nvPairs[i];
  372. if (pstr) {
  373. pair = pstr.split(/=/);
  374. n = pair[0];
  375. v = pair[1];
  376. if (result[n] === undefined) {
  377. result[n] = v;
  378. } else {
  379. if (typeof result[n] !== "object") {
  380. result[n] = [result[n]];
  381. }
  382. result[n].push(v);
  383. }
  384. }
  385. }
  386. return result;
  387. }
  388. // The idea here is to listen for any pagebeforechange notifications from
  389. // jQuery Mobile, and then muck with the toPage and options so that query
  390. // params can be passed to embedded/internal pages. So for example, if a
  391. // changePage() request for a URL like:
  392. //
  393. // http://mycompany.com/myapp/#page-1?foo=1&bar=2
  394. //
  395. // is made, the page that will actually get shown is:
  396. //
  397. // http://mycompany.com/myapp/#page-1
  398. //
  399. // The browser's location will still be updated to show the original URL.
  400. // The query params for the embedded page are also added as a property/value
  401. // object on the options object. You can access it from your page notifications
  402. // via data.options.pageData.
  403. $(document).bind("pagebeforechange", function (e, data) {
  404. // We only want to handle the case where we are being asked
  405. // to go to a page by URL, and only if that URL is referring
  406. // to an internal page by id.
  407. if (typeof data.toPage === "string") {
  408. var u = $.mobile.path.parseUrl(data.toPage);
  409. if ($.mobile.path.isEmbeddedPage(u)) {
  410. // The request is for an internal page, if the hash
  411. // contains query (search) params, strip them off the
  412. // toPage URL and then set options.dataUrl appropriately
  413. // so the location.hash shows the originally requested URL
  414. // that hash the query params in the hash.
  415. var u2 = $.mobile.path.parseUrl(u.hash.replace(/^#/, ""));
  416. if (u2.search) {
  417. if (!data.options.dataUrl) {
  418. data.options.dataUrl = data.toPage;
  419. }
  420. data.options.pageData = queryStringToObject(u2.search);
  421. data.toPage = u.hrefNoHash + "#" + u2.pathname;
  422. }
  423. }
  424. }
  425. });
  426. })(jQuery, window);