bgneal@45
|
1 /**
|
bgneal@45
|
2 * mctabs.js
|
bgneal@45
|
3 *
|
bgneal@45
|
4 * Copyright 2009, Moxiecode Systems AB
|
bgneal@45
|
5 * Released under LGPL License.
|
bgneal@45
|
6 *
|
bgneal@45
|
7 * License: http://tinymce.moxiecode.com/license
|
bgneal@45
|
8 * Contributing: http://tinymce.moxiecode.com/contributing
|
bgneal@45
|
9 */
|
bgneal@45
|
10
|
bgneal@45
|
11 function MCTabs() {
|
bgneal@45
|
12 this.settings = [];
|
bgneal@45
|
13 this.onChange = tinyMCEPopup.editor.windowManager.createInstance('tinymce.util.Dispatcher');
|
bgneal@45
|
14 };
|
bgneal@45
|
15
|
bgneal@45
|
16 MCTabs.prototype.init = function(settings) {
|
bgneal@45
|
17 this.settings = settings;
|
bgneal@45
|
18 };
|
bgneal@45
|
19
|
bgneal@45
|
20 MCTabs.prototype.getParam = function(name, default_value) {
|
bgneal@45
|
21 var value = null;
|
bgneal@45
|
22
|
bgneal@45
|
23 value = (typeof(this.settings[name]) == "undefined") ? default_value : this.settings[name];
|
bgneal@45
|
24
|
bgneal@45
|
25 // Fix bool values
|
bgneal@45
|
26 if (value == "true" || value == "false")
|
bgneal@45
|
27 return (value == "true");
|
bgneal@45
|
28
|
bgneal@45
|
29 return value;
|
bgneal@45
|
30 };
|
bgneal@45
|
31
|
bgneal@45
|
32 MCTabs.prototype.showTab =function(tab){
|
bgneal@45
|
33 tab.className = 'current';
|
bgneal@45
|
34 tab.setAttribute("aria-selected", true);
|
bgneal@45
|
35 tab.setAttribute("aria-expanded", true);
|
bgneal@45
|
36 tab.tabIndex = 0;
|
bgneal@45
|
37 };
|
bgneal@45
|
38
|
bgneal@45
|
39 MCTabs.prototype.hideTab =function(tab){
|
bgneal@45
|
40 var t=this;
|
bgneal@45
|
41
|
bgneal@45
|
42 tab.className = '';
|
bgneal@45
|
43 tab.setAttribute("aria-selected", false);
|
bgneal@45
|
44 tab.setAttribute("aria-expanded", false);
|
bgneal@45
|
45 tab.tabIndex = -1;
|
bgneal@45
|
46 };
|
bgneal@45
|
47
|
bgneal@45
|
48 MCTabs.prototype.showPanel = function(panel) {
|
bgneal@45
|
49 panel.className = 'current';
|
bgneal@45
|
50 panel.setAttribute("aria-hidden", false);
|
bgneal@45
|
51 };
|
bgneal@45
|
52
|
bgneal@45
|
53 MCTabs.prototype.hidePanel = function(panel) {
|
bgneal@45
|
54 panel.className = 'panel';
|
bgneal@45
|
55 panel.setAttribute("aria-hidden", true);
|
bgneal@45
|
56 };
|
bgneal@45
|
57
|
bgneal@45
|
58 MCTabs.prototype.getPanelForTab = function(tabElm) {
|
bgneal@45
|
59 return tinyMCEPopup.dom.getAttrib(tabElm, "aria-controls");
|
bgneal@45
|
60 };
|
bgneal@45
|
61
|
bgneal@45
|
62 MCTabs.prototype.displayTab = function(tab_id, panel_id, avoid_focus) {
|
bgneal@45
|
63 var panelElm, panelContainerElm, tabElm, tabContainerElm, selectionClass, nodes, i, t = this;
|
bgneal@45
|
64
|
bgneal@45
|
65 tabElm = document.getElementById(tab_id);
|
bgneal@45
|
66
|
bgneal@45
|
67 if (panel_id === undefined) {
|
bgneal@45
|
68 panel_id = t.getPanelForTab(tabElm);
|
bgneal@45
|
69 }
|
bgneal@45
|
70
|
bgneal@45
|
71 panelElm= document.getElementById(panel_id);
|
bgneal@45
|
72 panelContainerElm = panelElm ? panelElm.parentNode : null;
|
bgneal@45
|
73 tabContainerElm = tabElm ? tabElm.parentNode : null;
|
bgneal@45
|
74 selectionClass = t.getParam('selection_class', 'current');
|
bgneal@45
|
75
|
bgneal@45
|
76 if (tabElm && tabContainerElm) {
|
bgneal@45
|
77 nodes = tabContainerElm.childNodes;
|
bgneal@45
|
78
|
bgneal@45
|
79 // Hide all other tabs
|
bgneal@45
|
80 for (i = 0; i < nodes.length; i++) {
|
bgneal@45
|
81 if (nodes[i].nodeName == "LI") {
|
bgneal@45
|
82 t.hideTab(nodes[i]);
|
bgneal@45
|
83 }
|
bgneal@45
|
84 }
|
bgneal@45
|
85
|
bgneal@45
|
86 // Show selected tab
|
bgneal@45
|
87 t.showTab(tabElm);
|
bgneal@45
|
88 }
|
bgneal@45
|
89
|
bgneal@45
|
90 if (panelElm && panelContainerElm) {
|
bgneal@45
|
91 nodes = panelContainerElm.childNodes;
|
bgneal@45
|
92
|
bgneal@45
|
93 // Hide all other panels
|
bgneal@45
|
94 for (i = 0; i < nodes.length; i++) {
|
bgneal@45
|
95 if (nodes[i].nodeName == "DIV")
|
bgneal@45
|
96 t.hidePanel(nodes[i]);
|
bgneal@45
|
97 }
|
bgneal@45
|
98
|
bgneal@45
|
99 if (!avoid_focus) {
|
bgneal@45
|
100 tabElm.focus();
|
bgneal@45
|
101 }
|
bgneal@45
|
102
|
bgneal@45
|
103 // Show selected panel
|
bgneal@45
|
104 t.showPanel(panelElm);
|
bgneal@45
|
105 }
|
bgneal@45
|
106 };
|
bgneal@45
|
107
|
bgneal@45
|
108 MCTabs.prototype.getAnchor = function() {
|
bgneal@45
|
109 var pos, url = document.location.href;
|
bgneal@45
|
110
|
bgneal@45
|
111 if ((pos = url.lastIndexOf('#')) != -1)
|
bgneal@45
|
112 return url.substring(pos + 1);
|
bgneal@45
|
113
|
bgneal@45
|
114 return "";
|
bgneal@45
|
115 };
|
bgneal@45
|
116
|
bgneal@45
|
117
|
bgneal@45
|
118 //Global instance
|
bgneal@45
|
119 var mcTabs = new MCTabs();
|
bgneal@45
|
120
|
bgneal@45
|
121 tinyMCEPopup.onInit.add(function() {
|
bgneal@45
|
122 var tinymce = tinyMCEPopup.getWin().tinymce, dom = tinyMCEPopup.dom, each = tinymce.each;
|
bgneal@45
|
123
|
bgneal@45
|
124 each(dom.select('div.tabs'), function(tabContainerElm) {
|
bgneal@45
|
125 var keyNav;
|
bgneal@45
|
126
|
bgneal@45
|
127 dom.setAttrib(tabContainerElm, "role", "tablist");
|
bgneal@45
|
128
|
bgneal@45
|
129 var items = tinyMCEPopup.dom.select('li', tabContainerElm);
|
bgneal@45
|
130 var action = function(id) {
|
bgneal@45
|
131 mcTabs.displayTab(id, mcTabs.getPanelForTab(id));
|
bgneal@45
|
132 mcTabs.onChange.dispatch(id);
|
bgneal@45
|
133 };
|
bgneal@45
|
134
|
bgneal@45
|
135 each(items, function(item) {
|
bgneal@45
|
136 dom.setAttrib(item, 'role', 'tab');
|
bgneal@45
|
137 dom.bind(item, 'click', function(evt) {
|
bgneal@45
|
138 action(item.id);
|
bgneal@45
|
139 });
|
bgneal@45
|
140 });
|
bgneal@45
|
141
|
bgneal@45
|
142 dom.bind(dom.getRoot(), 'keydown', function(evt) {
|
bgneal@45
|
143 if (evt.keyCode === 9 && evt.ctrlKey && !evt.altKey) { // Tab
|
bgneal@45
|
144 keyNav.moveFocus(evt.shiftKey ? -1 : 1);
|
bgneal@45
|
145 tinymce.dom.Event.cancel(evt);
|
bgneal@45
|
146 }
|
bgneal@45
|
147 });
|
bgneal@45
|
148
|
bgneal@45
|
149 each(dom.select('a', tabContainerElm), function(a) {
|
bgneal@45
|
150 dom.setAttrib(a, 'tabindex', '-1');
|
bgneal@45
|
151 });
|
bgneal@45
|
152
|
bgneal@45
|
153 keyNav = tinyMCEPopup.editor.windowManager.createInstance('tinymce.ui.KeyboardNavigation', {
|
bgneal@45
|
154 root: tabContainerElm,
|
bgneal@45
|
155 items: items,
|
bgneal@45
|
156 onAction: action,
|
bgneal@45
|
157 actOnFocus: true,
|
bgneal@45
|
158 enableLeftRight: true,
|
bgneal@45
|
159 enableUpDown: true
|
bgneal@45
|
160 }, tinyMCEPopup.dom);
|
bgneal@45
|
161 });
|
bgneal@45
|
162 }); |