      1 /**
      2  * @ignore
      3  */
      4 var Ozone = Ozone || {};
      5 Ozone.util = Ozone.util || {};
      6 Ozone.util.formField = Ozone.util.formField || {};
      7 Ozone.config = Ozone.config || {};
      8
      9 /**
     10  * @private
     11  *
     12  * @description This method is useful if you want to see if a specfic url is the same
     13  * as the local url.  Can be used to test if AJAX can be used.  If url
     14  * is not a URL, it's assumed to be a relative filename, so this function
     15  * returns true.
     16  *
     17  * @param {String} url String that you want to check to see if its local.
     18  *
     19  * @returns Boolean
     20  *
     21  * @throws "Not a valid URL" if the parameter is not a valid url
     22  */
     23 Ozone.util.isUrlLocal = function(url) {
     24
     25     var webContextPath = Ozone.util.contextPath();
     26
     27     //append last '/' this value should never be null
     28     if (webContextPath != '' && webContextPath != null) {
     29         webContextPath += '/';
     30     }
     31
     32     //this regex matches urls against the configured webcontext path https://<contextPath>/.....
     33     //only one match is possible since this regex matches from the start of the string
     34     var regex = new RegExp("^(https?:)//([^/:]+):?(.*)" + webContextPath);
     35     var server = url.match(regex);
     36
     37     //check if this might be a relative url
     38     if (!server) {
     39         if (url.match(new RegExp('^https?:\/\/'))) {
     40             return false;
     41         }
     42         else {
     43             return true;
     44         }
     45     }
     46
     47     var port = window.location.port;// || ( window.location.protocol === "https:" ? "443" : "80" )
     48
     49     //todo need to find a better way of checking same domain requests
     50     // see if solution posted here works: http://stackoverflow.com/questions/9404793/check-if-same-origin-policy-applies
     51     return window.location.protocol === server[1] && window.location.hostname === server[2] && port === server[3]
     52 };
     53
     54 /**
     55  * @private
     56  *
     57  * @description This method will convert a string into a json object.  There is a check
     58  * done to ensure no unsafe json is included.
     59  *
     60  * @param {String} str String that represents a json object
     61  *
     62  * @returns {Object} json object
     63  *
     64  * @throws Error if parameter is not a string
     65  * @throws Error if secure check finds unsafe JSON
     66  * @throws Error if there is an issue converting to JSON
     67  *
     68  * @requires dojox.secure.capability
     69  * @requires dojo base
     70  */
     71 Ozone.util.parseJson = function(str) {
     72     if (typeof(str) === 'string') {
     73         owfdojox.secure.capability.validate(str,[],{}); // will error if there is unsafe JSON
     74         var x = owfdojo.fromJson(str);
     75         return x;
     76     } else {
     77         throw "Ozone.util.parseJson expected a string, but didn't get one";
     78     }
     79 };
     80
     81 /**
     82  * @private
     83  */
     84 Ozone.util.HTMLEncodeReservedJS = function(str) {
     85     return str.replace(/"/g, '"').replace(/'/g, "'");
     86 };
     87
     88 /**
     89  * @private
     90  *
     91  * @description Similar to Ext.util.Format.htmlEncode except this method also handles the single quote
     92  */
     93 Ozone.util.HTMLEncode = function(str){
     94     return !str ? str : String(str).replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """).replace(/'/g, "'");
     95 };
     96
     97 /**
     98  * @private
     99  *
    100  * Constructively clears the entire array
    101  */
    102 Ozone.util.parseWindowNameData = function() {
    103   var configParams = null;
    104   return function() {
    105
    106     //if already parsed just return the value
    107     if (configParams) return configParams;
    108
    109     //parse out the config
    110     try {
    111       configParams = Ozone.util.parseJson(
    112               window.name
    113       );
    114       return configParams;
    115     }
    116     catch (e) {
    117       return null;
    118     }
    119   }
    120 }();
    121
    122
    123 /**
    124  * @private
    125  *
    126  * @description This method returns the current context path by
    127  * calling a controller at the server (will not
    128  * make the call if it has already been done).
    129  *
    130  * @param {Object} o unused
    131  *
    132  * @returns context path with leading slash
    133  *          (ex. "/owf")
    134  *
    135  * @requires Ext base, dojo
    136  */
    137 Ozone.util.contextPath = (function() {
    138     var configParams = Ozone.util.parseWindowNameData(),
    139         contextPath = Ozone.config.webContextPath;
    140
    141     if (configParams && configParams.webContextPath) {
    142         contextPath = Ozone.config.webContextPath = configParams.webContextPath;
    143     }
    144
    145     return function () {
    146         return contextPath || '';
    147     }
    148 })();
    149
    150 /**
    151  * @private
    152  *
    153  * @description Checks whether the url context is local or not,
    154  * then returns the valid url w/ context.
    155  *
    156  * @param {String} value the url
    157  * @param {String} validContext valid context
    158  *
    159  * @returns valid url w/context if necessary, if not local then the url
    160  */
    161 Ozone.util.validUrlContext = function(value, validContext){
    162     var containsRelDotPath = (value.indexOf("../") == 0)?true:false;
    163     var containsRelPath = (value.indexOf("/") == 0)?true:false;
    164     var isLocalUrl = (containsRelPath || containsRelDotPath || (value.indexOf("localhost") == 7) || (value.indexOf("localhost") == 11) || (value.indexOf("127.0.0.1") == 7)) ? true : false;
    165     var urlValidContext = value;
    166     validContext = ((validContext == undefined) ? Ozone.util.contextPath() : validContext);
    167     if((isLocalUrl == true) && (validContext != null)){
    168         if(containsRelPath){
    169             urlValidContext = String.format("{0}{1}", validContext, value);
    170         }else if(containsRelDotPath){
    171             var valuePathNoRel = value.substring(3);
    172             urlValidContext = String.format("{0}/{1}", validContext, valuePathNoRel);
    173         }
    174     }
    175     return urlValidContext;
    176 };
    177
    178 /**
    179  * @private
    180  *
    181  * @description This method centralizes the container relay file for
    182  * RPC calls.
    183  *
    184  * @returns relay file path with context
    185  */
    186 Ozone.util.getContainerRelay = function() {
    187     return Ozone.util.contextPath() +
    188     '/js/eventing/rpc_relay.uncompressed.html';
    189 };
    190
    191 /**
    192  * @private
    193  *
    194  * @description This method will convert anything to a string.
    195  * There is no check for recursion, so don't do that
    196  *
    197  * @param {Object} obj object to convert
    198  *
    199  * @returns string
    200  *
    201  * @requires dojo base
    202  */
    203 Ozone.util.toString = function(obj) {
    204     if (typeof(obj) === 'object') {
    205         return owfdojo.toJson(obj);
    206     } else {
    207         return obj+'';
    208     }
    209 };
    210
    211 /**
    212  * @private
    213  */
    214 Ozone.util.formatWindowNameData = function(data) {
    215     // this value needs to be not uri encoded
    216     // return decodeURIComponent(owfdojo.objectToQuery(data));
    217     return Ozone.util.toString(data);
    218 };
    219
    220 /**
    221  * @private
    222  *
    223  * Removes the Leading and Trailing Spaces in any ExtJs Form Field
    224  */
    225 Ozone.util.formField.removeLeadingTrailingSpaces = function(thisField){
    226     var thisField_noLeadingTrailingSpacesValue = thisField.getValue().replace(new RegExp(Ozone.lang.regexLeadingTailingSpaceChars), '');
    227     thisField.setValue(thisField_noLeadingTrailingSpacesValue);
    228     return thisField_noLeadingTrailingSpacesValue;
    229 };
    230
    231 if (!Ozone.util.ModalBox) {
    232     Ozone.util.ModalBox = function() {
    233         var ab = 'absolute';
    234         var n = 'none';
    235         var obody = document.getElementsByTagName('body')[0];
    236         var frag = document.createDocumentFragment();
    237         var obol = document.createElement('div');
    238         obol.setAttribute('id', 'ol');
    239         obol.style.display = n;
    240         obol.style.position = ab;
    241         obol.style.top = 0;
    242         obol.style.left = 0;
    243         obol.style.zIndex = 10000;
    244         obol.style.width = '100%';
    245         obol.style.backgroundColor = "#555";
    246         obol.style.filter = 'alpha(opacity=50)';
    247         obol.style.MozOpacity = 0.5;
    248         obol.style.opacity = 0.5;
    249
    250
    251         frag.appendChild(obol);
    252         this.obol = obol;
    253         var obbx = document.createElement('div');
    254         obbx.setAttribute('id', 'mbox');
    255         obbx.style.display = n;
    256         obbx.style.position = ab;
    257         obbx.style.backgroundColor = '#eee';
    258         obbx.style.padding = '8px';
    259         obbx.style.border = '2px outset #666';
    260         obbx.style.zIndex = 10001;
    261         this.obbx = obbx;
    262         var obl = document.createElement('span');
    263         obbx.appendChild(obl);
    264         var obbxd = document.createElement('div');
    265         obbxd.setAttribute('id', 'mbd');
    266         var d = document.createElement('div');
    267         var txt = document.createElement('span');
    268         txt.innerHTML = 'Press OK to continue.';
    269         this.txt = txt;
    270         var d2 = document.createElement('div');
    271         d2.style.textAlign = 'center';
    272         d2.style.fontSize = '14px';
    273         d2.appendChild(txt);
    274         d2.appendChild(document.createElement('br'));
    275         var but = document.createElement('button');
    276
    277         but.innerHTML = 'OK';
    278         var self = this;
    279         but.onclick= function()
    280         {
    281             self.hide();
    282         }
    283         d2.appendChild(but);
    284         d.appendChild(d2);
    285         obbxd.appendChild(d);
    286         this.obbxd = obbxd;
    287         obl.appendChild(obbxd);
    288         frag.insertBefore(obbx, obol.nextSibling);
    289         obody.insertBefore(frag, obody.firstChild);
    290         window.onscroll = function(){
    291             self.scrollFix();
    292         }
    293         window.onresize = function(){
    294             self.sizeFix();
    295         }
    296     }
    297
    298     Ozone.util.ModalBox.prototype.pageWidth = function () {
    299         return window.innerWidth != null ? window.innerWidth
    300         : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth
    301         : document.body != null ? document.body.clientWidth : null;
    302     }
    303     Ozone.util.ModalBox.prototype.pageHeight = function () {
    304         return window.innerHeight != null ? window.innerHeight
    305         : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight
    306         : document.body != null ? document.body.clientHeight : null;
    307     }
    308     Ozone.util.ModalBox.prototype.posLeft = function() {
    309         return typeof window.pageXOffset != 'undefined' ? window.pageXOffset
    310         : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft
    311         : document.body.scrollLeft ? document.body.scrollLeft : 0;
    312     }
    313     Ozone.util.ModalBox.prototype.posTop = function () {
    314         return typeof window.pageYOffset != 'undefined' ? window.pageYOffset
    315         : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop
    316         : document.body.scrollTop ? document.body.scrollTop : 0;
    317     }
    318
    319     Ozone.util.ModalBox.prototype.scrollFix = function()
    320     {
    321         //var obol = $('ol');
    322         this.obol.style.top = this.posTop() + 'px';
    323         this.obol.style.left = this.posLeft() + 'px'
    324     }
    325     Ozone.util.ModalBox.prototype.sizeFix = function () {
    326         //var obol = $('ol');
    327         this.obol.style.height = this.pageHeight() + 'px';
    328         this.obol.style.width = this.pageWidth() + 'px';
    329         var tp = this.posTop() + ((this.pageHeight() - this.height) / 2) - 12;
    330         var lt = this.posLeft() + ((this.pageWidth() - this.width) / 2) - 12;
    331         this.obbx.style.top = (tp < 0 ? 0 : tp) + 'px';
    332         this.obbx.style.left = (lt < 0 ? 0 : lt) + 'px';
    333
    334     }
    335     Ozone.util.ModalBox.prototype.kp = function (e) {
    336         ky = e ? e.which : event.keyCode;
    337         if (ky == 88 || ky == 120)
    338             this.hide();
    339         return false
    340     }
    341     Ozone.util.ModalBox.prototype.inf = function (h) {
    342         tag = document.getElementsByTagName('select');
    343         for (i = tag.length - 1; i >= 0; i--)
    344             tag[i].style.visibility = h;
    345         tag = document.getElementsByTagName('iframe');
    346         for (i = tag.length - 1; i >= 0; i--)
    347             tag[i].style.visibility = h;
    348         tag = document.getElementsByTagName('object');
    349         for (i = tag.length - 1; i >= 0; i--)
    350             tag[i].style.visibility = h;
    351     }
    352     Ozone.util.ModalBox.prototype.hide = function () {
    353         var v = 'visible';
    354         var n = 'none';
    355         this.obol.style.display = n;
    356         this.obbx.style.display = n;
    357         this.inf(v);
    358         document.onkeypress = ''
    359     }
    360     Ozone.util.ModalBox.prototype.show = function (msg,wd, ht) {
    361         var h = 'hidden';
    362         var b = 'block';
    363         var p = 'px';
    364         wd = wd | 200;
    365         ht = ht | 100;
    366         var obol = this.obol
    367         var obbxd = this.obbxd;
    368         this.txt.innerHTML = msg;
    369         obol.style.height = this.pageHeight() + p;
    370         obol.style.width = this.pageWidth() + p;
    371         obol.style.top = this.posTop() + p;
    372         obol.style.left = this.posLeft() + p;
    373         obol.style.display = b;
    374         var tp = this.posTop() + ((this.pageHeight() - ht) / 2) - 12;
    375         var lt = this.posLeft() + ((this.pageWidth() - wd) / 2) - 12;
    376         var obbx = this.obbx
    377         obbx.style.top = (tp < 0 ? 0 : tp) + p;
    378         obbx.style.left = (lt < 0 ? 0 : lt) + p;
    379         obbx.style.width = wd + p;
    380         obbx.style.height = ht + p;
    381         this.inf(h);
    382         obbx.style.display = b;
    383         this.width = wd;
    384         this.height = ht;
    385         return false;
    386     }
    387 }
    388 if (!Ozone.util.ErrorDlg)
    389 {
    390     Ozone.util.ErrorDlg = {};
    391     Ozone.util.ErrorDlg.show=function(msg,width,height)
    392     {
    393         if (!this.dlgBox)
    394             this.dlgBox = new Ozone.util.ModalBox();
    395         this.dlgBox.show(msg,width,height);
    396     };
    397 }
    398
    399
    400 /**
    401  * @private
    402  */
    403 Ozone.util.fireBrowserEvent = function(dom, type, bubble, cancelable) {
    404     if (document.createEvent) {
    405         var event = document.createEvent('Events');
    406         event.initEvent(type, bubble || true, cancelable || true);
    407         dom.dispatchEvent(event);
    408     }
    409     else if (document.createEventObject) {
    410         dom.fireEvent('on' + type);
    411     }
    412 };
    413
    414 /**
    415     Clones dashboard and returns a dashboard cfg object that can be used to create new dashboards.
    416     @name cloneDashboard
    417     @methodOf OWF.Util
    418
    419     @returns Object dashboard cfg object that can be used to create new dashboards.
    420  */
    421 Ozone.util.cloneDashboard = function(dashboardCfg, regenerateIds, removeLaunchData) {
    422
    423     var dashboardStr = Ozone.util.toString(dashboardCfg);
    424
    425     if(regenerateIds === true) {
    426         var newDashboardGuid = guid.util.guid(),
    427             dashboardGuid = dashboardCfg.guid;
    428
    429         // update dashboard guid
    430         dashboardStr = dashboardStr.replace(new RegExp(dashboardGuid, 'g'), newDashboardGuid);
    431         dashboardStrCopy = dashboardStr;
    432
    433         // update widget instance ids
    434         var widgetInstanceIdRegex = /\"uniqueId\"\:\"([A-Fa-f\d]{8}-[A-Fa-f\d]{4}-[A-Fa-f\d]{4}-[A-Fa-f\d]{4}-[A-Fa-f\d]{12})\"/g;
    435         var result;
    436         while ((result = widgetInstanceIdRegex.exec(dashboardStrCopy)) != null) {
    437             // var msg = "Found " + result[1];
    438             // console.log(msg);
    439             dashboardStr = dashboardStr.replace(new RegExp(result[1], 'g'), guid.util.guid());
    440         }
    441     }
    442
    443     var dashboard = Ozone.util.parseJson(dashboardStr);
    444
    445     if(removeLaunchData === true) {
    446         var cleanData = function(cfg) {
    447             if(!cfg || !cfg.items)
    448                 return;
    449
    450             if(cfg.items.length === 0 && cfg.widgets) {
    451                 for(var i = 0, len = cfg.widgets.length; i < len; i++) {
    452                     delete cfg.widgets[i].launchData;
    453                 }
    454             }
    455             else {
    456                 for(var i = 0, len = cfg.items.length; i < len; i++) {
    457                     cleanData(cfg.items[i]);
    458                 }
    459
    460             }
    461         };
    462
    463         cleanData(dashboard.layoutConfig);
    464     }
    465
    466     return dashboard;
    467 };
    468
    469 Ozone.util.createRequiredLabel= function(label) {
    470     return "<span class='required-label'>" + label + " <span class='required-indicator'>*</span></span>";
    471 }