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