Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

new autcomplete features + 1 bug fix #23

Closed
wants to merge 3 commits into from

2 participants

@grisevg

I've made few changes to the plugin, mainly to autocomplete feature:

  1. minLength and delay (simply mappings of original autocomplete options),
  2. showAllOnFocus option which showing autocomplete with all options when you focus on input
  3. autocompleteHeader option, which setting text on top of autocomplete box
  4. 1 Bug Fix: additional tag were created if autocomplete is used with half-typed by checking if blur event happened when mouse was over autocomplete.

PS: Oh and there were some tabs in files, though mostly it was spaces, so I converted everything to spaces... cleaner that way, bet because of that it's gonna look messy at first in diff tool.

Hope you will find this changes useful, so will users of your plugin

grisevg added some commits
@grisevg grisevg 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.
90bcd8d
@grisevg grisevg Fixed original bug which creates garbage tag when autocomplete is use…
…d; Added option to set header for autocomplete list; Replaced tabs with spaces;
6ae73a5
@grisevg grisevg Merge https://github.com/aehlke/tag-it
Conflicts:
	css/jquery.tagit.css
	js/tag-it.js
79f8747
@aehlke
Owner

Hey, thanks for your work! I used this as inspiration for some changes I made. I added showAutocompleteOnFocus and an autocomplete option for adding arbitrary extra options such as minLength and delay. That bug is also fixed.

I didn't add autocompleteHeader since it seems kind of complex and I don't really understand the use case for it.

Thanks!!

@aehlke aehlke closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 24, 2011
  1. @grisevg

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

    grisevg authored
    …mplete); Added showAllOnFocus option to show autocomplete with all tags on focus; Updated examples.html with example for new options.
Commits on Sep 6, 2011
  1. @grisevg

    Fixed original bug which creates garbage tag when autocomplete is use…

    grisevg authored
    …d; Added option to set header for autocomplete list; Replaced tabs with spaces;
  2. @grisevg

    Merge https://github.com/aehlke/tag-it

    grisevg authored
    Conflicts:
    	css/jquery.tagit.css
    	js/tag-it.js
This page is out of date. Refresh to see the latest.
Showing with 182 additions and 117 deletions.
  1. +25 −17 css/jquery.tagit.css
  2. +99 −76 examples.html
  3. +58 −24 js/tag-it.js
View
42 css/jquery.tagit.css
@@ -1,29 +1,29 @@
ul.tagit {
- padding: 1px 5px;
- overflow: auto;
+ padding: 1px 5px;
+ overflow: auto;
margin-left: inherit; /* usually we don't want the regular ul margins. */
margin-right: inherit;
}
ul.tagit li {
- display: block;
- float: left;
- margin: 2px 5px 2px 0;
+ display: block;
+ float: left;
+ margin: 2px 5px 2px 0;
}
ul.tagit li.tagit-choice {
- padding: .2em 18px .2em .5em;
+ padding: .2em 18px .2em .5em;
position: relative;
line-height: inherit;
}
ul.tagit li.tagit-new {
- padding: .25em 4px .25em 0;
+ padding: .25em 4px .25em 0;
}
ul.tagit li.tagit-choice a.tagit-label {
- cursor: pointer;
- text-decoration: none;
+ cursor: pointer;
+ text-decoration: none;
}
ul.tagit li.tagit-choice .tagit-close {
- cursor: pointer;
+ cursor: pointer;
position: absolute;
right: .1em;
top: 50%;
@@ -36,9 +36,9 @@ ul.tagit li.tagit-choice .tagit-close .text-icon {
}
ul.tagit li.tagit-choice input {
- display: block;
- float: left;
- margin: 2px 5px 2px 0;
+ display: block;
+ float: left;
+ margin: 2px 5px 2px 0;
}
ul.tagit input[type="text"] {
-moz-box-sizing: border-box;
@@ -46,9 +46,17 @@ ul.tagit input[type="text"] {
box-sizing: border-box;
border: none;
- margin: 0;
- padding: 0;
- width: inherit;
- background-color: inherit;
+ margin: 0;
+ padding: 0;
+ width: inherit;
+ background-color: inherit;
outline: none;
}
+
+/*Autocomplete header text*/
+li.ui-autocomplete-header {
+ font-weight: bold;
+ padding: .2em .4em;
+ margin: .8em 0 .2em;
+ line-height: 1.5;
+}
View
175 examples.html
@@ -21,14 +21,14 @@
<link href="css/tagit.ui-zendesk.css" rel="stylesheet" type="text/css">
<!-- jQuery and jQuery UI are required dependencies. -->
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript" charset="utf-8"></script>
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<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.
@@ -106,8 +106,19 @@
removeConfirmation: true
});
- });
- </script>
+ //-------------------------------
+ // Full Autocomplete
+ //-------------------------------
+ $('#autocompleteTags').tagit({
+ availableTags: sampleTags,
+ showAllOnFocus: true,
+ delay: 0,
+ minLength: 0,
+ autocompleteHeader: 'Suggestions are:'
+ });
+
+ });
+ </script>
</head>
<body>
@@ -115,31 +126,31 @@
<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 +159,102 @@
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>Autocomplete options</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).
+ To set header text for autocomplete list, user autocompleteHeader attribute.
+ </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
82 js/tag-it.js
@@ -30,11 +30,17 @@
options: {
itemName : 'item',
fieldName : 'tags',
- availableTags : [],
- tagSource : null,
removeConfirmation: false,
caseSensitive : true,
+ // Autocomplete settings
+ autocompleteHeader: null,
+ availableTags : [],
+ tagSource : null,
+ minLength : 1,
+ delay : 300,
+ showAllOnFocus : false,
+
// When enabled, quotes are not neccesary
// for inputting multi-word tags.
allowSpaces: false,
@@ -101,20 +107,22 @@
this._tagInput.attr('tabindex', this.options.tabIndex);
}
- this.options.tagSource = this.options.tagSource || function(search, showChoices) {
- var filter = search.term.toLowerCase();
+ if (!this.options.tagSource && this.options.availableTags) {
+ this.options.tagSource = function(search, showChoices) {
+ var filter = search.term.toLowerCase();
var choices = $.grep(this.options.availableTags, function(element) {
- // Only match autocomplete options that begin with the search term.
- // (Case insensitive.)
- return (element.toLowerCase().indexOf(filter) === 0);
- });
+ // Only match autocomplete options that begin with the search term.
+ // (Case insensitive.)
+ return (element.toLowerCase().indexOf(filter) === 0);
+ });
showChoices(this._subtractArray(choices, this.assignedTags()));
- };
+ };
// Bind tagSource callback functions to this context.
if ($.isFunction(this.options.tagSource)) {
this.options.tagSource = $.proxy(this.options.tagSource, this);
}
+
this.tagList
.addClass('tagit')
@@ -201,33 +209,59 @@
// The autocomplete doesn't close automatically when TAB is pressed.
// So let's ensure that it closes.
- that._tagInput.autocomplete('close');
+ that._tagInput.tagitAutocomplete('close');
}
}).blur(function(e){
- // Create a tag when the element loses focus (unless it's empty).
- that.createTag(that._cleanedInput());
+ //If autocomplete is enabled and suggestion was clicked, just delete the input, autocomplete will put value itself
+ if (that.options.tagSource && $(that._tagInput).data('autocomplete_list').data('mouseover')) {
+ that._cleanedInput();
+ } else {
+ that.createTag(that._cleanedInput());
+ }
});
// Autocomplete.
- if (this.options.availableTags || this.options.tagSource) {
- this._tagInput.autocomplete({
+ //Extending UI Autocomplete to rewrite how it renders
+ $.widget( "custom.tagitAutocomplete", $.ui.autocomplete, {
+ _renderMenu: function( ul, items ) {
+ //Assign autocomplete list to html element, for it to be accessible in other parts of the code.
+ $(this.element).data('autocomplete_list', ul);
+
+ //We need to store if mouse over autocomplete list to predict if it was clicked when input blured.
+ ul.data('mouseover', false);
+ ul.mouseenter(function(){$(this).data('mouseover', true);});
+ ul.mouseleave(function(){$(this).data('mouseover', false);});
+
+ if (that.options.autocompleteHeader) {
+ ul.prepend("<li class='ui-autocomplete-header'>"+ that.options.autocompleteHeader +"</li>");
+ }
+
+ var self = this;
+ $.each( items, function( index, item ) {
+ self._renderItem( ul, item );
+ });
+ },
+ });
+ if (this.options.tagSource) {
+ this._tagInput.tagitAutocomplete({
source: this.options.tagSource,
+ minLength: this.options.minLength,
+ delay: this.options.delay,
+ autofocus: true,
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
- // the user clicks an autocomplete item.
- // The only artifact of this is that while the user holds down the mouse button
- // on the selected autocomplete item, a tag is shown with the pre-autocompleted text,
- // and is changed to the autocompleted text upon mouseup.
- if (that._tagInput.val() === '') {
- that.removeTag(that._lastTag(), false);
- }
that.createTag(ui.item.value);
// Preventing the tag input to be updated with the chosen value.
return false;
- }
+ },
+ appendTo:this.tagList[0],//Autocomplete list should be appended to tagit element, instead of <body>
});
+ //Show all posibilities on focus
+ if (this.options.showAllOnFocus) {
+ this._tagInput.focus(function(event, ui) {
+ $(this).tagitAutocomplete( "search", '');
+ })
+ }
}
},
Something went wrong with that request. Please try again.