/** * $Id: ListBox.js 1045 2009-03-04 20:03:18Z spocke $ * * @author Moxiecode * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved. */ (function(tinymce) { var DOM = tinymce.DOM, Event = tinymce.dom.Event, each = tinymce.each, Dispatcher = tinymce.util.Dispatcher; /**#@+ * @class 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. * @member tinymce.ui.ListBox * @base tinymce.ui.Control */ tinymce.create('tinymce.ui.ListBox:tinymce.ui.Control', { /** * Constructs a new listbox control instance. * * @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); t.items = []; t.onChange = new Dispatcher(t); t.onPostRender = new Dispatcher(t); t.onAdd = new Dispatcher(t); t.onRenderMenu = new tinymce.util.Dispatcher(this); t.classPrefix = 'mceListBox'; }, /**#@+ * @method */ /** * 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. * * @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. * * @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. * * @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. * * @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. * * @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 += '