Skip to content

Commit bc7846b

Browse files
disabled state feature with tests. Based on
#95
1 parent 7f20074 commit bc7846b

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

js/bootstrap-combobox.js

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@
5353
return combobox;
5454
}
5555

56+
, disable: function() {
57+
this.$element.prop('disabled', true)
58+
this.$button.attr('disabled', true)
59+
this.disabled = true
60+
}
61+
62+
, enable: function() {
63+
this.$element.prop('disabled', false)
64+
this.$button.attr('disabled', false)
65+
this.disabled = false
66+
}
5667
, parse: function () {
5768
var that = this
5869
, map = {}
@@ -83,17 +94,19 @@
8394
}
8495

8596
, transferAttributes: function() {
86-
this.options.placeholder = this.$source.attr('data-placeholder') || this.options.placeholder;
87-
this.$element.attr('placeholder', this.options.placeholder);
88-
this.$target.prop('name', this.$source.prop('name'));
89-
this.$target.val(this.$source.val());
90-
this.$source.removeAttr('name'); // Remove from source otherwise form will pass parameter twice.
91-
this.$element.attr('required', this.$source.attr('required'));
92-
this.$element.attr('rel', this.$source.attr('rel'));
93-
this.$element.attr('title', this.$source.attr('title'));
94-
this.$element.attr('class', this.$source.attr('class'));
95-
this.$element.attr('tabindex', this.$source.attr('tabindex'));
96-
this.$source.removeAttr('tabindex');
97+
this.options.placeholder = this.$source.attr('data-placeholder') || this.options.placeholder
98+
this.$element.attr('placeholder', this.options.placeholder)
99+
this.$target.prop('name', this.$source.prop('name'))
100+
this.$target.val(this.$source.val())
101+
this.$source.removeAttr('name') // Remove from source otherwise form will pass parameter twice.
102+
this.$element.attr('required', this.$source.attr('required'))
103+
this.$element.attr('rel', this.$source.attr('rel'))
104+
this.$element.attr('title', this.$source.attr('title'))
105+
this.$element.attr('class', this.$source.attr('class'))
106+
this.$element.attr('tabindex', this.$source.attr('tabindex'))
107+
this.$source.removeAttr('tabindex')
108+
if (this.$source.attr('disabled')!==undefined)
109+
this.disable();
97110
}
98111

99112
, select: function () {
@@ -225,16 +238,18 @@
225238
}
226239

227240
, toggle: function () {
228-
if (this.$container.hasClass('combobox-selected')) {
229-
this.clearTarget();
230-
this.triggerChange();
231-
this.clearElement();
232-
} else {
233-
if (this.shown) {
234-
this.hide();
235-
} else {
241+
if (!this.disabled) {
242+
if (this.$container.hasClass('combobox-selected')) {
243+
this.clearTarget();
244+
this.triggerChange();
236245
this.clearElement();
237-
this.lookup();
246+
} else {
247+
if (this.shown) {
248+
this.hide();
249+
} else {
250+
this.clearElement();
251+
this.lookup();
252+
}
238253
}
239254
}
240255
}
@@ -391,7 +406,6 @@
391406

392407
/* COMBOBOX PLUGIN DEFINITION
393408
* =========================== */
394-
395409
$.fn.combobox = function ( option ) {
396410
return this.each(function () {
397411
var $this = $(this)

js/tests/unit/bootstrap-combobox.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,16 @@ $(function () {
308308

309309
combobox.$menu.remove()
310310
})
311+
312+
test("should respect disabled attribute", function() {
313+
var $select = $('<select title="A title" disabled><option></option><option>aa</option><option selected>ab</option><option>ac</option></select>')
314+
, $input = $select.combobox().data('combobox').$element
315+
, combobox = $select.data('combobox')
316+
317+
equal($input.prop('disabled'), true)
318+
equal(combobox.$button.attr('disabled'), "disabled")
319+
equal(combobox.disabled, true)
320+
321+
combobox.$menu.remove()
322+
})
311323
})

0 commit comments

Comments
 (0)