| 1 | // This is a simple object that manages a group of tabs |
|---|
| 2 | // and keeps them synchronized so that only one is selected |
|---|
| 3 | // at a time |
|---|
| 4 | |
|---|
| 5 | TabGroupView = new Class(View, { |
|---|
| 6 | /* |
|---|
| 7 | element: what element to put the tabs in |
|---|
| 8 | |
|---|
| 9 | tabs: array of tab info with the fields: |
|---|
| 10 | content: HTML to display in the tab OR |
|---|
| 11 | contentRenderer: a callback to render the tab |
|---|
| 12 | selected: is this tab selected by default |
|---|
| 13 | clickHandler: callback when the tab is selected |
|---|
| 14 | |
|---|
| 15 | styles: hash of classnames for the tabs |
|---|
| 16 | tabClass: the class to be applied to all the tabs |
|---|
| 17 | tabSelectedClass: the class to be applied to the currently selected tab |
|---|
| 18 | */ |
|---|
| 19 | init: function (element, tabs, styles) { |
|---|
| 20 | TabGroupView.superClass.init.apply(this, [{'view': element}]); |
|---|
| 21 | |
|---|
| 22 | if (!element) |
|---|
| 23 | return; |
|---|
| 24 | |
|---|
| 25 | this.tabs = []; |
|---|
| 26 | this.ele = element; |
|---|
| 27 | this.clickHandlers = {}; |
|---|
| 28 | |
|---|
| 29 | if (!tabs || !tabs.length) |
|---|
| 30 | return; |
|---|
| 31 | |
|---|
| 32 | for (var i=0; i < tabs.length; i++) { |
|---|
| 33 | var tabInfo = tabs[i]; |
|---|
| 34 | var tabElement = document.createElement("span"); |
|---|
| 35 | this.ele.appendChild(tabElement); |
|---|
| 36 | |
|---|
| 37 | tabInfo.tabSelectedClass = styles.tabSelectedClass; |
|---|
| 38 | tabInfo.tabClass = styles.tabClass; |
|---|
| 39 | |
|---|
| 40 | var tab = new TabView(); |
|---|
| 41 | tab.init({'view': tabElement}, tabInfo); |
|---|
| 42 | |
|---|
| 43 | if (tabInfo.selected) |
|---|
| 44 | tab.select(); |
|---|
| 45 | |
|---|
| 46 | tab.clickHandler2 = tabInfo.clickHandler; |
|---|
| 47 | |
|---|
| 48 | this.tabs.push(tab); |
|---|
| 49 | |
|---|
| 50 | var self = this; |
|---|
| 51 | |
|---|
| 52 | tab.setClickHandler(function (tabClicked) { |
|---|
| 53 | // deselect all the other tabs |
|---|
| 54 | for (var j=0; j < self.tabs.length; j++) { |
|---|
| 55 | if (self.tabs[j] != tabClicked) |
|---|
| 56 | self.tabs[j].deselect(); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | tabClicked.clickHandler2.call(); |
|---|
| 60 | }); |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | }); |
|---|