|
|
@ -4,10 +4,11 @@ var TagCollection = require('../Tags/TagCollection');
|
|
|
|
var TagModel = require('../Tags/TagModel');
|
|
|
|
var TagModel = require('../Tags/TagModel');
|
|
|
|
require('bootstrap.tagsinput');
|
|
|
|
require('bootstrap.tagsinput');
|
|
|
|
|
|
|
|
|
|
|
|
var substringMatcher = function() {
|
|
|
|
var substringMatcher = function(tagCollection) {
|
|
|
|
return function findMatches (q, cb) {
|
|
|
|
return function findMatches (q, cb) {
|
|
|
|
var matches = _.select(TagCollection.toJSON(), function(tag) {
|
|
|
|
q = q.replace(/[^-_a-z0-9]/gi, '').toLowerCase();
|
|
|
|
return tag.label.toLowerCase().indexOf(q.toLowerCase()) > -1;
|
|
|
|
var matches = _.select(tagCollection.toJSON(), function(tag) {
|
|
|
|
|
|
|
|
return tag.label.toLowerCase().indexOf(q) > -1;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
cb(matches);
|
|
|
|
cb(matches);
|
|
|
|
};
|
|
|
|
};
|
|
|
@ -33,22 +34,27 @@ var originalRemove = $.fn.tagsinput.Constructor.prototype.remove;
|
|
|
|
var originalBuild = $.fn.tagsinput.Constructor.prototype.build;
|
|
|
|
var originalBuild = $.fn.tagsinput.Constructor.prototype.build;
|
|
|
|
|
|
|
|
|
|
|
|
$.fn.tagsinput.Constructor.prototype.add = function(item, dontPushVal) {
|
|
|
|
$.fn.tagsinput.Constructor.prototype.add = function(item, dontPushVal) {
|
|
|
|
var self = this;
|
|
|
|
var tagCollection = this.options.tagCollection;
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof item === 'string' && this.options.tag) {
|
|
|
|
if (!tagCollection) {
|
|
|
|
var test = testTag(item);
|
|
|
|
originalAdd.call(this, item, dontPushVal);
|
|
|
|
if (item === null || item === '' || !testTag(item)) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
|
|
var existing = _.find(TagCollection.toJSON(), { label : item });
|
|
|
|
if (typeof item === 'string') {
|
|
|
|
|
|
|
|
var existing = _.find(tagCollection.toJSON(), { label : item });
|
|
|
|
|
|
|
|
|
|
|
|
if (existing) {
|
|
|
|
if (existing) {
|
|
|
|
originalAdd.call(this, existing, dontPushVal);
|
|
|
|
originalAdd.call(this, existing, dontPushVal);
|
|
|
|
} else {
|
|
|
|
} else if (this.options.allowNew) {
|
|
|
|
|
|
|
|
if (item === null || item === '' || !testTag(item)) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var newTag = new TagModel();
|
|
|
|
var newTag = new TagModel();
|
|
|
|
newTag.set({ label : item.toLowerCase() });
|
|
|
|
newTag.set({ label : item.toLowerCase() });
|
|
|
|
TagCollection.add(newTag);
|
|
|
|
tagCollection.add(newTag);
|
|
|
|
|
|
|
|
|
|
|
|
newTag.save().done(function() {
|
|
|
|
newTag.save().done(function() {
|
|
|
|
item = newTag.toJSON();
|
|
|
|
item = newTag.toJSON();
|
|
|
@ -56,12 +62,10 @@ $.fn.tagsinput.Constructor.prototype.add = function(item, dontPushVal) {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
originalAdd.call(this, item, dontPushVal);
|
|
|
|
originalAdd.call(self, item, dontPushVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (this.options.tag) {
|
|
|
|
|
|
|
|
self.$input.typeahead('val', '');
|
|
|
|
self.$input.typeahead('val', '');
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
$.fn.tagsinput.Constructor.prototype.remove = function(item, dontPushVal) {
|
|
|
|
$.fn.tagsinput.Constructor.prototype.remove = function(item, dontPushVal) {
|
|
|
@ -104,27 +108,32 @@ $.fn.tagsinput.Constructor.prototype.build = function(options) {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
$.fn.tagInput = function(options) {
|
|
|
|
$.fn.tagInput = function(options) {
|
|
|
|
|
|
|
|
options = $.extend({}, { allowNew : true }, options);
|
|
|
|
|
|
|
|
|
|
|
|
var input = this;
|
|
|
|
var input = this;
|
|
|
|
var model = options.model;
|
|
|
|
var model = options.model;
|
|
|
|
var property = options.property;
|
|
|
|
var property = options.property;
|
|
|
|
var tags = getExistingTags(model.get(property));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var tagInput = $(this).tagsinput({
|
|
|
|
var tagInput = $(this).tagsinput({
|
|
|
|
tag : true,
|
|
|
|
tagCollection : TagCollection,
|
|
|
|
freeInput : true,
|
|
|
|
freeInput : true,
|
|
|
|
|
|
|
|
allowNew : options.allowNew,
|
|
|
|
itemValue : 'id',
|
|
|
|
itemValue : 'id',
|
|
|
|
itemText : 'label',
|
|
|
|
itemText : 'label',
|
|
|
|
trimValue : true,
|
|
|
|
trimValue : true,
|
|
|
|
typeaheadjs : {
|
|
|
|
typeaheadjs : {
|
|
|
|
name : 'tags',
|
|
|
|
name : 'tags',
|
|
|
|
displayKey : 'label',
|
|
|
|
displayKey : 'label',
|
|
|
|
source : substringMatcher()
|
|
|
|
source : substringMatcher(TagCollection)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
//Override the free input being set to false because we're using objects
|
|
|
|
//Override the free input being set to false because we're using objects
|
|
|
|
$(tagInput)[0].options.freeInput = true;
|
|
|
|
$(tagInput)[0].options.freeInput = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (model) {
|
|
|
|
|
|
|
|
var tags = getExistingTags(model.get(property));
|
|
|
|
|
|
|
|
|
|
|
|
//Remove any existing tags and re-add them
|
|
|
|
//Remove any existing tags and re-add them
|
|
|
|
$(this).tagsinput('removeAll');
|
|
|
|
$(this).tagsinput('removeAll');
|
|
|
|
_.each(tags, function(tag) {
|
|
|
|
_.each(tags, function(tag) {
|
|
|
@ -143,4 +152,5 @@ $.fn.tagInput = function(options) {
|
|
|
|
var tags = _.without(model.get(property), event.item.id);
|
|
|
|
var tags = _.without(model.get(property), event.item.id);
|
|
|
|
model.set(property, tags);
|
|
|
|
model.set(property, tags);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|