diff --git a/jquery.selectlist.dev.js b/jquery.selectlist.dev.js
index d3731c8..0bc80ac 100755
--- a/jquery.selectlist.dev.js
+++ b/jquery.selectlist.dev.js
@@ -15,40 +15,40 @@
$.selectList = function (select, options) {
/* Variables starting with $ are jQuery objects */
- var
+ var
/* Single select element (drop-down list) */
$selectSingle,
-
+
/* List of selected options */
$list,
-
+
/* List items */
$item, $newItem,
-
+
/* Option element */
$option,
-
+
/*
* Keyboard event to use (keydown for MSIE and Safari, keypress for
* other browsers)
*/
keyEvent,
-
+
/* Has the plugin been initialized? */
ready,
-
+
/*
* The index of the first option element (equal to 1 if there is a hint,
* otherwise 0)
*/
first = 0,
-
+
/* Flags for keeping track of events */
change, click, keypress, enter;
/**
* Make list item visible.
- *
+ *
* @param $item
* A jQuery object representing the list item
* @param callback
@@ -69,7 +69,7 @@ $.selectList = function (select, options) {
/**
* Make list item hidden.
- *
+ *
* @param $item
* A jQuery object representing the list item
* @param callback
@@ -90,7 +90,7 @@ $.selectList = function (select, options) {
/**
* Compare two list items by text data or with an user-defined function.
- *
+ *
* @param item1
* A DOM object representing the first list item to compare
* @param item2
@@ -116,7 +116,7 @@ $.selectList = function (select, options) {
/**
* Add a new item to the list of selected items.
- *
+ *
* @param value
* Value of the new item or a DOM object representing a HTML
* option element
@@ -138,7 +138,7 @@ $.selectList = function (select, options) {
*/
if ($option[0].index < first)
return;
-
+
/* Extract value and text */
value = $option.val();
text = $option.text();
@@ -148,7 +148,7 @@ $.selectList = function (select, options) {
* Find the option with the given value (and possibly text) in the
* select element.
*/
- $option = $selectSingle.find("option[value=\"" +
+ $option = $selectSingle.find("option[value=\"" +
value.replace("'", "\\\"") + "\"]");
if ($option.length)
@@ -159,10 +159,10 @@ $.selectList = function (select, options) {
else
$option = null;
}
-
+
if (text === undefined)
text = $option ? $option.text() : value;
-
+
/* If duplicates are not allowed, disable the option */
if ($option && !options.duplicates)
$option.attr('disabled', 'disabled')
@@ -171,7 +171,7 @@ $.selectList = function (select, options) {
* our own flag as a workaround.
*/
.data('disabled', 1);
-
+
/*
* Create the new list item based on the template, and make it hidden.
* The $('').text(text).html() part is a trick to convert special
@@ -196,7 +196,7 @@ $.selectList = function (select, options) {
if (options.clickRemove)
remove($(this));
});
-
+
/*
* If there is a hint (first == 1), set it back as the selected option
* in the select element.
@@ -207,9 +207,9 @@ $.selectList = function (select, options) {
/* Callback function that will be called after the item is added */
var callback = function () {
if (callHandler !== false)
- options.onAdd(select, value, text);
+ options.onAdd(select, value, text);
};
-
+
if (options.sort && ($item = $list.children().eq(0)).length) {
/*
* If the list is supposed to be automatically sorted, look for the
@@ -218,27 +218,27 @@ $.selectList = function (select, options) {
*/
while ($item.length && cmp($newItem[0], $item[0]))
$item = $item.next();
-
+
show($item.length ? $newItem.insertBefore($item)
: $newItem.appendTo($list), callback);
}
else
/* Otherwise, append the new element at the end of the list. */
show($newItem.appendTo($list), callback);
-
+
$(select).empty();
-
+
$list.children().each(function () {
$(select).append($("").attr({ value: $(this).data('value'),
selected: "selected" }));
});
-
+
checkValidation();
}
-
+
/**
* Remove an item from the list of selected items.
- *
+ *
* @param $item
* A jQuery object representing the item to be removed
* @param callHandler
@@ -250,25 +250,25 @@ $.selectList = function (select, options) {
hide($item, function () {
var value = $(this).data('value'),
text = $(this).data('text');
-
+
if ($(this).data('option'))
$(this).data('option').removeAttr('disabled')
.removeData('disabled');
-
+
$(this).remove();
-
+
$(select).find("option[value=\"" + value + "\"]").remove();
-
+
checkValidation();
-
+
if (callHandler !== false)
options.onRemove(select, value, text);
});
}
-
+
/**
* Check if the jQuery Validation plugin is in use and re-validate the
- * select element if it is marked as being invalid.
+ * select element if it is marked as being invalid.
*/
function checkValidation() {
if (select.form && typeof ($(select.form).validate) == "function" &&
@@ -276,22 +276,22 @@ $.selectList = function (select, options) {
.validate().settings.errorClass))
$(select.form).validate().element(select);
}
-
+
/* Publicly available methods */
-
+
/**
- * Get currently selected options, similar to using the code jQuery
+ * Get currently selected options, similar to using the code jQuery
* .val() method on a regular multiple selection element.
- *
+ *
* @returns An array of values corresponding to the selected options
*/
this.val = function () {
return $(select).val();
};
-
+
/**
* Add a new item to the selection.
- *
+ *
* @param value
* The value of the new item
* @param text
@@ -300,11 +300,11 @@ $.selectList = function (select, options) {
this.add = function (value, text) {
add(value, text);
};
-
+
/**
* Remove the item (or multiple items) with the given value from the
* selection.
- *
+ *
* @param value
* The value of the item(s) being removed
*/
@@ -317,7 +317,7 @@ $.selectList = function (select, options) {
/**
* Set plugin options.
- *
+ *
* @param newOptions
* An object representing the new options
*/
@@ -345,7 +345,7 @@ $.selectList = function (select, options) {
};
/* Initialization starts here */
-
+
this.setOptions(options = $.extend({
addAnimate: true,
classPrefix: 'selectlist',
@@ -356,6 +356,36 @@ $.selectList = function (select, options) {
onRemove: function () {}
}, options));
+
+ /*
+ * Inline-function to prevent code duplication.
+ */
+ var createTitle = function(title) {
+ $selectSingle.prepend($("", {"class": "selectlist-title"})
+ .text(title));
+ first = 1;
+ /*
+ * We could set the "selected" attribute for the option element above,
+ * but IE7 and IE8 seem to ignore it, so we need to set the hint with
+ * the selectedIndex property.
+ */
+ $selectSingle[0].selectedIndex = 0;
+ };
+
+ /*
+ * Function for setting the title of an already initialized selectList.
+ * Creates the title-object if it doesn't already exist.
+ */
+ this.setTitle = function(title) {
+ var $title = $(".selectlist-title", $selectSingle);
+ if($title.length) {
+ $title.html(title);
+ } else {
+ createTitle(title);
+ }
+ };
+
+
/*
* Create the single select element by cloning the original multiple select.
*/
@@ -379,7 +409,7 @@ $.selectList = function (select, options) {
* element to turn it into a drop-down list.
*/
$selectSingle.removeAttr('multiple');
- /*
+ /*
* Firefox 4 throws an error on removeAttr('size'), so we need to use the
* removeAttribute() DOM method instead.
*/
@@ -387,25 +417,17 @@ $.selectList = function (select, options) {
/* If there is a "title" attribute, we add it as the hint option. */
if ($selectSingle.attr("title")) {
- $selectSingle.prepend($("")
- .text($selectSingle.attr("title")));
- first = 1;
- /*
- * We could set the "selected" attribute for the option element above,
- * but IE7 and IE8 seem to ignore it, so we need to set the hint with
- * the selectedIndex property.
- */
- $selectSingle[0].selectedIndex = 0;
+ createTitle($selectSingle.attr("title"));
}
/*
* In MSIE and WebKit, we need to use the keydown event instead of keypress.
*/
keyEvent = $.browser.msie || $.browser.safari ? 'keydown' : 'keypress';
-
+
$selectSingle.bind(keyEvent, function (event) {
keypress = true;
-
+
/* Check if Enter has been pressed */
if ((event.keyCode || event.which) == 13) {
enter = true;
@@ -423,11 +445,11 @@ $.selectList = function (select, options) {
$option = $selectSingle.find("option:selected");
if (!$option.data("disabled") && (!keypress || enter))
add($option);
-
+
if (keypress)
/* Reset flags */
keypress = change = click = false;
-
+
enter = false;
})
/*
@@ -437,12 +459,12 @@ $.selectList = function (select, options) {
.mousedown(function () {
click = true;
});
-
+
/*
* Add a new item when an option is clicked, even if no onchange event
* occurred -- this is useful when duplicate items are allowed and the user
* wants to select the same item again.
- *
+ *
* This is only supported in browsers that report click events for option
* elements (works in FF3 and Opera, does not work in MSIE and Chrome).
*/
@@ -455,32 +477,32 @@ $.selectList = function (select, options) {
* occurred within the select element's boundaries.
*/
if ($.browser.mozilla && event.pageX >= $selectSingle.offset().left &&
- event.pageX <= $selectSingle.offset().left +
+ event.pageX <= $selectSingle.offset().left +
$selectSingle.outerWidth() &&
event.pageY >= $selectSingle.offset().top &&
- event.pageY <= $selectSingle.offset().top +
+ event.pageY <= $selectSingle.offset().top +
$selectSingle.outerHeight())
return false;
-
+
click = true;
-
+
if (!($(this).attr('disabled') || $(this).data('disabled') || keypress
- || change))
+ || change))
add($(this));
-
+
if (!keypress)
change = click = false;
-
+
return false;
});
-
+
/* Ready for action */
ready = true;
};
/**
* Invoke selectList on a jQuery object containing the select element(s)
- *
+ *
* @param options
* Options object
* @return The jQuery object or a reference to selectList instance (if the
@@ -506,6 +528,6 @@ $.fn.selectList = function (options) {
return this.filter('select').data('selectList');
return this;
-};
+};
-})(jQuery);
+})(jQuery);
\ No newline at end of file