otsdaq_utilities  v2_05_02_indev
DesktopWindow.js
1 //=====================================================================================
2 //
3 // Created Dec, 2012
4 // by Ryan Rivera ((rrivera at fnal.gov))
5 //
6 // DesktopWindow.js
7 //
8 // This is the desktop code for the user interface for ots. ots is the DAQ
9 // and control software for the Fermi Strips Telescope.
10 //
11 // The desktop consists of a dashboard and an arbitrary amount of windows
12 //
13 //=====================================================================================
14 
15 if (typeof Debug == 'undefined')
16  console.log('ERROR: Debug is undefined! Must include Debug.js before Desktop.js');
17 
18 if (typeof Desktop == 'undefined')
19  console.log('ERROR: Desktop is undefined! Must include Desktop.js before DesktopWindow.js');
20 else {
21 
22 
25  //define Desktop.createWindow to return window class
28  Desktop.createWindow = function(id,z,name,subname,url,w,h,x,y) {
29 
30  if(false === (this instanceof Desktop.createWindow)) {
31  //here to correct if called as "var v = Desktop.createDesktop();"
32  // instead of "var v = new Desktop.createDesktop();"
33  return new Desktop.createWindow(id,z,name,subname,url,w,h,x,y);
34  }
35 
36  //------------------------------------------------------------------
37  //create private members variables ----------------------
38  //------------------------------------------------------------------
39 
40 
41  //all units in pixels unless otherwise specified
42 
43  var _defaultWindowMinWidth = 100;
44  var _defaultWindowMinHeight = 100;
45 
46  var _defaultHeaderHeight = 30;
47  var _defaultHeaderLeftMargin = 10;
48  var _defaultHeaderFontSize = 16;
49  var _defaultButtonSize = 20;
50  var _defaultButtonLeftMargin = 2;
51  var _defaultButtonTopMargin = 1;
52 
53  var _defaultFrameBorder = 6;
54 
55  var _name;
56  var _subname;
57  var _url = url;
58  var _id = id; //unique window id, cannot change! Used as link between html div container and object
59 
60  var _winhdr,_winfrm,_winfrmHolder;
61 
62  var _w,_h,_x,_y; //position and size members
63  var _isMaximized = false;
64  var _isMinimized = false;
65  var _z = z;
66 
67  //------------------------------------------------------------------
68  //create public members variables ----------------------
69  //------------------------------------------------------------------
70  this.windiv;
71 
72  //------------------------------------------------------------------
73  //create PRIVATE members functions ----------------------
74  //------------------------------------------------------------------
75  //_refreshHeader()
76 
77  //_refreshHeader() ~~~
78  // private function to refresh header name based on window size
79  // clip text if too long
80  var _refreshHeader = function() {
81  var hdrW = _w-2*_defaultHeaderLeftMargin-5*(_defaultButtonSize+_defaultButtonLeftMargin)-1;
82  _winhdr.style.width = hdrW +"px";
83  _winhdr.innerHTML = _name + (_subname==""?"":" - ") + _subname;
84  while(_winhdr.scrollWidth > hdrW && _winhdr.innerHTML.length > 4)
85  _winhdr.innerHTML = _winhdr.innerHTML.substr(0,_winhdr.innerHTML.length-4)+"...";
86  }
87 
88  var _handleWindowContentLoading = function() {
89 
90  Debug.log("Server desktop sending first message to window: " + _id);
91 
92  //remove the "Loading" once iframe loades
93  if(_winfrmHolder.childNodes.length > 1)
94  _winfrmHolder.removeChild(
95  document.getElementById("DesktopWindowFrameLoadingDiv-"+_id));
96 
97  //To overcome same-origin policy
98  //The first message is initiated by the Desktop once the window frame has been loaded.
99  // The window content listener is defined in DesktopContent.js
100 
101  var initObject = {
102  "windowId": _id,
103  "gatewayURN": urnLid_,
104  "gatewayOrigin": serverOrigin_,
105  "cookieCode": Desktop.desktop.login.getCookieCode(),
106  "dashboardColor": Desktop.desktop.dashboard.getDefaultDashboardColor(),
107  "desktopColor": document.body.style.backgroundColor,
108  "windowFrameColor": Desktop.desktop.defaultWindowFrameColor
109  };
110 
111  _winfrm.contentWindow.postMessage(
112  initObject,"*");
113 
114  }
115  //------------------------------------------------------------------
116  //create PUBLIC members functions ----------------------
117  //------------------------------------------------------------------
118  //setWindowNameAndSubName
119  //setWindowSizeAndPosition
120  //moveWindowByOffset
121  //resizeAndPositionWindow(x,y,w,h)
122  //maximize
123  //minimize
124 
125  //member variable access functions ~~~
126  this.getWindowName = function() { return _name; }
127  this.getWindowSubName = function() { return _subname; }
128  this.getWindowUrl = function() { return _url; }
129  this.getWindowId = function() { return _id; }
130  this.getWindowX = function() { return _x; }
131  this.getWindowY = function() { return _y; }
132  this.getWindowZ = function() { return parseInt(this.windiv.style.zIndex);} //return integer
133  this.getWindowWidth = function() { return _w; }
134  this.getWindowHeight = function() { return _h; }
135  this.getWindowHeaderHeight = function() { return _defaultHeaderHeight; }
136  this.isMaximized = function() {return _isMaximized && !_isMinimized;} //make sure the maximized window is visible
137  this.isMinimized = function() {return _isMinimized;}
138 
139 
140  // bringing window to foreground then back to original location for refresh
141  this.setWindowZ = function(z) {
142  //console.log("z",z,this.getWindowName());
143  _z = z; this.windiv.style.zIndex = _z;
144  }
145 
146  this.showFrame = function() { _winfrm.style.display = "inline"; }
147  this.hideFrame = function() { _winfrm.style.display = "none"; }
148  this.getFrame = function() { return _winfrm; }
149 
150  //setWindowNameAndSubName() ~~~
151  // set name and subname
152  this.setWindowNameAndSubName = function(name,subname) {
153  _name = name; _subname = subname;
154  _refreshHeader();
155  Debug.log("Desktop Window name=" + name + " and subname=" + subname,Debug.LOW_PRIORITY);
156  }
157 
158  //setWindowSizeAndPosition() ~~~
159  // set size and position of window and its elements
160  this.setWindowSizeAndPosition = function(x,y,w,h) {
161  x = parseInt(x);
162  y = parseInt(y);
163  w = parseInt(w);
164  h = parseInt(h);
165 
166  //apply minimum size requirements and maximized state
167  _w = _isMaximized?Desktop.desktop.getDesktopContentWidth():(w < _defaultWindowMinWidth?_defaultWindowMinWidth:w);
168  _h = _isMaximized?Desktop.desktop.getDesktopContentHeight():(h < _defaultWindowMinHeight?_defaultWindowMinHeight:h);
169  _x = _isMaximized?Desktop.desktop.getDesktopContentX():x;
170  _y = _isMaximized?Desktop.desktop.getDesktopContentY():y;
171 
172  //keep window within desktop content bounds
173  if(_x + _w > Desktop.desktop.getDesktopContentX() + Desktop.desktop.getDesktopContentWidth())
174  _x = Desktop.desktop.getDesktopContentX() + Desktop.desktop.getDesktopContentWidth() - _w;
175  if(_y + _h > Desktop.desktop.getDesktopContentY() + Desktop.desktop.getDesktopContentHeight())
176  _y = Desktop.desktop.getDesktopContentY() + Desktop.desktop.getDesktopContentHeight() - _h;
177 
178  this.windiv.style.width = _w +"px";
179  this.windiv.style.height = _h+"px";
180  this.windiv.style.left = _x+"px";
181  this.windiv.style.top = _y+"px";
182 
183  _refreshHeader();
184 
185  _winfrm.style.width = (_w-2*_defaultFrameBorder-2)+"px"; //extra 2 for border pixels
186  _winfrm.style.height = (_h-_defaultHeaderHeight-_defaultFrameBorder-2)+"px"; //extra 2 for border pixels
187  _winfrmHolder.style.width = (_w-2*_defaultFrameBorder-2)+"px"; //extra 2 for border pixels
188  _winfrmHolder.style.height = (_h-_defaultHeaderHeight-_defaultFrameBorder-2)+"px"; //extra 2 for border pixels
189 
190  //Debug.log("Desktop Window position to " + _x + "," +
191  // _y + " size to " + _w + "," + _h,Debug.LOW_PRIORITY);
192 
193  if(_isMaximized){ //keep proper dimensions
194  _winfrm.style.position = "absolute";
195  _winfrm.style.zIndex = _z + 1;
196  _winfrm.style.width = _w + "px";
197  _winfrm.style.height = _h + "px";
198  _winfrm.style.left ="-1px";
199  _winfrm.style.top = "-1px";
200  _winfrmHolder.style.position = "absolute";
201  _winfrmHolder.style.width = (_w)+"px"; //extra 2 for border pixels
202  _winfrmHolder.style.height = (_h)+"px"; //extra 2 for border pixels
203  _winfrmHolder.style.left =(-_defaultFrameBorder-2) + "px";
204  _winfrmHolder.style.top = "-1px";
205 
206 
207  _w = w < _defaultWindowMinWidth?_defaultWindowMinWidth:w;
208  _h = h < _defaultWindowMinHeight?_defaultWindowMinHeight:h;
209  _x = x;
210  _y = y;
211 
212  //hide window header (in case user page is transparent)
213  var hdrs = this.windiv.getElementsByClassName("DesktopWindowButton");
214  for(var h=0;hdrs && h<hdrs.length;++h)
215  hdrs[h].style.display = "none";
216  hdrs = this.windiv.getElementsByClassName("DesktopWindowHeader");
217  for(var h=0;hdrs && h<hdrs.length;++h)
218  hdrs[h].style.display = "none";
219  }
220  else {
221  _winfrm.style.zIndex = _z;
222  _winfrm.style.position = "static";
223  _winfrmHolder.style.position = "static";
224 
225  //show window header (for case user page is transparent)
226  var hdrs = this.windiv.getElementsByClassName("DesktopWindowButton");
227  for(var h=0;hdrs && h<hdrs.length;++h)
228  hdrs[h].style.display = "block";
229  hdrs = this.windiv.getElementsByClassName("DesktopWindowHeader");
230  for(var h=0;hdrs && h<hdrs.length;++h)
231  hdrs[h].style.display = "block";
232  }
233 
234  Desktop.desktop.login.resetCurrentLayoutUpdateTimer();
235  }
236  //moveWindowByOffset() ~~~
237  // move position of window and its elements by an offset
238  this.moveWindowByOffset = function(dx,dy) {
239  _x += dx;
240  _y += dy;
241  //if(_x < Desktop.desktop.getDesktopContentX()) _x = Desktop.desktop.getDesktopContentX();
242  if(_y < Desktop.desktop.getDesktopContentY()) _y = Desktop.desktop.getDesktopContentY();
243  this.windiv.style.left = _x+"px";
244  this.windiv.style.top = _y+"px";
245 
246  //Debug.log("Desktop Window position to " + _x + "," +
247  // _y ,Debug.LOW_PRIORITY);
248 
249  //reset current layout update timer if a window moves
250  Desktop.desktop.login.resetCurrentLayoutUpdateTimer();
251  }
252  //resizeAndPositionWindow(x,y,w,h) ~~~
253  // resize and position of window and its elements
254  // do not allow weird re-size effects
255  this.resizeAndPositionWindow = function(x,y,w,h) {
256  if((w <= _defaultWindowMinWidth && x > _x) ||
257  (h <= _defaultWindowMinHeight && y > _y)) return;
258  if(x < Desktop.desktop.getDesktopContentX()) x = Desktop.desktop.getDesktopContentX();
259  if(y < Desktop.desktop.getDesktopContentY()) y = Desktop.desktop.getDesktopContentY();
260  this.setWindowSizeAndPosition(x,y,w,h);
261  }
262 
263  //maximize() ~~~
264  // maximize window toggle fulls screen mode
265  this.maximize = function() {
266 
267  if(_isMinimized) this.unminimize(); //untoggle minimize flag
268  _isMaximized = true;
269 
270  this.windiv.style.display = "inline"; //make sure is visible
271  this.setWindowSizeAndPosition(_x+10,_y,_w,_h);
272  window.parent.document.title= _name;
273  console.log(document.title, _name, "Maximize()");
274 
275  }
276 
277  this.unmaximize = function() {
278 
279  _isMaximized = false;
280 
281  this.windiv.style.display = "inline"; //make sure is visible
282  this.setWindowSizeAndPosition(_x,_y,_w,_h);
283  window.parent.document.title = Desktop.isWizardMode()?"ots wiz":"ots";
284  }
285 
286  //minimize() ~~~
287  // minimize window toggles visible or not (does not affect current position/size)
288  this.minimize = function() {
289 
290  if(_isMaximized)
291  window.parent.document.title = _name;
292  else
293  window.parent.document.title = Desktop.isWizardMode()?"ots wiz":"ots";
294 
295  _isMinimized = true;
296  this.windiv.style.display = "none";
297  Debug.log("-----------Chat this.windiv.style.display now is " + this.windiv.style.display);
298  }
299 
300  this.unminimize = function() {
301 
302  if(_isMaximized)
303  window.parent.document.title = _name;
304  else
305  window.parent.document.title = Desktop.isWizardMode()?"ots wiz":"ots";
306 
307  _isMinimized = false;
308  this.windiv.style.display = "inline";
309  Debug.log("-----------Chat this.windiv.style.display now is " + this.windiv.style.display);
310  }
311 
312  //------------------------------------------------------------------
313  //handle class construction ----------------------
314  //------------------------------------------------------------------
315 
316  //create holding container
317  this.windiv = document.createElement("div");
318  this.windiv.setAttribute("class", "DesktopWindow");
319  this.windiv.setAttribute("id", "DesktopWindow-" + _id); //setup ids
320  this.windiv.style.backgroundColor = Desktop.desktop.defaultWindowFrameColor;
321  this.windiv.style.position = "absolute";
322  this.windiv.style.zIndex = _z;
323 
324  //create header
325  _winhdr = document.createElement("div");
326  _winhdr.setAttribute("class", "DesktopWindowHeader");
327  _winhdr.setAttribute("id", "DesktopWindowHeader-" + _id);
328  _winhdr.style.height = _defaultHeaderHeight+"px";
329  _winhdr.style.marginLeft = _defaultHeaderLeftMargin+"px";
330  _winhdr.style.marginRight = (-100)+"px";
331  _winhdr.style.fontSize = _defaultHeaderFontSize+"px";
332  this.setWindowNameAndSubName(name, subname); // set name and subname
333  this.windiv.appendChild(_winhdr); //add header to window
334 
335  //create buttons
336 
337 
338  var tmpContainer = document.createElement("div");
339  tmpContainer.setAttribute("style", "float:right;white-space:nowrap;height:" + _defaultHeaderHeight + "px;overflow:auto;");
340  var tmpBtn = document.createElement("div");
341  tmpBtn.setAttribute("class", "DesktopWindowButton");
342  tmpBtn.setAttribute("id", "DesktopWindowButtonRefresh-" + _id);
343  tmpBtn.style.width = (_defaultButtonSize) +"px";
344  tmpBtn.style.height = (_defaultButtonSize) +"px";
345  tmpBtn.style.marginLeft = (_defaultButtonLeftMargin) +"px";
346  tmpBtn.style.marginTop = (_defaultButtonTopMargin) +"px";
347  tmpBtn.onmouseup = Desktop.handleWindowRefresh;
348  tmpBtn.onmousedown = Desktop.handleWindowButtonDown;
349  var tmpEl = document.createElement("div");
350  tmpEl.setAttribute("class", "DesktopWindowButtonGraphicRefresh");
351  if(Debug.BROWSER_TYPE == Debug.BROWSER_TYPE_FIREFOX) //firefox
352  {
353  //firefox shows circle-arrow character smaller
354  tmpEl.innerHTML =
355  "<div style='font-size:28px; margin: -4px 0 0 -1px; color:inherit;' "+
356  "title='Click to refresh onl this window'>↻</div>";
357  //_fullScreenRefreshBtn.style.height = "16px";
358  //_fullScreenRefreshBtn.style.padding = "3px 10px 7px 10px";
359  }
360  else //chrome
361  {
362  tmpEl.innerHTML =
363  "<div style='font-size:16px; margin-left:1px; font-weight: 400; color:inherit;' "+
364  "title='Click to refresh onl this window'>↻</div>";
365  //_fullScreenRefreshBtn.style.height = "16px";
366  //_fullScreenRefreshBtn.style.padding = "3px 10px 7px 10px";
367  }
368 
369  //tmpEl.innerHTML = "↻";
370  tmpBtn.appendChild(tmpEl);
371  tmpContainer.appendChild(tmpBtn); //add button to window
372 
373  //create new window element next to the refresh button with the question mark
374  var tmpBtn = document.createElement("div");
375  tmpBtn.setAttribute("class", "DesktopWindowButton");
376  tmpBtn.setAttribute("id", "DesktopWindowButtonHelp-" + _id);
377  tmpBtn.style.width = (_defaultButtonSize) +"px";
378  tmpBtn.style.height = (_defaultButtonSize) +"px";
379  tmpBtn.style.marginLeft = (_defaultButtonLeftMargin) +"px";
380  tmpBtn.style.marginTop = (_defaultButtonTopMargin) +"px";
381  tmpBtn.onmouseup = Desktop.handleWindowHelp;
382  tmpBtn.onmousedown = Desktop.handleWindowButtonDown;
383  var tmpEl = document.createElement("div");
384  tmpEl.setAttribute("class", "DesktopWindowButtonGraphicHelp");
385  tmpEl.innerHTML = "?";
386  tmpBtn.appendChild(tmpEl);
387  tmpContainer.appendChild(tmpBtn); //add button to window
388 
389 
390  var tmpBtn = document.createElement("div");
391  tmpBtn.setAttribute("class", "DesktopWindowButton");
392  tmpBtn.setAttribute("id", "DesktopWindowButtonMin-" + _id);
393  tmpBtn.style.width = (_defaultButtonSize) +"px";
394  tmpBtn.style.height = (_defaultButtonSize) +"px";
395  tmpBtn.style.marginLeft = (_defaultButtonLeftMargin) +"px";
396  tmpBtn.style.marginTop = (_defaultButtonTopMargin) +"px";
397  tmpBtn.onmouseup = Desktop.handleWindowMinimize;
398  tmpBtn.onmousedown = Desktop.handleWindowButtonDown;
399  var tmpEl = document.createElement("div");
400  tmpEl.setAttribute("class", "DesktopWindowButtonGraphicMin");
401  tmpBtn.appendChild(tmpEl);
402  tmpContainer.appendChild(tmpBtn); //add button to window
403 
404  tmpBtn = document.createElement("div");
405  tmpBtn.setAttribute("class", "DesktopWindowButton");
406  tmpBtn.setAttribute("id", "DesktopWindowButtonMax-" + _id);
407  tmpBtn.style.width = (_defaultButtonSize) +"px";
408  tmpBtn.style.height = (_defaultButtonSize) +"px";
409  tmpBtn.style.marginLeft = (_defaultButtonLeftMargin) +"px";
410  tmpBtn.style.marginTop = (_defaultButtonTopMargin) +"px";
411  tmpBtn.onmouseup = Desktop.handleWindowMaximize;
412  tmpBtn.onmousedown = Desktop.handleWindowButtonDown;
413  var tmpEl = document.createElement("div");
414  tmpEl.setAttribute("class", "DesktopWindowButtonGraphicMax");
415  tmpBtn.appendChild(tmpEl);
416  tmpContainer.appendChild(tmpBtn); //add button to window
417 
418  tmpBtn = document.createElement("div");
419  tmpBtn.setAttribute("class", "DesktopWindowButton");
420  tmpBtn.setAttribute("id", "DesktopWindowButtonClose-" + _id);
421  tmpBtn.style.width = (_defaultButtonSize) +"px";
422  tmpBtn.style.height = (_defaultButtonSize) +"px";
423  tmpBtn.style.marginLeft = (_defaultButtonLeftMargin) +"px";
424  tmpBtn.style.marginTop = (_defaultButtonTopMargin) +"px";
425  tmpBtn.onmouseup = Desktop.handleWindowClose;
426  tmpBtn.onmousedown = Desktop.handleWindowButtonDown;
427  var tmpEl = document.createElement("div");
428  tmpEl.setAttribute("class", "DesktopWindowButtonGraphicClose");
429  tmpEl.innerHTML = "x";
430  tmpBtn.appendChild(tmpEl);
431  tmpContainer.appendChild(tmpBtn); //add button to window
432 
433 
434  this.windiv.appendChild(tmpContainer); //add button container to window
435 
436  //create iframe holder for displaying during window manipulations
437  _winfrmHolder = document.createElement("div");
438  _winfrmHolder.setAttribute("class", "DesktopWindowFrameHolder");
439  _winfrmHolder.setAttribute("id", "DesktopWindowFrameHolder-" + _id);
440  _winfrmHolder.style.marginLeft = _defaultFrameBorder+"px";
441  _winfrmHolder.innerHTML =
442  "<div class='DesktopWindowHeader' id='DesktopWindowFrameLoadingDiv-"+
443  _id + "' style='width:100px;height:50px;position:relative;top:50%;left:50%;margin-top:-25px;margin-left:-50px;text-align:center;margin-bottom:-50px;'>" +
444  "Loading..." + "</div>";
445 
446  //create iframe
447  _winfrm = document.createElement("iframe");
448  _winfrm.setAttribute("class", "DesktopWindowFrame");
449  _winfrm.setAttribute("id", "DesktopWindowFrame-" + _id);
450  _winfrm.setAttribute("name", "DesktopWindowFrame-" + _id);
451  _winfrm.setAttribute("allow", "autoplay"); //allow sounds without user clicking page first
452  _winfrm.onload = _handleWindowContentLoading; //event to delete "Loading"
453  _winfrm.setAttribute("src", _url);
454  _winfrmHolder.appendChild(_winfrm); //add frame to holder
455  this.windiv.appendChild(_winfrmHolder); //add holder to window
456 
457  this.setWindowSizeAndPosition(x,y,w,h); //setup size and position
458 
459  //add mouse handlers
460  this.windiv.onmousedown = Desktop.handleWindowMouseDown;
461  this.windiv.onmouseup = Desktop.handleWindowMouseUp;
462  this.windiv.onmousemove = Desktop.handleWindowMouseMove;
463  this.windiv.ondblclick = Desktop.handleWindowMaximize;
464 
465  //add touch handlers (for mobile devices)
466  this.windiv.addEventListener('touchstart',Desktop.handleTouchStart);
467  this.windiv.addEventListener('touchend',Desktop.handleTouchEnd);
468  this.windiv.addEventListener('touchmove',Desktop.handleTouchMove);
469 
470 
471  Debug.log("Desktop Window Created",Debug.LOW_PRIORITY);
472 
473  }
474 }