Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Globe.global.Util = {
  2.         TRANSACTION_REDIRECTIONOTHER: "Page Redirection to other page/application",
  3.         shortMonth: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  4.         getCookie: function (name) {
  5.                 var nameEQ = name + "=";
  6.                 var ca = document.cookie.split(';');
  7.                 for (var i = 0; i < ca.length; i++) {
  8.                         var c = ca[i];
  9.                         while (c.charAt(0) == ' ') c = c.substring(1, c.length);
  10.                         if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
  11.                 }
  12.                 return null;
  13.         },
  14.         setCookie: function (c_name, value, days) {
  15.                 if (!this.is_int(days)) {
  16.                         days = 365;
  17.                 }
  18.                 var exdate = new Date();
  19.                 exdate.setDate(exdate.getDate() + days);
  20.                 var c_value = value + "; expires=" + exdate.toUTCString() + "; path=/";
  21.                 document.cookie = c_name + "=" + c_value;
  22.         },
  23.         setCookieByHour: function (c_name, value, hours) {
  24.                 if (!this.is_int(hours)) {
  25.                         hours = 24;
  26.                 }
  27.                 var exdate = new Date();
  28.                 exdate.setHours(exdate.getHours() + hours);
  29.                 var c_value = value + "; expires=" + exdate.toUTCString() + "; path=/";
  30.                 document.cookie = c_name + "=" + c_value;
  31.         },
  32.         deleteCookie: function (name) {
  33.                 document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/;';
  34.         },
  35.         is_int: function (value) {
  36.                 if ((parseFloat(value) == parseInt(value)) && !isNaN(value)) {
  37.                         return true;
  38.                 } else {
  39.                         return false;
  40.                 }
  41.         },
  42.         parseDate: function (date) {
  43.                 var dateOut = '';
  44.                 if (date) {
  45.                         // fix for non-chrome browsers
  46.                         var dateString = date.replace(/-/g, "/");
  47.  
  48.                         dateOut = new Date(dateString);
  49.                 }
  50.                 return dateOut;
  51.         },
  52.         parseFloat: function (value) {
  53.                 if (!isNaN(parseFloat(value))) {
  54.                         return parseFloat(value);
  55.                 }
  56.                 return 0;
  57.         },
  58.         getParameterByName: function (name) {
  59.                 name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  60.                 var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
  61.                         results = regex.exec(location.search);
  62.                 return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
  63.         },
  64.         animateToId: function (id) {
  65.                 $('html, body').animate({scrollTop: $('#' + id).offset().top}, 'fast');
  66.         },
  67.         animateToTop: function () {
  68.                 $('html, body').animate({scrollTop: 0}, 'fast');
  69.         },
  70.         clickElem: function (id) {
  71.                 $('#' + id).click();
  72.         },
  73.         // http://stackoverflow.com/questions/1184624/convert-form-data-to-javascript-object-with-jquery
  74.         serializeObject: function (form) {
  75.                 // form = $('#formId');
  76.                 var a = form.serializeArray();
  77.                 var o = {};
  78.                 $.each(a, function () {
  79.                         if (o[this.name] !== undefined) {
  80.                                 if (!o[this.name].push) {
  81.                                         o[this.name] = [o[this.name]];
  82.                                 }
  83.                                 o[this.name].push(this.value || '');
  84.                         } else {
  85.                                 o[this.name] = this.value || '';
  86.                         }
  87.                 });
  88.                 return o;
  89.         },
  90.         setCacheFlag: function (name) {
  91.                 var _this = Globe.global.Util;
  92.                 // 1 day cache
  93.                 _this.setCookie(name, "usecache", 1);
  94.         },
  95.         useCache: function (name) {
  96.                 var _this = Globe.global.Util;
  97.                 // get value from cookie
  98.                 var cacheVal = _this.getCookie(name);
  99.                 if (cacheVal && cacheVal == "usecache") {
  100.                         return true;
  101.                 }
  102.                 return false;
  103.         },
  104.         /*
  105.          * detect ie version
  106.          * returns version of ie or false, if browser is not ie
  107.          */
  108.         detectIE: function () {
  109.                 var ua = window.navigator.userAgent,
  110.                         msie = ua.indexOf('MSIE ');
  111.  
  112.                 if (msie > 0) {
  113.                         // IE 10 or older => return version number
  114.                         return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
  115.                 }
  116.  
  117.                 var trident = ua.indexOf('Trident/');
  118.                 if (trident > 0) {
  119.                         // IE 11 => return version number
  120.                         var rv = ua.indexOf('rv:');
  121.                         return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
  122.                 }
  123.  
  124.                 var edge = ua.indexOf('Edge/');
  125.                 if (edge > 0) {
  126.                         // IE 12 => return version number
  127.                         return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
  128.                 }
  129.  
  130.                 // other browser
  131.                 return false;
  132.         },
  133.         getErrorCode: function (errorList) {
  134.                 if (errorList) {
  135.                         // get first error's code
  136.                         var errorCode = errorList[0].code;
  137.                         return errorCode;
  138.                 }
  139.                 return "";
  140.         },
  141.         convertToCSV: function (objArray) {
  142.                 var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
  143.                 var str = '';
  144.  
  145.                 for (var i = 0; i < array.length; i++) {
  146.                         var line = '';
  147.                         for (var index in array[i]) {
  148.                                 if (line != '') line += ','
  149.  
  150.                                 line += array[i][index];
  151.                         }
  152.                         str += line + '\r\n';
  153.                 }
  154.                 return str;
  155.         },
  156.  
  157.         downloadCSV: function (formatJsonArr, fileName) {
  158.                 var _util = Globe.global.Util;
  159.                 var jsonObject = JSON.stringify(formatJsonArr);
  160.  
  161.                 var a = document.createElement('a');
  162.                 a.href = 'data:application/csv,' + encodeURIComponent(Globe.global.Util.convertToCSV(jsonObject));
  163.                 a.target = '_jsonObject';
  164.                 a.download = fileName + '.csv';
  165.                 //a.click();
  166.                 _util.fireClick(a);
  167.  
  168.                 //add to audit trail
  169.                 try {
  170.                         var auditTrailObj = Globe.webtools.AuditTrail;
  171.                         var tType = auditTrailObj.TRANSACTION_TYPE_REPORTS;
  172.                         var tDetails = auditTrailObj.getTransactionDetails(tType, 'transactions'); //key should be transactionName's page name
  173.                         var tAction = auditTrailObj.getTransactionAction(tDetails.action, {"fileName": a.download});
  174.                         var addTransactionParams = {
  175.                                 "userId": "N/A",
  176.                                 "transactionType": tType,
  177.                                 "transactionName": tDetails.name,
  178.                                 "action": tAction,
  179.                         }
  180.                         auditTrailObj.addTransaction(auditTrailObj.setWTTransactionUrl, addTransactionParams);
  181.                 } catch (e) {
  182.                         console.log("Add transaction to audit trail failed " + e);
  183.                 }
  184.         },
  185.  
  186.         /**
  187.          * Add MouseEvent handler to cater for scenario where browser does not support a.click()
  188.          * i.e. Safari earlier versions
  189.          */
  190.         fireClick: function (node) {
  191.                 var isChrome = /Chrome/.test(navigator.userAgent);
  192.                 var isSafari = /Safari/.test(navigator.userAgent);
  193.                 var isFirefox = /Firefox/.test(navigator.userAgent);
  194.                 var isIE = /MSIE/.test(navigator.userAgent); //valid IE before IE11
  195.                 var isIE11Plus = /Trident.*rv[ :]*(\d+\.\d+)/.test(navigator.userAgent);
  196.  
  197.                 var evt = document.createEvent('MouseEvents');
  198.                 evt.initEvent('click', true, false, window);
  199.  
  200.                 if (isChrome) {
  201.                         node.click();
  202.                 } else if (isSafari) {
  203.                         if (/Windows/.test(navigator.userAgent)) {
  204.                                 console.info("Detected Windows Safari.");
  205.                                 if (document.createEvent) {
  206.                                         node.dispatchEvent(evt);
  207.                                 }
  208.                         } else {
  209.                                 console.info("Detected Safari.");
  210.                                 try {
  211.                                         node.click();
  212.                                 } catch (e) {
  213.                                         if (document.createEvent) {
  214.                                                 node.dispatchEvent(evt);
  215.                                         }
  216.                                 }
  217.                         }
  218.                 } else if (isFirefox) {
  219.                         if (document.createEvent) {
  220.                                 node.dispatchEvent(evt);
  221.                         }
  222.                 } else if (isIE || isIE11Plus) {
  223.                         console.info("Detected IE version less than 11.");
  224.                         var blob = new Blob([decodeURIComponent(node.href)], {
  225.                                 type: "text/csv;charset=utf-8;"
  226.                         });
  227.                         navigator.msSaveBlob(blob, node.download);
  228.  
  229.                 } else {
  230.                         console.info("Detected other browser");
  231.                         node.click();
  232.                 }
  233.  
  234.  
  235.         },
  236.         /**
  237.          * Change Date format of Datepicker to ISO format
  238.          *
  239.          */
  240.         changeDateFormat: function (dateElementName) {
  241.  
  242.                 try {
  243.  
  244.                         var evalStr = "$('input[name=\"" + dateElementName + "\"]').getDateIso();";
  245.                         var date = eval(evalStr);
  246.  
  247.                         var evalStr2 = "document.getElementById(\'" + dateElementName + "\').value = date;";
  248.                         eval(evalStr2);
  249.                         //console.log("final date: " + date);
  250.  
  251.                 } catch (e) {
  252.  
  253.                         console.log("Cannot convert datepicker date format " + e);
  254.  
  255.  
  256.                 }
  257.         },
  258.         /**
  259.          * Get Selected value from dropdown
  260.          *
  261.          */
  262.         getSelectedDropdownValue: function (ulClass, elementId) {
  263.  
  264.                 var a = ulClass + " li";
  265.                 var id = elementId;
  266.  
  267.                 try {
  268.                         $(a).click(function () {
  269.                                 var selectedOption = $(this).attr('value');
  270.                                 document.getElementById(id).value = selectedOption;
  271.                                 console.log(selectedOption);
  272.                         });
  273.  
  274.                 } catch (e) {
  275.  
  276.                         console.log("Error on getting selected dropdown value " + e);
  277.                 }
  278.         },
  279.         /**
  280.          *
  281.          * Author: Kyle Ohagan
  282.          *
  283.          * Method name: appendSuffixToDate
  284.          *
  285.          * Parameter: monthDayInt
  286.          *
  287.          * Description:
  288.          * Accepts an integer which represents a Date's day.
  289.          * Returns a string (the initial integer and the suffix) that can be displayed on the front end
  290.          *
  291.          */
  292.         appendSuffixToDate: function (monthDayInt) {
  293.  
  294.                 if (monthDayInt >= 11 && monthDayInt <= 13) {
  295.  
  296.                         monthDayInt = monthDayInt + "th";
  297.  
  298.                 } else {
  299.  
  300.                         var temp = monthDayInt % 10;
  301.  
  302.                         if (temp == 1) {
  303.  
  304.                                 monthDayInt = monthDayInt + "st";
  305.  
  306.                         } else if (temp == 2) {
  307.  
  308.                                 monthDayInt = monthDayInt + "nd";
  309.  
  310.                         } else if (temp == 3) {
  311.  
  312.                                 monthDayInt = monthDayInt + "rd";
  313.  
  314.                         } else {
  315.  
  316.                                 monthDayInt = monthDayInt + "th";
  317.                         }
  318.  
  319.                 }
  320.  
  321.                 return monthDayInt;
  322.  
  323.         },
  324.         /**
  325.          * Redirect to the page with the Url passed in and add a transaction record for the redirection.
  326.          *
  327.          */
  328.         pageRedirection: function (pageUrl, pageName, openNewWindow, transactionName) {
  329.                 var _this = Globe.global.Util;
  330.                 var _transaction = Globe.global.Transaction;
  331.  
  332.                 if (pageUrl) {
  333.                         var actionParam = {};
  334.                         /* If no transaction name is specified or
  335.                          redirected to a page which is not listed in Transaction Type */
  336.                         if (!transactionName || transactionName == _this.TRANSACTION_REDIRECTIONOTHER) {
  337.                                 transactionName = _this.TRANSACTION_REDIRECTIONOTHER;
  338.                                 if (pageName) {
  339.                                         actionParam.pageName = pageName;
  340.                                 }
  341.                                 else {
  342.                                         actionParam.pageName = pageUrl;
  343.                                 }
  344.                         }
  345.  
  346.                         var transaction = _transaction.getTransactionDetails(transactionName, actionParam);
  347.                         transaction.referenceId = "";
  348.                         transaction.processType = "";
  349.                         _transaction.addTransaction(transaction);
  350.  
  351.                         window.open(pageUrl, openNewWindow ? '_blank' : '_top');
  352.                 }
  353.         },
  354.  
  355.         recontractRedirect: function (pageUrl, pageName, openNewWindow, transactionName, getOmniTokenURL) {
  356.                 var _ccUtil = Globe.global.ClientContextUtil;
  357.                 var accountInfoStoreMgr = _ccUtil.getAccountInfoStoreMgr();
  358.  
  359.                 var profileInfoStoreMgr = _ccUtil.getProfileInfoStoreMgr();
  360.                 var userId = profileInfoStoreMgr.get("userId");
  361.                 var activeAccount = accountInfoStoreMgr.getActiveAccount();
  362.                 var serviceNumber = activeAccount.serviceNumber;
  363.  
  364.         console.log("pageUrl - " + pageUrl);
  365.  
  366.         if (pageUrl) {
  367.                 var actionParam = {};
  368.                 /* If no transaction name is specified or
  369.                    redirected to a page which is not listed in Transaction Type */
  370.                 if (!transactionName || transactionName == _this.TRANSACTION_REDIRECTIONOTHER) {
  371.                         transactionName = _this.TRANSACTION_REDIRECTIONOTHER;
  372.                         if (pageName) {
  373.                                 actionParam.pageName = pageName;
  374.                         }
  375.                         else {
  376.                                 actionParam.pageName = pageUrl;
  377.                         }
  378.                 }
  379.  
  380.                 var transaction = _transaction.getTransactionDetails(transactionName, actionParam);
  381.                 transaction.referenceId = "";
  382.                 transaction.processType = "";
  383.                         _transaction.addTransaction(transaction);
  384.         }
  385.     },
  386.  
  387.         addTransactionHist: function (transactionName, actionParam) {
  388.  
  389.                 var _transaction = Globe.global.Transaction;
  390.  
  391.                 var transaction = _transaction.getTransactionDetails(transactionName, actionParam);
  392.                 transaction.referenceId = "";
  393.                 transaction.processType = "";
  394.                 _transaction.addTransaction(transaction);
  395.  
  396.         },
  397.  
  398.         // Checking for null value for different browsers ( different browsers shows different null values
  399.         // Author : Davin Jaya
  400.         isNull: function (arg) {
  401.  
  402.                 if (arg == 'NaN' || arg == 'undefined' || arg == undefined || arg == '' || arg == null) {
  403.                         return "";
  404.                 }
  405.  
  406.                 else {
  407.                         return arg;
  408.                 }
  409.         },
  410.         //replace all svgs with a temp canvas
  411.  
  412.         convertSvgToCanvas: function (svgElements) {
  413.                 svgElements.each(function (index) {
  414.                         var canvas, xml;
  415.  
  416.                         // canvg doesn't cope very well with em font sizes so find the calculated size in pixels and replace it in the element.
  417.                         $.each($(this).find('[style*=em]'), function (index, el) {
  418.                                 $(this).css('font-size', getStyle(el, 'font-size'));
  419.                         });
  420.  
  421.                         canvas = document.createElement("canvas");
  422.                         canvas.className = "screenShotTempCanvas";
  423.                         //convert SVG into a XML string
  424.                         xml = (new XMLSerializer()).serializeToString(this);
  425.  
  426.                         // Removing the name space as IE throws an error
  427.                         xml = xml.replace(/xmlns=\"http:\/\/www\.w3\.org\/2000\/svg\"/, '');
  428.  
  429.                         //draw the SVG onto a canvas
  430.                         canvg(canvas, xml);
  431.                         $(canvas).insertAfter(this);
  432.                         //hide the SVG element
  433.                         $(this).attr('class', 'tempHide');
  434.                         $(this).hide();
  435.                 });
  436.         }
  437. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement