Browse files

Added features for custom z-indexing, regular expression based token …

…creation, and a very rudimentary destroy() method
  • Loading branch information...
1 parent 33fa8e0 commit fb649fbe1efc76c5f66f98939d8a2dc79b2a6232 @ckinsey committed Jan 19, 2012
Showing with 43 additions and 9 deletions.
  1. +43 −9 src/jquery.tokeninput.js
View
52 src/jquery.tokeninput.js
@@ -20,6 +20,7 @@ var DEFAULT_SETTINGS = {
jsonContainer: null,
contentType: "json",
allowCreation: false,
+ creationRegEx: false,
// Prepopulation settings
prePopulate: null,
@@ -35,6 +36,8 @@ var DEFAULT_SETTINGS = {
theme: null,
resultsFormatter: function(item){ return "<li>" + item[this.propertyToSearch]+ "</li>" },
tokenFormatter: function(item) { return "<li><p>" + item[this.propertyToSearch] + "</p></li>" },
+ // Added by casey@studio98.com for custom z-indexing
+ displayIndex: 999,
// Tokenization settings
tokenLimit: null,
@@ -57,6 +60,7 @@ var DEFAULT_SETTINGS = {
var DEFAULT_CLASSES = {
tokenList: "token-input-list",
token: "token-input-token",
+ token_created: "created",
tokenDelete: "token-input-delete-token",
selectedToken: "token-input-selected-token",
highlightedToken: "token-input-highlighted-token",
@@ -106,6 +110,15 @@ var methods = {
this.data("tokenInputObject").clear();
return this;
},
+ destroy: function() {
+ // call destructor
+ this.data("tokenInputObject").destroy();
+
+ // null data
+ this.data("tokenInputObject", null);
+
+ return this;
+ },
add: function(item) {
this.data("tokenInputObject").add(item);
return this;
@@ -412,6 +425,20 @@ $.TokenList = function (input, url_or_data, settings) {
}
});
}
+
+ this.destroy = function() {
+ this.clear();
+
+ // Remove all elements
+ input_resizer.remove();
+ dropdown.remove();
+ input_token.remove();
+ token_list.remove();
+ hidden_input.remove();
+ input_box.remove();
+
+ // Clear stored data
+ }
this.getTokens = function() {
return saved_tokens;
@@ -451,6 +478,9 @@ $.TokenList = function (input, url_or_data, settings) {
this_token = $(this_token)
.addClass(settings.classes.token)
.insertBefore(input_token);
+
+ if ( item.wasCreated )
+ this_token.addClass( settings.classes.token_created );
// The 'delete token' button
$("<span>" + settings.deleteText + "</span>")
@@ -640,7 +670,7 @@ $.TokenList = function (input, url_or_data, settings) {
position: "absolute",
top: $(token_list).offset().top + $(token_list).outerHeight(),
left: $(token_list).offset().left,
- 'z-index': 999
+ 'z-index': settings.displayIndex
})
.show();
}
@@ -837,14 +867,18 @@ $.TokenList = function (input, url_or_data, settings) {
displayedItem['name'] = input_box.val() + ' ' + settings.createTokenText;
displayedItem[settings.tokenValue] = input_box.val();
displayedItem['wasCreated'] = true;
- results.push(displayedItem);
+ if( ! settings.creationRegEx || displayedItem['name'].match( settings.creationRegEx ) ) {
+ results.push(displayedItem);
- if($.isFunction(settings.onCreate)) {
- var item = {};
- item['name'] = input_box.val();
- item[settings.tokenValue] = input_box.val();
- settings.onCreate.call(hidden_input, item);
- }
+ if($.isFunction(settings.onCreate)) {
+ var item = {};
+ item['name'] = input_box.val();
+ item[settings.tokenValue] = input_box.val();
+ settings.onCreate.call(hidden_input, item);
+ }
+ } else {
+ return false;
+ }
}
// compute the dynamic URL
@@ -887,4 +921,4 @@ $.TokenList.Cache = function (options) {
return data[query];
};
};
-}(jQuery));
+}(jQuery));

0 comments on commit fb649fb

Please sign in to comment.