4 * Copyright 2009, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://tinymce.moxiecode.com/license
8 * Contributing: http://tinymce.moxiecode.com/contributing
12 var DOM = tinymce.DOM, Event = tinymce.dom.Event, each = tinymce.each;
15 * This class is used to create a split button. A button with a menu attached to it.
17 * @class tinymce.ui.SplitButton
18 * @extends tinymce.ui.Button
20 * // Creates a new plugin class and a custom split button
21 * tinymce.create('tinymce.plugins.ExamplePlugin', {
22 * createControl: function(n, cm) {
24 * case 'mysplitbutton':
25 * var c = cm.createSplitButton('mysplitbutton', {
26 * title : 'My split button',
28 * onclick : function() {
29 * alert('Button was clicked.');
33 * c.onRenderMenu.add(function(c, m) {
34 * m.add({title : 'Some title', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
36 * m.add({title : 'Some item 1', onclick : function() {
37 * alert('Some item 1 was clicked.');
40 * m.add({title : 'Some item 2', onclick : function() {
41 * alert('Some item 2 was clicked.');
45 * // Return the new splitbutton instance
53 tinymce.create('tinymce.ui.SplitButton:tinymce.ui.MenuButton', {
55 * Constructs a new split button control instance.
59 * @param {String} id Control id for the split button.
60 * @param {Object} s Optional name/value settings object.
61 * @param {Editor} ed Optional the editor instance this button is for.
63 SplitButton : function(id, s, ed) {
64 this.parent(id, s, ed);
65 this.classPrefix = 'mceSplitButton';
69 * Renders the split button as a HTML string. This method is much faster than using the DOM and when
70 * creating a whole toolbar with buttons it does make a lot of difference.
73 * @return {String} HTML for the split button control element.
75 renderHTML : function() {
76 var h, t = this, s = t.settings, h1;
81 h1 = DOM.createHTML('img ', {src : s.image, role: 'presentation', 'class' : 'mceAction ' + s['class']});
83 h1 = DOM.createHTML('span', {'class' : 'mceAction ' + s['class']}, '');
85 h1 += DOM.createHTML('span', {'class': 'mceVoiceLabel mceIconOnly', id: t.id + '_voice', style: 'display:none;'}, s.title);
86 h += '<td >' + DOM.createHTML('a', {role: 'button', id : t.id + '_action', tabindex: '-1', href : 'javascript:;', 'class' : 'mceAction ' + s['class'], onclick : "return false;", onmousedown : 'return false;', title : s.title}, h1) + '</td>';
88 h1 = DOM.createHTML('span', {'class' : 'mceOpen ' + s['class']}, '<span style="display:none;" class="mceIconOnly" aria-hidden="true">\u25BC</span>');
89 h += '<td >' + DOM.createHTML('a', {role: 'button', id : t.id + '_open', tabindex: '-1', href : 'javascript:;', 'class' : 'mceOpen ' + s['class'], onclick : "return false;", onmousedown : 'return false;', title : s.title}, h1) + '</td>';
92 h = DOM.createHTML('table', {id : t.id, role: 'presentation', tabindex: '0', 'class' : 'mceSplitButton mceSplitButtonEnabled ' + s['class'], cellpadding : '0', cellspacing : '0', title : s.title}, h);
93 return DOM.createHTML('span', {role: 'button', 'aria-labelledby': t.id + '_voice', 'aria-haspopup': 'true'}, h);
97 * Post render handler. This function will be called after the UI has been
98 * rendered so that events can be added.
102 postRender : function() {
103 var t = this, s = t.settings, activate;
106 activate = function(evt) {
107 if (!t.isDisabled()) {
112 Event.add(t.id + '_action', 'click', activate);
113 Event.add(t.id, ['click', 'keydown'], function(evt) {
114 var DOM_VK_SPACE = 32, DOM_VK_ENTER = 14, DOM_VK_RETURN = 13, DOM_VK_UP = 38, DOM_VK_DOWN = 40;
115 if ((evt.keyCode === 32 || evt.keyCode === 13 || evt.keyCode === 14) && !evt.altKey && !evt.ctrlKey && !evt.metaKey) {
118 } else if (evt.type === 'click' || evt.keyCode === DOM_VK_DOWN) {
125 Event.add(t.id + '_open', 'click', function (evt) {
129 Event.add([t.id, t.id + '_open'], 'focus', function() {t._focused = 1;});
130 Event.add([t.id, t.id + '_open'], 'blur', function() {t._focused = 0;});
132 // Old IE doesn't have hover on all elements
133 if (tinymce.isIE6 || !DOM.boxModel) {
134 Event.add(t.id, 'mouseover', function() {
135 if (!DOM.hasClass(t.id, 'mceSplitButtonDisabled'))
136 DOM.addClass(t.id, 'mceSplitButtonHover');
139 Event.add(t.id, 'mouseout', function() {
140 if (!DOM.hasClass(t.id, 'mceSplitButtonDisabled'))
141 DOM.removeClass(t.id, 'mceSplitButtonHover');
146 destroy : function() {
149 Event.clear(this.id + '_action');
150 Event.clear(this.id + '_open');
151 Event.clear(this.id);