/** * ListBox.js * * Copyright 2009, Moxiecode Systems AB * Released under LGPL License. * * License: http://tinymce.moxiecode.com/license * Contributing: http://tinymce.moxiecode.com/contributing */ (function(tinymce) { var DOM = tinymce.DOM, Event = tinymce.dom.Event, each = tinymce.each, Dispatcher = tinymce.util.Dispatcher; /** * This class is used to create list boxes/select list. This one will generate * a non native control. This one has the benefits of having visual items added. * * @class tinymce.ui.ListBox * @extends tinymce.ui.Control */ tinymce.create('tinymce.ui.ListBox:tinymce.ui.Control', { /** * Constructs a new listbox control instance. * * @constructor * @method ListBox * @param {String} id Control id for the list box. * @param {Object} s Optional name/value settings object. */ ListBox : function(id, s) { var t = this; t.parent(id, s); /** * Array of ListBox items. * * @property items * @type Array */ t.items = []; /** * Fires when the selection has been changed. * * @event onChange */ t.onChange = new Dispatcher(t); /** * Fires after the element has been rendered to DOM. * * @event onPostRender */ t.onPostRender = new Dispatcher(t); /** * Fires when a new item is added. * * @event onAdd */ t.onAdd = new Dispatcher(t); /** * Fires when the menu gets rendered. * * @event onRenderMenu */ t.onRenderMenu = new tinymce.util.Dispatcher(this); t.classPrefix = 'mceListBox'; }, /** * Selects a item/option by value. This will both add a visual selection to the * item and change the title of the control to the title of the option. * * @method select * @param {String/function} va Value to look for inside the list box or a function selector. */ select : function(va) { var t = this, fv, f; if (va == undefined) return t.selectByIndex(-1); // Is string or number make function selector if (va && va.call) f = va; else { f = function(v) { return v == va; }; } // Do we need to do something? if (va != t.selectedValue) { // Find item each(t.items, function(o, i) { if (f(o.value)) { fv = 1; t.selectByIndex(i); return false; } }); if (!fv) t.selectByIndex(-1); } }, /** * Selects a item/option by index. This will both add a visual selection to the * item and change the title of the control to the title of the option. * * @method selectByIndex * @param {String} idx Index to select, pass -1 to select menu/title of select box. */ selectByIndex : function(idx) { var t = this, e, o; if (idx != t.selectedIndex) { e = DOM.get(t.id + '_text'); o = t.items[idx]; if (o) { t.selectedValue = o.value; t.selectedIndex = idx; DOM.setHTML(e, DOM.encode(o.title)); DOM.removeClass(e, 'mceTitle'); } else { DOM.setHTML(e, DOM.encode(t.settings.title)); DOM.addClass(e, 'mceTitle'); t.selectedValue = t.selectedIndex = null; } e = 0; } }, /** * Adds a option item to the list box. * * @method add * @param {String} n Title for the new option. * @param {String} v Value for the new option. * @param {Object} o Optional object with settings like for example class. */ add : function(n, v, o) { var t = this; o = o || {}; o = tinymce.extend(o, { title : n, value : v }); t.items.push(o); t.onAdd.dispatch(t, o); }, /** * Returns the number of items inside the list box. * * @method getLength * @param {Number} Number of items inside the list box. */ getLength : function() { return this.items.length; }, /** * Renders the list box as a HTML string. This method is much faster than using the DOM and when * creating a whole toolbar with buttons it does make a lot of difference. * * @method renderHTML * @return {String} HTML for the list box control element. */ renderHTML : function() { var h = '', t = this, s = t.settings, cp = t.classPrefix; h = '
' + DOM.createHTML('a', {id : t.id + '_text', href : 'javascript:;', 'class' : 'mceText', onclick : "return false;", onmousedown : 'return false;'}, DOM.encode(t.settings.title)) + ' | '; h += '' + DOM.createHTML('a', {id : t.id + '_open', tabindex : -1, href : 'javascript:;', 'class' : 'mceOpen', onclick : "return false;", onmousedown : 'return false;'}, '') + ' | '; h += '