CToolbarButton.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Viewer
  5. *| (C) Copyright IBM Corp. 2001, 2011
  6. *|
  7. *| US Government Users Restricted Rights - Use, duplication or
  8. *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. *|
  10. *+------------------------------------------------------------------------+
  11. */
  12. /*-----------------------------------------------------------------------------------------------------
  13. Class : CToolbarButton
  14. Description :
  15. -----------------------------------------------------------------------------------------------------*/
  16. var tbUniqueId = 0;
  17. function makeId() {
  18. return tbUniqueId++;
  19. }
  20. gDropDownButtonStyle = new CUIStyle('dropDownArrow','dropDownArrowOver','','','');
  21. function CToolbarButton(parent, action, iconPath, toolTip, style, bHideDropDown, label, dropDownToolTip) {
  22. this.m_id = 'tbbutton'+makeId();
  23. this.m_bVisible = true;
  24. this.m_action = action;
  25. this.m_toolTip = toolTip;
  26. this.m_icon = (iconPath) ? new CIcon(iconPath, toolTip) : null;
  27. this.m_parent = parent;
  28. this.m_menu = null;
  29. if (typeof bHideDropDown == "boolean") {
  30. this.m_bHideDropDown = bHideDropDown;
  31. }
  32. else {
  33. this.m_bHideDropDown = false;
  34. }
  35. this.m_style = new CUIStyle(style.getNormalState(),style.getRolloverState(),style.getDepressedState(),style.getDepressedRolloverState(),style.getDisabledState());
  36. this.m_observers = new CObserver(this);
  37. if(typeof this.m_parent == "object" && typeof this.m_parent.add == "function") {
  38. this.m_parent.add(this);
  39. }
  40. this.m_label = (label) ? label : null;
  41. this.m_dropDownToolTip = (dropDownToolTip) ? dropDownToolTip : this.m_toolTip;
  42. }
  43. function CToolbarButton_getId() {
  44. return this.m_id;
  45. }
  46. function CToolbarButton_draw() {
  47. var html='';
  48. html += '<div style="margin-right:3px;"><button type="button" id="';
  49. html += this.m_id;
  50. html += '"';
  51. var oStyle = this.getStyle();
  52. if(oStyle)
  53. {
  54. html += ' class="' + oStyle.getActiveState() + '"';
  55. if (oStyle.getActiveState() != oStyle.getDisabledState())
  56. {
  57. html += ' tabIndex="0" hideFocus="true" accessKey="1"';
  58. }
  59. }
  60. if (this.m_toolTip != "")
  61. {
  62. html += ' title="' + this.m_toolTip + '"';
  63. }
  64. html += '>';
  65. if (this.m_icon != null)
  66. {
  67. html += this.m_icon.draw();
  68. }
  69. if (this.m_label != null)
  70. {
  71. html += this.m_label;
  72. }
  73. html += '</button>';
  74. if(this.m_menu != null && !this.m_bHideDropDown)
  75. {
  76. html += '<button type="button" id="';
  77. html += ('menu' + this.getId());
  78. html += '"';
  79. if(oStyle)
  80. {
  81. html += ' class="'+gDropDownButtonStyle.getActiveState() + '"';
  82. if (oStyle.getActiveState() != oStyle.getDisabledState())
  83. {
  84. html += ' tabIndex="0" hideFocus="true" accessKey="1"';
  85. }
  86. }
  87. if (this.m_dropDownToolTip != "")
  88. {
  89. html += ' title="' + this.m_dropDownToolTip + '"';
  90. }
  91. html += '><img border="0" src="../common/images/toolbar_drop_arrow.gif"';
  92. if (this.m_dropDownToolTip != "")
  93. {
  94. html += ' alt="' + this.m_dropDownToolTip + '"';
  95. html += ' title="' + this.m_dropDownToolTip + '"';
  96. }
  97. html += ' width="7" height="16"/></button>';
  98. }
  99. html += '</div>';
  100. return html;
  101. }
  102. function CToolbarButton_attachEvents() {
  103. if(typeof this.getParent().getHTMLContainer != "function") {
  104. return; // this method must be implemented by the parent
  105. }
  106. var htmlContainer = this.getParent().getHTMLContainer();
  107. if(htmlContainer == null) {
  108. return;
  109. }
  110. var hTbItem = eval(document.all ? htmlContainer.document.getElementById(this.m_id) : htmlContainer.ownerDocument.getElementById(this.m_id));
  111. if(hTbItem == null) {
  112. return; // just to be safe
  113. }
  114. hTbItem.onmouseover = this.onmouseover;
  115. hTbItem.onmouseout = this.onmouseout;
  116. hTbItem.onclick = this.onclick;
  117. hTbItem.onkeypress = this.onkeypress;
  118. hTbItem.onfocus = this.onfocus;
  119. hTbItem.onblur = this.onblur;
  120. hTbItem.tbItem = eval(this);
  121. // attach the drop down arrow event handlers to the toolbar button as well
  122. if(this.m_menu != null && !this.m_bHideDropDown)
  123. {
  124. var hmenu = eval(document.all ? htmlContainer.document.getElementById('menu'+this.getId()) : htmlContainer.ownerDocument.getElementById('menu'+this.getId()));
  125. hmenu.onmouseover = this.onmouseover;
  126. hmenu.onmouseout = this.onmouseout;
  127. hmenu.onclick = this.onclick;
  128. hmenu.onkeypress = this.onkeypress;
  129. hmenu.onfocus = this.onfocus;
  130. hmenu.onblur = this.onblur;
  131. hmenu.tbItem = eval(this);
  132. }
  133. }
  134. function CToolbarButton_createDropDownMenu(menuStyle, dropDownToolTip) {
  135. this.m_dropDownToolTip = (dropDownToolTip) ? dropDownToolTip : this.m_toolTip;
  136. this.m_menu = new CMenu('dropDown'+this.getId(),menuStyle);
  137. this.m_menu.setParent(this);
  138. return this.m_menu;
  139. }
  140. function CToolbarButton_addOwnerDrawControl(control) {
  141. this.m_menu = control;
  142. if(typeof control.setParent != "undefined") {
  143. this.m_menu.setParent(this);
  144. }
  145. }
  146. function CToolbarButton_getParent() {
  147. return this.m_parent;
  148. }
  149. function CToolbarButton_setParent(parent) {
  150. this.m_parent = parent;
  151. }
  152. function CToolbarButton_getAction() {
  153. return this.m_action;
  154. }
  155. function CToolbarButton_setAction(action) {
  156. this.m_action = action;
  157. }
  158. function CToolbarButton_getToolTip() {
  159. return this.m_toolTip;
  160. }
  161. function CToolbarButton_setToolTip(tooltip) {
  162. this.m_toolTip = tooltip;
  163. }
  164. function CToolbarButton_getDropDownToolTip() {
  165. return this.m_dropDownToolTip;
  166. }
  167. function CToolbarButton_setDropDownToolTip(tooltip) {
  168. this.m_dropDownToolTip = tooltip;
  169. }
  170. function CToolbarButton_getIcon() {
  171. return this.m_icon;
  172. }
  173. function CToolbarButton_setIcon(iconPath) {
  174. this.m_icon.setPath(iconPath);
  175. }
  176. function CToolbarButton_onmouseover(evt)
  177. {
  178. var toolbarButton = this.tbItem;
  179. if(typeof toolbarButton == "object")
  180. {
  181. if(!toolbarButton.isEnabled()) {
  182. return;
  183. }
  184. if(toolbarButton.getMenu() != null && !toolbarButton.m_bHideDropDown && ('menu'+toolbarButton.getId()) == this.id) {
  185. this.className = gDropDownButtonStyle.getActiveRolloverState();
  186. }
  187. else
  188. {
  189. if(typeof toolbarButton.getStyle() == "object") {
  190. this.className = toolbarButton.getStyle().getActiveRolloverState();
  191. }
  192. if(toolbarButton.getMenu() != null && !toolbarButton.m_bHideDropDown)
  193. {
  194. var dropDownArrow = document.all ? this.document.getElementById('menu'+toolbarButton.getId()) : this.ownerDocument.getElementById('menu'+toolbarButton.getId());
  195. if(typeof dropDownArrow == "object") {
  196. dropDownArrow.className = gDropDownButtonStyle.getActiveRolloverState();
  197. }
  198. }
  199. }
  200. // send the message up to our parent
  201. var oParent = toolbarButton.getParent();
  202. if(oParent && typeof oParent.onmouseover == "function") {
  203. oParent.onmouseover(evt);
  204. }
  205. // notify our observers of this event
  206. toolbarButton.getObservers().notify(CToolbarButton_onmouseover);
  207. }
  208. }
  209. function CToolbarButton_onmouseout(evt) {
  210. var toolbarButton = this.tbItem;
  211. if(typeof toolbarButton == "object")
  212. {
  213. if(!toolbarButton.isEnabled()) {
  214. return;
  215. }
  216. if(toolbarButton.getMenu() != null && !toolbarButton.m_bHideDropDown && ('menu'+toolbarButton.getId()) == this.id) {
  217. this.className = gDropDownButtonStyle.getActiveState();
  218. }
  219. else
  220. {
  221. if(typeof toolbarButton.getStyle() == "object") {
  222. this.className = toolbarButton.getStyle().getActiveState();
  223. }
  224. if(toolbarButton.getMenu() != null && !toolbarButton.m_bHideDropDown)
  225. {
  226. var dropDownArrow = document.all ? this.document.getElementById('menu'+toolbarButton.getId()) : this.ownerDocument.getElementById('menu'+toolbarButton.getId());
  227. if(typeof dropDownArrow == "object") {
  228. dropDownArrow.className = gDropDownButtonStyle.getActiveState();
  229. }
  230. }
  231. }
  232. // send the message up to our parent
  233. var oParent = toolbarButton.getParent();
  234. if(oParent && typeof oParent.onmouseout == "function") {
  235. oParent.onmouseout(evt);
  236. }
  237. // notify our observers of this event
  238. toolbarButton.getObservers().notify(CToolbarButton_onmouseout);
  239. }
  240. }
  241. function CToolbarButton_onclick(evt) {
  242. //get the event in a cross-browser fashion
  243. evt = (evt) ? evt : ((event) ? event : null);
  244. // get the toolbar button from the html element
  245. var toolbarButton = this.tbItem;
  246. if(toolbarButton != null) {
  247. if(!toolbarButton.isEnabled()) {
  248. return;
  249. }
  250. var menu = toolbarButton.getMenu();
  251. var sButtonId = toolbarButton.getId();
  252. if(menu && ((this.id == ('menu'+sButtonId)) || (toolbarButton.m_bHideDropDown && this.id == sButtonId))) {
  253. if(menu.isVisible()) {
  254. menu.remove();
  255. } else {
  256. // the user clicked the drop down arrow
  257. if(typeof menu.setHTMLContainer != "undefined") {
  258. menu.setHTMLContainer(document.all ? this.document.body : this.ownerDocument.body);
  259. }
  260. //Close all the other dropdown menus first
  261. if(typeof toolbarButton.m_parent.closeMenus == "function") {
  262. toolbarButton.m_parent.closeMenus();
  263. }
  264. menu.draw();
  265. menu.show();
  266. }
  267. } else {
  268. eval(this.tbItem.m_action);
  269. }
  270. // send the message up to our parent
  271. var oParent = toolbarButton.getParent();
  272. if(oParent && typeof oParent.onclick == "function") {
  273. oParent.onclick(evt);
  274. }
  275. // notify our observers of this event
  276. toolbarButton.getObservers().notify(CToolbarButton_onclick);
  277. }
  278. if (this.blur) {
  279. this.blur();
  280. }
  281. evt.cancelBubble = true;
  282. return false;
  283. }
  284. function CToolbarButton_onkeypress(evt) {
  285. //get the event in a cross-browser fashion
  286. evt = (evt) ? evt : ((event) ? event : null);
  287. //check for the Enter key
  288. if (evt.keyCode == 13) {
  289. // get the toolbar button from the html element
  290. var toolbarButton = this.tbItem;
  291. if(toolbarButton != null) {
  292. if(!toolbarButton.isEnabled()) {
  293. return;
  294. }
  295. var menu = toolbarButton.getMenu();
  296. var sButtonId = toolbarButton.getId();
  297. if(menu && ((this.id == ('menu'+sButtonId)) || (toolbarButton.m_bHideDropDown && this.id == sButtonId))) {
  298. if(menu.isVisible()) {
  299. menu.remove();
  300. } else {
  301. // the user clicked the drop down arrow
  302. if(typeof menu.setHTMLContainer != "undefined") {
  303. menu.setHTMLContainer(document.all ? this.document.body : this.ownerDocument.body);
  304. }
  305. menu.draw();
  306. menu.show();
  307. }
  308. } else {
  309. eval(this.tbItem.m_action);
  310. }
  311. // send the message up to our parent
  312. var oParent = toolbarButton.getParent();
  313. if(oParent && typeof oParent.onkeypress == "function") {
  314. oParent.onkeypress(evt);
  315. }
  316. // notify our observers of this event
  317. toolbarButton.getObservers().notify(CToolbarButton_onkeypress);
  318. }
  319. }
  320. evt.cancelBubble = true;
  321. return false;
  322. }
  323. function CToolbarButton_getMenu() {
  324. return this.m_menu;
  325. }
  326. function CToolbarButton_getMenuType() {
  327. // current toolbar buttons only support drop down menus
  328. return 'dropDown';
  329. }
  330. function CToolbarButton_setStyle(style) {
  331. this.m_style = style;
  332. }
  333. function CToolbarButton_getStyle() {
  334. return this.m_style;
  335. }
  336. function CToolbarButton_isVisible() {
  337. return this.m_bVisible;
  338. }
  339. function CToolbarButton_hide() {
  340. this.m_bVisible = false;
  341. }
  342. function CToolbarButton_show() {
  343. this.m_bVisible = true;
  344. }
  345. function CToolbarButton_enable() {
  346. var oStyle = this.getStyle();
  347. oStyle.setActiveState('normal');
  348. oStyle.setActiveRolloverState('normal');
  349. var oIcon = this.getIcon();
  350. if (oIcon)
  351. {
  352. oIcon.enable();
  353. }
  354. this.updateHTML();
  355. }
  356. function CToolbarButton_disable() {
  357. var oStyle = this.getStyle();
  358. oStyle.setActiveState('disabled');
  359. oStyle.setActiveRolloverState('disabled');
  360. var oIcon = this.getIcon();
  361. if (oIcon)
  362. {
  363. oIcon.disable();
  364. }
  365. this.updateHTML();
  366. }
  367. function CToolbarButton_isEnabled() {
  368. if (this.getIcon())
  369. {
  370. return this.getIcon().isEnabled();
  371. }
  372. else
  373. {
  374. return true;
  375. }
  376. }
  377. function CToolbarButton_pressed() {
  378. var oStyle = this.getStyle();
  379. oStyle.setActiveState('depressed');
  380. oStyle.setActiveRolloverState('depressed');
  381. this.updateHTML();
  382. }
  383. function CToolbarButton_reset() {
  384. var oStyle = this.getStyle();
  385. oStyle.setActiveState('normal');
  386. oStyle.setActiveRolloverState('normal');
  387. this.updateHTML();
  388. }
  389. function CToolbarButton_updateHTML() {
  390. var oStyle = this.getStyle();
  391. if(oStyle) {
  392. if(typeof this.getParent().getHTMLContainer == "function") {
  393. var htmlContainer = this.getParent().getHTMLContainer();
  394. if(htmlContainer != null) {
  395. var htmlElement = document.all ? htmlContainer.document.getElementById(this.getId()) : htmlContainer.ownerDocument.getElementById(this.getId());
  396. if(htmlElement != null) {
  397. var toolbarImage = htmlElement.getElementsByTagName("img");
  398. if(typeof toolbarImage != 'undefined') {
  399. if (this.getIcon())
  400. {
  401. if(this.getIcon().isEnabled()) {
  402. toolbarImage[0].src = this.getIcon().getPath();
  403. } else {
  404. toolbarImage[0].src = this.getIcon().getDisabledImagePath();
  405. }
  406. }
  407. }
  408. if(oStyle.getActiveState() != oStyle.getDisabledState()) {
  409. htmlElement.tabIndex = 0;
  410. if (this.getMenu() != null && !this.m_bHideDropDown) {
  411. htmlElement.nextSibling.tabIndex = 0;
  412. htmlElement.nextSibling.accessKey = 1;
  413. }
  414. } else {
  415. if (htmlElement.tabIndex != 'undefined') {
  416. htmlElement.removeAttribute("tabIndex");
  417. htmlElement.removeAttribute("accessKey");
  418. if (this.getMenu() != null) {
  419. htmlElement.nextSibling.removeAttribute("tabIndex");
  420. htmlElement.nextSibling.removeAttribute("accessKey");
  421. }
  422. }
  423. }
  424. htmlElement.className = oStyle.getActiveState();
  425. }
  426. }
  427. }
  428. }
  429. }
  430. function CToolbarButton_getObservers() {
  431. return this.m_observers;
  432. }
  433. function CToolbarButton_setFocus() {
  434. if (this.m_menu != null && !this.m_bHideDropDown) {
  435. document.getElementById(this.m_id).nextSibling.focus();
  436. }
  437. else {
  438. document.getElementById(this.m_id).focus();
  439. }
  440. }
  441. CToolbarButton.prototype.draw = CToolbarButton_draw;
  442. CToolbarButton.prototype.attachEvents = CToolbarButton_attachEvents;
  443. CToolbarButton.prototype.onblur = CToolbarButton_onmouseout;
  444. CToolbarButton.prototype.onfocus = CToolbarButton_onmouseover;
  445. CToolbarButton.prototype.onkeypress = CToolbarButton_onkeypress;
  446. CToolbarButton.prototype.onmouseover = CToolbarButton_onmouseover;
  447. CToolbarButton.prototype.onmouseout = CToolbarButton_onmouseout;
  448. CToolbarButton.prototype.onclick = CToolbarButton_onclick;
  449. CToolbarButton.prototype.setParent = CToolbarButton_setParent;
  450. CToolbarButton.prototype.getParent = CToolbarButton_getParent;
  451. CToolbarButton.prototype.getAction = CToolbarButton_getAction;
  452. CToolbarButton.prototype.setAction = CToolbarButton_setAction;
  453. CToolbarButton.prototype.getToolTip = CToolbarButton_getToolTip;
  454. CToolbarButton.prototype.setToolTip = CToolbarButton_setToolTip;
  455. CToolbarButton.prototype.getDropDownToolTip = CToolbarButton_getDropDownToolTip;
  456. CToolbarButton.prototype.setDropDownToolTip = CToolbarButton_setDropDownToolTip;
  457. CToolbarButton.prototype.getIcon = CToolbarButton_getIcon;
  458. CToolbarButton.prototype.setIcon = CToolbarButton_setIcon;
  459. CToolbarButton.prototype.getMenu = CToolbarButton_getMenu;
  460. CToolbarButton.prototype.getMenuType = CToolbarButton_getMenuType;
  461. CToolbarButton.prototype.getId = CToolbarButton_getId;
  462. CToolbarButton.prototype.setStyle = CToolbarButton_setStyle;
  463. CToolbarButton.prototype.getStyle = CToolbarButton_getStyle;
  464. CToolbarButton.prototype.createDropDownMenu = CToolbarButton_createDropDownMenu;
  465. CToolbarButton.prototype.addOwnerDrawControl = CToolbarButton_addOwnerDrawControl;
  466. CToolbarButton.prototype.getObservers = CToolbarButton_getObservers;
  467. CToolbarButton.prototype.update = new Function("return true");
  468. CToolbarButton.prototype.isVisible = CToolbarButton_isVisible;
  469. CToolbarButton.prototype.hide = CToolbarButton_hide;
  470. CToolbarButton.prototype.show = CToolbarButton_show;
  471. CToolbarButton.prototype.isEnabled = CToolbarButton_isEnabled;
  472. CToolbarButton.prototype.enable = CToolbarButton_enable;
  473. CToolbarButton.prototype.disable = CToolbarButton_disable;
  474. CToolbarButton.prototype.pressed = CToolbarButton_pressed;
  475. CToolbarButton.prototype.reset = CToolbarButton_reset;
  476. CToolbarButton.prototype.setFocus = CToolbarButton_setFocus;
  477. CToolbarButton.prototype.updateHTML = CToolbarButton_updateHTML;