public/javascripts/erp_app/utility.js in erp_app-3.1.16 vs public/javascripts/erp_app/utility.js in erp_app-4.0.0

- old
+ new

@@ -1,7 +1,26 @@ -Ext.ns("Compass.ErpApp.Utility"); +var Compass = window['Compass'] || {}; +Compass['ErpApp'] = Compass['ErpApp'] || {}; +Compass.ErpApp['Utility'] = Compass.ErpApp['Utility'] || {}; +//helper to create namespaces +Compass.ErpApp.Utility.createNamespace = function (namespaces) { + var spaces = namespaces.split('.'), + currentNamespace = null; + + for (var i = 0; i < spaces.length; i++) { + var space = spaces[i]; + + if (currentNamespace) { + currentNamespace = currentNamespace[space] = currentNamespace[space] || {} + } + else { + currentNamespace = window[space] = window[space] || {}; + } + } +}; + //handle session timeout Compass.ErpApp.Utility.SessionTimeout = { enabled: false, redirectTo: null, warnInMilliseconds: null, @@ -12,11 +31,39 @@ setForceRedirectTimer: function (action) { switch (action) { case 'start': var self = this; this.redirectTimer = window.setTimeout(function () { - window.location = self.redirectTo; + if (window['Ext']) { + Ext.Ajax.request({ + method: 'POST', + url: '/session/is_alive', + success: function (response, request) { + var result = Ext.decode(repsonse.responseText); + if (result.success) { + self.resetRedirect(); + } + else { + window.location = self.redirectTo; + } + } + }); + } + else{ + jQuery.ajax({ + method: 'GET', + url: '/session/is_alive', + success: function (result, request) { + if (result.success) { + self.resetRedirect(); + } + else { + window.location = self.redirectTo; + } + } + }); + } }, this.redirectInMilliseconds); break; case 'stop': clearTimeout(this.redirectTimer); break; @@ -25,63 +72,116 @@ setWarnTimer: function (action) { switch (action) { case 'start': var self = this; this.warnTimer = window.setTimeout(function () { - Ext.MessageBox.confirm('Confirm', 'Your session is about to expire due to inactivity. Do you wish to continue this session?', function (btn) { - if (btn == 'no') { + if (window['Ext']) { + Ext.MessageBox.confirm('Confirm', 'Your session is about to expire due to inactivity. Do you wish to continue this session?', function (btn) { + if (btn == 'no') { + window.location = self.redirectTo; + } + else if (btn == 'yes') { + Ext.Ajax.request({ + method: 'POST', + url: '/session/keep_alive', + success: function (result, request) { + self.reset(); + } + }); + } + }); + } + else { + var warningModal = jQuery('<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></div>'), + modalDialog = jQuery('<div class="modal-dialog"></div>'), + modalContent = jQuery('<div class="modal-content"></div>'), + modalHeader = jQuery('<div class="modal-header"><h4 class="modal-title" id="myModalLabel">Please Confirm</h4></div>'), + modalBody = jQuery('<div class="modal-body">Your session is about to expire due to inactivity. Do you wish to continue this session?</div>'), + footer = jQuery('<div class="modal-footer"></div>'), + yesBtn = jQuery('<button type="button" class="btn btn-default" data-dismiss="modal">Yes</button>'), + noBtn = jQuery('<button type="button" class="btn btn-primary" data-dismiss="modal">No</button>'); + + footer.append(yesBtn); + footer.append(noBtn); + modalContent.append(modalHeader); + modalContent.append(modalBody); + modalContent.append(footer); + modalDialog.append(modalContent); + warningModal.append(modalDialog); + + noBtn.bind('click', function () { + warningModal.modal('hide'); + warningModal.remove(); window.location = self.redirectTo; - } - else if (btn == 'yes') { - Ext.Ajax.request({ + }); + + yesBtn.bind('click', function () { + warningModal.modal('hide'); + warningModal.remove(); + + jQuery.ajax({ method: 'POST', url: '/session/keep_alive', success: function (result, request) { self.reset(); } }); - } - }); + }); + + jQuery("body").append(warningModal); + + warningModal.modal({backdrop: false}); + } + }, this.warnInMilliseconds); break; case 'stop': clearTimeout(this.warnTimer); break; } }, setupSessionTimeout: function (warnInMilliseconds, redirectInMilliseconds, redirectTo) { - var self = this; - Ext.Ajax.addListener('requestcomplete', this.reset, this); + if (window['Ext']) { + Ext.Ajax.addListener('requestcomplete', this.reset, this); + } this.enabled = true; this.redirectTo = redirectTo; this.warnInMilliseconds = warnInMilliseconds; this.redirectInMilliseconds = redirectInMilliseconds; this.setForceRedirectTimer('start'); this.setWarnTimer('start'); }, reset: function () { + this.resetWarning(); + this.resetRedirect(); + }, + resetWarning: function () { this.setWarnTimer('stop'); - this.setForceRedirectTimer('stop'); - this.setWarnTimer('start'); + }, + resetRedirect: function () { + this.setForceRedirectTimer('stop'); this.setForceRedirectTimer('start'); } + }; //end handle session timeout -Compass.ErpApp.Utility.confirmBrowserNavigation = function (additionalmessage) { - additionalmessage = additionalmessage || null; +Compass.ErpApp.Utility.confirmBrowserNavigation = function (additionalMessage) { + var me = this; + + additionalMessage = additionalMessage || null; window.onbeforeunload = function () { - return Ext.isEmpty(additionalmessage) ? '' : additionalmessage; + return me.isBlank(additionalMessage) ? '' : additionalMessage; } }; Compass.ErpApp.Utility.disableEnterSubmission = function () { - $(function () { - $("form").bind("keypress", function (e) { + jQuery(function () { + jQuery("form").bind("keypress", function (e) { if (e.keyCode == 13) return false; }); }); }; @@ -91,30 +191,65 @@ eval(scriptTags[i].text); } }; Compass.ErpApp.Utility.promptReload = function () { - Ext.MessageBox.confirm('Confirm', 'Page must reload for changes to take affect. Reload now?', function (btn) { - if (btn == 'no') { - return false; - } - else { - window.location.reload(); - } - }); + if (window['Ext']) { + Ext.MessageBox.confirm('Confirm', 'Page must reload for changes to take affect. Reload now?', function (btn) { + if (btn == 'no') { + return false; + } + else { + window.location.reload(); + } + }); + } }; +Compass.ErpApp.Utility.preventBrowserBack = function () { + // Push some history into this windows history to help prevent back button + for (var i = 0; i < 10; i++) { + window.history.pushState("history", 'CompassAE Desktop', "#" + i * new Date().getMilliseconds()); + } +}; + +Compass.ErpApp.Utility.setupErpAppLogoutRedirect = function () { + if (window['Ext']) { + var runner = new Ext.util.TaskRunner(), + task = runner.start({ + run: function () { + Ext.Ajax.request({ + url: '/session/is_alive', + method: 'GET', + success: function (response) { + var responseObj = Ext.decode(response.responseText); + if (!responseObj.alive) { + window.location = '/erp_app/login'; + } + }, + failure: function () { + // Log or alert + } + }); + }, + interval: 60000 + }); + } +}; + Compass.ErpApp.Utility.handleFormFailure = function (action) { - switch (action.failureType) { - case Ext.form.action.Action.CLIENT_INVALID: - Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values'); - break; - case Ext.form.action.Action.CONNECT_FAILURE: - Ext.Msg.alert('Failure', 'Ajax communication failed'); - break; - case Ext.form.action.Action.SERVER_INVALID: - Ext.Msg.alert('Failure', action.result.msg); + if (window['Ext']) { + switch (action.failureType) { + case Ext.form.action.Action.CLIENT_INVALID: + Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values'); + break; + case Ext.form.action.Action.CONNECT_FAILURE: + Ext.Msg.alert('Failure', 'Ajax communication failed'); + break; + case Ext.form.action.Action.SERVER_INVALID: + Ext.Msg.alert('Failure', action.result.msg); + } } }; Compass.ErpApp.Utility.randomString = function (length) { var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"; @@ -168,34 +303,10 @@ } else return false; }; -//FIXME: This is broken; missing the Ext.ux.util.clone file -Compass.ErpApp.Utility.clone = function (o) { - if (!o || 'object' !== typeof o) { - return o; - } - if ('function' === typeof o.clone) { - return o.clone(); - } - var c = '[object Array]' === Object.prototype.toString.call(o) ? [] : {}; - var p, v; - for (p in o) { - if (o.hasOwnProperty(p)) { - v = o[p]; - if (v && 'object' === typeof v) { - c[p] = Ext.ux.util.clone(v); - } - else { - c[p] = v; - } - } - } - return c; -}; - Compass.ErpApp.Utility.addCommas = function (nStr) { nStr += ''; var x = nStr.split('.'); var x1 = x[0]; var x2 = x.length > 1 ? '.' + x[1] : ''; @@ -204,16 +315,21 @@ x1 = x1.replace(rgx, '$1' + ',' + '$2'); } return x1 + x2; }; -Compass.ErpApp.Utility.isBlank = function (value) { - return Ext.isEmpty(value); +Compass.ErpApp.Utility.isBlank = function (obj) { + if (window['Ext']) { + return Ext.isEmpty(obj); + } + else { + return(!obj || jQuery.trim(obj) === "" || obj.length == 0); + } }; Compass.ErpApp.Utility.removeDublicates = function (arrayName) { - var newArray = new Array(); + var newArray = []; label:for (var i = 0; i < arrayName.length; i++) { for (var j = 0; j < newArray.length; j++) { if (newArray[j].unit_id == arrayName[i].unit_id) continue label; } @@ -254,11 +370,24 @@ num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3)); return (((sign) ? '' : '-') + '$' + num + '.' + cents); }; -function OnDemandLoadByAjax(){ +Compass.ErpApp.Utility.addEventHandler = function (obj, evt, handler) { + if (obj.addEventListener) { + // W3C method + obj.addEventListener(evt, handler, false); + } else if (obj.attachEvent) { + // IE method. + obj.attachEvent('on' + evt, handler); + } else { + // Old school method. + obj['on' + evt] = handler; + } +}; + +function OnDemandLoadByAjax() { this.load = function (components, callback) { this.components = components; this.successCallBack = callback; this.attempts = 0; this.scriptsToLoad = []; @@ -320,22 +449,32 @@ self.scriptDone(); } }; this.onSuccess = function () { - if (!Ext.isEmpty(this.successCallBack) && this.successCallBack) { + if (this.successCallBack) { this.successCallBack(); } }; this.onFailure = function () { }; -}; +} //Javascript Extensions //Array Extensions +Array.prototype.split = function (n) { + var len = this.length, out = [], i = 0; + while (i < len) { + var size = Math.ceil((len - i) / n--); + out.push(this.slice(i, i + size)); + i += size; + } + return out; +}; + Array.prototype.contains = function (element) { for (var i = 0; i < this.length; i++) { if (this[i] == element) { return true; } @@ -390,10 +529,61 @@ else { return this[this.length - 1]; } }; +//Lazy enumerator methods for Array + +Array.prototype.next = function() { + if(this.currentIndex == undefined){ + this.currentIndex = -1; + } + + if(this[this.currentIndex + 1] != undefined){ + return this[++this.currentIndex]; + }else{ + return false; + } + +}; + +Array.prototype.prev = function() { + if(this.currentIndex == undefined){ + this.currentIndex = -1; + } + + if(this[this.currentIndex - 1] != undefined){ + return this[--this.currentIndex]; + }else{ + return false; + } + +}; + +Array.prototype.current = function(){ + if(this.currentIndex == undefined){ + this.currentIndex = -1; + } + return this[this.currentIndex]; +}; + +Array.prototype.reset = function() { + this.currentIndex = -1; + return this[this.currentIndex]; +}; + +Array.prototype.peek = function() { + return this[this.currentIndex + 1]; +}; + +Array.prototype.seek = function() { + this.currentIndex = 0; + return this[this.currentIndex]; +}; + +//End of lazy enumerator methods for Array + Array.prototype.collect = function (item) { var items = []; try { for (var i = 0; i < this.length; i++) { items.push(this[i][item]); @@ -408,10 +598,13 @@ Array.prototype.empty = function () { return (this.length == 0); }; //String Extensions +String.prototype.contains = function () { + return String.prototype.indexOf.apply(this, arguments) !== -1; +}; String.prototype.underscore = function () { return this.replace(/\s/g, "_"); }; @@ -442,7 +635,20 @@ for (var i = 0; i < len; i++) { if (i > 0) titleized += ' '; titleized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1); } return titleized; +}; + +//Function Extensions + +Function.prototype.bindToEventHandler = function bindToEventHandler() { + var handler = this; + var boundParameters = Array.prototype.slice.call(arguments); + //create closure + return function (e) { + e = e || window.event; // get window.event if e argument missing (in IE) + boundParameters.unshift(e); + handler.apply(this, boundParameters); + } };