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, Dispatcher = tinymce.util.Dispatcher;
15 * This class is used to create list boxes/select list. This one will generate
16 * a native control the way that the browser produces them by default.
18 * @class tinymce.ui.NativeListBox
19 * @extends tinymce.ui.ListBox
21 tinymce.create('tinymce.ui.NativeListBox:tinymce.ui.ListBox', {
23 * Constructs a new button control instance.
26 * @method NativeListBox
27 * @param {String} id Button control id for the button.
28 * @param {Object} s Optional name/value settings object.
30 NativeListBox : function(id, s) {
32 this.classPrefix = 'mceNativeListBox';
36 * Sets the disabled state for the control. This will add CSS classes to the
37 * element that contains the control. So that it can be disabled visually.
40 * @param {Boolean} s Boolean state if the control should be disabled or not.
42 setDisabled : function(s) {
43 DOM.get(this.id).disabled = s;
44 this.setAriaProperty('disabled', s);
48 * Returns true/false if the control is disabled or not. This is a method since you can then
49 * choose to check some class or some internal bool state in subclasses.
52 * @return {Boolean} true/false if the control is disabled or not.
54 isDisabled : function() {
55 return DOM.get(this.id).disabled;
59 * Selects a item/option by value. This will both add a visual selection to the
60 * item and change the title of the control to the title of the option.
63 * @param {String/function} va Value to look for inside the list box or a function selector.
65 select : function(va) {
69 return t.selectByIndex(-1);
71 // Is string or number make function selector
80 // Do we need to do something?
81 if (va != t.selectedValue) {
83 each(t.items, function(o, i) {
97 * Selects a item/option by index. This will both add a visual selection to the
98 * item and change the title of the control to the title of the option.
100 * @method selectByIndex
101 * @param {String} idx Index to select, pass -1 to select menu/title of select box.
103 selectByIndex : function(idx) {
104 DOM.get(this.id).selectedIndex = idx + 1;
105 this.selectedValue = this.items[idx] ? this.items[idx].value : null;
109 * Adds a option item to the list box.
112 * @param {String} n Title for the new option.
113 * @param {String} v Value for the new option.
114 * @param {Object} o Optional object with settings like for example class.
116 add : function(n, v, a) {
123 DOM.add(DOM.get(this.id), 'option', a, n);
132 t.onAdd.dispatch(t, o);
136 * Executes the specified callback function for the menu item. In this case when the user clicks the menu item.
140 getLength : function() {
141 return this.items.length;
145 * Renders the list box as a HTML string. This method is much faster than using the DOM and when
146 * creating a whole toolbar with buttons it does make a lot of difference.
149 * @return {String} HTML for the list box control element.
151 renderHTML : function() {
154 h = DOM.createHTML('option', {value : ''}, '-- ' + t.settings.title + ' --');
156 each(t.items, function(it) {
157 h += DOM.createHTML('option', {value : it.value}, it.title);
160 h = DOM.createHTML('select', {id : t.id, 'class' : 'mceNativeListBox', 'aria-labelledby': t.id + '_aria'}, h);
161 h += DOM.createHTML('span', {id : t.id + '_aria', 'style': 'display: none'}, t.settings.title);
166 * Post render handler. This function will be called after the UI has been
167 * rendered so that events can be added.
171 postRender : function() {
172 var t = this, ch, changeListenerAdded = true;
176 function onChange(e) {
177 var v = t.items[e.target.selectedIndex - 1];
179 if (v && (v = v.value)) {
180 t.onChange.dispatch(t, v);
182 if (t.settings.onselect)
183 t.settings.onselect(v);
187 Event.add(t.id, 'change', onChange);
189 // Accessibility keyhandler
190 Event.add(t.id, 'keydown', function(e) {
193 Event.remove(t.id, 'change', ch);
194 changeListenerAdded = false;
196 bf = Event.add(t.id, 'blur', function() {
197 if (changeListenerAdded) return;
198 changeListenerAdded = true;
199 Event.add(t.id, 'change', onChange);
200 Event.remove(t.id, 'blur', bf);
203 if (e.keyCode == 13 || e.keyCode == 32) {
205 return Event.cancel(e);
209 t.onPostRender.dispatch(t, DOM.get(t.id));