1 var TabControl = Class.create({
3 Initializes a tab control. The variable +element_id+ must be the id of an HTML element
4 containing one element with it's class name set to 'tabs' and another element with it's
5 class name set to 'pages'.
7 initialize: function(element) {
8 this.element = $(element);
9 this.control_id = this.element.identify();
10 TabControl.controls.set(this.control_id, this);
11 this.tab_container = this.element.down('.tabs');
16 Creates a new tab. The variable +tab_id+ is a unique string used to identify the tab
17 when calling other methods. The variable +caption+ is a string containing the caption
18 of the tab. The variable +page+ is the ID of an HTML element, or the HTML element
19 itself. When a tab is initially added the page element is hidden.
21 addTab: function(tab_id, caption, page) {
22 var tab = new TabControl.Tab(this, tab_id, caption, page);
24 this.tabs.set(tab.id, tab);
25 return this.tab_container.appendChild(tab.createElement());
29 Removes +tab+. The variable +tab+ may be either a tab ID or a tab element.
31 removeTab: function(tab) {
32 if (Object.isString(tab)) tab = this.tabs.get(tab);
36 if (this.selected == tab) {
37 var first = this.firstTab();
38 if (first) this.select(first);
39 else this.selected = null;
44 Selects +tab+ updating the control. The variable +tab+ may be either a tab ID or a
47 select: function(tab) {
48 if (Object.isString(tab)) tab = this.tabs.get(tab);
49 if (this.selected) this.selected.unselect();
52 var persist = this.pageId() + ':' + this.selected.id;
53 document.cookie = "current_tab=" + persist + "; path=/admin";
57 Returns the first tab element that was added using #addTab().
59 firstTab: function() {
60 return this.tabs.get(this.tabs.keys().first());
64 Returns the the last tab element that was added using #addTab().
67 return this.tabs.get(this.tabs.keys().last());
71 Returns the total number of tab elements managed by the control.
73 tabCount: function() {
74 return this.tabs.keys().length;
77 autoSelect: function() {
78 if (!this.tabs.any()) return; // no tabs in control
80 var tab, matches = document.cookie.match(/current_tab=(.+?);/);
82 matches = matches[1].split(':');
83 var page = matches[0], tabId = matches[1];
84 if (!page || page == this.pageId()) tab = this.tabs.get(tabId);
86 this.select(tab || this.firstTab());
90 return /(\d+)/.test(window.location.pathname) ? RegExp.$1 : '';
94 TabControl.controls = $H();
96 TabControl.Tab = Class.create({
97 initialize: function(control, id, label, content) {
98 this.content = $(content).hide();
99 this.label = label || id;
101 this.control = control;
104 createElement: function() {
105 return this.element = new Element('a', { className: 'tab', href: '#' }).
106 update("<span>" + this.label + "</span>").
107 observe('click', function(event){
108 this.control.select(this.id);
110 }.bindAsEventListener(this));
115 this.element.addClassName('here');
118 unselect: function() {
120 this.element.removeClassName('here');
124 this.content.remove();
125 this.element.stopObserving('click').remove();