_TabContainerWidget.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. define([
  2. "dojo/_base/declare", // declare
  3. "dojo/_base/lang", // lang.mixin
  4. "dojo/dom",
  5. "dojo/hash",
  6. "dojo/router",
  7. "hpcc/_Widget",
  8. "dijit/registry"
  9. ], function (declare, lang, dom, hash, router,
  10. _Widget,
  11. registry) {
  12. return declare("_TabContainerWidget", [_Widget], {
  13. // Assumptions:
  14. // this.id + "BorderContainer" may exist.
  15. // this.id + "TabContainer" exits.
  16. // Child Tab Widgets ID have an underbar after the parent ID -> "${ID}_thisid" (this id for automatic back/forward button support.
  17. baseClass: "_TabContainerWidget",
  18. borderContainer: null,
  19. _tabContainer: null,
  20. disableHashing: 0,
  21. // String helpers ---
  22. idToPath: function (id) {
  23. var obj = id.split("_");
  24. return "/" + obj.join("/");
  25. },
  26. pathToId: function (path) {
  27. var obj = path.split("/");
  28. obj.splice(0, 1);
  29. return obj.join("_");
  30. },
  31. startsWith: function (tst, str) {
  32. if (tst.length > str.length) {
  33. return false;
  34. }
  35. for (var i = 0; i < tst.length; ++i) {
  36. if (tst.charAt(i) !== str.charAt(i)) {
  37. return false;
  38. }
  39. }
  40. return true;
  41. },
  42. buildRendering: function () {
  43. this.inherited(arguments);
  44. },
  45. postCreate: function (args) {
  46. this.inherited(arguments);
  47. this.borderContainer = registry.byId(this.id + "BorderContainer");
  48. this._tabContainer = registry.byId(this.id + "TabContainer");
  49. var context = this;
  50. this._tabContainer.watch("selectedChildWidget", function (name, oval, nval) {
  51. context.onNewTabSelection({
  52. oldWidget: oval,
  53. newWidget: nval
  54. });
  55. });
  56. },
  57. startup: function () {
  58. if (this._started) {
  59. return;
  60. }
  61. this.inherited(arguments);
  62. var context = this;
  63. var obj = router.register(this.getPath() + "/:sel", function (evt) {
  64. context.routerCallback(evt, true);
  65. });
  66. router.registerBefore(this.getPath() + "/:sel/*other", function (evt) {
  67. context.routerCallback(evt, false);
  68. });
  69. router.startup();
  70. },
  71. resize: function (args) {
  72. this.inherited(arguments);
  73. if (this.borderContainer) {
  74. this.borderContainer.resize();
  75. } else {
  76. this._tabContainer.resize();
  77. }
  78. },
  79. layout: function (args) {
  80. this.inherited(arguments);
  81. },
  82. destroy: function (args) {
  83. this.inherited(arguments);
  84. },
  85. // Hash Helpers ---
  86. onNewTabSelection: function (notification) {
  87. var currHash = hash();
  88. var newHash = this.getSelectedPath();
  89. if (newHash != this.idToPath(notification.newWidget.id)) {
  90. var d = 0;
  91. }
  92. if (this.disableHashing) {
  93. this.go(this.getSelectedPath(), false, true);
  94. } else {
  95. var overwrite = this.startsWith(currHash, newHash);
  96. this.go(this.getSelectedPath(), overwrite);
  97. }
  98. },
  99. go: function (path, replace, noHash) {
  100. console.log(this.id + ".go(" + path + ", " + replace + ", " + noHash + ")");
  101. if (noHash) {
  102. var d = 0;
  103. } else {
  104. hash(path, replace);
  105. }
  106. router._handlePathChange(path);
  107. },
  108. routerCallback: function (evt) {
  109. var currSel = this.getSelectedChild();
  110. var newSel = this.id + "_" + evt.params.sel;
  111. if (!currSel || currSel.id != newSel) {
  112. this.selectChild(newSel, null);
  113. }
  114. if (this.initTab) {
  115. this.initTab();
  116. }
  117. },
  118. getPath: function () {
  119. return this.idToPath(this.id);
  120. },
  121. getSelectedPath: function () {
  122. var selWidget = this._tabContainer.get("selectedChildWidget");
  123. if (!selWidget || selWidget == this._tabContainer) {
  124. return null;
  125. }
  126. if (selWidget.getPath) {
  127. return selWidget.getPath();
  128. }
  129. return this.idToPath(selWidget.id);
  130. },
  131. // Tab Helpers ---
  132. getSelectedChild: function () {
  133. return this._tabContainer.get("selectedChildWidget");
  134. },
  135. getChildren: function () {
  136. return this._tabContainer.getChildren();
  137. },
  138. addChild: function (child, pos) {
  139. //this.disableHashing++;
  140. var retVal = this._tabContainer.addChild(child, pos);
  141. //this.disableHashing--;
  142. return retVal;
  143. },
  144. removeChild: function (child) {
  145. this._tabContainer.removeChild(child);
  146. child.destroyRecursive();
  147. },
  148. removeAllChildren: function() {
  149. var tabs = this._tabContainer.getChildren();
  150. for (var i = 0; i < tabs.length; ++i) {
  151. this.removeChild(tabs[i]);
  152. }
  153. },
  154. selectChild: function (child, doHash) {
  155. if (!doHash) {
  156. this.disableHashing++;
  157. }
  158. var currSel = this.getSelectedChild();
  159. if (currSel != child) {
  160. var nodeExists = dom.byId(child);
  161. if (nodeExists) {
  162. this._tabContainer.selectChild(child);
  163. }
  164. } else {
  165. this.onNewTabSelection({
  166. oldWidget: null,
  167. newWidget: child
  168. })
  169. }
  170. if (!doHash) {
  171. this.disableHashing--;
  172. }
  173. }
  174. });
  175. });