1
0
mirror of synced 2025-03-10 22:56:12 +03:00

disabled mode support wip, issue #76

This commit is contained in:
Igor Vaynberg 2012-06-06 21:52:08 -07:00
parent c27880c19c
commit 6fe77c6110

View File

@ -363,6 +363,7 @@
this.destroy(); this.destroy();
} }
this.enabled=true;
this.container = this.createContainer(); this.container = this.createContainer();
if (opts.element.attr("class") !== undefined) { if (opts.element.attr("class") !== undefined) {
@ -429,6 +430,8 @@
// we monitor the change event on the element and trigger it, allowing for two way synchronization // we monitor the change event on the element and trigger it, allowing for two way synchronization
this.monitorSource(); this.monitorSource();
} }
if (opts.element.is(":disabled")) this.disable();
}, },
destroy: function () { destroy: function () {
@ -541,6 +544,23 @@
this.opts.element.data("select2-change-triggered", false); this.opts.element.data("select2-change-triggered", false);
}, },
enable: function() {
if (this.enabled) return;
this.enabled=true;
this.container.removeClass("select2-container-disabled");
},
disable: function() {
if (!this.enabled) return;
this.close();
this.enabled=false;
this.container.addClass("select2-container-disabled");
},
opened: function () { opened: function () {
return this.container.hasClass("select2-dropdown-open"); return this.container.hasClass("select2-dropdown-open");
}, },
@ -888,7 +908,7 @@
if (this.opened()) { if (this.opened()) {
this.close(); this.close();
selection.focus(); selection.focus();
} else { } else if (this.enabled) {
this.open(); this.open();
} }
e.preventDefault(); e.preventDefault();
@ -896,7 +916,7 @@
clickingInside = false; clickingInside = false;
})); }));
container.delegate(selector, "keydown", this.bind(function (e) { container.delegate(selector, "keydown", this.bind(function (e) {
if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e) || e.which === KEY.ESC) { if (!this.enabled || e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e) || e.which === KEY.ESC) {
return; return;
} }
this.open(); this.open();
@ -909,13 +929,14 @@
killEvent(e); killEvent(e);
} }
})); }));
container.delegate(selector, "focus", function () { container.addClass("select2-container-active"); }); container.delegate(selector, "focus", function () { if (this.enabled) container.addClass("select2-container-active"); });
container.delegate(selector, "blur", this.bind(function () { container.delegate(selector, "blur", this.bind(function () {
if (clickingInside) return; if (clickingInside) return;
if (!this.opened()) this.blur(); if (!this.opened()) this.blur();
})); }));
selection.delegate("abbr", "click", this.bind(function (e) { selection.delegate("abbr", "click", this.bind(function (e) {
if (!this.enabled) return;
this.val(""); this.val("");
killEvent(e); killEvent(e);
this.close(); this.close();
@ -1110,6 +1131,8 @@
this.selection = selection = this.container.find(selector); this.selection = selection = this.container.find(selector);
this.search.bind("keydown", this.bind(function (e) { this.search.bind("keydown", this.bind(function (e) {
if (!this.enabled) return;
if (e.which === KEY.BACKSPACE && this.search.val() === "") { if (e.which === KEY.BACKSPACE && this.search.val() === "") {
this.close(); this.close();
@ -1164,12 +1187,14 @@
this.search.bind("keyup", this.bind(this.resizeSearch)); this.search.bind("keyup", this.bind(this.resizeSearch));
this.container.delegate(selector, "click", this.bind(function (e) { this.container.delegate(selector, "click", this.bind(function (e) {
if (!this.enabled) return;
this.open(); this.open();
this.focusSearch(); this.focusSearch();
e.preventDefault(); e.preventDefault();
})); }));
this.container.delegate(selector, "focus", this.bind(function () { this.container.delegate(selector, "focus", this.bind(function () {
if (!this.enabled) return;
this.container.addClass("select2-container-active"); this.container.addClass("select2-container-active");
this.clearPlaceholder(); this.clearPlaceholder();
})); }));
@ -1178,6 +1203,22 @@
this.clearSearch(); this.clearSearch();
}, },
enable: function() {
if (this.enabled) return;
this.parent.enable.apply(this, arguments);
this.search.show();
},
disable: function() {
if (!this.enabled) return;
this.parent.disable.apply(this, arguments);
this.search.hide();
},
initSelection: function () { initSelection: function () {
var data; var data;
if (this.opts.element.val() === "") { if (this.opts.element.val() === "") {
@ -1296,12 +1337,15 @@
choice = $(parts.join("")); choice = $(parts.join(""));
choice.find("a") choice.find("a")
.bind("click dblclick", this.bind(function (e) { .bind("click dblclick", this.bind(function (e) {
if (!this.enabled) return;
this.unselect($(e.target)); this.unselect($(e.target));
this.selection.find(".select2-search-choice-focus").removeClass("select2-search-choice-focus"); this.selection.find(".select2-search-choice-focus").removeClass("select2-search-choice-focus");
killEvent(e); killEvent(e);
this.close(); this.close();
this.focusSearch(); this.focusSearch();
})).bind("focus", this.bind(function () { })).bind("focus", this.bind(function () {
if (!this.enabled) return;
this.container.addClass("select2-container-active"); this.container.addClass("select2-container-active");
})); }));
@ -1424,7 +1468,7 @@
val = (val === null) ? [] : val; val = (val === null) ? [] : val;
this.setVal(val); this.setVal(val);
// val is a list of objects // val is a list of objects
st
$(val).each(function () { data.push(self.id(this)); }); $(val).each(function () { data.push(self.id(this)); });
this.setVal(data); this.setVal(data);
this.updateSelection(val); this.updateSelection(val);
@ -1468,7 +1512,7 @@
var args = Array.prototype.slice.call(arguments, 0), var args = Array.prototype.slice.call(arguments, 0),
opts, opts,
select2, select2,
value, multiple, allowedMethods = ["val", "destroy", "open", "close", "focus", "isFocused", "container", "onSortStart", "onSortEnd"]; value, multiple, allowedMethods = ["val", "destroy", "open", "close", "focus", "isFocused", "container", "onSortStart", "onSortEnd", "enable", "disable"];
this.each(function () { this.each(function () {
if (args.length === 0 || typeof(args[0]) === "object") { if (args.length === 0 || typeof(args[0]) === "object") {