TabPanel.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * TabPanel.js
  3. *
  4. * Copyright, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://www.tinymce.com/license
  8. * Contributing: http://www.tinymce.com/contributing
  9. */
  10. /**
  11. * Creates a tab panel control.
  12. *
  13. * @-x-less TabPanel.less
  14. * @class tinymce.ui.TabPanel
  15. * @extends tinymce.ui.Panel
  16. *
  17. * @setting {Number} activeTab Active tab index.
  18. */
  19. define("tinymce/ui/TabPanel", [
  20. "tinymce/ui/Panel",
  21. "tinymce/ui/DomUtils"
  22. ], function(Panel, DomUtils) {
  23. "use strict";
  24. return Panel.extend({
  25. lastIdx: 0,
  26. Defaults: {
  27. layout: 'absolute',
  28. defaults: {
  29. type: 'panel'
  30. }
  31. },
  32. /**
  33. * Activates the specified tab by index.
  34. *
  35. * @method activateTab
  36. * @param {Number} idx Index of the tab to activate.
  37. */
  38. activateTab: function(idx) {
  39. var activeTabElm;
  40. if (this.activeTabId) {
  41. activeTabElm = this.getEl(this.activeTabId);
  42. DomUtils.removeClass(activeTabElm, this.classPrefix + 'active');
  43. activeTabElm.setAttribute('aria-selected', "false");
  44. }
  45. this.activeTabId = 't' + idx;
  46. activeTabElm = this.getEl('t' + idx);
  47. activeTabElm.setAttribute('aria-selected', "true");
  48. DomUtils.addClass(activeTabElm, this.classPrefix + 'active');
  49. if (idx != this.lastIdx) {
  50. this.items()[this.lastIdx].hide();
  51. this.lastIdx = idx;
  52. }
  53. this.items()[idx].show().fire('showtab');
  54. this.reflow();
  55. },
  56. /**
  57. * Renders the control as a HTML string.
  58. *
  59. * @method renderHtml
  60. * @return {String} HTML representing the control.
  61. */
  62. renderHtml: function() {
  63. var self = this, layout = self._layout, tabsHtml = '', prefix = self.classPrefix;
  64. self.preRender();
  65. layout.preRender(self);
  66. self.items().each(function(ctrl, i) {
  67. var id = self._id + '-t' + i;
  68. ctrl.aria('role', 'tabpanel');
  69. ctrl.aria('labelledby', id);
  70. tabsHtml += (
  71. '<div id="' + id + '" class="' + prefix + 'tab" ' +
  72. 'unselectable="on" role="tab" aria-controls="' + ctrl._id + '" aria-selected="false" tabIndex="-1">' +
  73. self.encode(ctrl.settings.title) +
  74. '</div>'
  75. );
  76. });
  77. return (
  78. '<div id="' + self._id + '" class="' + self.classes() + '" hidefocus="1" tabindex="-1">' +
  79. '<div id="' + self._id + '-head" class="' + prefix + 'tabs" role="tablist">' +
  80. tabsHtml +
  81. '</div>' +
  82. '<div id="' + self._id + '-body" class="' + self.classes('body') + '">' +
  83. layout.renderHtml(self) +
  84. '</div>' +
  85. '</div>'
  86. );
  87. },
  88. /**
  89. * Called after the control has been rendered.
  90. *
  91. * @method postRender
  92. */
  93. postRender: function() {
  94. var self = this;
  95. self._super();
  96. self.settings.activeTab = self.settings.activeTab || 0;
  97. self.activateTab(self.settings.activeTab);
  98. this.on('click', function(e) {
  99. var targetParent = e.target.parentNode;
  100. if (e.target.parentNode.id == self._id + '-head') {
  101. var i = targetParent.childNodes.length;
  102. while (i--) {
  103. if (targetParent.childNodes[i] == e.target) {
  104. self.activateTab(i);
  105. }
  106. }
  107. }
  108. });
  109. },
  110. /**
  111. * Initializes the current controls layout rect.
  112. * This will be executed by the layout managers to determine the
  113. * default minWidth/minHeight etc.
  114. *
  115. * @method initLayoutRect
  116. * @return {Object} Layout rect instance.
  117. */
  118. initLayoutRect: function() {
  119. var self = this, rect, minW, minH;
  120. minW = DomUtils.getSize(self.getEl('head')).width;
  121. minW = minW < 0 ? 0 : minW;
  122. minH = 0;
  123. self.items().each(function(item, i) {
  124. minW = Math.max(minW, item.layoutRect().minW);
  125. minH = Math.max(minH, item.layoutRect().minH);
  126. if (self.settings.activeTab != i) {
  127. item.hide();
  128. }
  129. });
  130. self.items().each(function(ctrl) {
  131. ctrl.settings.x = 0;
  132. ctrl.settings.y = 0;
  133. ctrl.settings.w = minW;
  134. ctrl.settings.h = minH;
  135. ctrl.layoutRect({
  136. x: 0,
  137. y: 0,
  138. w: minW,
  139. h: minH
  140. });
  141. });
  142. var headH = DomUtils.getSize(self.getEl('head')).height;
  143. self.settings.minWidth = minW;
  144. self.settings.minHeight = minH + headH;
  145. rect = self._super();
  146. rect.deltaH += headH;
  147. rect.innerH = rect.h - rect.deltaH;
  148. return rect;
  149. }
  150. });
  151. });