ViewerIWidgetInlineDialog.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Viewer
  5. *| (C) Copyright IBM Corp. 2001, 2013
  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. dojo.provide("ViewerIWidgetInlineDialog");
  13. dojo.require("dijit._base.focus");
  14. /**
  15. * Class that will take care of showing an inline dialog centered in the widget.
  16. * @param {Object} oCV
  17. * @param {Object} sHTML
  18. */
  19. dojo.declare("ViewerIWidgetInlineDialog", null, {
  20. constructor: function(oCV, sHTML, aButtons, sButtonContainerId) {
  21. this.initialize(oCV, sHTML);
  22. this.m_aButtons = aButtons;
  23. this.m_sButtonContainerId = sButtonContainerId;
  24. },
  25. initialize: function(oCV, sHTML) {
  26. this.m_oCV = oCV;
  27. this.m_html = sHTML;
  28. this.m_viewerDiv = dojo.byId("RVContent" + this.m_oCV.getId());
  29. this.positionOptions = {horizontal:"center", vertical:"center"};
  30. this.onScrollHandle = null;
  31. this.m_dialogDiv = null;
  32. if( this.m_oCV.getViewerWidget ){
  33. this.m_background = this.m_oCV.getViewerWidget().getReportBlocker();;
  34. }
  35. },
  36. /**
  37. * Sets the vertical option for the dialog. Currently only supports top and middle
  38. * @param {Object} option
  39. */
  40. setVerticalPositionOption: function(option) {
  41. this.positionOptions.vertical = option;
  42. },
  43. getDialogDiv: function() {
  44. return this.m_dialogDiv;
  45. },
  46. initializeDialog: function() {
  47. this.m_dialogDiv = document.createElement("div");
  48. dojo.addClass(this.m_dialogDiv, 'inlineDialog');
  49. this.m_dialogDiv.style.display = "none";
  50. this.m_dialogDiv.innerHTML = this.m_html;
  51. dojo.place(this.m_dialogDiv, this.m_background.getNode().nextSibling, 'before');
  52. var buttonDiv = dojo.byId(this.m_sButtonContainerId);
  53. if (buttonDiv && this.m_aButtons && this.m_aButtons.length > 0) {
  54. dojo.forEach(this.m_aButtons, function(button) {
  55. var span = document.createElement("span");
  56. buttonDiv.appendChild(span);
  57. var dijitButton = new dijit.form.Button(button, span);
  58. });
  59. }
  60. },
  61. createBackgroundTabCatchDiv: function()
  62. {
  63. // we need a div with a tabIndex of zero as the first child of the Viewer div. This will catch the user
  64. // hitting F12 on a widget and place the focus on the first focusable item in the dialog.
  65. if (this.m_tabCatch == null) {
  66. this.m_tabCatch = dojo.byId("BACKGROUND_TABCATCH" + this.m_oCV.getId());
  67. if (!this.m_tabCatch) {
  68. this.m_tabCatch = document.createElement("div");
  69. this.m_tabCatch.id = "BACKGROUND_TABCATCH" + this.m_oCV.getId();
  70. this.m_tabCatch.style.display = "none";
  71. this.m_tabCatch.style.position = "absolute";
  72. this.m_tabCatch.style.left = "0";
  73. this.m_tabCatch.style.top = "0";
  74. this.m_tabCatch.style.height = "0px";
  75. this.m_tabCatch.style.width = "0px";
  76. this.m_tabCatch.setAttribute("tabIndex", "0");
  77. if (this.m_viewerDiv.firstChild) { // may be empty in JsUnit tests
  78. dojo.place(this.m_tabCatch, this.m_viewerDiv.firstChild, "before");
  79. }
  80. else {
  81. this.m_viewerDiv.appendChild(this.m_tabCatch);
  82. }
  83. }
  84. }
  85. },
  86. initializeBackground: function() {
  87. this.createBackgroundTabCatchDiv();
  88. },
  89. initializeKeyboardSupport: function() {
  90. // setup the keyboard support so we keep looping through
  91. // the open dialog when the user hits TAB
  92. var tabElems = dijit._getTabNavigable(this.m_dialogDiv);
  93. var firstElem = tabElems.lowest || tabElems.first;
  94. var lastElem = tabElems.last || tabElems.highest || firstElem;
  95. if (firstElem) {
  96. this.m_tabCatch.onfocus = function() {dijit.focus(firstElem);};
  97. this.m_background.getNode().onfocus = function() {dijit.focus(firstElem);};
  98. }
  99. if (lastElem && firstElem) {
  100. lastElem.onblur = function() {dijit.focus(firstElem);};
  101. } else if (firstElem) {
  102. firstElem.onblur = function() {dijit.focus(firstElem);};
  103. }
  104. if (this.m_oCV.getKeepFocus() != false && firstElem && firstElem.focus) {
  105. firstElem.focus();
  106. }
  107. },
  108. show: function() {
  109. this.m_oCV.setVisibleDialog(this);
  110. if (this.m_tabCatch == null) {
  111. this.initializeBackground();
  112. }
  113. if (this.m_dialogDiv == null) {
  114. this.initializeDialog();
  115. }
  116. this.m_background.setClassNameOverride( 'inlineDialog-background-error' );
  117. this.m_background.show();
  118. this.m_tabCatch.style.display = "block";
  119. this.m_dialogDiv.style.display = "block";
  120. this.initializeKeyboardSupport();
  121. },
  122. hide: function() {
  123. this.m_oCV.setVisibleDialog(null);
  124. if (this.onScrollHandle != null) {
  125. dojo.disconnect(this.onScrollHandle);
  126. this.onScrollHandle = null;
  127. }
  128. if (this.m_background != null) {
  129. this.m_background.hide();
  130. }
  131. if (this.m_tabCatch != null) {
  132. this.m_tabCatch.style.display = "none";
  133. }
  134. if (this.m_dialogDiv != null) {
  135. this.m_dialogDiv.style.display = "none";
  136. }
  137. },
  138. destroy: function() {
  139. this.hide();
  140. if (this.m_dialogDiv != null) {
  141. this.m_dialogDiv.parentNode.removeChild(this.m_dialogDiv);
  142. this.m_dialogDiv = null;
  143. }
  144. if (this.m_tabCatch != null) {
  145. this.m_tabCatch.parentNode.removeChild(this.m_tabCatch);
  146. this.m_tabCatch = null;
  147. }
  148. if (this.m_background != null) {
  149. this.m_background.destroy();
  150. this.m_background = null;
  151. }
  152. },
  153. /**
  154. * Will add a open/close link at that the top of page for debugging
  155. * @param {Object} sDialogObj
  156. */
  157. setDebugHelper: function(sDialogObj) {
  158. var debugDiv = document.createElement("div");
  159. debugDiv.style.zIndex = "100";
  160. debugDiv.style.display = "block";
  161. debugDiv.style.position = "relative";
  162. debugDiv.style.backgroundColor = "#FFFFFF";
  163. debugDiv.innerHTML = "<table><tr><td>" +
  164. "<a href=\"#\" onclick=\"" + sDialogObj + ".show();\">show</a>" + " " +
  165. "<a href=\"#\" onclick=\"" + sDialogObj + ".hide();\">hide</a></td></tr></table>";
  166. document.body.insertBefore(debugDiv, document.body.firstChild);
  167. }
  168. });