popup.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*##############################################################################
  2. # Copyright (C) 2011 HPCC Systems.
  3. #
  4. # All rights reserved. This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ############################################################################## */
  17. function popup_on_click()
  18. {
  19. contextMenu.hide();
  20. return this.doAction ? this.doAction() : null;
  21. }
  22. function popup_on_mouse_over()
  23. {
  24. this.style.backgroundColor='highlight';
  25. var button=this.firstChild;
  26. if(button.noaction)
  27. {
  28. button.disabled=false;
  29. button.style.color='graytext';
  30. }
  31. else
  32. {
  33. button.style.color='highlighttext';
  34. }
  35. }
  36. function popup_on_mouse_out()
  37. {
  38. this.style.backgroundColor='';
  39. var button=this.firstChild;
  40. if(button.noaction)
  41. button.disabled=true;
  42. button.style.color='menutext';
  43. };
  44. function popup_popup()
  45. {
  46. return false;
  47. }
  48. function showPopup(menu,posx,posy)
  49. {
  50. if(!window.createPopup)
  51. return false;
  52. contextMenu=window.createPopup();
  53. var popupDoc=contextMenu.document;
  54. var popupBody=popupDoc.body;
  55. popupBody.style.backgroundColor = "menu";
  56. popupBody.style.border = "outset white 2px";
  57. var tab=popupDoc.createElement('TABLE');
  58. tab.style.font='menu';
  59. tab.id='tab';
  60. tab.width=10;
  61. tab.oncontextmenu=popup_popup;
  62. tab.onselectstart=popup_popup;
  63. tab.cellSpacing=0;
  64. var tbody = popupDoc.createElement('TBODY');
  65. var lasttd=null;
  66. for(var item in menu)
  67. {
  68. if(menu[item] && menu[item].length>=2)
  69. {
  70. var tr=popupDoc.createElement('TR');
  71. tr.doAction=menu[item][1];
  72. tr.onclick=popup_on_click;
  73. var td=popupDoc.createElement('TD');
  74. td.style.padding='3px 20px 3px 20px';
  75. td.style.whiteSpace='nowrap';
  76. td.onmouseover=popup_on_mouse_over;
  77. td.onmouseout=popup_on_mouse_out;
  78. if(lasttd && lasttd.groupend)
  79. {
  80. td.style.borderTop='2px outset #fff';
  81. td.style.paddingTop='4px';
  82. }
  83. var span=popupDoc.createElement('SPAN');
  84. span.name='button';
  85. span.disabled=span.noaction=(!menu[item][1]);
  86. span.style.backgroundColor='transparent';
  87. span.style.cursor='default';
  88. span.appendChild(popupDoc.createTextNode(menu[item][0]));
  89. td.appendChild(span);
  90. if(menu[item][2]=='checked')
  91. {
  92. var check=popupDoc.createElement('SPAN');
  93. check.innerHTML='&radic;';
  94. check.style.position='absolute';
  95. check.style.left='5px';
  96. check.style.fontWeight='bolder';
  97. td.appendChild(check);
  98. }
  99. tr.appendChild(td);
  100. tbody.appendChild(tr);
  101. lasttd=td;
  102. }
  103. else
  104. {
  105. if(lasttd)
  106. {
  107. lasttd.style.borderBottom='1px outset #fff';
  108. lasttd.style.paddingBottom='5px';
  109. lasttd.groupend=true;
  110. }
  111. }
  112. }
  113. tab.appendChild(tbody);
  114. popupBody.appendChild(tab);
  115. contextMenu.show(posx, posy, 300, 300, null);
  116. var w=popupBody.all.tab.clientWidth+5,
  117. h=popupBody.all.tab.clientHeight+5;
  118. contextMenu.show(posx, posy, w, h, null);
  119. return true;
  120. }
  121. function showColumnPopup(tableId, colIndex, toggleMultiSelect)
  122. {
  123. function setColumn()
  124. {
  125. toggleMultiSelect( tableId, colIndex, true);
  126. }
  127. function alignColumn(alignment)
  128. {
  129. var table = document.getElementById( tableId );
  130. if (table) {
  131. var rows = table.rows;
  132. var nrows= rows.length;
  133. var i = document.getElementById('H.' + tableId) ? 0 : 1;
  134. for (; i<nrows; i++) {
  135. var row = rows[i];
  136. if (row.id.indexOf('.toggle') == -1)
  137. rows[i].cells[colIndex].style.textAlign = alignment;
  138. }
  139. }
  140. }
  141. function alignColumnLeft()
  142. {
  143. alignColumn('left');
  144. }
  145. function alignColumnCenter()
  146. {
  147. alignColumn('center');
  148. }
  149. function alignColumnRight()
  150. {
  151. alignColumn('right');
  152. }
  153. function changeColumnWidth(htable, table, points)
  154. {
  155. if (table) {
  156. var rows = table.rows;
  157. var nrows= rows.length;
  158. var htableRow = htable ? htable.rows[0] : null;
  159. var hw=0;
  160. for (var i=0; i<nrows; i++) {
  161. var row = rows[i];
  162. if (row.id.indexOf('.toggle') == -1)
  163. {
  164. cell = row.cells[colIndex];
  165. var w = cell.offsetWidth * (1 + points)
  166. cell.style.width = w;
  167. if (hw==0)
  168. hw = w;
  169. }
  170. }
  171. if (htableRow)
  172. htableRow.cells[colIndex].style.width = hw;
  173. }
  174. }
  175. function increaseWidth()
  176. {
  177. var table = document.getElementById( tableId );
  178. var htable = document.getElementById('H.' + tableId);
  179. changeColumnWidth(htable, table, 0.2)
  180. }
  181. function decreaseWidth()
  182. {
  183. var table = document.getElementById( tableId );
  184. var htable = document.getElementById('H.' + tableId);
  185. changeColumnWidth(htable, table, -0.2)
  186. }
  187. var menu=[];
  188. if (toggleMultiSelect) {
  189. menu.push( ["Set Column...", setColumn] );
  190. menu.push( null );
  191. }
  192. menu.push( ["Align Left", alignColumnLeft] );
  193. menu.push( ["Align Center", alignColumnCenter] );
  194. menu.push( ["Align Right", alignColumnRight] );
  195. menu.push( ["Increase Width", increaseWidth]);
  196. menu.push( ["Decrease Width", decreaseWidth]);
  197. var x, y;
  198. if (window.event)
  199. {
  200. x = window.event.screenX;
  201. y = window.event.screenY;
  202. window.event.cancelBubble = true;
  203. }
  204. else
  205. x = y = 0;
  206. showPopup(menu, x, y);
  207. return false;//suppress default browser popup
  208. }