4 * Copyright, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://www.tinymce.com/license
8 * Contributing: http://www.tinymce.com/contributing
12 * This class creates a combobox control. Select box that you select a value from or
15 * @-x-less ComboBox.less
16 * @class tinymce.ui.ComboBox
17 * @extends tinymce.ui.Widget
19 define("tinymce/ui/ComboBox", [
22 ], function(Widget, DomUtils) {
25 return Widget.extend({
27 * Constructs a new control instance with the specified settings.
30 * @param {Object} settings Name/value object with settings.
31 * @setting {String} placeholder Placeholder text to display.
33 init: function(settings) {
36 self._super(settings);
37 self.addClass('combobox');
39 self.on('click', function(e) {
43 if (elm.id && elm.id.indexOf('-open') != -1) {
52 self.on('keydown', function(e) {
53 if (e.target.nodeName == "INPUT" && e.keyCode == 13) {
54 self.parents().reverse().each(function(ctrl) {
66 if (settings.placeholder) {
67 self.addClass('placeholder');
69 self.on('focusin', function() {
70 if (!self._hasOnChange) {
71 DomUtils.on(self.getEl('inp'), 'change', function() {
75 self._hasOnChange = true;
78 if (self.hasClass('placeholder')) {
79 self.getEl('inp').value = '';
80 self.removeClass('placeholder');
84 self.on('focusout', function() {
85 if (self.value().length === 0) {
86 self.getEl('inp').value = settings.placeholder;
87 self.addClass('placeholder');
94 * Getter/setter function for the control value.
97 * @param {String} [value] Value to be set.
98 * @return {String|tinymce.ui.ComboBox} Value or self if it's a set operation.
100 value: function(value) {
103 if (typeof(value) != "undefined") {
105 self.removeClass('placeholder');
107 if (self._rendered) {
108 self.getEl('inp').value = value;
114 if (self._rendered) {
115 value = self.getEl('inp').value;
117 if (value != self.settings.placeholder) {
128 * Getter/setter function for the disabled state.
131 * @param {Boolean} [state] State to be set.
132 * @return {Boolean|tinymce.ui.ComboBox} True/false or self if it's a set operation.
134 disabled: function(state) {
139 if (self._rendered) {
140 self.getEl('inp').disabled = state;
145 * Focuses the input area of the control.
150 this.getEl('inp').focus();
154 * Repaints the control after a layout operation.
158 repaint: function() {
159 var self = this, elm = self.getEl(), openElm = self.getEl('open'), rect = self.layoutRect();
160 var width, lineHeight;
163 width = rect.w - openElm.offsetWidth - 10;
168 // Detect old IE 7+8 add lineHeight to align caret vertically in the middle
170 if (doc.all && (!doc.documentMode || doc.documentMode <= 8)) {
171 lineHeight = (self.layoutRect().h - 2) + 'px';
174 DomUtils.css(elm.firstChild, {
176 lineHeight: lineHeight
185 * Post render method. Called after the control has been rendered to the target.
188 * @return {tinymce.ui.ComboBox} Current combobox instance.
190 postRender: function() {
193 DomUtils.on(this.getEl('inp'), 'change', function() {
197 return self._super();
201 * Renders the control as a HTML string.
204 * @return {String} HTML representing the control.
206 renderHtml: function() {
207 var self = this, id = self._id, settings = self.settings, prefix = self.classPrefix;
208 var value = settings.value || settings.placeholder || '';
209 var icon, text, openBtnHtml = '';
211 icon = settings.icon ? prefix + 'ico ' + prefix + 'i-' + settings.icon : '';
216 '<div id="' + id + '-open" class="' + prefix + 'btn ' + prefix + 'open" tabIndex="-1">' +
217 '<button id="' + id + '-action" type="button" hidefocus tabindex="-1">' +
218 (icon ? '<i class="' + icon + '"></i>' : '<i class="' + prefix + 'caret"></i>') +
219 (text ? (icon ? ' ' : '') + text : '') +
224 self.addClass('has-open');
228 '<div id="' + id + '" class="' + self.classes() + '">' +
229 '<input id="' + id + '-inp" class="' + prefix + 'textbox ' + prefix + 'placeholder" value="' +
230 value + '" hidefocus="true">' +