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