tagmanager-rails 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- MmQyMGVhN2E1NGJkNjE4NGEzMTcwMTI1ODNiYTZhZGViNzhmYWE2Zg==
5
- data.tar.gz: !binary |-
6
- MjRjMTBiNThiMDFlOWJjNTBiNjhiNDI1YzNkOWRhNjg1NjI5NzNiMg==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- OWY3OTU4YjAyZjA3NjMxMjdiNTFlMWMyZTgwOGI0OTMzNzExY2JkMmVjNmM5
10
- ZTAzYWU3ZWFjMTk0MWFlOTk5ZTYyYjg2YzM5MTc0OTU5ZThjNmMzZTA0NDZk
11
- MGY4YzkyYjMyOGY2YjljMTA1OGUwNTQ4ZjIyZDZjODJkNWEzNjU=
12
- data.tar.gz: !binary |-
13
- MGJjMTE3ODhlZTQ3MzgzNDZlODZhMGQyNDFjYjc4NDRmNzM1MGRkYzI3ZTZm
14
- YjUwMGQ4MGY3NWQ3MjliNDRiNWZlOTIxZDAzNTdmOTRkM2E5NTc2OGZiOGIw
15
- NmZlOGFkNWQ2MDAxZDVjNTI5OGEwOTMwNWRhNDQwZTk5MDc4OTQ=
2
+ SHA1:
3
+ metadata.gz: 7f998760c26811f00fd14c8a0d77d474dc1e6e5e
4
+ data.tar.gz: 663fbab4bc681dd17f29648f3d6c5b2f7bfc92e0
5
+ SHA512:
6
+ metadata.gz: 54e9299eb4e31417d53101dc926b1032858e1bc00fd14482d8ae639a16be08c0463c5cd4fc9fd1b62baad750e8d975db915e99b9d89799351bf7fc7b464a96db
7
+ data.tar.gz: 98196d4eebec118009777f3d8857e1adbd4986ceaa9d7da8f475fed6548159a214cadbfc319a9fbc5c87e4a5147d25c7633293df8068b8cba08e10d213120685
data/README.md CHANGED
@@ -1,12 +1,17 @@
1
1
  # tagmanager-rails
2
2
 
3
- This gem provides [Tag Manager](https://github.com/max-favilli/tagmanager) for Rails asset pipeline.
3
+ [![Gem Version](https://badge.fury.io/rb/tagmanager-rails.png)](http://badge.fury.io/rb/tagmanager-rails)
4
+ [![Dependency Status](https://gemnasium.com/tmaier/tagmanager-rails.png)](https://gemnasium.com/tmaier/tagmanager-rails)
5
+ [![Code Climate](https://codeclimate.com/github/tmaier/tagmanager-rails.png)](https://codeclimate.com/github/tmaier/tagmanager-rails)
6
+
7
+ This gem provides [Tag Manager][] for Rails asset pipeline.
4
8
 
5
9
  It includes the CSS; SCSS and LESS version of the stylesheet.
6
10
 
7
11
  ## Version
8
12
 
9
- [3f2dff68105a2adb89513104534082360e25c373](https://github.com/max-favilli/tagmanager/commit/3f2dff68105a2adb89513104534082360e25c373)
13
+ Tag Manager version 2.4.2
14
+ [8d91c8683546409ab9ec459456f12afa1a0a870f](https://github.com/max-favilli/tagmanager/commit/8d91c8683546409ab9ec459456f12afa1a0a870f)
10
15
 
11
16
  ## Installation
12
17
 
@@ -61,7 +66,9 @@ Feel free to open an issue ticket if you find something that could be improved.
61
66
 
62
67
  ## Acknowledgements
63
68
 
64
- Many thanks to [max-favilli](https://github.com/max-favilli) for [Tag Manager](https://github.com/max-favilli) and all Rails committer for Rails.
69
+ Many thanks to [max-favilli](https://github.com/max-favilli) for [Tag Manager][] and all Rails committer for Rails.
65
70
 
66
71
  Copyright [Tobias L. Maier](http://tobiasmaier.info), released under the MIT License.
67
72
  Tag Manager itself is under Mozilla Public License. See [license.txt](https://github.com/max-favilli/tagmanager/blob/master/license.txt) of the project.
73
+
74
+ [Tag Manager]: https://github.com/max-favilli/tagmanager
@@ -1,6 +1,6 @@
1
1
  module TagManager
2
2
  module Rails
3
- VERSION = '0.0.1'
4
- TAGMANAGER_VERSION = '3f2dff68105a2adb89513104534082360e25c373'
3
+ VERSION = '0.0.2'
4
+ TAGMANAGER_VERSION = '8d91c8683546409ab9ec459456f12afa1a0a870f'
5
5
  end
6
6
  end
@@ -1,5 +1,5 @@
1
1
  /* ===================================================
2
- * bootstrap-tagmanager.js v2.3
2
+ * bootstrap-tagmanager.js v2.4.2
3
3
  * http://welldonethings.com/tags/manager
4
4
  * ===================================================
5
5
  * Copyright 2012 Max Favilli
@@ -15,35 +15,39 @@
15
15
  * limitations under the License.
16
16
  * ========================================================== */
17
17
 
18
- "use strict";
18
+ (function($){
19
+
20
+ "use strict";
19
21
 
20
- (function (jQuery) {
21
22
  if (typeof console === "undefined" || typeof console.log === "undefined") {
22
23
  console = {};
23
24
  console.log = function () { };
24
25
  }
25
26
 
26
- jQuery.fn.tagsManager = function (options,tagToManipulate) {
27
+ $.fn.tagsManager = function (options,tagToManipulate) {
27
28
  var tagManagerOptions = {
28
29
  prefilled: null,
29
30
  CapitalizeFirstLetter: false,
30
- preventSubmitOnEnter: true,
31
- isClearInputOnEsc: true,
31
+ preventSubmitOnEnter: true, // deprecated
32
+ isClearInputOnEsc: true, // deprecated
32
33
  typeahead: false,
34
+ typeaheadAjaxMethod: "POST",
33
35
  typeaheadAjaxSource: null,
34
36
  typeaheadAjaxPolling: false,
35
37
  typeaheadOverrides: null,
38
+ typeaheadDelegate: {},
36
39
  typeaheadSource: null,
37
40
  AjaxPush: null,
38
41
  AjaxPushAllTags: null,
39
- delimeters: [44, 188, 13, 9],
42
+ AjaxPushParameters: null,
43
+ delimiters: [9,13,44], // tab, enter, comma
40
44
  backspace: [8],
41
45
  maxTags: 0,
42
46
  hiddenTagListName: null,
43
47
  hiddenTagListId: null,
44
- deleteTagsOnBackspace: true,
48
+ deleteTagsOnBackspace: true, // deprecated
45
49
  tagsContainer: null,
46
- tagCloseIcon: 'x',
50
+ tagCloseIcon: '×',
47
51
  tagClass: '',
48
52
  validator: null,
49
53
  onlyTagList: false
@@ -54,8 +58,8 @@
54
58
  this.instanceSelectHandler = null;
55
59
  this.selectedClass = "selected";
56
60
  this.select = null;
57
- if ("typeahead" in jQuery.fn) {
58
- this.instanceSelectHandler = jQuery.fn.typeahead.Constructor.prototype.select;
61
+ if ("typeahead" in $.fn) {
62
+ this.instanceSelectHandler = $.fn.typeahead.Constructor.prototype.select;
59
63
  this.select = function (overrides) {
60
64
  this.$menu.find(".active").addClass(overrides.selectedClass);
61
65
  overrides.instanceSelectHandler.apply(this, arguments);
@@ -72,7 +76,7 @@
72
76
 
73
77
  tagManagerOptions.typeaheadOverrides = new TypeaheadOverrides();
74
78
 
75
- jQuery.extend(tagManagerOptions, options);
79
+ $.extend(tagManagerOptions, options);
76
80
 
77
81
  if (tagManagerOptions.hiddenTagListName === null) {
78
82
  tagManagerOptions.hiddenTagListName = "hidden-" + this.attr('name');
@@ -80,27 +84,43 @@
80
84
 
81
85
  var obj = this;
82
86
  var objName = obj.attr('name').replace(/[^\w]/g, '_');
83
- var queuedTag = "";
84
- var delimeters = tagManagerOptions.delimeters;
87
+ var delimiters = tagManagerOptions.delimeters || tagManagerOptions.delimiters; // 'delimeter' is deprecated
88
+ // delimiter values to be handled as key codes
89
+ var keyNums = [9,13,17,18,19,37,38,39,40];
90
+ var delimiterChars = [], delimiterKeys = [];
91
+ $.each(delimiters, function(i,v){
92
+ if ( $.inArray(v, keyNums) != -1 ){
93
+ delimiterKeys.push(v);
94
+ } else {
95
+ delimiterChars.push(v);
96
+ }
97
+ });
98
+ var baseDelimiter = String.fromCharCode(delimiterChars[0] || 44);
85
99
  var backspace = tagManagerOptions.backspace;
86
- var isInitialized = false;
100
+ var tagBaseClass = 'tm-tag';
101
+ var inputBaseClass = 'tm-input';
102
+
103
+ if ($.isFunction(tagManagerOptions.validator)) obj.data('validator', tagManagerOptions.validator);
87
104
 
88
105
  var setupTypeahead = function () {
89
106
  if (!obj.typeahead) return;
90
107
 
91
- if (tagManagerOptions.typeaheadSource != null && jQuery.isFunction(tagManagerOptions.typeaheadSource)) {
92
- obj.typeahead({ source: tagManagerOptions.typeaheadSource });
108
+ var taOpts = tagManagerOptions.typeaheadDelegate;
109
+
110
+ if (tagManagerOptions.typeaheadSource != null && $.isFunction(tagManagerOptions.typeaheadSource)) {
111
+ $.extend(taOpts, { source: tagManagerOptions.typeaheadSource });
112
+ obj.typeahead(taOpts);
93
113
  } else if (tagManagerOptions.typeaheadSource != null) {
94
- obj.typeahead();
114
+ obj.typeahead(taOpts);
95
115
  setTypeaheadSource(tagManagerOptions.typeaheadSource);
96
116
  } else if (tagManagerOptions.typeaheadAjaxSource != null) {
97
117
  if (!tagManagerOptions.typeaheadAjaxPolling) {
98
- obj.typeahead();
118
+ obj.typeahead(taOpts);
99
119
 
100
120
  if (typeof (tagManagerOptions.typeaheadAjaxSource) == "string") {
101
- jQuery.ajax({
121
+ $.ajax({
102
122
  cache: false,
103
- type: "POST",
123
+ type: tagManagerOptions.typeaheadAjaxMethod,
104
124
  contentType: "application/json",
105
125
  dataType: "json",
106
126
  url: tagManagerOptions.typeaheadAjaxSource,
@@ -109,18 +129,17 @@
109
129
  });
110
130
  }
111
131
  } else if (tagManagerOptions.typeaheadAjaxPolling) {
112
- obj.typeahead({ source: ajaxPolling });
132
+ $.extend(taOpts, { source: ajaxPolling });
133
+ obj.typeahead(taOpts);
113
134
  }
114
- } else if (tagManagerOptions.typeaheadDelegate) {
115
- obj.typeahead(tagManagerOptions.typeaheadDelegate);
116
135
  }
117
136
 
118
137
  var data = obj.data('typeahead');
119
138
  if (data) {
120
139
  // set the overrided handler
121
- data.select = jQuery.proxy(tagManagerOptions.typeaheadOverrides.select,
122
- obj.data('typeahead'),
123
- tagManagerOptions.typeaheadOverrides);
140
+ data.select = $.proxy(tagManagerOptions.typeaheadOverrides.select,
141
+ obj.data('typeahead'),
142
+ tagManagerOptions.typeaheadOverrides);
124
143
  }
125
144
  };
126
145
 
@@ -133,14 +152,14 @@
133
152
  if (data && data.tags) {
134
153
  var sourceAjaxArray = [];
135
154
  sourceAjaxArray.length = 0;
136
- jQuery.each(data.tags, function (key, val) {
155
+ $.each(data.tags, function (key, val) {
137
156
  sourceAjaxArray.push(val.tag);
138
157
  if (isSetTypeaheadSource) {
139
158
  setTypeaheadSource(sourceAjaxArray);
140
159
  }
141
160
  });
142
161
 
143
- if (jQuery.isFunction(process)) {
162
+ if ($.isFunction(process)) {
144
163
  process(sourceAjaxArray);
145
164
  }
146
165
  }
@@ -149,12 +168,23 @@
149
168
  var setTypeaheadSource = function (source) {
150
169
  obj.data('active', true);
151
170
  obj.data('typeahead').source = source;
171
+ tagManagerOptions.typeaheadSource = source;
152
172
  obj.data('active', false);
153
173
  };
154
174
 
175
+ var typeaheadSelectedItem = function () {
176
+ var listItemSelector = '.' + tagManagerOptions.typeaheadOverrides.selectedClass;
177
+ var typeahead_data = obj.data('typeahead');
178
+ return typeahead_data ? typeahead_data.$menu.find(listItemSelector) : undefined;
179
+ };
180
+
181
+ var typeaheadVisible = function () {
182
+ return $('.typeahead:visible')[0];
183
+ };
184
+
155
185
  var ajaxPolling = function (query, process) {
156
186
  if (typeof (tagManagerOptions.typeaheadAjaxSource) == "string") {
157
- jQuery.ajax({
187
+ $.ajax({
158
188
  cache: false,
159
189
  type: "POST",
160
190
  contentType: "application/json",
@@ -166,29 +196,30 @@
166
196
  }
167
197
  };
168
198
 
169
- var trimTag = function (tag) {
170
- var txt = jQuery.trim(tag);
171
-
172
- var l = txt.length;
173
- var t = 0;
174
-
175
- for (var i = l - 1; i >= 0; i--) {
176
- if (-1 == jQuery.inArray(txt.charCodeAt(i), delimeters)) break;
177
- t++;
199
+ var tagClasses = function () {
200
+ // 1) default class (tm-tag)
201
+ var cl = tagBaseClass;
202
+ // 2) interpolate from input class: tm-input-xxx --> tm-tag-xxx
203
+ if (obj.attr('class')) {
204
+ $.each(obj.attr('class').split(' '), function(index, value) {
205
+ if (value.indexOf(inputBaseClass+'-') != -1){
206
+ cl += ' ' + tagBaseClass + value.substring(inputBaseClass.length);
207
+ }
208
+ });
178
209
  }
210
+ // 3) tags from tagClass option
211
+ cl += (tagManagerOptions.tagClass ? ' ' + tagManagerOptions.tagClass : '');
212
+ return cl;
213
+ };
179
214
 
180
- txt = txt.substring(0, l - t);
181
- l = txt.length;
182
- t = 0;
183
-
184
- //remove from head
185
- for (var i = 0; i < l; i++) {
186
- if (-1 == jQuery.inArray(txt.charCodeAt(i), delimeters)) break;
187
- t++;
215
+ var trimTag = function (tag) {
216
+ tag = $.trim(tag);
217
+ // truncate at the first delimiter char
218
+ var i = 0;
219
+ for (i; i < tag.length; i++) {
220
+ if ($.inArray(tag.charCodeAt(i), delimiterChars) != -1) break;
188
221
  }
189
-
190
- txt = txt.substring(t, l);
191
- return txt;
222
+ return tag.substring(0, i);
192
223
  };
193
224
 
194
225
  var popTag = function () {
@@ -199,7 +230,7 @@
199
230
  var tagId = tlid.pop();
200
231
  tlis.pop();
201
232
  // console.log("TagIdToRemove: " + tagId);
202
- jQuery("#" + objName + "_" + tagId).remove();
233
+ $("#" + objName + "_" + tagId).remove();
203
234
  refreshHiddenTagList();
204
235
  // console.log(tlis);
205
236
  }
@@ -213,7 +244,7 @@
213
244
  var tagId = tlid.pop();
214
245
  tlis.pop();
215
246
  // console.log("TagIdToRemove: " + tagId);
216
- jQuery("#" + objName + "_" + tagId).remove();
247
+ $("#" + objName + "_" + tagId).remove();
217
248
  refreshHiddenTagList();
218
249
  // console.log(tlis);
219
250
  }
@@ -223,10 +254,10 @@
223
254
  var tlis = obj.data("tlis");
224
255
  var lhiddenTagList = obj.data("lhiddenTagList");
225
256
 
226
- obj.trigger('tags:refresh', tlis.join(","));
257
+ obj.trigger('tags:refresh', tlis.join(baseDelimiter));
227
258
 
228
259
  if (lhiddenTagList) {
229
- jQuery(lhiddenTagList).val(tlis.join(",")).change();
260
+ $(lhiddenTagList).val(tlis.join(baseDelimiter)).change();
230
261
  }
231
262
  };
232
263
 
@@ -234,13 +265,13 @@
234
265
  var tlis = obj.data("tlis");
235
266
  var tlid = obj.data("tlid");
236
267
 
237
- var p = jQuery.inArray(tagId, tlid);
268
+ var p = $.inArray(tagId, tlid);
238
269
 
239
270
  // console.log("TagIdToRemove: " + tagId);
240
271
  // console.log("position: " + p);
241
272
 
242
273
  if (-1 != p) {
243
- jQuery("#" + objName + "_" + tagId).remove();
274
+ $("#" + objName + "_" + tagId).remove();
244
275
  tlis.splice(p, 1);
245
276
  tlid.splice(p, 1);
246
277
  refreshHiddenTagList();
@@ -254,19 +285,22 @@
254
285
 
255
286
  var pushAllTags = function (e, tagstring) {
256
287
  if (tagManagerOptions.AjaxPushAllTags) {
257
- jQuery.post(tagManagerOptions.AjaxPushAllTags, { tags: tagstring });
288
+ $.post(tagManagerOptions.AjaxPush, { tags: tagstring });
258
289
  }
259
290
  };
260
291
 
261
- var pushTag = function (tag, objToPush, isValid) {
262
- if (!tag || (!isValid) || tag.length <= 0) return;
292
+ var pushTag = function (tag) {
293
+ tag = trimTag(tag);
263
294
 
264
- if(tagManagerOptions.onlyTagList){
265
- if (tagManagerOptions.typeaheadSource != null) {
266
- if((jQuery.inArray(tag, tagManagerOptions.typeaheadSource)) == -1){
267
- return;
268
- }
269
- }
295
+ if (!tag || tag.length <= 0) return;
296
+
297
+ if (tagManagerOptions.typeaheadSource != null)
298
+ {
299
+ var source = $.isFunction(tagManagerOptions.typeaheadSource) ?
300
+ tagManagerOptions.typeaheadSource() : tagManagerOptions.typeaheadSource;
301
+
302
+ if(tagManagerOptions.onlyTagList &&
303
+ $.inArray(tag, source) == -1) return;
270
304
  }
271
305
 
272
306
  if (tagManagerOptions.CapitalizeFirstLetter && tag.length > 1) {
@@ -274,9 +308,7 @@
274
308
  }
275
309
 
276
310
  // call the validator (if any) and do not let the tag pass if invalid
277
- if (jQuery.isFunction(tagManagerOptions.validator)) {
278
- if (!tagManagerOptions.validator(tag)) return;
279
- }
311
+ if (obj.data('validator') && !obj.data('validator')(tag)) return;
280
312
 
281
313
  var tlis = obj.data("tlis");
282
314
  var tlid = obj.data("tlid");
@@ -286,7 +318,7 @@
286
318
 
287
319
  var alreadyInList = false;
288
320
  var tlisLowerCase = tlis.map(function(elem) { return elem.toLowerCase(); });
289
- var p = jQuery.inArray(tag.toLowerCase(), tlisLowerCase);
321
+ var p = $.inArray(tag.toLowerCase(), tlisLowerCase);
290
322
  if (-1 != p) {
291
323
  // console.log("tag:" + tag + " !!already in list!!");
292
324
  alreadyInList = true;
@@ -294,13 +326,13 @@
294
326
 
295
327
  if (alreadyInList) {
296
328
  var pTagId = tlid[p];
297
- jQuery("#" + objName + "_" + pTagId).stop()
298
- .animate({ backgroundColor: tagManagerOptions.blinkBGColor_1 }, 100)
299
- .animate({ backgroundColor: tagManagerOptions.blinkBGColor_2 }, 100)
300
- .animate({ backgroundColor: tagManagerOptions.blinkBGColor_1 }, 100)
301
- .animate({ backgroundColor: tagManagerOptions.blinkBGColor_2 }, 100)
302
- .animate({ backgroundColor: tagManagerOptions.blinkBGColor_1 }, 100)
303
- .animate({ backgroundColor: tagManagerOptions.blinkBGColor_2 }, 100);
329
+ $("#" + objName + "_" + pTagId).stop()
330
+ .animate({ backgroundColor: tagManagerOptions.blinkBGColor_1 }, 100)
331
+ .animate({ backgroundColor: tagManagerOptions.blinkBGColor_2 }, 100)
332
+ .animate({ backgroundColor: tagManagerOptions.blinkBGColor_1 }, 100)
333
+ .animate({ backgroundColor: tagManagerOptions.blinkBGColor_2 }, 100)
334
+ .animate({ backgroundColor: tagManagerOptions.blinkBGColor_1 }, 100)
335
+ .animate({ backgroundColor: tagManagerOptions.blinkBGColor_2 }, 100);
304
336
  } else {
305
337
  var max = Math.max.apply(null, tlid);
306
338
  max = max == -Infinity ? 0 : max;
@@ -310,26 +342,30 @@
310
342
  tlid.push(tagId);
311
343
 
312
344
  if (tagManagerOptions.AjaxPush != null) {
313
- jQuery.post(tagManagerOptions.AjaxPush, { tag: tag });
345
+ $.post(tagManagerOptions.AjaxPush, $.extend({ tag: tag }, tagManagerOptions.AjaxPushParameters));
314
346
  }
315
347
 
316
348
  // console.log("tagList: " + tlis);
317
349
 
318
350
  var newTagId = objName + '_' + tagId;
319
351
  var newTagRemoveId = objName + '_Remover_' + tagId;
320
- var html = '';
321
- var cl = tagManagerOptions.tagClass ? ' '+tagManagerOptions.tagClass : '';
322
- html += '<span class="myTag'+cl+'" id="' + newTagId + '"><span>' + tag + '&nbsp;&nbsp;</span><a href="#" class="myTagRemover" id="' + newTagRemoveId + '" TagIdToRemove="' + tagId + '" title="Remove">' + tagManagerOptions.tagCloseIcon + '</a></span> ';
352
+ var escaped = $("<span></span>").text(tag).html();
353
+
354
+ var html = '<span class="' + tagClasses() + '" id="' + newTagId + '">';
355
+ html += '<span>' + escaped + '</span>';
356
+ html += '<a href="#" class="tm-tag-remove" id="' + newTagRemoveId + '" TagIdToRemove="' + tagId + '">';
357
+ html += tagManagerOptions.tagCloseIcon + '</a></span> ';
358
+ var $el = $(html);
323
359
 
324
360
  if (tagManagerOptions.tagsContainer != null) {
325
- jQuery(tagManagerOptions.tagsContainer).append(html);
361
+ $(tagManagerOptions.tagsContainer).append($el);
326
362
  } else {
327
- obj.before(html);
363
+ obj.before($el);
328
364
  }
329
365
 
330
- jQuery("#" + newTagRemoveId).on("click", obj, function (e) {
366
+ $el.find("#" + newTagRemoveId).on("click", obj, function (e) {
331
367
  e.preventDefault();
332
- var TagIdToRemove = parseInt(jQuery(this).attr("TagIdToRemove"));
368
+ var TagIdToRemove = parseInt($(this).attr("TagIdToRemove"));
333
369
  spliceTag(TagIdToRemove, e.data);
334
370
  });
335
371
 
@@ -342,20 +378,33 @@
342
378
  obj.val("");
343
379
  };
344
380
 
345
- var initialize = function () {
346
- if (tagManagerOptions.AjaxPushAllTags) {
347
- obj.on('tags:refresh', pushAllTags);
348
- }
381
+ var prefill = function (pta) {
382
+ $.each(pta, function (key, val) {
383
+ pushTag(val);
384
+ });
349
385
  };
350
386
 
351
- if (!isInitialized) {
352
- initialize();
353
- }
387
+ var killEvent = function (e) {
388
+ e.cancelBubble = true;
389
+ e.returnValue = false;
390
+ e.stopPropagation();
391
+ e.preventDefault();
392
+ };
354
393
 
355
- return this.each(function () {
394
+ var keyInArray = function (e, ary) {
395
+ return $.inArray(e.which, ary) != -1
396
+ };
356
397
 
357
- var tagIsValid = false;
358
- var isSelectedFromList = false;
398
+ var applyDelimiter = function (e) {
399
+ var taItem = typeaheadSelectedItem();
400
+ var taVisible = typeaheadVisible();
401
+ if (!(e.which==13 && taItem && taVisible)) {
402
+ pushTag(obj.val());
403
+ }
404
+ e.preventDefault();
405
+ };
406
+
407
+ return this.each(function () {
359
408
 
360
409
  if (typeof options == 'string') {
361
410
  switch (options) {
@@ -366,13 +415,17 @@
366
415
  popTag();
367
416
  break;
368
417
  case "pushTag":
369
- pushTag(tagToManipulate, null, true);
418
+ pushTag(tagToManipulate);
370
419
  break;
371
420
  }
372
421
  return;
373
422
  }
374
423
 
375
- //let's store some instance specific data directly into the DOM object
424
+ // prevent double-initialization of TagManager
425
+ if ($(this).data('tagManager')){ return false; }
426
+ $(this).data('tagManager', true);
427
+
428
+ // store instance-specific data in the DOM object
376
429
  var tlis = new Array();
377
430
  var tlid = new Array();
378
431
  obj.data("tlis", tlis); //list of string tags
@@ -388,213 +441,103 @@
388
441
  html += "<input name='" + tagManagerOptions.hiddenTagListName + "' type='hidden' value=''/>";
389
442
  obj.after(html);
390
443
  obj.data("lhiddenTagList",
391
- obj.siblings("input[name='" + tagManagerOptions.hiddenTagListName + "']")[0]
444
+ obj.siblings("input[name='" + tagManagerOptions.hiddenTagListName + "']")[0]
392
445
  );
393
446
  } else {
394
- obj.data("lhiddenTagList", jQuery('#' + tagManagerOptions.hiddenTagListId))
447
+ obj.data("lhiddenTagList", $('#' + tagManagerOptions.hiddenTagListId))
395
448
  }
396
449
 
397
450
  if (tagManagerOptions.typeahead) {
398
451
  setupTypeahead();
399
- //obj.typeahead({ source: SourceArray })
400
452
  }
401
453
 
402
- obj.on("focus", function (e) {
403
- if (jQuery(this).popover) {
404
- jQuery(this).popover("hide");
405
- //jQuery(this).popover = null;
454
+ if (tagManagerOptions.AjaxPushAllTags) {
455
+ obj.on('tags:refresh', pushAllTags);
456
+ }
457
+
458
+ // hide popovers on focus and keypress events
459
+ obj.on('focus keypress', function (e) {
460
+ if ($(this).popover) {
461
+ $(this).popover('hide');
406
462
  }
407
463
  });
408
464
 
409
- // clear input field on Esc
465
+ // handle ESC (keyup used for browser compatibility)
410
466
  if (tagManagerOptions.isClearInputOnEsc) {
411
- obj.on("keyup", function (e) {
467
+ obj.on('keyup', function (e) {
412
468
  if (e.which == 27) {
413
- jQuery(this).val("");
414
- e.cancelBubble = true;
415
- e.returnValue = false;
416
- e.stopPropagation();
417
- e.preventDefault();
418
- return false;
469
+ // console.log('esc detected');
470
+ $(this).val('');
471
+ killEvent(e);
419
472
  }
420
473
  });
421
474
  }
422
475
 
423
- // disable submit on enter for this input field
424
- obj.on("keypress", function (e) {
425
- if (jQuery(this).popover) {
426
- jQuery(this).popover("hide");
427
- //jQuery(this).popover = null;
476
+ obj.on('keypress', function (e) {
477
+ // push ASCII-based delimiters
478
+ if (keyInArray(e, delimiterChars)) {
479
+ applyDelimiter(e);
428
480
  }
481
+ });
429
482
 
430
- if (tagManagerOptions.preventSubmitOnEnter) {
431
- if (e.which == 13) {
432
- e.cancelBubble = true;
433
- e.returnValue = false;
434
- e.stopPropagation();
435
- e.preventDefault();
436
- //e.keyCode = 9;
437
- return false;
483
+ obj.on('keydown', function (e) {
484
+ // disable ENTER
485
+ if (e.which == 13) {
486
+ if (tagManagerOptions.preventSubmitOnEnter) {
487
+ killEvent(e);
438
488
  }
439
489
  }
440
490
 
441
- var p = jQuery.inArray(e.which, delimeters);
442
- var isKeyInList = '0' in jQuery(".typeahead:visible");
443
- if (!isKeyInList && (- 1 != p)) {
444
- //user just entered a valid delimeter
445
- tagIsValid = true;
446
- var user_input = jQuery(this).val(); //user_input = jQuery().inArray(delimeters[p]);
447
- user_input = trimTag(user_input);
448
- pushTag(user_input, e.data, tagIsValid);
449
- e.preventDefault();
450
- // console.log("pushTag: keypress");
491
+ // push key-based delimiters (includes <enter> by default)
492
+ if (keyInArray(e, delimiterKeys)) {
493
+ applyDelimiter(e);
451
494
  }
452
- else {
453
- tagIsValid = false;
454
- }
455
-
456
- // console.log("keypress: " + e.which);
457
495
  });
458
496
 
497
+ // BACKSPACE (keydown used for browser compatibility)
459
498
  if (tagManagerOptions.deleteTagsOnBackspace) {
460
- obj.on("keydown", obj, function (e) {
461
- var p = jQuery.inArray(e.which, backspace);
462
- if (-1 != p) {
463
- //user just entered backspace or equivalent
464
- var user_input = jQuery(this).val(); //user_input = jQuery().inArray(delimeters[p]);
465
- var i = user_input.length;
466
- if (i <= 0) {
467
- // console.log("backspace detected");
468
- e.preventDefault();
499
+ obj.on('keydown', function (e) {
500
+ if (keyInArray(e, backspace)) {
501
+ // console.log("backspace detected");
502
+ if ($(this).val().length <= 0) {
469
503
  popTag();
504
+ killEvent(e);
470
505
  }
471
506
  }
472
507
  });
473
508
  }
474
509
 
475
510
  obj.change(function (e) {
476
- e.cancelBubble = true;
477
- e.returnValue = false;
478
- e.stopPropagation();
479
- e.preventDefault();
480
-
481
- var selectedItemClass = tagManagerOptions.typeaheadOverrides.selectedClass;
482
- var listItemSelector = '.' + selectedItemClass;
483
-
484
- // check the typeahead list selection
485
- var data = $(this).data('typeahead');
486
- if (data) {
487
- isSelectedFromList = $(this).data('typeahead').$menu.find("*")
488
- .filter(listItemSelector)
489
- .hasClass(selectedItemClass);
490
-
491
- if (isSelectedFromList) {
492
- tagIsValid = true;
493
- }
494
- }
495
511
 
496
- if (!tagIsValid) {
497
- return false;
498
- }
512
+ if (!/webkit/.test(navigator.userAgent.toLowerCase())) { $(this).focus(); } // why?
499
513
 
500
- var is_chrome = navigator.userAgent.indexOf('Chrome') > -1;
501
- var is_explorer = navigator.userAgent.indexOf('MSIE') > -1;
502
- var is_firefox = navigator.userAgent.indexOf('Firefox') > -1;
503
- var is_safari = navigator.userAgent.indexOf("Safari") > -1;
504
-
505
- if (!is_chrome && !is_safari)
506
- jQuery(this).focus();
507
-
508
- // console.log('Handler for .change() called, value selected:' + obj.val());
509
- var ao = jQuery(".typeahead:visible");
510
- if (ao[0] != undefined) {
511
- // console.log('change: typeaheadIsVisible is visible');
512
- //when the user click with the mouse on the typeahead li element we get the change event fired twice, once when the input field loose focus and later with the input field value is replaced with li value
513
-
514
- var isClear = !isSelectedFromList;
515
-
516
- if (isSelectedFromList) {
517
- // if user selected from list
518
- var user_input = $(this).data('typeahead').$menu.find(listItemSelector).attr('data-value');
519
- user_input = trimTag(user_input);
520
- if (queuedTag == jQuery(this).val() && queuedTag == user_input) {
521
- isClear = true;
522
- } else {
523
- pushTag(user_input, null, true);
524
- queuedTag = user_input;
525
- // console.log('Handler for .change() called, typeahead value pushed:' + queuedTag);
526
- }
527
- isSelectedFromList = false;
528
- $(this).data('typeahead').$menu.find(listItemSelector).removeClass(selectedItemClass);
529
- }
514
+ var taItem = typeaheadSelectedItem();
515
+ var taVisible = typeaheadVisible();
530
516
 
531
- if (isClear) {
532
- queuedTag = "";
533
- jQuery(this).val(queuedTag);
534
- }
535
- } else {
536
- // console.log('change: typeaheadIsVisible is NOT visible');
537
- var user_input = jQuery(this).val(); //user_input = jQuery().inArray(delimeters[p]);
538
- user_input = trimTag(user_input);
539
- pushTag(user_input, null, true);
540
- // console.log("pushTag: change ");
517
+ if (taItem && taVisible) {
518
+ taItem.removeClass(tagManagerOptions.typeaheadOverrides.selectedClass);
519
+ pushTag(taItem.attr('data-value'));
520
+ // console.log('change: pushTypeAheadTag ' + tag);
541
521
  }
542
-
543
- tagIsValid = false;
544
-
545
- return false; //cancel bubble
522
+ /* unimplemented mode to push tag on blur
523
+ else if (tagManagerOptions.pushTagOnBlur) {
524
+ console.log('change: pushTagOnBlur ' + tag);
525
+ pushTag($(this).val());
526
+ } */
527
+ killEvent(e);
546
528
  });
547
529
 
548
- if (1 == 1 || !tagManagerOptions.typeahead) {
549
- obj.on("blur", function (e) {
550
- //lost focus
551
- e.cancelBubble = true;
552
- e.returnValue = false;
553
- e.stopPropagation();
554
- e.preventDefault();
555
-
556
- var push = true;
557
- if (tagManagerOptions.typeahead) {
558
- var ao = jQuery(".typeahead:visible");
559
- if (ao[0] != undefined) {
560
- // console.log('blur: typeaheadIsVisible is visible');
561
- push = false;
562
- } else {
563
- // console.log('blur: typeaheadIsVisible is NOT visible');
564
- push = true;
565
- }
566
- }
567
-
568
- if (push) {
569
- // console.log('lost focus');
570
- var user_input = jQuery(this).val(); //user_input = jQuery().inArray(delimeters[p]);
571
- user_input = trimTag(user_input);
572
- pushTag(user_input, null, tagIsValid);
573
- // console.log("pushTag: blur");
574
- }
575
-
576
- return false;
577
- });
578
- }
579
-
580
530
  if (tagManagerOptions.prefilled != null) {
581
531
  if (typeof (tagManagerOptions.prefilled) == "object") {
582
- var pta = tagManagerOptions.prefilled;
583
- jQuery.each(pta, function (key, val) {
584
- var a = 1;
585
- pushTag(val, obj, true);
586
- });
532
+ prefill(tagManagerOptions.prefilled);
587
533
  } else if (typeof (tagManagerOptions.prefilled) == "string") {
588
- var pta = tagManagerOptions.prefilled.split(',');
589
-
590
- jQuery.each(pta, function (key, val) {
591
- var a = 1;
592
- pushTag(val, obj, true);
593
- });
594
-
534
+ prefill(tagManagerOptions.prefilled.split(baseDelimiter));
535
+ } else if (typeof (tagManagerOptions.prefilled) == "function") {
536
+ prefill(tagManagerOptions.prefilled());
595
537
  }
538
+ } else if (tagManagerOptions.hiddenTagListId != null) {
539
+ prefill($('#' + tagManagerOptions.hiddenTagListId).val().split(baseDelimiter));
596
540
  }
597
541
  });
598
-
599
542
  }
600
543
  })(jQuery);