1 // Copyright (c) 2005 spinelz.org (http://script.spinelz.org/)
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 NavPanel = Class.create();
23 NavPanel.className = {
24 navPanel : 'navPanel',
25 panel: 'navPanel_panel',
27 tabLeftInactive : 'navPanel_tabLeftInactive',
28 tabLeftActive: 'navPanel_tabLeftActive',
29 tabMiddleInactive : 'navPanel_tabMiddleInactive',
30 tabMiddleActive : 'navPanel_tabMiddleActive',
31 tabRightInactive : 'navPanel_tabRightInactive',
32 tabRightActive : 'navPanel_tabRightActive'
34 NavPanel.prototype = {
36 initialize: function(element) {
37 var options = Object.extend({
40 }, arguments[1] || {});
42 this.options = options;
43 this.element = $(element);
44 Element.setStyle(this.element, {visibility: 'hidden'});
45 Element.hide(this.element);
47 var customCss = CssUtil.appendPrefix(this.options.cssPrefix, NavPanel.className);
48 this.classNames = new CssUtil([NavPanel.className, customCss]);
50 this.classNames.addClassNames(this.element, 'navPanel');
52 this.selected = (this.options.selected > 0) ? this.options.selected - 1 : 0 ;
55 Element.setStyle(this.element, {visibility: 'visible'});
56 Element.show(this.element);
64 this.tabId = this.element.id + '_tab';
65 this.tabLeftId = this.tabId + '_left';
66 this.tabMiddleId = this.tabId + '_middle';
67 this.tabRightId = this.tabId + '_right';
68 this.panelId = this.element.id + '_panel';
74 Element.cleanWhitespace(this.element);
75 this.panelList = this.element.childNodes;
77 for (var i = 0; i < this.panelList.length; i++) {
78 if (this.panelList[i].nodeType != 1) {
79 Element.remove(this.panelList[i]);
83 Element.cleanWhitespace(this.panelList[i]);
84 var navSet = this.panelList[i].childNodes;
85 this.buildTab(navSet[0], i);
86 this.buildPanel(navSet[0], i);
92 buildTab: function(tabTitle, i) {
93 var tab = Builder.node('div', {id:this.tabId + i});
94 this.classNames.addClassNames(tab, 'tab');
95 var tabLeft = Builder.node('div', {id:this.tabLeftId + i});
96 var tabMiddle = Builder.node('div', {id:this.tabMiddleId + i});
97 tabMiddle.appendChild(tabTitle);
98 var tabRight = Builder.node('div',{id:this.tabRightId + i});
100 tab.appendChild(tabLeft);
101 tab.appendChild(tabMiddle);
102 tab.appendChild(tabRight);
103 Event.observe(tab, 'click', this.selectTab.bindAsEventListener(this));
106 this.setTabInactive(tab);
107 this.panelList[i].appendChild(tab);
110 buildPanel: function(panelContent, i) {
111 var panel = Builder.node('div', {id:this.panelId + i});
112 this.classNames.addClassNames(panel, 'panel');
113 panel.appendChild(panelContent);
115 this.panels[i] = panel;
116 this.panelList[i].appendChild(panel);
119 selectTab: function(e) {
121 if (!this.panels[this.selected]) this.selected = 0;
122 Element.show(this.panels[this.selected]);
123 this.setTabActive(this.tabs[this.selected]);
127 var targetElement = Event.element(e);
128 var targetIndex = this.getTargetIndex(targetElement);
129 if (targetIndex == this.selected) {
133 var currentPanel = this.panels[this.selected];
134 var targetPanel = this.panels[targetIndex];
135 this.setTabInactive(this.tabs[this.selected]);
136 this.setTabActive(this.tabs[targetIndex]);
137 Element.show(targetPanel);
138 Element.hide(currentPanel);
139 this.selected = targetIndex;
142 setTabActive: function(tab) {
143 var tabChildren = tab.childNodes;
145 this.classNames.refreshClassNames(tabChildren[0], 'tabLeftActive');
146 this.classNames.refreshClassNames(tabChildren[1], 'tabMiddleActive');
147 this.classNames.refreshClassNames(tabChildren[2], 'tabRightActive');
150 setTabInactive: function(tab) {
151 var tabChildren = tab.childNodes;
153 this.classNames.refreshClassNames(tabChildren[0], 'tabLeftInactive');
154 this.classNames.refreshClassNames(tabChildren[1], 'tabMiddleInactive');
155 this.classNames.refreshClassNames(tabChildren[2], 'tabRightInactive');
158 getTargetIndex: function(element) {
161 if (element.id && element.id.indexOf(this.tabId, 0) >= 0) {
162 var index = element.id.substring(this.tabId.length);
167 element = element.parentNode;