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