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

Commit

Permalink
simplify change evt / add blur evt
Browse files Browse the repository at this point in the history
  • Loading branch information
dansnetwork committed Jan 16, 2013
1 parent 93c06f4 commit 0300132
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License

Copyright (c) 2013 Sprout Social, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ <h2>Options</h2>
<ul>
<li>allowDuplicates <span>(type : Boolean :: default : false)</span></li>

<li>autoCompleteSource <span>(type : Array :: default : [])</span>
<li>autoCompleteSource <span>(type : Array/String/Function :: default : [])</span>
<p class="alert alert-info">when specified, autocomplete will be instantiated</p>
</li>

Expand Down
25 changes: 15 additions & 10 deletions inputosaurus.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

// bindable events
//
// 'change' - triggered whenever a tag is added or removed (should be similart to binding the the change event of the instantiated input
// 'change' - triggered whenever a tag is added or removed (should be similar to binding the the change event of the instantiated input
// 'keyup' - keyup event on the newly created input

// while typing, the user can separate values using these delimiters
Expand All @@ -45,8 +45,8 @@

width : null,

// simply passing an array of values will instantiate autocomplete
autoCompleteSource : [],
// simply passing an autoComplete source (array, string or function) will instantiate autocomplete functionality
autoCompleteSource : '',

// manipulate and return the input value after parseInput() parsing
// the array of tag names is passed and expected to be returned as an array after manipulation
Expand Down Expand Up @@ -90,7 +90,7 @@
},

_instAutocomplete : function() {
if(this.options.autoCompleteSource.length){
if(this.options.autoCompleteSource){
var widget = this;

this.elements.input.autocomplete({
Expand All @@ -105,7 +105,7 @@
widget.parseInput();
},
open : function() {
$(this).autocomplete('widget').width(widget.elements.ul.width());
$(this).autocomplete('widget').width(widget.elements.ul.outerWidth());
}
});
}
Expand All @@ -123,7 +123,7 @@

if(delimiterFound !== false){
values = val.split(delimiterFound);
} else if(!ev || ev.which === $.ui.keyCode.ENTER){
} else if(!ev || ev.type === 'blur' || ev.which === $.ui.keyCode.ENTER){
values.push(val);
ev && ev.preventDefault();
}
Expand Down Expand Up @@ -182,6 +182,7 @@
ev.stopPropagation();

if((!$(ev.currentTarget).val() || (('selectionStart' in ev.currentTarget) && ev.currentTarget.selectionStart === 0 && ev.currentTarget.selectionEnd === 0)) && lastTag.size()){
ev.preventDefault();
lastTag.find('a').focus();
}

Expand Down Expand Up @@ -297,10 +298,9 @@
self._chosenValues.push(obj);

self._renderTags();
self._setValue(self._buildValue());
self._trigger('change');
}
});
self._setValue(self._buildValue());
},

_buildValue : function() {
Expand All @@ -315,7 +315,12 @@
},

_setValue : function(value) {
this.element.val(value);
var val = this.element.val();

if(val !== value){
this.element.val(value);
this._trigger('change');
}
},

// @name text for tag
Expand Down Expand Up @@ -354,7 +359,6 @@
indexFound !== false && widget._chosenValues.splice(indexFound, 1);

widget._setValue(widget._buildValue());
widget._trigger('change');

$(ev.currentTarget).parent().remove();
},
Expand Down Expand Up @@ -396,6 +400,7 @@
this.elements.input.on('keydown.inputosaurus', {widget : widget}, this._inputKeypress);
this.elements.input.on('change.inputosaurus', {widget : widget}, this._inputKeypress);
this.elements.input.on('focus.inputosaurus', {widget : widget}, this._inputFocus);
this.elements.input.on('blur.inputosaurus', {widget : widget}, this.parseInput);

this.elements.ul.on('click.inputosaurus', {widget : widget}, this._focus);
this.elements.ul.on('click.inputosaurus', 'a', {widget : widget}, this._removeTag);
Expand Down

0 comments on commit 0300132

Please sign in to comment.