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