@ -1,14 +1,14 @@
var $ = require ( 'jquery' ) ;
var $ = require ( 'jquery' ) ;
var _ = require ( 'underscore' ) ;
var _ = require ( 'underscore' ) ;
var TagCollection = require ( '../Tags/TagCollection' ) ;
var TagCollection = require ( '../Tags/TagCollection' ) ;
var TagModel = require ( '../Tags/TagModel' ) ;
var TagModel = require ( '../Tags/TagModel' ) ;
require ( 'bootstrap.tagsinput' ) ;
require ( 'bootstrap.tagsinput' ) ;
var substringMatcher = function ( tag Collection ) {
var substringMatcher = function ( tag s, selector ) {
return function findMatches ( q , cb ) {
return function findMatches ( q , cb ) {
q = q . replace ( /[^-_a-z0-9]/gi , '' ) . toLowerCase ( ) ;
q = q . replace ( /[^-_a-z0-9]/gi , '' ) . toLowerCase ( ) ;
var matches = _ . select ( tag Collection. toJSON ( ) , function ( tag ) {
var matches = _ . select ( tag s , function ( tag ) {
return tag. label . toLowerCase ( ) . indexOf ( q ) > - 1 ;
return selector( tag ) . toLowerCase ( ) . indexOf ( q ) > - 1 ;
} ) ;
} ) ;
cb ( matches ) ;
cb ( matches ) ;
} ;
} ;
@ -108,49 +108,91 @@ $.fn.tagsinput.Constructor.prototype.build = function(options) {
} ;
} ;
$ . fn . tagInput = function ( options ) {
$ . fn . tagInput = function ( options ) {
options = $ . extend ( { } , { allowNew : true } , options ) ;
var input = this ;
var model = options . model ;
var property = options . property ;
var tagInput = $ ( this ) . tagsinput ( {
tagCollection : TagCollection ,
freeInput : true ,
allowNew : options . allowNew ,
itemValue : 'id' ,
itemText : 'label' ,
trimValue : true ,
typeaheadjs : {
name : 'tags' ,
displayKey : 'label' ,
source : substringMatcher ( TagCollection )
}
} ) ;
//Override the free input being set to false because we're using objects
this . each ( function ( ) {
$ ( tagInput ) [ 0 ] . options . freeInput = true ;
var input = $ ( this ) ;
var tagInput = null ;
if ( input [ 0 ] . hasAttribute ( 'tag-source' ) ) {
var listItems = JSON . parse ( input . attr ( 'tag-source' ) ) ;
tagInput = input . tagsinput ( {
freeInput : false ,
allowNew : false ,
allowDuplicates : false ,
itemValue : 'value' ,
itemText : 'name' ,
typeaheadjs : {
displayKey : 'name' ,
source : substringMatcher ( listItems , function ( t ) { return t . name ; } )
}
} ) ;
if ( model ) {
var origValue = input . val ( ) ;
var tags = getExistingTags ( model . get ( property ) ) ;
//Remove any existing tags and re-add them
input . tagsinput ( 'removeAll' ) ;
$ ( this ) . tagsinput ( 'removeAll' ) ;
_ . each ( tags , function ( tag ) {
if ( origValue ) {
$ ( input ) . tagsinput ( 'add' , tag ) ;
_ . each ( origValue . split ( ',' ) , function ( v ) {
} ) ;
var parsed = parseInt ( v ) ;
$ ( this ) . tagsinput ( 'refresh' ) ;
var found = _ . find ( listItems , function ( t ) { return t . value === parsed ; } ) ;
$ ( this ) . on ( 'itemAdded' , function ( event ) {
var tags = model . get ( property ) ;
if ( found ) {
tags . push ( event . item . id ) ;
input . tagsinput ( 'add' , found ) ;
model . set ( property , tags ) ;
}
} ) ;
} ) ;
$ ( this ) . on ( 'itemRemoved' , function ( event ) {
if ( ! event . item ) {
return ;
}
}
var tags = _ . without ( model . get ( property ) , event . item . id ) ;
}
model . set ( property , tags ) ;
else {
} ) ;
}
options = $ . extend ( { } , { allowNew : true } , options ) ;
var model = options . model ;
var property = options . property ;
tagInput = input . tagsinput ( {
tagCollection : TagCollection ,
freeInput : true ,
allowNew : options . allowNew ,
itemValue : 'id' ,
itemText : 'label' ,
trimValue : true ,
typeaheadjs : {
name : 'tags' ,
displayKey : 'label' ,
source : substringMatcher ( TagCollection . toJSON ( ) , function ( t ) { return t . label ; } )
}
} ) ;
//Override the free input being set to false because we're using objects
$ ( tagInput ) [ 0 ] . options . freeInput = true ;
if ( model ) {
var tags = getExistingTags ( model . get ( property ) ) ;
//Remove any existing tags and re-add them
input . tagsinput ( 'removeAll' ) ;
_ . each ( tags , function ( tag ) {
input . tagsinput ( 'add' , tag ) ;
} ) ;
input . tagsinput ( 'refresh' ) ;
input . on ( 'itemAdded' , function ( event ) {
var tags = model . get ( property ) ;
tags . push ( event . item . id ) ;
model . set ( property , tags ) ;
} ) ;
input . on ( 'itemRemoved' , function ( event ) {
if ( ! event . item ) {
return ;
}
var tags = _ . without ( model . get ( property ) , event . item . id ) ;
model . set ( property , tags ) ;
} ) ;
}
}
} ) ;
} ;
} ;