forked from soliantconsulting/tagmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtagmanager.js
198 lines (198 loc) · 6.85 KB
/
tagmanager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
var TagManager = (function () {
function TagManager(element, options) {
var defaults = {
strategy: 'array',
tagFieldName: 'tags[]',
ajaxCreate: null,
ajaxDelete: null,
initialCap: true,
backspaceChars: [
8
],
delimiterChars: [
13,
44,
188
],
createHandler: function (tagManager, tag, isImport) {
return;
},
deleteHandler: function (tagManager, tag, isEmpty) {
return;
},
createElementHandler: function (tagManager, tagElement, isImport) {
tagManager.$element.before(tagElement);
},
validateHandler: function (tagManager, tag, isImport) {
return tag;
}
};
this.$element = $(element);
this.tagIds = [];
this.tagStrings = [];
this.options = $.extend({
}, defaults, options);
$(element).data('tagmanager', this);
this.listen();
}
TagManager.prototype.keypress = function (e) {
if($.inArray(e.which, this.options.backspaceChars) != -1) {
if(!this.$element.val()) {
e.preventDefault();
this.pop();
}
}
if($.inArray(e.which, this.options.delimiterChars) != -1) {
e.preventDefault();
e.stopPropagation();
if(this.$element.data('typeahead') && this.$element.data('typeahead').shown && this.$element.data('typeahead').$menu.find('.active').length) {
return false;
}
this.create(this.$element.val());
}
};
TagManager.prototype.empty = function () {
var manager = this;
$(this.tagIds).each(function (index, value) {
manager.delete(value, true);
});
};
TagManager.prototype.pop = function () {
if(this.tagIds.length > 0) {
this.delete(this.tagIds[this.tagIds.length - 1]);
}
};
TagManager.prototype.delete = function (tagId, isEmpty) {
var tagString = $('#' + tagId).attr('tag');
if(this.options.deleteHandler) {
this.options.deleteHandler(this, tagString, isEmpty);
}
if(this.options.strategy = 'ajax' && this.options.ajaxDelete && !isEmpty) {
$.ajax({
url: this.options.ajaxDelete,
type: 'post',
data: {
tag: tagString
},
dataType: 'json'
});
}
var index = $.inArray(tagId, this.tagIds);
this.tagStrings.splice(index, 1);
this.tagIds.splice(index, 1);
$('#' + tagId).remove();
};
TagManager.prototype.import = function (tags) {
var manager = this;
$.each(tags, function (key, val) {
manager.create(val, true);
});
};
TagManager.prototype.create = function (rawTag, isImport) {
var tag = $.trim(rawTag);
if(!tag) {
this.$element.val('');
return;
}
if(this.options.initialCap) {
tag = tag.charAt(0).toUpperCase() + tag.slice(1);
}
tag = this.options.validateHandler(this, tag, isImport);
if(!tag) {
this.$element.val('');
return;
}
if(this.options.strategy = 'ajax' && this.options.ajaxCreate && !isImport) {
$.ajax({
url: this.options.ajaxCreate,
type: 'post',
data: {
tag: tag
},
dataType: 'json'
});
}
if(this.options.createHandler) {
this.options.createHandler(this, tag, isImport);
}
var randomString = function (length) {
var result = '';
var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
for(var i = length; i > 0; --i) {
result += chars[Math.round(Math.random() * (chars.length - 1))];
}
return result;
};
var id = 'tag_' + randomString(32);
this.tagIds.push(id);
this.tagStrings.push(tag);
var tagClass = new Tag(this, id, tag);
this.options.createElementHandler(this, tagClass.render(), isImport);
this.$element.val('');
this.$element.focus();
};
TagManager.prototype.listen = function () {
this.$element.on('keypress', $.proxy(this.keypress, this));
};
return TagManager;
})();
var Tag = (function () {
function Tag(manager, id, value) {
this.setManager(manager);
this.setId(id);
this.setTag(value);
}
Tag.prototype.getManager = function () {
return this.manager;
};
Tag.prototype.setManager = function (value) {
this.manager = value;
return this;
};
Tag.prototype.getId = function () {
return this.id;
};
Tag.prototype.setId = function (value) {
this.id = value;
return this;
};
Tag.prototype.getTag = function () {
return this.tag;
};
Tag.prototype.setTag = function (value) {
this.tag = value;
return this;
};
Tag.prototype.validate = function () {
if(this.getManager().options.strategy == 'array' && !this.getManager().options.tagFieldName) {
alert('Array strategy used with no field name');
}
};
Tag.prototype.render = function () {
this.validate();
var tagHtml = $('<span />').addClass('tagmanagerTag').attr('tag', this.getTag()).attr('id', this.getId()).data('tagmanager', this.getManager()).text(this.getTag());
if(this.getManager().options.strategy == 'array') {
$('<input>').attr('type', 'hidden').attr('name', this.getManager().options.tagFieldName).val(this.getTag()).appendTo(tagHtml);
}
var tagRemover = $('<a />').addClass('tagmanagerRemoveTag').attr('title', 'Remove').attr('href', '#').text('x').appendTo(tagHtml);
var id = this.getId();
var manager = this.getManager();
$(tagRemover).click(function (e) {
manager.delete(id);
return false;
});
return tagHtml;
};
return Tag;
})();
$.fn.tagmanager = function (option) {
return this.each(function () {
var $this = $(this), data = $this.data('tagmanager'), options = typeof option == 'object' && option;
if(!data) {
$this.data('tagmanager', (data = new TagManager(this, options)));
}
if(typeof option == 'string') {
data[option]();
}
});
};