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