CViewerToolbar.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Viewer
  5. *| (C) Copyright IBM Corp. 2001, 2017
  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. * CViewerToolbar
  14. * @constructor
  15. */
  16. function CViewerToolbar()
  17. {
  18. this.m_specification = null;
  19. this.m_oCBar = null;
  20. this.m_sWebContentRoot = null;
  21. this.m_sSkin = null;
  22. }
  23. /**
  24. * Returns the namespace defined in the JSON specification
  25. * @return (string)
  26. */
  27. CViewerToolbar.prototype.getNamespace = function()
  28. {
  29. if(this.m_specification && typeof this.m_specification.namespace != "undefined")
  30. {
  31. return this.m_specification.namespace;
  32. }
  33. return "";
  34. };
  35. /**
  36. * Returns the webContentRoot
  37. * @return (string)
  38. */
  39. CViewerToolbar.prototype.getSkin = function()
  40. {
  41. if (this.m_sSkin == null)
  42. {
  43. var oCV = null;
  44. try
  45. {
  46. oCV = getCognosViewerObjectRef(this.getNamespace());
  47. }
  48. catch(exception){}
  49. if (oCV)
  50. {
  51. this.m_sSkin = oCV.getSkin();
  52. }
  53. else
  54. {
  55. this.m_sSkin = this.getWebContentRoot() + "/skins/corporate";
  56. }
  57. }
  58. return this.m_sSkin;
  59. };
  60. /**
  61. * Returns the webContentRoot
  62. * @return (string)
  63. */
  64. CViewerToolbar.prototype.getWebContentRoot = function()
  65. {
  66. if (this.m_sWebContentRoot == null)
  67. {
  68. var oCV = null;
  69. try
  70. {
  71. oCV = getCognosViewerObjectRef(this.getNamespace());
  72. }
  73. catch(exception){}
  74. if (oCV)
  75. {
  76. this.m_sWebContentRoot = oCV.getWebContentRoot();
  77. }
  78. else
  79. {
  80. this.m_sWebContentRoot = "..";
  81. }
  82. }
  83. return this.m_sWebContentRoot;
  84. };
  85. /**
  86. * Returns the div id where the toolbar renders itself
  87. * @return (string)
  88. */
  89. CViewerToolbar.prototype.getDivId = function()
  90. {
  91. if(this.m_specification && typeof this.m_specification.divId != "undefined")
  92. {
  93. return this.m_specification.divId;
  94. }
  95. return "";
  96. };
  97. /**
  98. * Returns the local defined style used by the viewer front end
  99. * @return (string)
  100. */
  101. CViewerToolbar.prototype.getStyle = function()
  102. {
  103. if(this.m_specification && typeof this.m_specification.style != "undefined")
  104. {
  105. return this.m_specification.style;
  106. }
  107. return "";
  108. };
  109. /**
  110. * Returns the toolbar specification (CViewerToolbarSpecification) defined by the JSON specification passed in to the initialize method
  111. * @return CViewerToolbarSpecification
  112. */
  113. CViewerToolbar.prototype.getToolbarSpecification = function()
  114. {
  115. if(this.m_specification && typeof this.m_specification.S != "undefined")
  116. {
  117. return new CViewerToolbarSpecification(this,this.m_specification.S);
  118. }
  119. return null;
  120. };
  121. /**
  122. * Returns the ui item specified by the id (the id should not contain the namespace, the viewer toolbar will look after appending it
  123. * @return UI item (CMenuItem, CToolbarButton, StaticText, etc)
  124. */
  125. CViewerToolbar.prototype.getItem = function(sId)
  126. {
  127. if(this.m_oCBar)
  128. {
  129. var numItems = this.m_oCBar.getNumItems();
  130. sId = this.getNamespace() + sId;
  131. for(var index = 0; index < numItems; ++index)
  132. {
  133. var uiItem = this.m_oCBar.get(index);
  134. if(typeof uiItem.getId == "function" && uiItem.getId() == sId)
  135. {
  136. return uiItem;
  137. }
  138. }
  139. }
  140. return null;
  141. };
  142. /**
  143. * Initializes the viewer toolbar
  144. * @param jsonSpecification - JSON object specification
  145. */
  146. CViewerToolbar.prototype.init = function(jsonSpecification)
  147. {
  148. if(typeof jsonSpecification != "undefined" && typeof jsonSpecification == "object" && jsonSpecification != null)
  149. {
  150. this.m_specification = jsonSpecification;
  151. }
  152. };
  153. /**
  154. * Returns the CBar object
  155. */
  156. CViewerToolbar.prototype.getCBar = function()
  157. {
  158. if (!this.m_oCBar && this.m_specification)
  159. {
  160. this.load();
  161. }
  162. return this.m_oCBar;
  163. };
  164. /**
  165. * Creates a CBar object using the json specification
  166. */
  167. CViewerToolbar.prototype.load = function()
  168. {
  169. var toolbar = null;
  170. if(this.m_specification != null)
  171. {
  172. var divId = this.getDivId();
  173. var divNode = document.getElementById(divId);
  174. var toolbarSpecification = this.getToolbarSpecification();
  175. if(divNode && toolbarSpecification)
  176. {
  177. toolbar = toolbarSpecification.draw();
  178. }
  179. }
  180. this.m_oCBar = toolbar;
  181. return toolbar;
  182. };
  183. /**
  184. * Renders the viewer toolbar within the containing <div> element.
  185. */
  186. CViewerToolbar.prototype.draw = function()
  187. {
  188. if (this.m_oCBar)
  189. {
  190. this.m_oCBar.draw();
  191. }
  192. };
  193. /**
  194. * CViewerToolbarSpecification
  195. * @constructor
  196. */
  197. function CViewerToolbarSpecification(viewerToolbar, jsonSpecification)
  198. {
  199. this.m_viewerToolbar = viewerToolbar;
  200. this.m_toolbarSpecification = jsonSpecification;
  201. }
  202. /**
  203. * Builds ui elements using the toolbar specification
  204. */
  205. CViewerToolbarSpecification.prototype.draw = function()
  206. {
  207. if(this.m_toolbarSpecification)
  208. {
  209. var toolbarStyle = gToolbarStyle;
  210. if (this.m_viewerToolbar.getStyle() === 'banner')
  211. {
  212. toolbarStyle = gBannerToolbarStyle;
  213. }
  214. var toolbar = new CBar(this.m_viewerToolbar.getDivId(), toolbarStyle, null, this.m_viewerToolbar.getWebContentRoot());
  215. toolbar.setMenuType(cHorizonalBar); // TODO: Where is cHorizontalBar defined?
  216. toolbar.style = this.m_viewerToolbar.getStyle();
  217. toolbar.setAlign("right");
  218. // have we placed a button, menuItem on the toolbar yet
  219. var bToolbarHasItem = false;
  220. // keeps the seperatorSpec until we go to place the next item
  221. var oSeperatorObj = null;
  222. var oSeperatorSpecification = null;
  223. for (var iIndex=0; iIndex < this.m_toolbarSpecification.length; iIndex++)
  224. {
  225. for(var uiItem in this.m_toolbarSpecification[iIndex])
  226. {
  227. try
  228. {
  229. var object = eval("new " + uiItem + "();");
  230. // P = Seperator
  231. if (uiItem == "P")
  232. {
  233. if (bToolbarHasItem && oSeperatorSpecification == null)
  234. {
  235. oSeperatorObj = object;
  236. oSeperatorSpecification = this.m_toolbarSpecification[iIndex][uiItem];
  237. }
  238. }
  239. else
  240. {
  241. var itemSpec = this.m_toolbarSpecification[iIndex][uiItem];
  242. if (itemSpec.N !== 'openWith' || !window.isIOS()) {
  243. bToolbarHasItem = true;
  244. if (oSeperatorSpecification != null && oSeperatorObj != null) {
  245. oSeperatorObj.load(toolbar, oSeperatorSpecification, this.m_viewerToolbar);
  246. oSeperatorObj = null;
  247. oSeperatorSpecification = null;
  248. }
  249. object.load(toolbar, itemSpec, this.m_viewerToolbar);
  250. }
  251. }
  252. }
  253. catch(exception){}
  254. }
  255. }
  256. return toolbar;
  257. }
  258. return null;
  259. };
  260. // CVButton
  261. function B(){}
  262. B.prototype.isValid = function(jsonSpecification)
  263. {
  264. if(jsonSpecification != null)
  265. {
  266. return true;
  267. }
  268. return false;
  269. };
  270. B.prototype.load = function(parentObject, jsonSpecification, viewerToolbar)
  271. {
  272. if(this.isValid(jsonSpecification))
  273. {
  274. var sTooltip = "";
  275. var sImage = "";
  276. var sAction = "";
  277. var sName = jsonSpecification.N;
  278. var menuItems = null;
  279. if (typeof jsonSpecification.M != "undefined" && jsonSpecification.M.IS != "undefined")
  280. {
  281. menuItems = jsonSpecification.M.IS;
  282. }
  283. // if we don't have an icon defined, then use the first menu item for the button
  284. if (typeof jsonSpecification.C == "undefined")
  285. {
  286. if(menuItems)
  287. {
  288. var firstMenuItem = menuItems[0]["I"];
  289. if (firstMenuItem != null && this.isValid(firstMenuItem))
  290. {
  291. sTooltip = firstMenuItem.O;
  292. if (typeof sTooltip == "undefined" || sTooltip == "")
  293. {
  294. sTooltip = firstMenuItem.E;
  295. }
  296. sImage = firstMenuItem.C;
  297. sAction = firstMenuItem.A;
  298. }
  299. }
  300. }
  301. else
  302. {
  303. sTooltip = jsonSpecification.O;
  304. sImage = jsonSpecification.C;
  305. sAction = jsonSpecification.A;
  306. }
  307. var toolbarButton = null;
  308. if (viewerToolbar.getStyle() === "banner")
  309. {
  310. toolbarButton = new CMenuItem(parentObject, "", sAction, sImage, gBannerButtonStyle, viewerToolbar.getWebContentRoot(), viewerToolbar.getSkin());
  311. toolbarButton.setDropDownArrow("dropdown_arrow_banner.gif");
  312. }
  313. else
  314. {
  315. toolbarButton = new CMenuItem(parentObject, "", sAction, sImage, gMenuItemStyle, viewerToolbar.getWebContentRoot(), viewerToolbar.getSkin());
  316. toolbarButton.setDropDownArrow("dropdown_arrow_narrow.gif");
  317. }
  318. toolbarButton.setId(viewerToolbar.getNamespace() + sName);
  319. toolbarButton.setToolTip(sTooltip);
  320. if (typeof jsonSpecification.ALT != "undefined")
  321. {
  322. toolbarButton.setAltText(jsonSpecification.ALT);
  323. }
  324. if(typeof jsonSpecification.D != "undefined" && jsonSpecification.D == "true")
  325. {
  326. toolbarButton.disable();
  327. }
  328. if(typeof jsonSpecification.M != "undefined")
  329. {
  330. var menuSpecification = jsonSpecification.M;
  331. // only add the menu if there's more then one entry, or we have a callback to populate the menu
  332. if (
  333. typeof menuSpecification.Y != "undefined" &&
  334. (
  335. typeof menuSpecification.A != "undefined" ||
  336. (menuItems && menuItems.length > 1) ||
  337. (typeof menuSpecification.H == "undefined" || menuSpecification.H == "false")
  338. )
  339. )
  340. {
  341. var menu = new M();
  342. toolbarButton.m_menu = menu.load(parentObject, menuSpecification, viewerToolbar);
  343. toolbarButton.m_menu.setParent(toolbarButton);
  344. toolbarButton.m_menuType = menuSpecification.Y;
  345. }
  346. }
  347. return toolbarButton;
  348. }
  349. return null;
  350. };
  351. // Menu Item
  352. function I(){}
  353. I.prototype.isValid = function(jsonSpecification)
  354. {
  355. if(typeof jsonSpecification != "undefined" && jsonSpecification != null)
  356. {
  357. return true;
  358. }
  359. return false;
  360. };
  361. I.prototype.load = function(parentObject, jsonSpecification, viewerToolbar)
  362. {
  363. if(this.isValid(jsonSpecification))
  364. {
  365. var sText = jsonSpecification.E;
  366. var sImage = jsonSpecification.C;
  367. var sAction = jsonSpecification.A;
  368. var sName = jsonSpecification.N;
  369. var menuItems = null;
  370. if (typeof jsonSpecification.M != "undefined" && jsonSpecification.M.IS != "undefined")
  371. {
  372. menuItems = jsonSpecification.M.IS;
  373. }
  374. // if we don't have an icon defined, then use the first menu item for the button
  375. if (typeof jsonSpecification.E == "undefined")
  376. {
  377. if(menuItems && menuItems[0])
  378. {
  379. var firstMenuItem = menuItems[0]["I"];
  380. if (firstMenuItem != null && this.isValid(firstMenuItem))
  381. {
  382. sText = firstMenuItem.E;
  383. if (typeof sText == "undefined" || sText == "")
  384. {
  385. sText = firstMenuItem.O;
  386. }
  387. sAction = firstMenuItem.A;
  388. }
  389. }
  390. else {
  391. return null;
  392. }
  393. }
  394. else
  395. {
  396. sText = jsonSpecification.E;
  397. sImage = jsonSpecification.C;
  398. sAction = jsonSpecification.A;
  399. }
  400. var menuStyle = null;
  401. if (parentObject.style && parentObject.style === "banner")
  402. {
  403. menuStyle = gBannerItemStyle;
  404. }
  405. else
  406. {
  407. menuStyle = gMenuItemStyle;
  408. }
  409. var menuItem = new CMenuItem(parentObject, sText, sAction, sImage, menuStyle, viewerToolbar.getWebContentRoot(), viewerToolbar.getSkin());
  410. if(typeof jsonSpecification.ALT != "undefined")
  411. {
  412. menuItem.setAltText(jsonSpecification.ALT);
  413. }
  414. if (parentObject.style && parentObject.style === "banner")
  415. {
  416. menuItem.setDropDownArrow("dropdown_arrow_banner.gif");
  417. }
  418. else
  419. {
  420. menuItem.setDropDownArrow("dropdown_arrow_narrow.gif");
  421. }
  422. menuItem.setId(viewerToolbar.getNamespace() + sName);
  423. if(typeof jsonSpecification.D != "undefined" && jsonSpecification.D == "true")
  424. {
  425. menuItem.disable();
  426. }
  427. if(typeof jsonSpecification.M != "undefined")
  428. {
  429. var menuSpecification = jsonSpecification.M;
  430. // only add the menu if there's more then one entry, or we have a callback to populate the menu
  431. if (
  432. typeof menuSpecification.Y != "undefined" &&
  433. (
  434. typeof menuSpecification.A != "undefined" ||
  435. (menuItems && menuItems.length > 1) ||
  436. (typeof menuSpecification.H == "undefined" || menuSpecification.H == "false")
  437. )
  438. )
  439. {
  440. var menu = new M();
  441. menuItem.m_menu = menu.load(parentObject, menuSpecification, viewerToolbar);
  442. menuItem.m_menu.setParent(menuItem);
  443. menuItem.m_menuType = jsonSpecification.M.Y;
  444. }
  445. }
  446. return menuItem;
  447. }
  448. return null;
  449. };
  450. // Menu
  451. function M(){}
  452. M.prototype.isValid = function(jsonSpecification)
  453. {
  454. return (typeof jsonSpecification != "undefined" && jsonSpecification != null && typeof jsonSpecification.id != "undefined");
  455. };
  456. M.prototype.load = function(parentObject, jsonSpecification, viewerToolbar)
  457. {
  458. if(this.isValid(jsonSpecification))
  459. {
  460. var menu = new CMenu(jsonSpecification.id, gMenuStyle, viewerToolbar.getWebContentRoot());
  461. menu.setParent(parentObject);
  462. if (typeof jsonSpecification.ALT != "undefined")
  463. {
  464. menu.setAltText(jsonSpecification.ALT);
  465. }
  466. try
  467. {
  468. menu.m_oCV = getCognosViewerObjectRef(viewerToolbar.getNamespace());
  469. }
  470. catch(e){}//if we can't get the cognos viewr object, eat the exception
  471. if(typeof jsonSpecification.A != "undefined")
  472. {
  473. menu.registerCallback(jsonSpecification.A);
  474. }
  475. var menuItems = jsonSpecification.IS;
  476. if (menuItems)
  477. {
  478. for(var iIndex=0; iIndex < menuItems.length; iIndex++)
  479. {
  480. for(var uiItem in menuItems[iIndex])
  481. {
  482. try
  483. {
  484. var menuItem = new I();
  485. menuItem.load(menu, menuItems[iIndex][uiItem], viewerToolbar);
  486. }
  487. catch(exception){}
  488. }
  489. }
  490. }
  491. return menu;
  492. }
  493. return null;
  494. };
  495. // StaticText
  496. function T(){}
  497. T.prototype.isValid = function(jsonSpecification)
  498. {
  499. return (typeof jsonSpecification != "undefined" && jsonSpecification != null && typeof jsonSpecification.E != "undefined");
  500. };
  501. T.prototype.load = function(parentObject, jsonSpecification, viewerToolbar)
  502. {
  503. if(this.isValid(jsonSpecification))
  504. {
  505. var textStyle = null;
  506. if (viewerToolbar.getStyle() === "banner")
  507. {
  508. textStyle = gBannerStaticText;
  509. }
  510. else
  511. {
  512. //todo
  513. }
  514. if (jsonSpecification.E && jsonSpecification.E.length > 0) {
  515. var oStaticText = new CStaticText(jsonSpecification.E, textStyle);
  516. if (jsonSpecification.N == "userName")
  517. {
  518. oStaticText.setId("userNameTD" + viewerToolbar.getNamespace());
  519. }
  520. if (jsonSpecification.ALT) {
  521. oStaticText.setLabelledBy(jsonSpecification.ALT + " " + jsonSpecification.E);
  522. }
  523. parentObject.add(oStaticText);
  524. }
  525. }
  526. return null;
  527. };
  528. // Link
  529. function L(){}
  530. L.prototype.isValid = function(jsonSpecification)
  531. {
  532. return (typeof jsonSpecification != "undefined" && jsonSpecification != null && typeof jsonSpecification.E != "undefined");
  533. };
  534. L.prototype.load = function(parentObject, jsonSpecification, viewerToolbar)
  535. {
  536. if(this.isValid(jsonSpecification))
  537. {
  538. var linkStyle = null;
  539. if (viewerToolbar.getStyle() === "banner")
  540. {
  541. linkStyle = gBannerLink;
  542. }
  543. else
  544. {
  545. //todo
  546. }
  547. var sAction = jsonSpecification.A;
  548. // todo: for now simply use the CMenuItem class
  549. var menuItem = new CMenuItem(parentObject, jsonSpecification.E, sAction, "", linkStyle, viewerToolbar.getWebContentRoot(), viewerToolbar.getSkin());
  550. // Workaround since CMenuItem currently looks at all its siblings to see if one has an icon
  551. menuItem.iconPlaceholder = false;
  552. if (jsonSpecification.ALT != "undefined")
  553. {
  554. menuItem.setAltText(jsonSpecification.ALT);
  555. }
  556. return menuItem;
  557. }
  558. return null;
  559. };
  560. // Seperator
  561. function P() {}
  562. P.prototype.isValid = function(jsonSpecification)
  563. {
  564. return (typeof jsonSpecification != "undefined" && jsonSpecification != null && typeof jsonSpecification.Y != "undefined");
  565. };
  566. P.prototype.load = function(parentObject, jsonSpecification, viewerToolbar)
  567. {
  568. if(this.isValid(jsonSpecification))
  569. {
  570. var seperator = new CSeperator(jsonSpecification.Y, "", "", viewerToolbar.getWebContentRoot());
  571. if (viewerToolbar.getStyle() === "banner")
  572. {
  573. seperator.setToolbarSeperatorClass("bannerDivider");
  574. }
  575. else
  576. {
  577. seperator.setToolbarSeperatorClass("toolbarDivider");
  578. }
  579. parentObject.add(seperator);
  580. return seperator;
  581. }
  582. return null;
  583. };