- Overview
- Documents
At.js is an autocompletion library to autocomplete mentions, smileys etc. just like on Github!
Features Preview
- Supports HTML5 contentEditable elements (NOT include IE 8)
- Can listen to any character and not just '@'. Can set up multiple listeners for different characters with different behavior and data
- Listener events can be bound to multiple inputors.
- Format returned data using templates
-
Keyboard controls in addition to mouse
- Tab or Enter keys select the value
- Up and Down navigate between values (and Ctrl-P and Ctrl-N also)
- Right and left will re-search the keyword.
- Custom data handlers and template renderers using a group of configurable callbacks
- Supports AMD
Requirements
- jQuery >= 1.7.0.
- Caret.js (You can use Component or Bower to install it.)
Source: github.com
1. INCLUDE CSS AND JS FILES
<link href="css/jquery.atwho.css" rel="stylesheet"> <script src="http://code.jquery.com/jquery.js"></script> <script src="js/jquery.caret.js"></script> <script src="js/jquery.atwho.js"></script>
2. HTML
<textarea id="inputor" class="inputor" name="at" rows="8" cols="40"> this textarea register "@" with static data and ":" with ajax. type "@" to try </textarea>
3. JAVASCRIPT
$('#inputor').atwho({ at: "@", data:['Peter', 'Tom', 'Anne'] });
4. OPTIONS
$('.atwho-inputor').atwho({ // key char for observing such as `@` at: void 0, /* alias name of `at` it would be an id attribute of the popup view. */ alias: void 0, /* should be a plain object *Array* or a *URL* would save *Array* directly. would load and save remote JSON data by *URL* */ data: null, /* would eval it and assign value of the key contained in `${}` key-value ( {'name': "one"} ) is an item in `data` Array. */ tpl: "<li data-value='${atwho-at}${name}'>${name}</li>", /* It's for `contentEditable` mode only. Will be evaluated and inserted in the inputor. `atwho-data-value` is the value of `data-value` of `li` element, and it is just some `default value hook`. You change it into anything you want. */ insert_tpl: "<span id='${id}'>${atwho-data-value}</span>" /* There are several data processors that can be overriden here such as `filter`. we will cover it later. */ callbacks: DEFAULT_CALLBACKS, /* would matching item by test against the value of this `search_key` with query string. */ search_key: "name", /* limit number of items to show in popup list. */ limit: 5, /* setting the max length of the string after `at` that would be matched It will stop matching if the query string is longer than `max_len`. */ max_len: 20, /* if `yes`, At.js will match the query with a space before the `at`. */ start_with_space: true, display_timeout: 300, // highlight_first suggestion in popup menu highlight_first: true // delay time trigger At.js while typing. For example: delay: 400 delay: null, // suffix for inserting string. suffix: undefined, // don't show dropdown view without `suffix` hide_without_suffix: false });
5. EVENTS
All At.js events are normal jQuery events. You can bind them in the jQuery way:
$inputor.on("matched.atwho", function(event, flag, query) { console.log(event, "matched " + flag + " and the result is " + query); });
If you have configured alias setting, the event name would be added a suffix.
Suppose your alias setting is at-mentions, so that the event name would be: matched-at-mentions.atwho
And, all event's context is the controller object.
Triggered after match a word. It would receive these arguments:
- event - jQuery Event : Just a jQuery Event|
- flag - String : the at char
- query - String : Query String
Triggered after user choose a popup item in any way. It would receive these arguments:
- $li - jQuery Object : List Item which have been chosen
- event - jQueryEvent : the real event object.
Triggered after reposition popup . It would receive these arguments:
- event - jQueryEvent: Just a jQuery Event.
- offset - Hash : Offset after reposition.Used to .offset jQuery method, the structure looks like it: {left:x, top: y}
Triggered before destroy plugin. There is no arguments for it.
triggered after view shown
- event - jQueryEvent : Just a jQuery Event.
triggered after view hidden
- event - jQueryEvent : Just a jQuery Event.
6. APIS
load
You can invoke this method to load data when data is empty.
It wouldn't reload data if they are already exists.
-
flag - [String] - the at char
-
data - [Array] - data to storage
$inputor.atwho('load', '@', [{name: 'one'}, {nick: 'two'}]);
Run it!
empty
$.getJSON(url, params, function(data) { $inputor.atwho('load',':', data).atwho('run'); });
destroy it.
empty
$inputor.atwho('destroy');
To set iframe element which the text field belongs to.
-
iframe - [DOM] - iframe element
$textField.atwho('setIframe', iframe); $textField.atwho({at: '@', data: ['Peter', 'Anne']});
7. EXAMPLES
Custom Template
You can customize what will be shown in popup's item , such as user’s avatar.
A valid template should meet the following requirements:
- It should be a li HTML element
- It should have the data-value attribute, whose value will be inserted into the inputor if the item is selected.
<li data-value='${atwho-at}${word}'>anything here</li>
You can put any HTML element into the template such as img. Its just a HTML element.
var emojis = ["smile", "iphone", "girl", "smiley", "heart", "kiss", "copyright", "coffee"]; var emojis_list = $.map(emojis, function(value, i) { return {'id':i, 'name':value}; }); //http://a248.e.akamai.net/assets.github.com/images/icons/emoji/8.png $(".container textarea").atwho({ at: ':', tpl: "<li data-value=':${name}:'><img src='http://a248.e.akamai.net/assets.github.com/images/icons/emoji/${name}.png' height='20' width='20'/> ${name} </li>", data: emojis_list });
At.js can listen to multiple at and every Keyword can have its own settings.
var emojis = ["smile", "iphone", "girl", "smiley", "heart", "kiss", "copyright", "coffee"]; var names = ["Jacob", "Isabella", "Ethan", "Emma", "Michael", "Olivia", "Alexander", "Sophia", "William", "Ava", "Joshua", "Emily", "Daniel", "Madison", "Jayden", "Abigail", "Noah", "Chloe", "你好", "你你你"]; var emojis_list = $.map(emojis, function(value, i) { return {'id':i, 'name':value}; }); var issues = [ { name: "1", content: "stay foolish"}, { name: "2", content: "stay hungry"}, { name: "3", content: "stay heathly"}, { name: "4", content: "this happiess"}, ]; //http://a248.e.akamai.net/assets.github.com/images/icons/emoji/8.png $(".container textarea") .atwho({ at: "@", data: names }) .atwho({ at: "#", tpl: '<li data-value="#${name}">${name} <small>${content}</small></li>', data: issues }) .atwho({ at: ":", tpl: "<li data-value=':${name}:'><img src='http://a248.e.akamai.net/assets.github.com/images/icons/emoji/${name}.png' height='20' width='20'/> ${name} </li>", data: emojis_list });
When you set data as URL string, At.js will launch Ajax request to the server to get the JSON data
$('textarea').atwho({ at: "@", data: "http://www.atjs.com/users.json", limit: 7 });
If you want to support unicode characters, you can customize the Query matcher.
The matcher callback look like this:
$('#inputor').atwho({ at: "@", callbacks: { matcher: function(flag, subtext) { var match, matched, regexp; regexp = new XRegExp('(\\s+|^)' + flag + '(\\p{L}+)$', 'gi'); match = regexp.exec(subtext); // ... get matched result return matched; } //, ... others callbacks } });