bgneal@312: /** bgneal@312: * mctabs.js bgneal@312: * bgneal@312: * Copyright 2009, Moxiecode Systems AB bgneal@312: * Released under LGPL License. bgneal@312: * bgneal@312: * License: http://tinymce.moxiecode.com/license bgneal@312: * Contributing: http://tinymce.moxiecode.com/contributing bgneal@312: */ bgneal@312: bgneal@312: function MCTabs() { bgneal@312: this.settings = []; bgneal@442: this.onChange = tinyMCEPopup.editor.windowManager.createInstance('tinymce.util.Dispatcher'); bgneal@312: }; bgneal@312: bgneal@312: MCTabs.prototype.init = function(settings) { bgneal@312: this.settings = settings; bgneal@312: }; bgneal@312: bgneal@312: MCTabs.prototype.getParam = function(name, default_value) { bgneal@312: var value = null; bgneal@312: bgneal@312: value = (typeof(this.settings[name]) == "undefined") ? default_value : this.settings[name]; bgneal@312: bgneal@312: // Fix bool values bgneal@312: if (value == "true" || value == "false") bgneal@312: return (value == "true"); bgneal@312: bgneal@312: return value; bgneal@312: }; bgneal@312: bgneal@442: MCTabs.prototype.showTab =function(tab){ bgneal@442: tab.className = 'current'; bgneal@442: tab.setAttribute("aria-selected", true); bgneal@442: tab.setAttribute("aria-expanded", true); bgneal@442: tab.tabIndex = 0; bgneal@442: }; bgneal@442: bgneal@442: MCTabs.prototype.hideTab =function(tab){ bgneal@442: var t=this; bgneal@442: bgneal@442: tab.className = ''; bgneal@442: tab.setAttribute("aria-selected", false); bgneal@442: tab.setAttribute("aria-expanded", false); bgneal@442: tab.tabIndex = -1; bgneal@442: }; bgneal@442: bgneal@442: MCTabs.prototype.showPanel = function(panel) { bgneal@442: panel.className = 'current'; bgneal@442: panel.setAttribute("aria-hidden", false); bgneal@442: }; bgneal@442: bgneal@442: MCTabs.prototype.hidePanel = function(panel) { bgneal@442: panel.className = 'panel'; bgneal@442: panel.setAttribute("aria-hidden", true); bgneal@442: }; bgneal@442: bgneal@442: MCTabs.prototype.getPanelForTab = function(tabElm) { bgneal@442: return tinyMCEPopup.dom.getAttrib(tabElm, "aria-controls"); bgneal@442: }; bgneal@442: bgneal@442: MCTabs.prototype.displayTab = function(tab_id, panel_id, avoid_focus) { bgneal@442: var panelElm, panelContainerElm, tabElm, tabContainerElm, selectionClass, nodes, i, t = this; bgneal@442: bgneal@442: tabElm = document.getElementById(tab_id); bgneal@442: bgneal@442: if (panel_id === undefined) { bgneal@442: panel_id = t.getPanelForTab(tabElm); bgneal@442: } bgneal@312: bgneal@312: panelElm= document.getElementById(panel_id); bgneal@312: panelContainerElm = panelElm ? panelElm.parentNode : null; bgneal@312: tabContainerElm = tabElm ? tabElm.parentNode : null; bgneal@442: selectionClass = t.getParam('selection_class', 'current'); bgneal@312: bgneal@312: if (tabElm && tabContainerElm) { bgneal@312: nodes = tabContainerElm.childNodes; bgneal@312: bgneal@312: // Hide all other tabs bgneal@312: for (i = 0; i < nodes.length; i++) { bgneal@442: if (nodes[i].nodeName == "LI") { bgneal@442: t.hideTab(nodes[i]); bgneal@442: } bgneal@312: } bgneal@312: bgneal@312: // Show selected tab bgneal@442: t.showTab(tabElm); bgneal@312: } bgneal@312: bgneal@312: if (panelElm && panelContainerElm) { bgneal@312: nodes = panelContainerElm.childNodes; bgneal@312: bgneal@312: // Hide all other panels bgneal@312: for (i = 0; i < nodes.length; i++) { bgneal@312: if (nodes[i].nodeName == "DIV") bgneal@442: t.hidePanel(nodes[i]); bgneal@442: } bgneal@442: bgneal@442: if (!avoid_focus) { bgneal@442: tabElm.focus(); bgneal@312: } bgneal@312: bgneal@312: // Show selected panel bgneal@442: t.showPanel(panelElm); bgneal@312: } bgneal@312: }; bgneal@312: bgneal@312: MCTabs.prototype.getAnchor = function() { bgneal@312: var pos, url = document.location.href; bgneal@312: bgneal@312: if ((pos = url.lastIndexOf('#')) != -1) bgneal@312: return url.substring(pos + 1); bgneal@312: bgneal@312: return ""; bgneal@312: }; bgneal@312: bgneal@442: bgneal@442: //Global instance bgneal@312: var mcTabs = new MCTabs(); bgneal@442: bgneal@442: tinyMCEPopup.onInit.add(function() { bgneal@442: var tinymce = tinyMCEPopup.getWin().tinymce, dom = tinyMCEPopup.dom, each = tinymce.each; bgneal@442: bgneal@442: each(dom.select('div.tabs'), function(tabContainerElm) { bgneal@442: var keyNav; bgneal@442: bgneal@442: dom.setAttrib(tabContainerElm, "role", "tablist"); bgneal@442: bgneal@442: var items = tinyMCEPopup.dom.select('li', tabContainerElm); bgneal@442: var action = function(id) { bgneal@442: mcTabs.displayTab(id, mcTabs.getPanelForTab(id)); bgneal@442: mcTabs.onChange.dispatch(id); bgneal@442: }; bgneal@442: bgneal@442: each(items, function(item) { bgneal@442: dom.setAttrib(item, 'role', 'tab'); bgneal@442: dom.bind(item, 'click', function(evt) { bgneal@442: action(item.id); bgneal@442: }); bgneal@442: }); bgneal@442: bgneal@442: dom.bind(dom.getRoot(), 'keydown', function(evt) { bgneal@442: if (evt.keyCode === 9 && evt.ctrlKey && !evt.altKey) { // Tab bgneal@442: keyNav.moveFocus(evt.shiftKey ? -1 : 1); bgneal@442: tinymce.dom.Event.cancel(evt); bgneal@442: } bgneal@442: }); bgneal@442: bgneal@442: each(dom.select('a', tabContainerElm), function(a) { bgneal@442: dom.setAttrib(a, 'tabindex', '-1'); bgneal@442: }); bgneal@442: bgneal@442: keyNav = tinyMCEPopup.editor.windowManager.createInstance('tinymce.ui.KeyboardNavigation', { bgneal@442: root: tabContainerElm, bgneal@442: items: items, bgneal@442: onAction: action, bgneal@442: actOnFocus: true, bgneal@442: enableLeftRight: true, bgneal@442: enableUpDown: true bgneal@442: }, tinyMCEPopup.dom); bgneal@442: }); bgneal@442: });