_TabContainerWidget.js 6.1 KB

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