This plugin transforms ordinary form input fields into fields which allow easy creation of tags.
var input = document.getElementById("input"), inputInputtags = new Inputtags(input);
Simple use case to work with unique tags
var input = document.getElementById("input"), inputInputtags = new Inputtags(input,{ "duplicate":true });
Repeat tags case allows you work with not unique tags
var input = document.getElementById("input"), inputInputtags = new Inputtags(input,{ "keyName":"key", "valueName":"value" });
Work with array of objects without messing object's stucture if set "keyName" and "valueName" properties.
var input = document.getElementById("input"), inputInputtags = new Inputtags(input,{ "source":function(cursorValue){ var source; if(cursorValue.toLowerCase().indexOf("t") !== -1){ source = ["Tag1","Tag2","Tag3","Tag4","Tag5","Tag6"]; }else{ source = ["Link1","Link2","Link3","Link4","Link5","Link6"]; } return source; } });
Inputtags is also capable to use different sources of pre-defined tags.
var input = document.getElementById("input"), inputInputtags = new Inputtags(input,{ "source":function(cursorValue){ return ["Tag1","Tag2","Tag3","Link1","Link2","Link3"]; }, "getTagClass":function(tag){ var text = tag.getVal(); return (text.indexOf("Link")!== -1)?"bInputtags__tag label-info":"bInputtags__tag"; } });
In this case we check item and set custom style for it.
Inputtags can be styled by modifying "getClass" handlers of parameters. For example:
- "getInputtagsClass" set css class for Inputtags element
- "getTagClass" set css class for Tag element
- "getSourceItemClass" set css class for Item element in source list
raw input element (HTML element)InputtagsTag xTag xCursorSourceListsource list itemsource list itemsource list item
There is only four object which construct Inputtags (Cursor, SourceList, Tag and Inputtags). Each of them has own view (HTML element) and some individual methods. You have direct access only to the base object (Inputtags), but all constructors are put in vitologi.inputtags._class namespace. This allows you overwrite some prototype methods or create object instance.
Also there is an object with parameters (imagine a builder). You can redefine it when you create Inputtags from HTML-input.
Class/Method name Description Inputtags(element, parameters) Main plugin's constructor, it is responsible for combination of all plugin elements and synchronization with modified input
element - raw form input (HTML element)
parameters - configuration objectgetVal() get all tags data setVal(item) set tags data
item - array or JSON-string of tagshasVal(item) check the existence of the tag/item
item - tag dataclear() remove all tags from inputtags synchronize() synchronize object with DOM Element Tag(item, parameters) Tags constructor
item - tag data
parameters - configuration objectgetView() get block's HTML element getVal() get tag data Cursor(parameters) Entry point for input tags
parameters - configuration objectgetView() get block's HTML element getVal() get cursor value setVal(val) set cursor value
val - setted textSourceList(parameters) List of tags (source)
parameters - configuration objectgetView() get block's HTML element addItem(item) append new item to tags list
item - item (tag data)removeAllItems() delete all items from tags list
* be aware, environments (this) of all executable function can be changed
Property name Description _inputtags store current Inputtags object _cursor store current Cursor object _sourceList store current SourceList object _tags[] array of Tag objects duplicate {boolean} flag which allows to have duplicate tags keyName {string} name of object's identifier (if it is set, than tag's data is treated like an object) valueName {string} name of object's property to show (if it is set, than tag's data is treated like an object) getInputtagsClass(item) returns string with css class name to set style for Inputtags HTML element
item - Inputtags instancegetCursorClass(item) returns string with css class name to set style for Cursor HTML element
item - Cursor instancegetTagClass(item) returns string with css class name to set style for Tag HTML element
item - Tag instancegetTagRemoverClass(item) return string with css class name to set style for Tag component (close button)
item - Tag instancegetSourceListClass(item) returns string with css class name to set style for SourceList HTML element
item - SourceList instancegetSourceItemClass(item) returns string with css class name to set style for SourceList component (list item)
item - tag data objectsource(item) returns array of tag's data, which is used to create the source list
item - user input text (value of Cursor)onChange() execute after add or remove tags cursorHandler(e) handler for cursor's "keyup" event
e - event objectsourceListHandler(item, e) handler for choosing item in source list
item - chosen list item (tag data) e - event objectaddTag(tag) function appends tag to collection and HTML element to view
tag - added tagremoveTag(tag) function removes tag from collection and HTML element to view
tag - removed tag
var input = document.getElementById("input"), // small function for server simulation serverSimulator = function(needle, callback){ window.setTimeout(function(){ callback(null, ["Tag1","Tag2","Tag3","Link1","Link2","Link3"]); }, (Math.random()*2)*1000); }, inputInputtags = new Inputtags(input,{ "source":function(cursorValue){ // get current list and cursor var list = this._sourceList, cursor = this._cursor; // send request on server serverSimulator(cursorValue, function(error, result){ if(error)throw new Error("Get server data error."); // clear exist data list.removeAllItems(); var aItem = result, cursorIsEmpty = cursor.getVal() === "", itemRegexp = new RegExp("^"+cursor.getVal()), len = aItem.length, i = 0, item; if(!len)return; // add received data to the list for(; i<len; i++){ item = aItem[i]; if(cursorIsEmpty || !itemRegexp.test(item))continue; list.addItem(result[i]); } }); } });
Just start typing like "Li" or "T" (uppercase is need) and source list will be filled by some items (2s delay).
This is an example of extend plugin. In fact, ajax source feature will be added in next version (I hope :)).
In the case above, you see how easily we can get access to main plugin components and overwrite them.