Skip to content
This repository has been archived by the owner on Apr 28, 2020. It is now read-only.

Commit

Permalink
merged loopj's public methods support
Browse files Browse the repository at this point in the history
  • Loading branch information
vdepizzol committed Jul 15, 2011
1 parent 1853d3b commit f38cd5c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 7 deletions.
30 changes: 30 additions & 0 deletions demo.html
Expand Up @@ -287,5 +287,35 @@ <h2 id="parsename">Using local data with parseName function and searchColumns se
});
</script>
</div>

<h2 id="plugin-methods">Using the add, remove and clear Methods</h2>
<div>
<a href="#" id="plugin-methods-add">Add Token</a> | <a href="#" id="plugin-methods-remove">Remove Token</a> | <a href="#" id="plugin-methods-clear">Clear Tokens</a><br />
<input type="text" id="demo-input-plugin-methods" name="blah" />
<input type="button" value="Submit" />
<script type="text/javascript">
$(document).ready(function() {
$("#demo-input-plugin-methods").tokenInput("http://shell.loopj.com/tokeninput/tvshows.php");

// Add a token programatically
$("#plugin-methods-add").click(function () {
$("#demo-input-plugin-methods").tokenInput("add", {id: 999, name: "James was here"});
return false;
});

// Remove a token programatically
$("#plugin-methods-remove").click(function () {
$("#demo-input-plugin-methods").tokenInput("remove", {name: "James was here"});
return false;
});

// Clear all tokens
$("#plugin-methods-clear").click(function () {
$("#demo-input-plugin-methods").tokenInput("clear");
return false;
});
});
</script>
</div>
</body>
</html>
74 changes: 67 additions & 7 deletions src/jquery.tokeninput.js
Expand Up @@ -103,13 +103,37 @@ var KEY = {
COMMA: 188
};

// Expose the .tokenInput function to jQuery as a plugin
$.fn.tokenInput = function (url_or_data_or_function, options) {
var settings = $.extend({}, DEFAULT_SETTINGS, options || {});
// Additional public (exposed) methods
var methods = {
init: function(url_or_data_or_function, options) {
var settings = $.extend({}, DEFAULT_SETTINGS, options || {});

return this.each(function () {
$(this).data("tokenInputObject", new $.TokenList(this, url_or_data_or_function, settings));
});
},
clear: function() {
this.data("tokenInputObject").clear();
return this;
},
add: function(item) {
this.data("tokenInputObject").add(item);
return this;
},
remove: function(item) {
this.data("tokenInputObject").remove(item);
return this;
}
}

return this.each(function () {
new $.TokenList(this, url_or_data_or_function, settings);
});
// Expose the .tokenInput function to jQuery as a plugin
$.fn.tokenInput = function (method) {
// Method calling and initialization logic
if(methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else {
return methods.init.apply(this, arguments);
}
};


Expand Down Expand Up @@ -459,6 +483,39 @@ $.TokenList = function (input, url_or_data_or_function, settings) {
hide_dropdown();
}
}


//
// Public functions
//

this.clear = function() {
token_list.children("li").each(function() {
if ($(this).children("input").length === 0) {
delete_token($(this));
}
});
}

this.add = function(item) {
add_token(item);
}

this.remove = function(item) {
token_list.children("li[data-uniqueid]").each(function() {
var currToken = $(this).data("tokeninput");
var match = true;
for (var prop in item) {
if (item[prop] !== currToken[prop]) {
match = false;
break;
}
}
if (match) {
delete_token($(this));
}
});
}



Expand Down Expand Up @@ -538,11 +595,14 @@ $.TokenList = function (input, url_or_data_or_function, settings) {

// Add a token to the token list based on user input
function add_token (item) {


if(typeof(item) === "string") {
var li_data = {name: item};
} else {
} else if(item[0]) {
var li_data = $.data(item.get(0), "tokeninput");
} else {
var li_data = item;
}

if(!li_data) {
Expand Down

0 comments on commit f38cd5c

Please sign in to comment.