APIs

Show:
  1. /**
  2. Lib is a general library of additional utilities and helper commands used in StudioLite
  3. @class Lib
  4. @constructor
  5. @return {Object} instantiated Lib
  6. **/
  7. define(['jquery', 'backbone', 'platform'], function ($, Backbone, platform) {
  8. var Lib = function (type) {
  9. this.type = type;
  10. };
  11.  
  12. _.extend(Lib.prototype, {
  13.  
  14. /**
  15. Output formatted string to console and omit error on old browsers...
  16. @method log
  17. @param {String} msg
  18. **/
  19. log: function (msg) {
  20. if (platform.name == 'Chrome')
  21. console.log(new Date().toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1") + ': ' + msg);
  22. },
  23.  
  24. /**
  25. Add the now deprecated Backbone > View > Options so we can pass as args to new views
  26. @method addBackboneViewOptions
  27. **/
  28. addBackboneViewOptions: function () {
  29. Backbone.View = (function (View) {
  30. return View.extend({
  31. constructor: function (options) {
  32. this.options = options || {};
  33. View.apply(this, arguments);
  34. }
  35. });
  36. })(Backbone.View);
  37. },
  38.  
  39. /**
  40. Add to backbone collection save option
  41. @method addBackboneCollectionSave
  42. **/
  43. addBackboneCollectionSave: function () {
  44. Backbone.Collection.prototype.save = function (options) {
  45. Backbone.sync("create", this, options);
  46. };
  47. },
  48.  
  49. /**
  50. Prompt on application exit
  51. @method promptOnExit
  52. **/
  53. promptOnExit: function () {
  54. if (!this.inDevMode()){
  55. $(window).on('beforeunload', function () {
  56. return 'Did you save your work?'
  57. });
  58. }
  59. },
  60.  
  61. /**
  62. In dev mode
  63. @method inDevMode
  64. @return {Boolean}
  65. **/
  66. inDevMode: function () {
  67. if (window.location.href.indexOf('dev') > -1) {
  68. return true;
  69. } else {
  70. return false;
  71. }
  72. },
  73.  
  74. /**
  75. log errors in distribution mode
  76. @method logErrors
  77. **/
  78. logErrors: function (i_businessID) {
  79. if (window.location.href.indexOf('dist') > -1) {
  80. //Bugsense.initAndStartSession( { apiKey: "32eabe70" } );
  81. //Bugsense.initAndStartSession( { apiKey: "f09b1967" } );
  82. //Bugsense.initAndStartSession( { apiKey: "fc064f8c" } );
  83. //Bugsense.addExtraData('business_id', i_businessID);
  84. }
  85. },
  86.  
  87. /**
  88. Load non primary CSS
  89. @method loadCss
  90. @param {String} i_url
  91. **/
  92. loadCss: function (i_url) {
  93. var link = document.createElement("link");
  94. link.type = "text/css";
  95. link.rel = "stylesheet";
  96. link.href = i_url;
  97. document.getElementsByTagName("head")[0].appendChild(link);
  98. },
  99.  
  100. /**
  101. Is running platform a mobile device
  102. @method isMobile
  103. @return {Boolean}
  104. **/
  105. isMobile: function () {
  106. if (BB.platform.os.family == 'Android' || BB.platform.os.family == 'iOS')
  107. return true;
  108. return false;
  109. },
  110.  
  111. /**
  112. Returns this model's attributes as...
  113. @method isEmpty
  114. @param {String} i_string
  115. @return {Boolean}
  116. **/
  117. isEmpty: function (i_string) {
  118. if (!i_string.trim()){
  119. return true;
  120. }
  121. return false;
  122. },
  123.  
  124. /**
  125. Force browser compatability
  126. @method foreceBrowserCompatability
  127. **/
  128. forceBrowserCompatibility: function () {
  129. // BB.lib.isMobile();
  130. var version = parseInt(BB.platform.version);
  131. var os = BB.platform.os;
  132. var family = BB.platform.os.family;
  133. var browser = BB.platform.name;
  134. var failLevel = 0;
  135.  
  136. // alert('|browser:' + browser + '|family: ' + family + '|version: ' + version + '|os: ' + os);
  137.  
  138. if (browser == 'IE') {
  139. $(Elements.WAITS_SCREEN_ENTRY_APP).find('img').eq(1).remove();
  140. } else {
  141. $(Elements.WAITS_SCREEN_ENTRY_APP).find('img').eq(0).remove();
  142. }
  143.  
  144. require(['bootbox'], function (bootbox) {
  145. if (browser == 'Safari' && family == 'Windows')
  146. failLevel = 2;
  147. if (browser == 'IE' && version < 11)
  148. failLevel = 1;
  149.  
  150. switch (failLevel) {
  151. case 0:
  152. {
  153. break
  154. }
  155. case 1:
  156. {
  157. bootbox.dialog({
  158. message: $(Elements.MSG_BOOTBOX_OLD_BROWSER).text(),
  159. buttons: {
  160. danger: {
  161. label: $(Elements.MSG_BOOTBOX_OK).text(),
  162. className: "btn-danger",
  163. callback: function () {
  164. }
  165. }
  166. }
  167. });
  168. break
  169. }
  170. case 2:
  171. {
  172. bootbox.dialog({
  173. message: $(Elements.MSG_BOOTBOX_OLD_BROWSER).text(),
  174. buttons: {
  175. danger: {
  176. label: $(Elements.MSG_BOOTBOX_OK).text(),
  177. className: "btn-danger",
  178. callback: function () {
  179. $('body').empty();
  180. }
  181. }
  182. }
  183. });
  184. break
  185. }
  186. }
  187. });
  188. },
  189.  
  190. /**
  191. Validate email address format using regexp
  192. @method validateEmail
  193. @param {String} emailAddress
  194. @return {Boolean}
  195. **/
  196. validateEmail: function (emailAddress) {
  197. var emailRegex = new RegExp(/^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$/i);
  198. var valid = emailRegex.test(emailAddress);
  199. if (!valid) {
  200. return false;
  201. } else {
  202. return true;
  203. }
  204. },
  205.  
  206. /**
  207. Set user agent / browser version
  208. @method initUserAgent
  209. **/
  210. initUserAgent: function () {
  211.  
  212. var ua = navigator.userAgent.toLowerCase(),
  213. match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  214. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  215. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  216. /(msie) ([\w.]+)/.exec(ua) ||
  217. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) || [],
  218. browser = match[1] || "",
  219. version = match[2] || "0";
  220.  
  221. $.browser = {};
  222. $.browser.type = '';
  223.  
  224. if (browser) {
  225. $.browser[browser] = true;
  226. $.browser.version = version;
  227. }
  228.  
  229. // Chrome is Webkit, but Webkit is also Safari.
  230. if (jQuery.browser.chrome) {
  231. jQuery.browser.webkit = true;
  232. } else if (jQuery.browser.webkit) {
  233. jQuery.browser.safari = true;
  234. }
  235.  
  236. if (!(window.mozInnerScreenX == null)) {
  237. $.browser.type = 'FF';
  238. return;
  239. }
  240.  
  241. if ($.browser.msie) {
  242. $.browser.type = 'IE';
  243. }
  244.  
  245. if (/Android/i.test(navigator.userAgent)) {
  246. $.browser.type = 'ANDROID';
  247. }
  248.  
  249. if (/webOS/i.test(navigator.userAgent)) {
  250. $.browser.type = 'WEBOS';
  251. }
  252.  
  253. if (/iPhone/i.test(navigator.userAgent)) {
  254. $.browser.type = 'IPHONE';
  255. }
  256.  
  257. if (/iPad/i.test(navigator.userAgent)) {
  258. $.browser.type = 'IPAD';
  259. }
  260.  
  261. if (/iPod/i.test(navigator.userAgent)) {
  262. $.browser.type = 'IPOD';
  263. }
  264.  
  265. if (/BlackBerry/i.test(navigator.userAgent)) {
  266. $.browser.type = 'BLACKBARRY';
  267. }
  268.  
  269. if (navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Chrome') == -1) {
  270. $.browser.type = 'SAFARI';
  271. return;
  272. }
  273. },
  274.  
  275. /**
  276. Simplify a string to basic character set
  277. @method cleanChar
  278. @param {String} value
  279. @return {String} cleaned string
  280. **/
  281. cleanChar: function (value) {
  282. if (value == null)
  283. value = '';
  284. if ($.isNumeric(value))
  285. return value;
  286. value = value.replace(/,/g, ' ');
  287. value = value.replace(/\\}/g, ' ');
  288. value = value.replace(/{/g, ' ');
  289. value = value.replace(/"/g, ' ');
  290. value = value.replace(/'/g, ' ');
  291. value = value.replace(/&/g, 'and');
  292. value = value.replace(/>/g, ' ');
  293. value = value.replace(/</g, ' ');
  294. value = value.replace(/\[/g, ' ');
  295. value = value.replace(/]/g, ' ');
  296. return value;
  297. },
  298.  
  299. unclass: function (value) {
  300. return value.replace(/\./g, '');
  301. },
  302.  
  303. unhash: function (value) {
  304. return value.replace(/\#/g, '');
  305. },
  306.  
  307. /**
  308. Get DOM comment string
  309. @method getComment
  310. @param {String} str
  311. @return {String} string of comment if retrieved
  312. **/
  313. getComment: function (str) {
  314. var content = jQuery('body').html();
  315. var search = '<!-- ' + str + '.*?-->';
  316. var re = new RegExp(search, 'g');
  317. var data = content.match(re);
  318. var myRegexp = /<!-- (.*?) -->/g;
  319. var match = myRegexp.exec(data);
  320. if (match == null) {
  321. return undefined
  322. } else {
  323. return match[1];
  324. }
  325. },
  326.  
  327. /**
  328. Convert an XML data format to a DOM enabled data structure
  329. @method parseXml
  330. @param {XML} xml data to parse
  331. @return {Object} xml data structure
  332. **/
  333. parseXml: function (xml) {
  334. var dom = null;
  335. if (window.DOMParser) {
  336. try {
  337. dom = (new DOMParser()).parseFromString(xml, "text/xml");
  338. }
  339. catch (e) {
  340. dom = null;
  341. }
  342. }
  343. else if (window.ActiveXObject) {
  344. try {
  345. dom = new ActiveXObject('Microsoft.XMLDOM');
  346. dom.async = false;
  347. if (!dom.loadXML(xml)) // parse error ..
  348.  
  349. window.alert('alt ' + dom.parseError.reason + dom.parseError.srcText);
  350. }
  351. catch (e) {
  352. dom = null;
  353. }
  354. }
  355. else
  356. alert("cannot parse xml string!");
  357. return dom;
  358. },
  359.  
  360. /**
  361. Convert an XML data format to json
  362. @method xml2json
  363. @param {XML} xml
  364. @param {Object} internal
  365. @return {Object} json data structure
  366. **/
  367. xml2json: function (xml, tab) {
  368. // http://goessner.net/download/prj/jsonxml/
  369. var X = {
  370. toObj: function (xml) {
  371. var o = {};
  372. if (xml.nodeType == 1) { // element node ..
  373. if (xml.attributes.length) // element with attributes ..
  374. for (var i = 0; i < xml.attributes.length; i++)
  375. o["@" + xml.attributes[i].nodeName] = (xml.attributes[i].nodeValue || "").toString();
  376. if (xml.firstChild) { // element has child nodes ..
  377. var textChild = 0, cdataChild = 0, hasElementChild = false;
  378. for (var n = xml.firstChild; n; n = n.nextSibling) {
  379. if (n.nodeType == 1) hasElementChild = true;
  380. else if (n.nodeType == 3 && n.nodeValue.match(/[^ \f\n\r\t\v]/)) textChild++; // non-whitespace text
  381. else if (n.nodeType == 4) cdataChild++; // cdata section node
  382. }
  383. if (hasElementChild) {
  384. if (textChild < 2 && cdataChild < 2) { // structured element with evtl. a single text or/and cdata node ..
  385. X.removeWhite(xml);
  386. for (var n = xml.firstChild; n; n = n.nextSibling) {
  387. if (n.nodeType == 3) // text node
  388. o["#text"] = X.escape(n.nodeValue);
  389. else if (n.nodeType == 4) // cdata node
  390. o["#cdata"] = X.escape(n.nodeValue);
  391. else if (o[n.nodeName]) { // multiple occurence of element ..
  392. if (o[n.nodeName] instanceof Array)
  393. o[n.nodeName][o[n.nodeName].length] = X.toObj(n);
  394. else
  395. o[n.nodeName] = [o[n.nodeName], X.toObj(n)];
  396. }
  397. else // first occurence of element..
  398. o[n.nodeName] = X.toObj(n);
  399. }
  400. }
  401. else { // mixed content
  402. if (!xml.attributes.length)
  403. o = X.escape(X.innerXml(xml));
  404. else
  405. o["#text"] = X.escape(X.innerXml(xml));
  406. }
  407. }
  408. else if (textChild) { // pure text
  409. if (!xml.attributes.length)
  410. o = X.escape(X.innerXml(xml));
  411. else
  412. o["#text"] = X.escape(X.innerXml(xml));
  413. }
  414. else if (cdataChild) { // cdata
  415. if (cdataChild > 1)
  416. o = X.escape(X.innerXml(xml));
  417. else
  418. for (var n = xml.firstChild; n; n = n.nextSibling)
  419. o["#cdata"] = X.escape(n.nodeValue);
  420. }
  421. }
  422. if (!xml.attributes.length && !xml.firstChild) o = null;
  423. }
  424. else if (xml.nodeType == 9) { // document.node
  425. o = X.toObj(xml.documentElement);
  426. }
  427. else
  428. alert("unhandled node type: " + xml.nodeType);
  429. return o;
  430. },
  431. toJson: function (o, name, ind) {
  432. var json = name ? ("\"" + name + "\"") : "";
  433. if (o instanceof Array) {
  434. for (var i = 0, n = o.length; i < n; i++)
  435. o[i] = X.toJson(o[i], "", ind + "\t");
  436. json += (name ? ":[" : "[") + (o.length > 1 ? ("\n" + ind + "\t" + o.join(",\n" + ind + "\t") + "\n" + ind) : o.join("")) + "]";
  437. }
  438. else if (o == null)
  439. json += (name && ":") + "null";
  440. else if (typeof(o) == "object") {
  441. var arr = [];
  442. for (var m in o)
  443. arr[arr.length] = X.toJson(o[m], m, ind + "\t");
  444. json += (name ? ":{" : "{") + (arr.length > 1 ? ("\n" + ind + "\t" + arr.join(",\n" + ind + "\t") + "\n" + ind) : arr.join("")) + "}";
  445. }
  446. else if (typeof(o) == "string")
  447. json += (name && ":") + "\"" + o.toString() + "\"";
  448. else
  449. json += (name && ":") + o.toString();
  450. return json;
  451. },
  452. innerXml: function (node) {
  453. var s = ""
  454. if ("innerHTML" in node)
  455. s = node.innerHTML;
  456. else {
  457. var asXml = function (n) {
  458. var s = "";
  459. if (n.nodeType == 1) {
  460. s += "<" + n.nodeName;
  461. for (var i = 0; i < n.attributes.length; i++)
  462. s += " " + n.attributes[i].nodeName + "=\"" + (n.attributes[i].nodeValue || "").toString() + "\"";
  463. if (n.firstChild) {
  464. s += ">";
  465. for (var c = n.firstChild; c; c = c.nextSibling)
  466. s += asXml(c);
  467. s += "</" + n.nodeName + ">";
  468. }
  469. else
  470. s += "/>";
  471. }
  472. else if (n.nodeType == 3)
  473. s += n.nodeValue;
  474. else if (n.nodeType == 4)
  475. s += "<![CDATA[" + n.nodeValue + "]]>";
  476. return s;
  477. };
  478. for (var c = node.firstChild; c; c = c.nextSibling)
  479. s += asXml(c);
  480. }
  481. return s;
  482. },
  483. escape: function (txt) {
  484. return txt.replace(/[\\]/g, "\\\\")
  485. .replace(/[\"]/g, '\\"')
  486. .replace(/[\n]/g, '\\n')
  487. .replace(/[\r]/g, '\\r');
  488. },
  489. removeWhite: function (e) {
  490. e.normalize();
  491. for (var n = e.firstChild; n;) {
  492. if (n.nodeType == 3) { // text node
  493. if (!n.nodeValue.match(/[^ \f\n\r\t\v]/)) { // pure whitespace text node
  494. var nxt = n.nextSibling;
  495. e.removeChild(n);
  496. n = nxt;
  497. }
  498. else
  499. n = n.nextSibling;
  500. }
  501. else if (n.nodeType == 1) { // element node
  502. X.removeWhite(n);
  503. n = n.nextSibling;
  504. }
  505. else // any other node
  506. n = n.nextSibling;
  507. }
  508. return e;
  509. }
  510. };
  511. if (xml.nodeType == 9) // document node
  512. xml = xml.documentElement;
  513. var json = X.toJson(X.toObj(X.removeWhite(xml)), xml.nodeName, "\t");
  514. return "{\n" + tab + (tab ? json.replace(/\t/g, tab) : json.replace(/\t|\n/g, "")) + "\n}";
  515. },
  516.  
  517. /**
  518. Convert a json data format to xml
  519. @method xml2json
  520. @param {JSON} o
  521. @param {Object} internal
  522. @return {Object} xml data structure
  523. **/
  524. json2xml: function (o, tab) {
  525. var toXml = function (v, name, ind) {
  526. var xml = "";
  527. if (v instanceof Array) {
  528. for (var i = 0, n = v.length; i < n; i++)
  529. xml += ind + toXml(v[i], name, ind + "\t") + "\n";
  530. }
  531. else if (typeof(v) == "object") {
  532. var hasChild = false;
  533. xml += ind + "<" + name;
  534. for (var m in v) {
  535. if (m.charAt(0) == "@")
  536. xml += " " + m.substr(1) + "=\"" + v[m].toString() + "\"";
  537. else
  538. hasChild = true;
  539. }
  540. xml += hasChild ? ">" : "/>";
  541. if (hasChild) {
  542. for (var m in v) {
  543. if (m == "#text")
  544. xml += v[m];
  545. else if (m == "#cdata")
  546. xml += "<![CDATA[" + v[m] + "]]>";
  547. else if (m.charAt(0) != "@")
  548. xml += toXml(v[m], m, ind + "\t");
  549. }
  550. xml += (xml.charAt(xml.length - 1) == "\n" ? ind : "") + "</" + name + ">";
  551. }
  552. }
  553. else {
  554. xml += ind + "<" + name + ">" + v.toString() + "</" + name + ">";
  555. }
  556. return xml;
  557. }, xml = "";
  558. for (var m in o)
  559. xml += toXml(o[m], m, "");
  560. return tab ? xml.replace(/\t/g, tab) : xml.replace(/\t|\n/g, "");
  561. },
  562.  
  563. /**
  564. Convert number or string to float with double precision
  565. @method parseToFloatDouble
  566. @param {Object} i_value
  567. @return {Number}
  568. **/
  569. parseToFloatDouble: function (i_value) {
  570. return parseFloat(parseFloat(i_value).toFixed(2));
  571. },
  572.  
  573. /**
  574. Returns the total unique members of an array
  575. @method uniqueArray
  576. @param {Array} i_array
  577. @return {Number} total unique members
  578. **/
  579. uniqueArraySize: function (i_array) {
  580. function onlyUnique(value, index, self) {
  581. return self.indexOf(value) === index;
  582. }
  583.  
  584. var a = i_array.filter(onlyUnique);
  585. return a.length;
  586. },
  587.  
  588. /**
  589. Check if a remote file exists
  590. @method remoteFileExits
  591. @param {String} url
  592. @return {Boolean}
  593. **/
  594. remoteFileExits: function (url) {
  595. if (url) {
  596. var req = new XMLHttpRequest();
  597. req.open('GET', url, false);
  598. req.send();
  599. return req.status == 200;
  600. } else {
  601. return false;
  602. }
  603. },
  604.  
  605. /**
  606. Get specific param name from URL
  607. @method function
  608. @param {String} i_name
  609. @return {String}
  610. **/
  611. getURLParameter: function (i_name) {
  612. return decodeURIComponent(
  613. (location.search.match(RegExp("[?|&]" + i_name + '=(.+?)(&|$)')) || [, null])[1]
  614. );
  615. },
  616.  
  617. /**
  618. Returns Epoch base time
  619. @method getEpochTime
  620. @return {Number}
  621. **/
  622. getEpochTime: function () {
  623. var d = new Date();
  624. var n = d.getTime();
  625. return n;
  626. },
  627.  
  628. /**
  629. Decimal to hex converter
  630. @method decimalToHex
  631. @param {Number} d
  632. @return {String} hex
  633. **/
  634. decimalToHex: function (d) {
  635. var hex = Number(d).toString(16);
  636. hex = "000000".substr(0, 6 - hex.length) + hex;
  637. return hex;
  638. },
  639.  
  640. /**
  641. Hex to decimal converter
  642. @method hexToDecimal
  643. @param {String} h
  644. @return {Number} decimal
  645. **/
  646. hexToDecimal: function (h) {
  647. var h = h.replace(/#/gi, '');
  648. return parseInt(h, 16);
  649. },
  650.  
  651. /**
  652. RGB color to hex converter
  653. @method rgbToHex
  654. @param {Number} rgb
  655. @return {String} hex
  656. **/
  657. rgbToHex: function (rgb) {
  658. function componentFromStr(numStr, percent) {
  659. var num = Math.max(0, parseInt(numStr, 10));
  660. return percent ?
  661. Math.floor(255 * Math.min(100, num) / 100) : Math.min(255, num);
  662. }
  663.  
  664. var rgbRegex = /^rgb\(\s*(-?\d+)(%?)\s*,\s*(-?\d+)(%?)\s*,\s*(-?\d+)(%?)\s*\)$/;
  665. var result, r, g, b, hex = "";
  666. if ((result = rgbRegex.exec(rgb))) {
  667. r = componentFromStr(result[1], result[2]);
  668. g = componentFromStr(result[3], result[4]);
  669. b = componentFromStr(result[5], result[6]);
  670. hex = (0x1000000 + (r << 16) + (g << 8) + b).toString(16).slice(1);
  671. }
  672. return hex;
  673. },
  674.  
  675. /**
  676. Smart convert color (many) to decinal
  677. @method colorToDecimal
  678. @param {String} color
  679. @return {Number} decimal
  680. **/
  681. colorToDecimal: function (color) {
  682. if (color.match('rgb')) {
  683. color = this.rgbToHex(color);
  684. return this.hexToDecimal(color)
  685. }
  686. return this.hexToDecimal(color);
  687. },
  688.  
  689. /**
  690. Smart convert color (many) to hex
  691. @method colorToHex
  692. @param {String} color
  693. @return {String} hex
  694. **/
  695. colorToHex: function (color) {
  696. if (color.match('#')) {
  697. return color;
  698. }
  699. if (color.match('rgb')) {
  700. return '#' + this.rgbToHex(color);
  701. }
  702. return '#' + color;
  703. },
  704.  
  705. /**
  706. Capitilize first letter
  707. @method capitaliseFirst
  708. @param {String} string
  709. @return {String} string
  710. **/
  711. capitaliseFirst: function (string) {
  712. return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
  713. },
  714.  
  715. /**
  716. Run a function n number of times with sleep in between
  717. @method setIntervalTimes
  718. @param {Function} i_func
  719. @param {Number} i_sleep
  720. @param {Number} i_timesRun
  721. **/
  722. setIntervalTimes: function (i_func, i_sleep, i_timesRun) {
  723. var timesRun = 0;
  724. var interval = setInterval(function () {
  725. timesRun += 1;
  726. if (timesRun === i_timesRun) {
  727. clearInterval(interval);
  728. }
  729. i_func();
  730. }, i_sleep);
  731. },
  732.  
  733.  
  734. /**
  735. Pad zeros
  736. @method padZeros
  737. @param {Number} n value
  738. @param {Number} width pre-pad width
  739. @param {Number} z negative as in '-'
  740. @return {Number} zero padded string
  741. **/
  742. padZeros: function (n, width, z) {
  743. z = z || '0';
  744. n = n + '';
  745. return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
  746. },
  747.  
  748. /**
  749. base64Encode
  750. @method base64Encode
  751. @param {String}
  752. @return {String}
  753. **/
  754. base64Encode: function (str) {
  755. var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){var t="";var n,r,i,s,o,u,a;var f=0;e=Base64._utf8_encode(e);while(f<e.length){n=e.charCodeAt(f++);r=e.charCodeAt(f++);i=e.charCodeAt(f++);s=n>>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+this._keyStr.charAt(s)+this._keyStr.charAt(o)+this._keyStr.charAt(u)+this._keyStr.charAt(a)}return t},decode:function(e){var t="";var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(f<e.length){s=this._keyStr.indexOf(e.charAt(f++));o=this._keyStr.indexOf(e.charAt(f++));u=this._keyStr.indexOf(e.charAt(f++));a=this._keyStr.indexOf(e.charAt(f++));n=s<<2|o>>4;r=(o&15)<<4|u>>2;i=(u&3)<<6|a;t=t+String.fromCharCode(n);if(u!=64){t=t+String.fromCharCode(r)}if(a!=64){t=t+String.fromCharCode(i)}}t=Base64._utf8_decode(t);return t},_utf8_encode:function(e){e=e.replace(/\r\n/g,"\n");var t="";for(var n=0;n<e.length;n++){var r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r)}else if(r>127&&r<2048){t+=String.fromCharCode(r>>6|192);t+=String.fromCharCode(r&63|128)}else{t+=String.fromCharCode(r>>12|224);t+=String.fromCharCode(r>>6&63|128);t+=String.fromCharCode(r&63|128)}}return t},_utf8_decode:function(e){var t="";var n=0;var r=c1=c2=0;while(n<e.length){r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r);n++}else if(r>191&&r<224){c2=e.charCodeAt(n+1);t+=String.fromCharCode((r&31)<<6|c2&63);n+=2}else{c2=e.charCodeAt(n+1);c3=e.charCodeAt(n+2);t+=String.fromCharCode((r&15)<<12|(c2&63)<<6|c3&63);n+=3}}return t}}
  756. return Base64.encode(str);
  757. },
  758.  
  759. /**
  760. base64Decode
  761. @method base64Decode
  762. @param {String}
  763. @return {String}
  764. **/
  765. base64Decode: function (str) {
  766. var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){var t="";var n,r,i,s,o,u,a;var f=0;e=Base64._utf8_encode(e);while(f<e.length){n=e.charCodeAt(f++);r=e.charCodeAt(f++);i=e.charCodeAt(f++);s=n>>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+this._keyStr.charAt(s)+this._keyStr.charAt(o)+this._keyStr.charAt(u)+this._keyStr.charAt(a)}return t},decode:function(e){var t="";var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^A-Za-z0-9\+\/\=]/g,"");while(f<e.length){s=this._keyStr.indexOf(e.charAt(f++));o=this._keyStr.indexOf(e.charAt(f++));u=this._keyStr.indexOf(e.charAt(f++));a=this._keyStr.indexOf(e.charAt(f++));n=s<<2|o>>4;r=(o&15)<<4|u>>2;i=(u&3)<<6|a;t=t+String.fromCharCode(n);if(u!=64){t=t+String.fromCharCode(r)}if(a!=64){t=t+String.fromCharCode(i)}}t=Base64._utf8_decode(t);return t},_utf8_encode:function(e){e=e.replace(/\r\n/g,"\n");var t="";for(var n=0;n<e.length;n++){var r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r)}else if(r>127&&r<2048){t+=String.fromCharCode(r>>6|192);t+=String.fromCharCode(r&63|128)}else{t+=String.fromCharCode(r>>12|224);t+=String.fromCharCode(r>>6&63|128);t+=String.fromCharCode(r&63|128)}}return t},_utf8_decode:function(e){var t="";var n=0;var r=c1=c2=0;while(n<e.length){r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r);n++}else if(r>191&&r<224){c2=e.charCodeAt(n+1);t+=String.fromCharCode((r&31)<<6|c2&63);n+=2}else{c2=e.charCodeAt(n+1);c3=e.charCodeAt(n+2);t+=String.fromCharCode((r&15)<<12|(c2&63)<<6|c3&63);n+=3}}return t}}
  767. return Base64.decode(str);
  768. },
  769.  
  770. /**
  771. Remove characters that a problemtaic to app / js
  772. @method cleanProbCharacters
  773. @param {String} i_string
  774. @param {Number} i_restriction
  775. @return {String} string
  776. **/
  777. cleanProbCharacters: function (i_string, i_restriction) {
  778. switch (i_restriction){
  779. case 1: {
  780. i_string = i_string.replace(/{/ig, "(");
  781. i_string = i_string.replace(/}/ig, ")");
  782. }
  783. case 2: {
  784. i_string = i_string.replace(/</ig, "(");
  785. i_string = i_string.replace(/>/ig, ")");
  786. }
  787. case 3: {
  788. i_string = i_string.replace(/&/ig, "and");
  789. }
  790. case 4: {
  791. i_string = i_string.replace(/"/ig, "`");
  792. i_string = i_string.replace(/'/ig, "`");
  793. }
  794. }
  795. return i_string;
  796. },
  797.  
  798. /**
  799. Get current selection theme color
  800. @method getThemeColor
  801. @params {String} color
  802. **/
  803. getThemeColor: function () {
  804. if (BB.CONSTS['THEME'] == 'light')
  805. return '#428ac9 ';
  806. return '#eb7c66';
  807. },
  808.  
  809. /**
  810. Enable selection switcher via jquery plugin
  811. usage: $('#element').disableSelection() or$('#element').enableSelection()
  812. @method selectionSwitcher
  813. **/
  814. selectionSwitcher: function () {
  815. (function ($) {
  816. $.fn.disableSelection = function () {
  817. return this
  818. .attr('unselectable', 'on')
  819. .css('user-select', 'none')
  820. .css('-moz-user-select', 'none')
  821. .css('-khtml-user-select', 'none')
  822. .css('-webkit-user-select', 'none')
  823. .on('selectstart', false)
  824. .on('contextmenu', false)
  825. .on('keydown', false)
  826. .on('mousedown', false);
  827. };
  828.  
  829. $.fn.enableSelection = function () {
  830. return this
  831. .attr('unselectable', '')
  832. .css('user-select', '')
  833. .css('-moz-user-select', '')
  834. .css('-khtml-user-select', '')
  835. .css('-webkit-user-select', '')
  836. .off('selectstart', false)
  837. .off('contextmenu', false)
  838. .off('keydown', false)
  839. .off('mousedown', false);
  840. };
  841.  
  842. })(jQuery);
  843. }
  844. });
  845.  
  846. return Lib;
  847. });
  848.  
  849.