From 6a5719db8083b3552caf6f60ddf2e96d972f516e Mon Sep 17 00:00:00 2001 From: Igor Vaynberg Date: Thu, 5 Jul 2012 11:07:31 +0300 Subject: [PATCH] added open event --- select2.js | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/select2.js b/select2.js index 07d91722..7a02b93b 100755 --- a/select2.js +++ b/select2.js @@ -737,9 +737,27 @@ this.dropdown.css(css); }, + // abstract + shouldOpen: function() { + var event; + + if (this.opened()) return false; + + event = jQuery.Event("open"); + this.opts.element.trigger(event); + return !event.isDefaultPrevented(); + }, + + /** + * Opens the dropdown + * + * @return {Boolean} whether or not dropdown was opened. This method will return false if, for example, + * the dropdown is already open, or if the 'open' event listener on the element called preventDefault(). + */ // abstract open: function () { - if (this.opened()) return; + + if (!this.shouldOpen()) return false; this.container.addClass("select2-dropdown-open").addClass("select2-container-active"); if(this.dropdown[0] !== this.body.children().last()[0]) { @@ -755,6 +773,8 @@ this.dropdown.show(); this.ensureHighlightVisible(); this.focusSearch(); + + return true; }, // abstract @@ -1060,15 +1080,6 @@ ""].join("")); }, - // single - open: function () { - - if (this.opened()) return; - - this.parent.open.apply(this, arguments); - - }, - // single close: function () { if (!this.opened()) return; @@ -1544,11 +1555,11 @@ // multi open: function () { - if (this.opened()) return; - this.parent.open.apply(this, arguments); + if (this.parent.open.apply(this, arguments) === false) return false; this.clearPlaceholder(); this.resizeSearch(); this.focusSearch(); + return true; }, // multi