otsdaq_utilities  v2_05_02_indev
Debug.js
1 //=====================================================================================
2 //
3 // Created Dec, 2012
4 // by Ryan Rivera ((rrivera at fnal.gov))
5 //
6 // Debug.js
7 //
8 // Since different browser have different console print statements, all ots code
9 // should use Debug.log(str,num[optional, default=0]). Where str is the string to print to console and
10 // num is the priority number 0 being highest.
11 //
12 // Debug.log(args) will print to console if Debug.mode = 1 and if num < Debug.level
13 // Debug.logv({var}) will decorate a variable and print to console with Debug.log()
14 //
15 // Debug.err(args) will colorize console and Error popup
16 // Debug.warn(args) will colorize console and Warning popup
17 // Debug.info(args) will colorize console and Info popup
18 // Debug.med(args) will colorize console but no popup
19 //
20 // Note: An error pop up box will occur so that users see Debug.HIGH_PRIORITY log messages.
21 // Note: A warning pop up box will occur so that users see Debug.WARN_PRIORITY log messages.
22 // Note: An info pop up box will occur so that users see Debug.INFO_PRIORITY log messages.
23 //
24 //=====================================================================================
25 
26 var Debug = Debug || {}; //define Debug namespace
27 
28 Debug.mode = 1; //0 - debug off, 1 - debug on
29 Debug.simple = 0; //0 - use priority (more detail in printout), 1 - simple, no priority
30 Debug.level = 100; //priority level, (100 should be all, 0 only high priority)
31  //all logs with lower priority level are printed
32 
33 Debug.lastLog = "";
34 Debug.lastLogger = "";
35 
36 Debug.prependMessage = ""; //use to have a message always show up before log messages
37 
38 //setup default priorities
39 Debug.HIGH_PRIORITY = {"DEBUG_PRIORITY":0};
40 Debug.WARN_PRIORITY = {"DEBUG_PRIORITY":1}; //1;
41 Debug.INFO_PRIORITY = {"DEBUG_PRIORITY":2}; //2;
42 Debug.TIP_PRIORITY = {"DEBUG_PRIORITY":3}; //3;
43 Debug.MED_PRIORITY = {"DEBUG_PRIORITY":50};// 50;
44 Debug.LOW_PRIORITY = {"DEBUG_PRIORITY":100}; //100;
45 
46 
47 //determine if chrome or firefox or other
48 // 0:other, 1:chrome, 2:firefox
49 Debug.BROWSER_TYPE_OTHER = 0;
50 Debug.BROWSER_TYPE_CHROME = 1;
51 Debug.BROWSER_TYPE_FIREFOX = 2;
52 Debug.BROWSER_TYPE = Debug.BROWSER_TYPE_OTHER;
53 {
54  var tmp = (new Error).stack;
55  if(tmp[0] == 'E')
56  Debug.BROWSER_TYPE = Debug.BROWSER_TYPE_CHROME;
57  else if(tmp[0] == '@')
58  Debug.BROWSER_TYPE = Debug.BROWSER_TYPE_FIREFOX;
59 }
60 console.log("Browser type = ", Debug.BROWSER_TYPE);
61 
62 
63 if (Debug.mode) //IF DEBUG MODE IS ON!
64 {
65  //load dialog fonts
66  try
67  {
68  Debug.FontInconsolata = new FontFace('Inconsolata', 'url(/WebPath/fonts/inconsolata/Inconsolata-Regular.ttf)');
69  document.fonts.add(Debug.FontInconsolata);
70  } catch(e){console.log("Ignoring font errors: " + e);}
71  try
72  {
73  Debug.FontComfortaa = new FontFace('Comfortaa', 'url(/WebPath/fonts/comfortaa/Comfortaa-Regular.ttf)');
74  document.fonts.add(Debug.FontComfortaa);
75  } catch(e){console.log("Ignoring font errors: " + e);}
76  try
77  {
78  Debug.FontInconsolataBold = new FontFace('Inconsolata-Bold', 'url(/WebPath/fonts/inconsolata/Inconsolata-Bold.ttf)');
79  document.fonts.add(Debug.FontInconsolataBold);
80  } catch(e){console.log("Ignoring font errors: " + e);}
81  try
82  {
83  Debug.FontComfortaaBold = new FontFace('Comfortaa-Bold', 'url(/WebPath/fonts/comfortaa/Comfortaa-Bold.ttf)');
84  document.fonts.add(Debug.FontComfortaaBold);
85  } catch(e){console.log("Ignoring font errors: " + e);}
86  try
87  {
88  Debug.FontComfortaaLight = new FontFace('Comfortaa-Light', 'url(/WebPath/fonts/comfortaa/Comfortaa-Light.ttf)');
89  document.fonts.add(Debug.FontComfortaaLight);
90  } catch(e){console.log("Ignoring font errors: " + e);}
91 
92  //========================
93  //special quick decoration for a variable
94  // Note: must call with brackets e.g. Debug.logv({firstInit_});
95  Debug.logv = varObj =>
96  {
97  var keys = Object.keys(varObj);
98  Debug.log(keys[0] + " \t= ",varObj[keys[0]]);
99  }; //end Debug.logv()
100  //========================
101  //special hot functions to priorities
102  Debug.presetPriority = Debug.LOW_PRIORITY.DEBUG_PRIORITY; //pre-set input priority
103  Debug.err = function() { //colorize console and popup
104  Debug.presetPriority = Debug.HIGH_PRIORITY.DEBUG_PRIORITY;
105  if(Debug.BROWSER_TYPE == 1) //chrome
106  {
107  Debug.lastLogger = (new Error).stack.split("\n")[2];
108  Debug.lastLog = Debug.lastLogger.slice(0,Debug.lastLogger.indexOf(' ('));
109  Debug.lastLogger = Debug.lastLogger.slice(Debug.lastLog.length+2,
110  Debug.lastLogger.length-1);
111  }
112  else if(Debug.BROWSER_TYPE == 2) //firefox
113  {
114  Debug.lastLogger = (new Error).stack.split("\n")[1];
115  Debug.lastLog = Debug.lastLogger.slice(0,Debug.lastLogger.indexOf('@'));
116  Debug.lastLogger = Debug.lastLogger.slice(Debug.lastLog.length+1,
117  Debug.lastLogger.length);
118  }
119  Debug.log.apply(null,arguments); //use apply to keep args consistent
120  }
121  Debug.warn = function() { //colorize console and popup
122  Debug.presetPriority = Debug.WARN_PRIORITY.DEBUG_PRIORITY;
123  if(Debug.BROWSER_TYPE == 1) //chrome
124  {
125  Debug.lastLogger = (new Error).stack.split("\n")[2];
126  Debug.lastLog = Debug.lastLogger.slice(0,Debug.lastLogger.indexOf(' ('));
127  Debug.lastLogger = Debug.lastLogger.slice(Debug.lastLog.length+2,
128  Debug.lastLogger.length-1);
129  }
130  else if(Debug.BROWSER_TYPE == 2) //firefox
131  {
132  Debug.lastLogger = (new Error).stack.split("\n")[1];
133  Debug.lastLog = Debug.lastLogger.slice(0,Debug.lastLogger.indexOf('@'));
134  Debug.lastLogger = Debug.lastLogger.slice(Debug.lastLog.length+1,
135  Debug.lastLogger.length);
136  }
137  Debug.log.apply(null,arguments); //use apply to keep args consistent
138  }
139  Debug.info = function() { //colorize console and popup
140  Debug.presetPriority = Debug.INFO_PRIORITY.DEBUG_PRIORITY;
141  if(Debug.BROWSER_TYPE == 1) //chrome
142  {
143  Debug.lastLogger = (new Error).stack.split("\n")[2];
144  Debug.lastLog = Debug.lastLogger.slice(0,Debug.lastLogger.indexOf(' ('));
145  Debug.lastLogger = Debug.lastLogger.slice(Debug.lastLog.length+2,
146  Debug.lastLogger.length-1);
147  }
148  else if(Debug.BROWSER_TYPE == 2) //firefox
149  {
150  Debug.lastLogger = (new Error).stack.split("\n")[1];
151  Debug.lastLog = Debug.lastLogger.slice(0,Debug.lastLogger.indexOf('@'));
152  Debug.lastLogger = Debug.lastLogger.slice(Debug.lastLog.length+1,
153  Debug.lastLogger.length);
154  }
155  Debug.log.apply(null,arguments); //use apply to keep args consistent
156  }
157  Debug.med = function() { //colorize console but no popup
158  Debug.presetPriority = Debug.MED_PRIORITY.DEBUG_PRIORITY;
159  if(Debug.BROWSER_TYPE == 1) //chrome
160  {
161  Debug.lastLogger = (new Error).stack.split("\n")[2];
162  Debug.lastLog = Debug.lastLogger.slice(0,Debug.lastLogger.indexOf(' ('));
163  Debug.lastLogger = Debug.lastLogger.slice(Debug.lastLog.length+2,
164  Debug.lastLogger.length-1);
165  }
166  else if(Debug.BROWSER_TYPE == 2) //firefox
167  {
168  Debug.lastLogger = (new Error).stack.split("\n")[1];
169  Debug.lastLog = Debug.lastLogger.slice(0,Debug.lastLogger.indexOf('@'));
170  Debug.lastLogger = Debug.lastLogger.slice(Debug.lastLog.length+1,
171  Debug.lastLogger.length);
172  }
173  Debug.log.apply(null,arguments); //use apply to keep args consistent
174  } //end special hot functions
175  //========================
176 
177 
178  if (Debug.simple)
179  {
180  //If want default console.log use this:
181  Debug.log = console.log.bind(window.console);
182  }
183  else
184  {
185 
186  //========================
187  //For fancy priority management use this:
188  Debug.log = function()//str,num)
189  {
190  //make num optional and default to lowest priority
191  var num = Debug.presetPriority;
192  var argsInStr = arguments.length;
193  if(arguments.length > 1 &&
194  arguments[arguments.length-1] !== undefined &&
195  arguments[arguments.length-1].DEBUG_PRIORITY !== undefined)
196  {
197  num = arguments[arguments.length-1].DEBUG_PRIORITY;
198  --argsInStr;
199  }
200  Debug.presetPriority = Debug.LOW_PRIORITY.DEBUG_PRIORITY; //reset preset
201 
202  var str = ""; //build from arguments
203  var useStrOnly = false;
204  //just do normal color if one argument string
205  if(argsInStr == 1 && typeof(arguments[0]) == "string")
206  useStrOnly = true;
207 
208  for(var i=0;i<argsInStr;++i)
209  str += arguments[i] + ' ';
210 
211  //add call out labels to file [line] text blobs
212  var returnStr;
213 
214  if(num < 4) //modify string for popup
215  returnStr = localCallOutDebugLocales(str);
216  if(returnStr)
217  str = returnStr;
218 
219 
220  if(Debug.level < 0) Debug.level = 0; //check for crazies, 0 is min level
221  if(Debug.mode && num <= Debug.level)
222  {
223  str = Debug.prependMessage + str; //add prepend message
224 
225  var type = num < 4?
226  (num==0?"High":(num==1?"Warn":(num==2?"Info":"Tip")))
227  :(num<99?"Med":"Low");
228 
229  //if not pre-poulated, get caller info
230  if(!Debug.lastLogger || Debug.lastLogger.length == 0)
231  {
232  if(Debug.BROWSER_TYPE == 1) //chrome
233  {
234  Debug.lastLogger = (new Error).stack.split("\n")[2];
235  Debug.lastLog = Debug.lastLogger.slice(0,Debug.lastLogger.indexOf(' ('));
236  Debug.lastLogger = Debug.lastLogger.slice(Debug.lastLog.length+2,
237  Debug.lastLogger.length-1);
238  }
239  else if(Debug.BROWSER_TYPE == 2) //firefox
240  {
241  Debug.lastLogger = (new Error).stack.split("\n")[1];
242  Debug.lastLog = Debug.lastLogger.slice(0,Debug.lastLogger.indexOf('@'));
243  Debug.lastLogger = Debug.lastLogger.slice(Debug.lastLog.length+1,
244  Debug.lastLogger.length);
245  }
246  }
247 
248  var source = window.location.href;
249  source = source.substr(source.lastIndexOf('/'));
250  source = source.substr(0,source.indexOf('?'));
251 
252  if(useStrOnly)
253  console.log("%c" + type + "-Priority" +
254  ":\t " + Debug.lastLog + " from " + source + ":\n" +
255  Debug.lastLogger + "::\t" + str,
256  num == 0?"color:#F30;" //chrome/firefox allow css styling
257  :(num == 1?"color:#F70" //warn
258  :(num < 99?"color:#092":"color:#333")));
259  else
260  {
261  var consoleArguments = [];
262  consoleArguments.push("%c" + type + "-Priority" +
263  ":\t " + Debug.lastLog + " from " + source + ":\n" +
264  Debug.lastLogger + "::\t");
265  consoleArguments.push(num == 0?"color:#F30;" //chrome/firefox allow css styling
266  :(num == 1?"color:#F70" //warn
267  :(num < 99?"color:#092":"color:#333")));
268 
269  for(var i=0;i<argsInStr;++i)
270  consoleArguments.push(arguments[i]);
271 
272  //pass arguments using special apply for arg consistency
273  console.log.apply(null,consoleArguments);
274  }
275 // console.log("%c" + type + "-Priority" +
276 // ":\t " + Debug.lastLog + " from " + source + ":\n" +
277 // Debug.lastLogger + "::\t",
278 // num == 0?"color:#F30;" //chrome/firefox allow css styling
279 // :(num == 1?"color:#F70" //warn
280 // :(num < 99?"color:#092":"color:#333")),
281 // arguments);
282  Debug.lastLog = str;
283  Debug.lastLogger = ""; //clear for next
284 
285  if(num < 4) //show all high priorities as popup!
286  Debug.errorPop(str,num);
287  }
288 
290  //localCallOutDebugLocales ~~
291  // add call out labels to file [line] text blobs
292  // returns undefined if no change
293  function localCallOutDebugLocales(str)
294  {
295  var i = 0;
296  var j,k,l;
297  var returnStr;
298  try
299  {
300  while((j = str.indexOf('[',i)) > 0 && (k = str.indexOf(']',i)) > 0)
301  {
302  if(j < 4)
303  {
304  i = k+1;
305  continue; //skip, too soon to be valid
306  }
307 
308  //found new possible call out
309  //console.log(str.substr(j,k-j+1));
310 
311  if(!returnStr) //check if need to define for the first time
312  returnStr = "";
313 
314  //look for icc, .cc and .h
315  if((str[j-3] == '.' && str[j-2] == 'h') ||
316  ((str[j-4] == '.' || str[j-4] == 'i') &&
317  str[j-3] == 'c' && str[j-2] == 'c'))
318  {
319  //find beginning of blob (first non-file/c++ character)
320  for(l = j-3; l >= i; --l)
321  if(!((str[l] >= 'a' && str[l] <= 'z') ||
322  (str[l] >= 'A' && str[l] <= 'Z') ||
323  (str[l] >= '0' && str[l] <= '9') ||
324  (str[l] == '.') ||
325  (str[l] == '_') ||
326  (str[l] == '-') ||
327  (str[l] == '/') ||
328  (str[l] == ':')))
329  break; //found beginning (-1)
330 
331  ++l; //increment to first character of blob
332 
333 
334  //previous chunk
335  returnStr += str.substr(i,l-i);
336 
337  //add label
338  returnStr += "<br><label class='" +
339  Debug._errBoxId + "-localCallOut'>";
340 
341  //add callout
342  returnStr += str.substr(l,k+1-l);
343 
344  //add end label
345  returnStr += "</label><br>";
346 
347  //skip any tabs and new lines (so that the next content is right below line #)
348  while(k+1 < str.length &&
349  (str[k+1] == '\n' || str[k+1] == '\t')) ++k;
350 
351  }
352  else //not a call out so grab previous chunk
353  returnStr += str.substr(i,k+1-i);
354 
355  i = k+1;
356  }
357  }
358  catch(e)
359  {
360  return undefined; //give up on errors
361  }
362 
363 
364  if(returnStr) //finish last chunk
365  returnStr += str.substr(i);
366 
367 
368  return returnStr; //if untouched, undefined return
369  }
370  }; //end Debug.log()
371  } //end Debug normal
372 
373 }
374 else //IF DEBUG MODE IS OFF!
375 { //do nothing with log functions
376  console.log = function(){};
377  Debug.log = function(){};
378  Debug.logv = function(){};
379  Debug.err = function(){};
380  Debug.warn = function(){};
381  Debug.info = function(){};
382  Debug.med = function(){};
383 }
384 
385 
386 // living and breathing examples:
387 Debug.log("Debug mode is on at level: " + Debug.level);
388 Debug.log("This is an example for posterity that is not printed due to debug priority.",Debug.level+1);
389 
390 
391 
392 //=====================================================================================
393 //=====================================================================================
394 //Error pop up helpers
395 
396 Debug._errBox = 0;
397 Debug._errBoxId = "Debug-error-box";
398 Debug._errBoxOffX = 0;
399 Debug._errBoxOffY = 0;
400 Debug._errBoxOffW = 0;
401 Debug._errBoxOffH = 0;
402 
403 
404 Debug._ERR_TRUNCATION_LENGTH = 10000;
405 
406 //=====================================================================================
407 //Note: effectively doing this: str.replace(/\n/g , "<br>").replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
408 // ...but str.replace() was really slowing down everything on big strings
409 Debug.errorPopConditionString = function(str) {
410  var rstr = "";
411 
412  for(var i=0;i<str.length && i<Debug._ERR_TRUNCATION_LENGTH; ++i)
413  if(str[i] == '\n')
414  rstr += "<br>";
415  else if(str[i] == '\t')
416  rstr += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
417  else
418  rstr += str[i];
419  if(str.length > Debug._ERR_TRUNCATION_LENGTH)
420  rstr += "...&lt;&lt;&lt; MESSAGE TRUNCATED &gt;&gt;&gt;";
421 
422  return rstr;
423 } //end errorPopConditionString()
424 
425 //=====================================================================================
426 //Show the error string err in the error popup on the window
427 // create error div if not yet created
428 Debug.errorPop = function(err,severity)
429 {
430 
431  var errBoxAlpha = "1.0";
432 
433  //check if Debug._errBox has been set
434  if(!Debug._errBox)
435  {
436  //check if there is already an error box with same id and share
437  var el = document.getElementById(Debug._errBoxId);
438  if(!el) //element doesn't already exist, so we need to create the element
439  {
440  var body = document.getElementsByTagName("BODY")[0];
441  if(!body) //maybe page not loaded yet.. so wait to report
442  {
443  //try again in 1 second
444  window.setTimeout(function() { Debug.errorPop(err,{"DEBUG_PRIORITY":severity})}, 1000);
445  return;
446  }
447 
448  //create the element
449  el = document.createElement("div");
450  el.setAttribute("id", Debug._errBoxId);
451  el.style.display = "none";
452  var str = "<a class='" +
453  Debug._errBoxId +
454  "-header' onclick='javascript:Debug.closeErrorPop();event.stopPropagation();' onmousemove='event.stopPropagation();' onmouseup='event.stopPropagation();' onmousedown='event.stopPropagation();'>Close Errors</a>";
455  str =
456  "<div class='" +
457  Debug._errBoxId +
458  "-moveBar' style='" +
459  "position:absolute;width:100%;height:15px;top:0;left:0;background-color:rgb(191, 191, 191);cursor:move;" +
460  "outline: none; /* to stop firefox selection*/ -webkit-user-select: none; /* prevent selection*/ -moz-user-select: none; user-select: none;" +
461  "' " +
462  "onmousedown='javascript:Debug.handleErrorMoveStart(event);event.stopPropagation();' " +
463  "title='Click and drag to reposition this popup window.' " +
464  "></div>" +
465  "<br>"+
466  str + "<br>" +
467  "<div style='color:white;font-size:16px;padding-bottom:5px;'>" +
468  "Note: Newest messages are at the top." +
469  "<label style='color:white;font-size:11px;'><br>(Press [ESC] to close and [SHIFT + ESC] to re-open)</font>" +
470  "<div id='downloadIconDiv' onclick='Debug.downloadMessages()' onmouseup='event.stopPropagation();' onmousedown='event.stopPropagation();' title='Download messages to text file.' style='float: right; margin: -10px 30px -100px -100px; cursor: pointer'>" +
471  //make download arrow
472  "<div style='display: block; margin-left: 3px; height:7px; width: 6px; background-color: white;'></div>" +
473  "<div style='display: block; width: 0; height: 0; border-left: 6px solid transparent; border-right: 6px solid transparent; border-top: 8px solid white;'></div>" +
474  "<div style='position: relative; top: 5px; width: 12px; height: 2px; display: block; background-color: white;'></div>" +
475  "</div>" +
476  "</div>" +
477  "<div id='" +
478  Debug._errBoxId +
479  "-err' class='" +
480  Debug._errBoxId +
481  "-err'></div>" +
482  "<br>" + str;
483 
484  str += "<div class='" + Debug._errBoxId + "-resizeBarLeft' " +
485  "style='" +
486  "background-color:rgb(191, 191, 191);position:absolute;width:15px;height:15px;top:-1000px;left:0;cursor:nesw-resize;" +
487  "outline: none; /* to stop firefox selection*/ -webkit-user-select: none; /* prevent selection*/ -moz-user-select: none; user-select: none;" +
488  "' " +
489  "onmousedown='javascript:Debug.handleErrorResizeStart(event,1,1);event.stopPropagation();' " +
490  "title='Click and drag to resize vertically this popup window.' " +
491  "></div>";
492  str += "<div class='" + Debug._errBoxId + "-resizeBar' " +
493  "style='" +
494  "background-color:transparent;position:absolute;width:100%;height:5px;top:-1000px;left:15px;cursor:ns-resize;" +
495  "outline: none; /* to stop firefox selection*/ -webkit-user-select: none; /* prevent selection*/ -moz-user-select: none; user-select: none;" +
496  "' " +
497  "onmousedown='javascript:Debug.handleErrorResizeStart(event);event.stopPropagation();' " +
498  "title='Click and drag to resize this popup window.' " +
499  "></div>";
500  str += "<div class='" + Debug._errBoxId + "-resizeBarRight' " +
501  "style='" +
502  "background-color:rgb(191, 191, 191);position:absolute;width:15px;height:15px;top:-1000px;left:0;cursor:nwse-resize;" +
503  "outline: none; /* to stop firefox selection*/ -webkit-user-select: none; /* prevent selection*/ -moz-user-select: none; user-select: none;" +
504  "' " +
505  "onmousedown='javascript:Debug.handleErrorResizeStart(event,1);event.stopPropagation();' " +
506  "title='Click and drag to resize this popup window.' " +
507  "></div>";
508  el.innerHTML = str;
509  body.appendChild(el); //add element to body of page
510  el.focus();
511  el.onmousemove = function(){
512  //console.log("mm");
513  DesktopContent.mouseMove(event,true /*onlyDesktopFunction*/); //allow only desktop movement functionality
514 
515  //if doing some resize or movement, then stop blocking event propagation
516  if(Debug._errBoxOffResizeStartY == -1 &&
517  Debug._errBoxOffMoveStartX == -1 &&
518  Debug._errBoxOffResizeStartX == -1)
519  event.stopPropagation();
520  }
521  el.onmousedown = function(){console.log("debug down"); event.stopPropagation();}
522  el.onmouseup = function(){console.log("debug up"); event.stopPropagation();}
523 
525  function localDebugKeyDownListener(e)
526  {
527  //Debug.log("Debug keydown c=" + keyCode + " " + c + " shift=" + e.shiftKey +
528  // " ctrl=" + e.ctrlKey + " command=" + _commandKeyDown);
529 
530  if(!e.shiftKey && e.keyCode == 27) //ESCAPE key, close popup
531  {
532  e.preventDefault();
533  e.stopPropagation();
534  Debug.closeErrorPop();
535  }
536  else if(e.shiftKey && e.keyCode == 27) //SHIFT+ESCAPE key, bring back popup
537  {
538  e.preventDefault();
539  e.stopPropagation();
540  Debug.bringBackErrorPop();
541  }
542  } //end localDebugKeyDownListener()
543 
544  document.body.removeEventListener("keydown",localDebugKeyDownListener);
545  document.body.addEventListener("keydown",localDebugKeyDownListener);
546 
547 
548  //add style for error to page HEAD tag
549  var css = "";
550 
551  //give undefined things monopsace type
552  css += "#" + Debug._errBoxId + " *" +
553  "{font-family: 'Comfortaa', arial;" +//"{font-family: 'Inconsolata', monospace;" +
554  "font-weight: 200;" +
555  "font-size: 18px;" +
556  "color: rgb(255,200,100);" +
557  "-webkit-user-select: text;" +
558  "-moz-user-select: text;" +
559  "user-select: text;" +
560  "}\n\n";
561 
562 
563  //error close link style
564  css += "#" + Debug._errBoxId + " a" +
565  ", #" + Debug._errBoxId + " center b" +
566  "{color: white; text-decoration: none; font-weight: bold;" +
567  "font-size: 18px; font-family: 'Comfortaa', arial;" +
568  "}\n\n";
569  css += "#" + Debug._errBoxId + " a:hover" +
570  "{text-decoration: underline;" +
571  "cursor:pointer;" +
572  "}\n\n";
573 
574  //error italics, underline, bold
575  css += "#" + Debug._errBoxId + " i" +
576  ", #" + Debug._errBoxId + " u" +
577  "{" +
578  "font-size: 18px; font-family: 'Comfortaa', arial;" +
579  "}\n\n";
580  css += "#" + Debug._errBoxId + " b" +
581  "{" +
582  "font-weight: bold;" +
583  "color: rgb(255, 231, 187);" +
584  "}\n\n";
585 
586  //error box style
587  css += "#" + Debug._errBoxId +
588  "{" +
589  "position: absolute; display: none; border: 2px solid gray;" +
590  "background-color: rgba(153,0,51, " + errBoxAlpha + "); overflow-y: hidden;" +
591  "overflow-x: hidden; padding: 5px; -moz-border-radius: 2px;" +
592  "-webkit-border-radius: 2px; border-radius: 2px;" +
593  "font-size: 18px; z-index: 2147483647;" + //max 32 bit number z-index
594  "font-family: 'Comfortaa', arial; text-align: center;" +
595  "left: 8px; top: 8px; margin-right: 8px; " +
596  "}\n\n";
597 
598  //error box err text style
599  css += "#" + Debug._errBoxId + "-err" +
600  "{" +
601  "color: rgb(255,200,100); font-size: 18px;" +
602  "font-family: 'Comfortaa', arial;" +
603  "left: 8px; top: 8px; margin-right: 8px;" +
604  "margin-bottom:-12px;" +
605  "text-align: left;" +
606  "overflow-y: scroll;" +
607  "overflow-x: auto;" +
608  "width: 100%;" +
609  "-webkit-user-select: text;" +
610  "-moz-user-select: text;" +
611  "user-select: text;" +
612  "}\n\n";
613 
614  css += "#" + Debug._errBoxId + "-err i" +
615  //",#" + Debug._errBoxId + "-err b" +
616  ",#" + Debug._errBoxId + "-err u" +
617  //",#" + Debug._errBoxId + "-err div" +
618  "{" +
619  "color: rgb(255,200,100); font-size: 18px;" +
620  "font-family: 'Comfortaa', arial;" +
621  "text-align: left;" +
622  "-webkit-user-select: text;" +
623  "-moz-user-select: text;" +
624  "user-select: text;" +
625  "}\n\n";
626 
627  css += //"#" + Debug._errBoxId + "-err i" +
628  //",#" + Debug._errBoxId + "-err b" +
629  //",#" + Debug._errBoxId + "-err u" +
630  "#" + Debug._errBoxId + "-err div" +
631  "{" +
632  "color: rgb(255,200,100); font-size: 18px;" +
633  "font-family: 'Comfortaa', arial;" +
634  "left: 8px, top: 8px; margin-right: 8px;" +
635  "text-align: left;" +
636  "-webkit-user-select: text;" +
637  "-moz-user-select: text;" +
638  "user-select: text;" +
639  "}\n\n";
640 
641  css += "#" + Debug._errBoxId + "-err b" +
642  "{" +
643  "color: rgb(255,225,200); font-size: 18px;" +
644  "font-family: 'Comfortaa', arial;" +
645  "text-align: left;" +
646  "-webkit-user-select: text;" +
647  "-moz-user-select: text;" +
648  "user-select: text;" +
649  "}\n\n";
650 
651  css += "#" + Debug._errBoxId + " ." + Debug._errBoxId + "-localCallOut" +
652  "{font-size: 10px;}\n\n";//color: rgb(191, 185, 193);}\n\n";
653 
654  //add style element to HEAD tag
655  var style = document.createElement('style');
656 
657  if (style.styleSheet) {
658  style.styleSheet.cssText = css;
659  } else {
660  style.appendChild(document.createTextNode(css));
661  }
662 
663  document.getElementsByTagName('head')[0].appendChild(style);
664 
665  window.removeEventListener("resize",localResize);
666  window.removeEventListener("scroll",localScroll);
667  window.removeEventListener("mouseup",Debug.handleErrorMoveStop);
668  window.removeEventListener("mousemove",Debug.handleErrorMove);
669  window.addEventListener("resize",localResize);
670  window.addEventListener("scroll",localScroll);
671  window.addEventListener("mouseup",Debug.handleErrorMoveStop);
672  window.addEventListener("mousemove",Debug.handleErrorMove);
673  }
674  Debug._errBox = el;
675  }
676 
677  //have error popup element now, so fill it with new error
678 
679  var el = document.getElementById(Debug._errBoxId + "-err");
680  var str = el.innerHTML; //keep currently displayed errors
681  var d = new Date();
682  var wasAlreadyContent = false;
683 
684  //add new err to top of errors
685  if(str.length)
686  wasAlreadyContent = true;
687 
688  var tstr = d.toLocaleTimeString();
689  tstr = tstr.substring(0,tstr.lastIndexOf(' ')) + //convert AM/PM to am/pm with no space
690  (tstr[tstr.length-2]=='A'?"am":"pm");
691 
692  if(severity == Debug.TIP_PRIORITY.DEBUG_PRIORITY) //put oldest at top so it reads like a document
693  str = str +
694  (wasAlreadyContent?"<br>...<br>":"") +
695  "<label style='color:white;font-size:16px;'>" +
696  d.toLocaleDateString() +
697  " " + tstr + " (Tip) :</label><br>" +
698  Debug.errorPopConditionString(err);
699  else //normally put newest at top since likely highest priority
700  str = "<label style='color:white;font-size:16px;'>" +
701  d.toLocaleDateString() +
702  " " + tstr + " " +
703  (severity == Debug.INFO_PRIORITY.DEBUG_PRIORITY ? '(Info)':'')+
704  (severity == Debug.WARN_PRIORITY.DEBUG_PRIORITY ? '(Warning)':'') +
705  ":</label><br>" +
706  Debug.errorPopConditionString(err) +
707  (wasAlreadyContent?"<br>...<br>":"") +
708  str;
709 
710  el.innerHTML = str;
711 
712  //show the error box whereever the current scroll is
713  function localResize()
714  {
715  Debug._errBoxOffX = 0;
716  Debug._errBoxOffY = 0;
717  Debug._errBoxOffH = 0;
718  Debug._errBoxOffW = 0;
719  Debug.handleErrorResize();
720  }
721  function localScroll()
722  {
723  Debug.handleErrorResize();
724  }
725  Debug.handleErrorResize(); //first size
726 
727 
728  Debug._errBox.style.display = "block";
729 
730  //change color based on info
731 
732  var els = document.getElementsByClassName(Debug._errBoxId + "-header");
733  el = els[0];
734  switch(severity)
735  {
736  case Debug.TIP_PRIORITY.DEBUG_PRIORITY:
737  //don't change color or header for info, if there are still errors displayed
738  if(wasAlreadyContent &&
739  (el.innerHTML == "Close Errors" ||
740  el.innerHTML == "Close Warnings" ||
741  el.innerHTML == "Close Info"))
742  return;
743  el.innerHTML = "Close Tooltip";
744  Debug._errBox.style.backgroundColor = "rgba(0, 49, 99, " + errBoxAlpha + ")";//"rgba(0, 79, 160, " + errBoxAlpha + ")";
745  break;
746  case Debug.INFO_PRIORITY.DEBUG_PRIORITY:
747  //don't change color or header for info, if there are still errors displayed
748  if(wasAlreadyContent &&
749  (el.innerHTML == "Close Errors" ||
750  el.innerHTML == "Close Warnings"))
751  return;
752  el.innerHTML = "Close Info";
753  Debug._errBox.style.backgroundColor = "rgba(0,153,51, " + errBoxAlpha + ")";
754  break;
755  case Debug.WARN_PRIORITY.DEBUG_PRIORITY:
756  //don't change color or header for info, if there are still errors displayed
757  if(wasAlreadyContent &&
758  el.innerHTML == "Close Errors")
759  return;
760  el.innerHTML = "Close Warnings";
761  Debug._errBox.style.backgroundColor = "rgba(160, 79, 0, " + errBoxAlpha + ")";
762  break;
763  default: //Debug.HIGH_PRIORITY
764  el.innerHTML = "Close Errors";
765  Debug._errBox.style.backgroundColor = "rgba(153,0,51, " + errBoxAlpha + ")";
766  }
767  els[1].innerHTML = el.innerHTML;
768 } //end errorPop()
769 
770 Debug._errBoxLastContent = "";
771 //=====================================================================================
772 //Close the error popup on the window
773 Debug.closeErrorPop = function()
774 {
775  document.getElementById(Debug._errBoxId).style.display = "none";
776  Debug._errBoxLastContent = document.getElementById(Debug._errBoxId + "-err").innerHTML;
777  document.getElementById(Debug._errBoxId + "-err").innerHTML = ""; //clear string
778 }
779 //=====================================================================================
780 //Bring the error popup back
781 Debug.bringBackErrorPop = function()
782 {
783  document.getElementById(Debug._errBoxId + "-err").innerHTML = Debug._errBoxLastContent; //bring back string
784  document.getElementById(Debug._errBoxId).style.display = "block";
785 }
786 
787 
788 Debug._errBoxOffMoveStartX = -1;
789 Debug._errBoxOffMoveStartY;
790 Debug._errBoxOffResizeStartX = -1;
791 Debug._errBoxOffResizeStartY = -1;
792 //=====================================================================================
793 Debug.handleErrorMoveStart = function(e)
794 {
795  Debug.log("Move Start");
796  Debug._errBoxOffMoveStartX = e.screenX - Debug._errBoxOffX;
797  Debug._errBoxOffMoveStartY = e.screenY - Debug._errBoxOffY;
798 }
799 
800 //=====================================================================================
801 Debug.handleErrorResizeStart = function(e,resizeW,moveLeft)
802 {
803  Debug.log("Resize Start");
804  Debug._errBoxOffResizeStartY = e.screenY - Debug._errBoxOffH;
805  if(moveLeft)
806  {
807  Debug._errBoxOffMoveStartX = e.screenX - Debug._errBoxOffX;
808  Debug._errBoxOffResizeStartX = e.screenX + Debug._errBoxOffW;
809  }
810  else if(resizeW)
811  Debug._errBoxOffResizeStartX = e.screenX - Debug._errBoxOffW;
812 
813 } //end handleErrorResizeStart()
814 
815 //=====================================================================================
816 Debug.handleErrorMoveStop = function(e)
817 {
818  if(Debug._errBoxOffResizeStartY != -1) //resize stop
819  {
820  Debug.log("Resize Stop");
821  Debug._errBoxOffH = e.screenY - Debug._errBoxOffResizeStartY;
822  Debug._errBoxOffResizeStartY = -1; //done with resize
823 
824  if(Debug._errBoxOffMoveStartX != -1) //resize from left
825  {
826  Debug._errBoxOffX = e.screenX - Debug._errBoxOffMoveStartX;
827  Debug._errBoxOffW = Debug._errBoxOffResizeStartX - e.screenX;
828  Debug._errBoxOffMoveStartX = -1; //done with resize
829  Debug._errBoxOffResizeStartX = -1; //done with resize
830  }
831  else if(Debug._errBoxOffResizeStartX != -1) //resize from right
832  {
833  Debug._errBoxOffW = e.screenX - Debug._errBoxOffResizeStartX;
834  Debug._errBoxOffResizeStartX = -1; //done with resize
835  }
836  Debug.handleErrorResize();
837  }
838  else if(Debug._errBoxOffMoveStartX != -1) //move stop
839  {
840  Debug.log("Move Stop");
841  Debug._errBoxOffX = e.screenX - Debug._errBoxOffMoveStartX;
842  Debug._errBoxOffY = e.screenY - Debug._errBoxOffMoveStartY;
843  Debug._errBoxOffMoveStartX = -1; //done with move
844  Debug.handleErrorResize();
845  }
846 
847 } //end handleErrorMoveStop()
848 
849 //=====================================================================================
850 Debug.handleErrorMove = function(e) {
851  //console.log("moving",e);
852 
853  if(Debug._errBoxOffMoveStartX == -1 &&
854  Debug._errBoxOffResizeStartY == -1) return; //do nothing, not moving
855 
856  if(e.buttons == 0)
857  {
858  Debug._errBoxOffMoveStartX = -1; //done with move
859  Debug._errBoxOffResizeStartY = -1; //done with resize
860  Debug._errBoxOffResizeStartX = -1; //done with resize
861  return;
862  }
863 
864  if(Debug._errBoxOffResizeStartY != -1) //resizing
865  {
866  Debug.log("Resize " + e.buttons);
867  Debug._errBoxOffH = e.screenY - Debug._errBoxOffResizeStartY;
868 
869  if(Debug._errBoxOffMoveStartX != -1) //resize from left
870  {
871  Debug._errBoxOffX = e.screenX - Debug._errBoxOffMoveStartX;
872  Debug._errBoxOffW = Debug._errBoxOffResizeStartX - e.screenX;
873  }
874  else if(Debug._errBoxOffResizeStartX != -1) //resize from right
875  Debug._errBoxOffW = e.screenX - Debug._errBoxOffResizeStartX;
876 
877  Debug.handleErrorResize();
878  }
879  else if(Debug._errBoxOffMoveStartX != -1) //moving
880  {
881  Debug.log("Move " + e.buttons);
882  Debug._errBoxOffX = e.screenX - Debug._errBoxOffMoveStartX;
883  Debug._errBoxOffY = e.screenY - Debug._errBoxOffMoveStartY;
884  Debug.handleErrorResize();
885  }
886 
887 } //end handleErrorMove()
888 
889 //=====================================================================================
890 Debug.handleErrorResize = function()
891 {
892 
893 
894  var offX = document.documentElement.scrollLeft || document.body.scrollLeft || 0;
895  var offY = document.documentElement.scrollTop || document.body.scrollTop || 0;
896  var w;
897  var screenh;
898 
899  //and, set width properly so error box is scrollable for long winded errors
900  if(typeof DesktopContent != 'undefined') //define width using DesktopContent
901  {
902  w = (DesktopContent.getWindowWidth()-16-14); //scroll width is 14px
903  screenh = (DesktopContent.getWindowHeight()-16-14);
904  }
905  else if(typeof Desktop != 'undefined' && Desktop.desktop) //define width using Desktop
906  {
907  w = (Desktop.desktop.getDesktopWidth()-16-14); //scroll width is 14px
908  screenh = (Desktop.desktop.getDesktopHeight()-16-14);
909  }
910 
911  var screenw = w;
912  var minx = 0;
913 
914  if(w > 900) //clip to 850 and center (for looks)
915  {
916  offX += (w-850)/2;
917  minx = -(w-850)/2;
918  w = 850;
919  }
920 
921  if(w + Debug._errBoxOffW < 200) //clip to minimum width
922  {
923  Debug._errBoxOffW = 200 - w;
924  }
925  w += Debug._errBoxOffW;
926 
927  var h = (screenh - 20) + Debug._errBoxOffH;
928  if(h < 200) //clip to minimum height
929  {
930  Debug._errBoxOffH = -200;
931  h = 200;
932  }
933 
934  //keep window on screen
935  if(Debug._errBoxOffX + w > screenw)
936  Debug._errBoxOffX = screenw - w;
937  if(Debug._errBoxOffX < minx)
938  Debug._errBoxOffX = minx;
939  if(Debug._errBoxOffY + h > screenh)
940  Debug._errBoxOffY = screenh - h;
941  if(Debug._errBoxOffY < 0)
942  Debug._errBoxOffY = 0;
943 
944  Debug._errBox.style.width = (w) + "px";
945  Debug._errBox.style.height = (h) + "px";
946  Debug._errBox.style.left = (Debug._errBoxOffX + offX + 8) + "px";
947  Debug._errBox.style.top = (Debug._errBoxOffY + offY + 8) + "px";
948  Debug._errBox.style.marginRight = -(w+10) + "px"; //more for border effects to avoid causing scroll
949  Debug._errBox.style.marginBottom = -(h+80) + "px"; //more for border effects to avoid causing scroll
950 
951 
952  var el = document.getElementsByClassName(Debug._errBoxId + "-resizeBar")[0];
953  el.style.top = (h+6) + "px";
954  el = document.getElementsByClassName(Debug._errBoxId + "-resizeBarLeft")[0];
955  el.style.top = (h+6-10) + "px";
956  el = document.getElementsByClassName(Debug._errBoxId + "-resizeBarRight")[0];
957  el.style.left = (w-5) + "px";
958  el.style.top = (h+6-10) + "px";
959 
960  el = document.getElementsByClassName(Debug._errBoxId + "-err")[0];
961  el.style.height = (h-115) + "px";
962 } //end handleErrorResize()
963 
964 
965 //=====================================================================================
966 Debug.downloadMessages = function() {
967 
968  console.log("downloading messages...");
969 
970  //create CSV data string from html table
971  var dataStr = "data:text/txt;charset=utf-8,";
972 
973  var lines = Debug._errBox.innerText.split('\n');
974  for(var i=2;i<lines.length-2;++i)
975  {
976  dataStr += encodeURIComponent(lines[i] + "\n"); //encoded \n
977  }
978 
979  var link = document.createElement("a");
980  link.setAttribute("href", dataStr); //double encode, so encoding remains in CSV
981  link.setAttribute("style", "display:none");
982  link.setAttribute("download", "otsdaq_Messages_download.txt");
983  document.body.appendChild(link); // Required for FF
984 
985  link.click(); // This will download the data file named "otsdaq_Messages_download.txt"
986 
987  link.parentNode.removeChild(link);
988 
989 } //end Debug.downloadMessages
990