Permalink
Browse files

Added delay and minLength options(just mapping for a jQuery UI Autoco…

…mplete); Added showAllOnFocus option to show autocomplete with all tags on focus; Updated examples.html with example for new options.
  • Loading branch information...
1 parent 440b9b7 commit 90bcd8de98ef79a06a4d0bc6198bf90951cfa1d6 @grisevg grisevg committed Aug 23, 2011
Showing with 112 additions and 79 deletions.
  1. +95 −75 examples.html
  2. +17 −4 js/tag-it.js
View
170 examples.html
@@ -25,10 +25,10 @@
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js" type="text/javascript" charset="utf-8"></script>
<!-- The real deal -->
- <script src="js/tag-it.js" type="text/javascript" charset="utf-8"></script>
+ <script src="js/tag-it.js" type="text/javascript" charset="utf-8"></script>
- <script>
- $(function(){
+ <script>
+ $(function(){
var sampleTags = ['c++', 'java', 'php', 'coldfusion', 'javascript', 'asp', 'ruby', 'python', 'c', 'scala', 'groovy', 'haskell', 'perl', 'erlang', 'apl', 'cobol', 'go', 'lua'];
//-------------------------------
@@ -39,29 +39,29 @@
//-------------------------------
// Single field
//-------------------------------
- $('#singleFieldTags').tagit({
- availableTags: sampleTags,
- // This will make Tag-it submit a single form value, as a comma-delimited field.
- singleField: true,
+ $('#singleFieldTags').tagit({
+ availableTags: sampleTags,
+ // This will make Tag-it submit a single form value, as a comma-delimited field.
+ singleField: true,
singleFieldNode: $('#mySingleField')
- });
+ });
// singleFieldTags2 is an INPUT element, rather than a UL as in the other
// examples, so it automatically defaults to singleField.
- $('#singleFieldTags2').tagit({
- availableTags: sampleTags
- });
+ $('#singleFieldTags2').tagit({
+ availableTags: sampleTags
+ });
//-------------------------------
// Preloading data in markup
//-------------------------------
$('#myULTags').tagit({
- availableTags: sampleTags, // this param is of course optional. it's for autocomplete.
- // configure the name of the input field (will be submitted with form), default: item[tags]
- itemName: 'item',
- fieldName: 'tags'
- });
+ availableTags: sampleTags, // this param is of course optional. it's for autocomplete.
+ // configure the name of the input field (will be submitted with form), default: item[tags]
+ itemName: 'item',
+ fieldName: 'tags'
+ });
//-------------------------------
// Tag events
@@ -87,8 +87,8 @@
// Tag-it methods
//-------------------------------
$('#methodTags').tagit({
- availableTags: sampleTags
- });
+ availableTags: sampleTags
+ });
//-------------------------------
// Allow spaces without quotes.
@@ -105,41 +105,51 @@
availableTags: sampleTags,
removeConfirmation: true
});
+
+ //-------------------------------
+ // Full Autocomplete
+ //-------------------------------
+ $('#autocompleteTags').tagit({
+ availableTags: sampleTags,
+ showAllOnFocus: true,
+ delay: 0,
+ minLength: 0
+ });
- });
- </script>
+ });
+ </script>
</head>
<body>
<a href="http://github.com/aehlke/tag-it"><img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub" /></a>
<div id="wrapper">
- <div id="header">
- <h2>Tag-it! Usage Examples</h2>
-
- <ul id="nav">
+ <div id="header">
+ <h2>Tag-it! Usage Examples</h2>
+
+ <ul id="nav">
<li><a href="http://aehlke.github.com/tag-it">&laquo; back to widget home</a></li>
- </ul>
- </div>
-
+ </ul>
+ </div>
+
<div id="content">
<p>These demo various features of Tag-it. View the source to see how each works.</p>
-
+ <p>To see Autocomplete in action use any of this tags: 'c++', 'java', 'php', 'coldfusion', 'javascript', 'asp', 'ruby', 'python', 'c', 'scala', 'groovy', 'haskell', 'perl', 'erlang', 'apl', 'cobol', 'go', 'lua'</p>
<hr>
<h3>Minimal</h3>
- <form>
+ <form>
<p>
Vanilla example &mdash; the absolute minimum amount of code required, no configuration. No autocomplete, either. See the other examples for that.
</p>
- <ul id="myTags"></ul>
- <input type="submit" value="Submit">
- </form>
+ <ul id="myTags"></ul>
+ <input type="submit" value="Submit">
+ </form>
<hr>
<h3>Single Input Field</h3>
- <form>
- <p>
+ <form>
+ <p>
Example using a single input form field to hold all the tag values, instead of one per tag (see settings.singleField).
This method is particularly useful if you have a form with one input field for comma-delimited tags that you want to trivially "upgrade" to this fancy jQuery UI widget.
This configuration will also degrade nicely as well for browsers without JS &mdash; the default behavior is to have one input per tag, which does not degrade as well as one comma-delimited input.
@@ -148,90 +158,100 @@
Normally this input field will be hidden &mdash; we leave it visible here so you can see how it is manipulated by the widget:
<input name="tags" id="mySingleField" value="Apple, Orange" disabled="true"> <!-- only disabled for demonstration purposes -->
</p>
- <ul id="singleFieldTags"></ul>
- <input type="submit" value="Submit">
- </form>
+ <ul id="singleFieldTags"></ul>
+ <input type="submit" value="Submit">
+ </form>
<hr>
<h3><a name="graceful-degredation"></a>Single Input Field (2)</h3>
- <form>
- <p>
+ <form>
+ <p>
If you instantiate Tag-it on an INPUT element, it will default to being singleField, with that INPUT element as the singleFieldNode. This is the simplest way to have a gracefully-degrading tag widget.
</p>
<input name="tags" id="singleFieldTags2" value="Apple, Orange">
- <input type="submit" value="Submit">
- </form>
+ <input type="submit" value="Submit">
+ </form>
<hr>
<h3>Spaces Allowed Without Quotes</h3>
<p>You can already do multiword tags with spaces in them by default, but those must be wrapped in quotes. This option lets you use spaces without requiring the user to quote the input.</p>
<p>There are normally 5 ways to insert a tag after inputting some text: space, comma, enter, selecting an autocomplete option, or defocusing the widget. With the "allowSpaces" option set to true, space no longer inserts a tag, it just adds a space to the current tag input.</p>
- <form>
- <p></p>
- <ul id="allowSpacesTags"></ul>
- <input type="submit" value="Submit">
- </form>
+ <form>
+ <p></p>
+ <ul id="allowSpacesTags"></ul>
+ <input type="submit" value="Submit">
+ </form>
<hr>
<h3>Preloading Data in Markup</h3>
- <form>
+ <form>
<p>
Using a UL in HTML to prefill the widget with some tags.
</p>
- <ul id="myULTags">
- <!-- Existing list items will be pre-added to the tags. -->
- <li>Tag1</li>
- <li>Tag2</li>
- </ul>
- <input type="submit" value="Submit">
- </form>
+ <ul id="myULTags">
+ <!-- Existing list items will be pre-added to the tags. -->
+ <li>Tag1</li>
+ <li>Tag2</li>
+ </ul>
+ <input type="submit" value="Submit">
+ </form>
<hr>
<h3>Events</h3>
- <form>
- <p>Example of tag events. Try adding or removing a tag, or clicking on a tag's label.</p>
- <ul id="eventTags">
+ <form>
+ <p>Example of tag events. Try adding or removing a tag, or clicking on a tag's label.</p>
+ <ul id="eventTags">
<li>Click my label</li>
<li>Remove me</li>
</ul>
- <input type="submit" value="Submit">
- </form>
+ <input type="submit" value="Submit">
+ </form>
<hr>
<h3>Methods</h3>
- <form>
- <p>Demos the available widget methods. Click the links below the widget to try them.</p>
- <ul id="methodTags"></ul>
+ <form>
+ <p>Demos the available widget methods. Click the links below the widget to try them.</p>
+ <ul id="methodTags"></ul>
<p><a href="#" onclick="var inp=prompt('Enter a tag value to test the createTag method.');$('#methodTags').tagit('createTag', inp);return false;">Create tag</a></p>
<p><a href="#" onclick="$('#methodTags').tagit('removeAll');return false;">Clear tags</a></p>
- <input type="submit" value="Submit">
- </form>
+ <input type="submit" value="Submit">
+ </form>
<hr>
<h3>Remove Confirmation</h3>
- <form>
- <p>
+ <form>
+ <p>
When removeConfirmation is enabled the user has to press the backspace key twice to remove the last tag.
</p>
- <ul id="removeConfirmationTags">
+ <ul id="removeConfirmationTags">
<li>backspace me</li>
<li>me too</li>
</ul>
- <input type="submit" value="Submit">
- </form>
+ <input type="submit" value="Submit">
+ </form>
+
+ <hr>
+ <h3>Show Autocomplete on focus</h3>
+ <form>
+ <p>
+ Sometimes it's usefull to show all available tags for autocomplete. This can be set with showAllOnFocus. Additionaly autocomplete can be tuned with delay and minLength attributes (same as in jQuery UI Autocomplete).
+ </p>
+ <ul id="autocompleteTags"></ul>
+ <input type="submit" value="Submit">
+ </form>
</div>
- <div id="footer">
+ <div id="footer">
<div class="left">
- <p>Built with <a href="http://jquery.com/" target="_blank">jQuery</a> and <a href="http://jqueryui.com/" target="_blank">jQuery UI</a>.</p>
- <p>Originally created by <a href="http://levycarneiro.com/">Levy Carneiro Jr</a>. Currently maintained by <a href="http://github.com/aehlke">Alex Ehlke</a>.</p>
+ <p>Built with <a href="http://jquery.com/" target="_blank">jQuery</a> and <a href="http://jqueryui.com/" target="_blank">jQuery UI</a>.</p>
+ <p>Originally created by <a href="http://levycarneiro.com/">Levy Carneiro Jr</a>. Currently maintained by <a href="http://github.com/aehlke">Alex Ehlke</a>.</p>
</div>
- <p class="right weak">Template adopted from <a href="http://orderedlist.com/demos/fancy-zoom-jquery/">orderedlist.com</a></p>
- <br class="clear"/>
- </div>
+ <p class="right weak">Template adopted from <a href="http://orderedlist.com/demos/fancy-zoom-jquery/">orderedlist.com</a></p>
+ <br class="clear"/>
+ </div>
</div>
</body>
</html>
View
21 js/tag-it.js
@@ -29,12 +29,17 @@
$.widget('ui.tagit', {
options: {
itemName : 'item',
- fieldName : 'tags',
- availableTags : [],
- tagSource : null,
+ fieldName : 'tags',
removeConfirmation: false,
caseSensitive : true,
+ // Autocomplete settings
+ availableTags : [],
+ tagSource : null,
+ minLength : 1,
+ delay :300,
+ showAllOnFocus : false,
+
// When enabled, quotes are not neccesary
// for inputting multi-word tags.
allowSpaces: false,
@@ -208,6 +213,8 @@
if (this.options.availableTags || this.options.tagSource) {
this._tagInput.autocomplete({
source: this.options.tagSource,
+ minLength: this.options.minLength,
+ delay: this.options.delay,
select: function(event, ui) {
// Delete the last tag if we autocomplete something despite the input being empty
// This happens because the input's blur event causes the tag to be created when
@@ -221,8 +228,14 @@
that.createTag(ui.item.value);
// Preventing the tag input to be updated with the chosen value.
return false;
- }
+ },
});
+ //Show all posibilities on focus
+ if (this.options.showAllOnFocus) {
+ this._tagInput.focus(function(event, ui) {
+ $(this).autocomplete( "search", '');
+ })
+ }
}
},

0 comments on commit 90bcd8d

Please sign in to comment.