CBar.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 : CBar
  14. Description :
  15. -----------------------------------------------------------------------------------------------------*/
  16. var cHorizonalBar = 0;
  17. var cVerticalBar = 1;
  18. function CBar(containerId, style, sId, imagePath, showTooltip, hideTooltip, cookieVar, cookieName) {
  19. this.m_align = 'left';
  20. this.m_items = [];
  21. this.m_htmlContainerId = containerId;
  22. this.m_htmlContainer = null;
  23. this.m_id = 'cbar'+containerId;
  24. this.m_menuType = cVerticalBar;
  25. this.m_style = style;
  26. this.m_parent = null;
  27. this.m_observers = new CObserver(this);
  28. this.m_cookieVar = cookieVar;
  29. this.m_cookieName = cookieName;
  30. //reference to the object name
  31. this.m_sId = (sId) ? sId : null;
  32. this.m_display = DISPLAY_INLINE;
  33. this.m_imagePath = (imagePath) ? imagePath : '../common/images/toolbar/';
  34. this.m_imgCollapseSrc = this.m_imagePath + 'toolbar_collapse.gif';
  35. this.m_imgExpandSrc = this.m_imagePath + 'toolbar_expand.gif';
  36. this.m_showTooltip = showTooltip ? showTooltip : null;
  37. this.m_hideTooltip = hideTooltip ? hideTooltip : null;
  38. }
  39. function CBar_hideBar() {
  40. var bar = document.getElementById('bar'+ this.m_id);
  41. var barToggleIcon = document.getElementById('barIcon'+ this.m_id);
  42. barToggleIcon.src= this.m_imgExpandSrc;
  43. if (this.m_showTooltip != null) {
  44. barToggleIcon.alt = this.m_showTooltip;
  45. barToggleIcon.title = this.m_showTooltip;
  46. }
  47. bar.style.display = DISPLAY_NONE;
  48. if (typeof setQSCookie == "function") {
  49. setQSCookie(this.m_cookieVar, this.m_cookieName, 0);
  50. }
  51. }
  52. function CBar_showBar() {
  53. var bar = document.getElementById('bar'+ this.m_id);
  54. var barToggleIcon = document.getElementById('barIcon'+ this.m_id);
  55. barToggleIcon.src= this.m_imgCollapseSrc;
  56. if (this.m_hideTooltip != null) {
  57. barToggleIcon.alt = this.m_hideTooltip;
  58. barToggleIcon.title = this.m_hideTooltip;
  59. }
  60. bar.style.display = this.m_display;
  61. if (typeof setQSCookie == "function") {
  62. setQSCookie(this.m_cookieVar, this.m_cookieName, 1);
  63. }
  64. }
  65. function CBar_toggleBar(){
  66. var bar = document.getElementById('bar'+ this.m_id);
  67. var barDisplay = bar.style.display;
  68. if ( (barDisplay == this.m_display) || (barDisplay==''))
  69. {
  70. this.hideBar();
  71. }
  72. else
  73. {
  74. this.showBar();
  75. }
  76. }
  77. function CBar_getParent() {
  78. return this.m_parent;
  79. }
  80. function CBar_setParent(parent) {
  81. this.m_parent = parent;
  82. }
  83. function CBar_draw() {
  84. if(this.m_htmlContainer == null) {
  85. this.m_htmlContainer = document.getElementById(this.m_htmlContainerId);
  86. if(this.m_htmlContainer == null) { // if we can't find the container, return
  87. return;
  88. }
  89. }
  90. var html = '';
  91. html += '<table cellpadding="0" cellspacing="0" border="0"';
  92. if (this.m_sId != null)
  93. {
  94. html += 'style="display: inline;"><tr>';
  95. /*The height of 26 is chosen so that the collapse/expand icons will always line up in Firefox. Bug #483255*/
  96. html += '<td'+(document.all ? ' style="vertical-align:bottom"':'')+' style="height:26px"><img id="barIcon'+ this.m_id +'" border="0" src="'+ this.m_imgCollapseSrc + '"';
  97. if (this.m_hideTooltip != null) {
  98. html += ' alt="'+this.m_hideTooltip+'" title="'+this.m_hideTooltip+'"';
  99. }
  100. html +=' onclick="'+this.m_sId +'.toggleBar();" style="cursor:pointer;cursor:hand;"></td>';
  101. }
  102. else
  103. {
  104. var marginStyle = '';
  105. if (this.m_htmlContainer.style.textAlign == 'right') {
  106. marginStyle = 'margin-left:auto; margin-right: 0;';
  107. }
  108. else if (this.m_htmlContainer.style.textAlign == 'left') {
  109. marginStyle = 'margin-left:0; margin-right: auto;';
  110. }
  111. else if (this.m_htmlContainer.style.textAlign == 'center') {
  112. marginStyle = 'margin-left:auto; margin-right: auto;';
  113. }
  114. if (marginStyle != '') {
  115. html += ' style="'+marginStyle+'"';
  116. }
  117. html += '><tr>';
  118. }
  119. html += '<td id="bar'+ this.m_id +'">';
  120. html += '<table cellpadding="0" cellspacing="0" border="0" class="';
  121. if(this.getStyle() != null)
  122. {
  123. html += this.getStyle().getNormalState();
  124. }
  125. html += '" id="';
  126. html += this.m_id;
  127. html += '" style="'+ this.m_style +'"><tr>';
  128. html += this.drawItems();
  129. html += '</tr></table></td>';
  130. html += '</tr></table>';
  131. this.m_htmlContainer.innerHTML = html;
  132. this.m_htmlContainer.style.textAlign = this.m_align;
  133. //initialize any items
  134. for(var i = 0; i < this.m_items.length; ++i) {
  135. if(typeof this.m_items[i].init == 'function')
  136. {
  137. this.m_items[i].init();
  138. }
  139. }
  140. this.attachEvents();
  141. }
  142. function CBar_drawItems() {
  143. var html = '';
  144. for(var i = 0; i < this.m_items.length; ++i) {
  145. if(typeof this.m_items[i].draw == 'function') {
  146. if(this.m_menuType == cHorizonalBar && !(this.m_items[i] instanceof CSeperator) ) {
  147. html += '<td style="white-space:nowrap;">';
  148. }
  149. if(this.m_items[i].isVisible()) {
  150. html += this.m_items[i].draw();
  151. }
  152. if(this.m_menuType == cHorizonalBar && !(this.m_items[i] instanceof CSeperator) ) {
  153. html += '</td>';
  154. }
  155. }
  156. }
  157. return html;
  158. }
  159. function CBar_attachEvents() {
  160. for(var i = 0; i < this.m_items.length; ++i) {
  161. if(typeof this.m_items[i].attachEvents == 'function' && this.m_items[i].isVisible()) {
  162. this.m_items[i].attachEvents();
  163. }
  164. }
  165. }
  166. function CBar_add(item) {
  167. if(typeof item.getObservers == "function" && typeof item.getObservers() == "object" && typeof item.onmouseover == "function" && item instanceof CMenuItem) {
  168. item.getObservers().attach(this, this.closeMenus, item.onmouseover);
  169. }
  170. this.m_items[this.m_items.length] = item;
  171. }
  172. function CBar_getNumItems() {
  173. return this.m_items.length;
  174. }
  175. function CBar_getId() {
  176. return this.m_id;
  177. }
  178. function CBar_get(index) {
  179. if(index >= 0 && index < this.getNumItems()) {
  180. return this.m_items[index];
  181. }
  182. return null;
  183. }
  184. function CBar_hide(index) {
  185. if(index > 0 && index < this.getNumItems()) {
  186. if(typeof this.m_items[i].hide == "function") {
  187. this.m_items[i].hide();
  188. }
  189. }
  190. }
  191. function CBar_show(index) {
  192. if(index > 0 && index < this.getNumItems()) {
  193. if(typeof this.m_items[i].show == "function") {
  194. this.m_items[i].show();
  195. }
  196. }
  197. }
  198. function CBar_enable(index) {
  199. if(index > 0 && index < this.getNumItems()) {
  200. if(typeof this.m_items[i].enable == "function") {
  201. this.m_items[i].enable();
  202. }
  203. }
  204. }
  205. function CBar_disable(index) {
  206. if(index > 0 && index < this.getNumItems()) {
  207. if(typeof this.m_items[i].disable == "function") {
  208. this.m_items[i].disable();
  209. }
  210. }
  211. }
  212. function CBar_getState(index) {
  213. if(index > 0 && index < this.getNumItems()) {
  214. if(typeof this.m_items[i].getState == "function") {
  215. this.m_items[i].getState();
  216. }
  217. }
  218. }
  219. function CBar_setMenuType(menuType) {
  220. this.m_menuType = menuType;
  221. }
  222. function CBar_getMenuType() {
  223. return this.m_menuType;
  224. }
  225. function CBar_setStyle(style) {
  226. this.m_style = style;
  227. }
  228. function CBar_setAlign(align) {
  229. this.m_align = align;
  230. }
  231. function CBar_getStyle() {
  232. return this.m_style;
  233. }
  234. function CBar_closeMenus(state) {
  235. // make sure we hide any submenus which have been opened.
  236. for(var i = 0; i < this.getNumItems(); i++) {
  237. var currentItem = this.get(i);
  238. if(typeof state == 'object') {
  239. if(state.getSubject() == currentItem) {
  240. continue;
  241. }
  242. }
  243. if(typeof currentItem.getMenu == "function") {
  244. var currentMenu = currentItem.getMenu();
  245. if (currentMenu != null && currentMenu.isVisible()) {
  246. currentMenu.remove();
  247. }
  248. }
  249. }
  250. }
  251. function CBar_getHTMLContainer() {
  252. return this.m_htmlContainer;
  253. }
  254. function CBar_getObservers() {
  255. return this.m_observers;
  256. }
  257. function CBar_onmouseover(evt) {
  258. //get the event in a cross-browser fashion
  259. evt = (evt) ? evt : ((event) ? event : null);
  260. // notify our parent (if one exists) of this event
  261. var parent = this.getParent();
  262. if(parent != null && typeof parent.onmouseover == 'function') {
  263. parent.onmouseover(evt);
  264. }
  265. // notify observers of this event
  266. this.getObservers().notify(CBar_onmouseover);
  267. }
  268. function CBar_onmouseout(evt) {
  269. //get the event in a cross-browser fashion
  270. evt = (evt) ? evt : ((event) ? event : null);
  271. // notify our parent (if one exists) of this event
  272. var parent = this.getParent();
  273. if(parent != null && typeof parent.onmouseout == 'function') {
  274. parent.onmouseout(evt);
  275. }
  276. // notify observers of this event
  277. this.getObservers().notify(CBar_onmouseout);
  278. }
  279. function CBar_onmouseup(evt) {
  280. //get the event in a cross-browser fashion
  281. evt = (evt) ? evt : ((event) ? event : null);
  282. // notify our parent (if one exists) of this event
  283. var parent = this.getParent();
  284. if(parent != null && typeof parent.onmouseup == 'function') {
  285. parent.onmouseup(evt);
  286. }
  287. // notify observers of this event
  288. this.getObservers().notify(CBar_onmouseup);
  289. }
  290. function CBar_onkeypress(evt) {
  291. //get the event in a cross-browser fashion
  292. evt = (evt) ? evt : ((event) ? event : null);
  293. // notify our parent (if one exists) of this event
  294. var parent = this.getParent();
  295. if(parent != null && typeof parent.onkeypress == 'function') {
  296. parent.onkeypress(evt);
  297. }
  298. // notify observers of this event
  299. this.getObservers().notify(CBar_onkeypress);
  300. }
  301. CBar.prototype.draw = CBar_draw;
  302. CBar.prototype.add = CBar_add;
  303. CBar.prototype.get = CBar_get;
  304. CBar.prototype.hide = CBar_hide;
  305. CBar.prototype.show = CBar_show;
  306. CBar.prototype.enable = CBar_enable;
  307. CBar.prototype.disable = CBar_disable;
  308. CBar.prototype.getState = CBar_getState;
  309. CBar.prototype.attachEvents = CBar_attachEvents;
  310. CBar.prototype.drawItems = CBar_drawItems;
  311. CBar.prototype.getId = CBar_getId;
  312. CBar.prototype.setMenuType = CBar_setMenuType;
  313. CBar.prototype.getMenuType = CBar_getMenuType;
  314. CBar.prototype.getNumItems = CBar_getNumItems;
  315. CBar.prototype.setStyle = CBar_setStyle;
  316. CBar.prototype.getStyle = CBar_getStyle;
  317. CBar.prototype.setAlign = CBar_setAlign;
  318. CBar.prototype.closeMenus = CBar_closeMenus;
  319. CBar.prototype.setParent = CBar_setParent;
  320. CBar.prototype.getParent = CBar_getParent;
  321. CBar.prototype.getHTMLContainer = CBar_getHTMLContainer;
  322. CBar.prototype.getObservers = CBar_getObservers;
  323. CBar.prototype.update = new Function("return true");
  324. CBar.prototype.getObservers = CBar_getObservers;
  325. CBar.prototype.onmouseover = CBar_onmouseover;
  326. CBar.prototype.onmouseout = CBar_onmouseout;
  327. CBar.prototype.onmouseup = CBar_onmouseup;
  328. CBar.prototype.onkeypress = CBar_onkeypress;
  329. CBar.prototype.hideBar = CBar_hideBar;
  330. CBar.prototype.showBar = CBar_showBar;
  331. CBar.prototype.toggleBar = CBar_toggleBar;
  332. /* Constants */
  333. var DISPLAY_INLINE = 'inline';
  334. var DISPLAY_NONE = 'none';
  335. var DISPLAY_BLOCK = 'block';