-
Notifications
You must be signed in to change notification settings - Fork 17
/
jquery.tagmate.js
439 lines (390 loc) · 18.6 KB
/
jquery.tagmate.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/**
* jquery.tagmate.js
* =================
* Copyright (c) 2011 Cold Brew Labs Inc., http://coldbrewlabs.com
* Licenced under MIT (see included LICENSE)
*
* Requirements
* ------------
* jquery.js (http://jquery.com)
* jquery.scrollTo.js (http://demos.flesler.com/jquery/scrollTo/)
* jquery.fieldselection.js - (included)
*/
//
// Global namespace stuff. These are provided as a convenience to plugin users.
//
var Tagmate = (function() {
var HASH_TAG_EXPR = "\\w+";
var NAME_TAG_EXPR = "\\w+(?: \\w+)*"; // allow spaces
var PRICE_TAG_EXPR = "(?:(?:\\d{1,3}(?:\\,\\d{3})+)|(?:\\d+))(?:\\.\\d{2})?";
return {
HASH_TAG_EXPR: HASH_TAG_EXPR,
NAME_TAG_EXPR: NAME_TAG_EXPR,
PRICE_TAG_EXPR: PRICE_TAG_EXPR,
DEFAULT_EXPRS: {
'@': NAME_TAG_EXPR,
'#': HASH_TAG_EXPR,
'$': PRICE_TAG_EXPR
},
// Remove options that don't match the filter.
filterOptions: function(options, term) {
var filtered = [];
for (var i = 0; i < options.length; i++) {
var label_lc = options[i].label.toLowerCase();
var term_lc = term.toLowerCase();
if (term_lc.length <= label_lc.length && label_lc.indexOf(term_lc) == 0)
filtered.push(options[i]);
}
return filtered;
}
};
})();
//
// jQuery plugin
//
(function($) {
// Similar to indexOf() but uses RegExp.
function regex_index_of(str, regex, startpos) {
var indexOf = str.substring(startpos || 0).search(regex);
return (indexOf >= 0) ? (indexOf + (startpos || 0)) : indexOf;
}
// Escape special RegExp chars.
function regex_escape(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
// Parse tags from a textarea (internal).
function parse_tags(textarea, exprs, sources) {
var tags = {};
for (tok in exprs) {
if (sources && sources[tok]) {
// Favor sources to raw tags if available
var matches = {}, indexes = {};
for (key in sources[tok]) {
var value = sources[tok][key].value;
var label = sources[tok][key].label;
var tag = regex_escape(tok + label);
// This regexp is insane. \b won't work because we allow spaces.
var e = ["(?:^(",")$|^(",")\\W|\\W(",")\\W|\\W(",")$)"].join(tag);
var i = 0, re = new RegExp(e, "gm");
while ((i = regex_index_of(textarea.val(), re, i)) > -1) {
var p = indexes[i] ? indexes[i] : null;
// Favor longer matches
if (!p || matches[p].length < label.length)
indexes[i] = value;
matches[value] = label;
i += label.length + 1;
}
}
// Keep only longest matches
for (i in indexes)
tags[tok + indexes[i]] = tok;
} else {
// Check for raw tags
var m = null, re = new RegExp("([" + tok + "]" + exprs[tok] + ")", "gm");
while (m = re.exec(textarea.val()))
tags[m[1]] = tok;
}
}
// Keep only uniques
var results = []
for (tag in tags)
results.push(tag);
return results;
}
$.fn.extend({
getTags: function(exprs, sources) {
var textarea = $(this);
exprs = exprs || textarea.data("_tagmate_exprs");
sources = sources || textarea.data("_tagmate_sources");
return parse_tags(textarea, exprs, sources);
},
tagmate: function(options) {
var defaults = {
exprs: Tagmate.DEFAULT_EXPRS,
sources: null, // { '@': [{label:'foo',value:'bar'}] }
capture_tag: null, // Callback fired when tags are captured. function(tag) {}
replace_tag: null, // Callback fired when tag is replaced. function(tag, label) {}
menu_class: "tagmate-menu",
menu_option_class: "tagmate-menu-option",
menu_option_active_class: "tagmate-menu-option-active",
highlight_tags: false, // EXPERIMENTAL: enable at your own risk!
highlight_class: 'tagmate-highlight'
};
// Get the previous position of tok starting at pos (or -1)
function prev_tok(str, tok, pos) {
var re = new RegExp("[" + tok + "]");
for (; pos >= 0 && !re.test(str[pos]); pos--) {};
return pos;
}
// Get tag value at current cursor position
function parse_tag(textarea) {
var text = textarea.val();
var sel = textarea.getSelection();
// Search left for closest matching tag token
var m_pos = -1, m_tok = null;
for (tok in defaults.exprs) {
var pos = prev_tok(text, tok, sel.start);
if (pos > m_pos) {
m_pos = pos;
m_tok = tok;
}
}
// Match from token to cursor
var sub = text.substring(m_pos + 1, sel.start);
// Look for raw matches
var re = new RegExp("^[" + m_tok + "]" + defaults.exprs[m_tok]);
if (re.exec(m_tok + sub))
return m_tok + sub
return null;
}
// Replace the textarea query text with the suggestion
function replace_tag(textarea, tag, value) {
var text = textarea.val();
// Replace occurrence at cursor position
var sel = textarea.getSelection();
var pos = prev_tok(text, tag[0], sel.start);
var l = text.substr(0, pos);
var r = text.substr(pos + tag.length);
textarea.val(l + tag[0] + value + r);
// Try to move cursor position at end of tag
var sel_pos = pos + value.length + 1;
textarea.setSelection(sel_pos, sel_pos);
// Callback for tag replacement
if (defaults.replace_tag)
defaults.replace_tag(tag, value);
}
// Show the menu of options
function update_menu(menu, options) {
// Sort results alphabetically
options = options.sort(function(a, b) {
var a_lc = a.label.toLowerCase();
var b_lc = b.label.toLowerCase();
if (a_lc > b_lc)
return 1;
else if (a_lc < b_lc)
return -1;
return 0;
});
// Append results to menu
for (var i = 0; i < options.length; i++) {
var label = options[i].label;
var value = options[i].value;
var image = options[i].image;
if (i == 0)
menu.html("");
var content = "<span>" + label + "</span>";
if (image)
content = "<img src='" + image + "' alt='" + label + "'/>" + content;
var classes = defaults.menu_option_class;
if (i == 0)
classes += " " + defaults.menu_option_active_class;
menu.append("<div class='" + classes + "'>" + content + "</div>");
}
}
// Move up or down in the selection menu
function scroll_menu(menu, direction) {
var child_selector = direction == "down" ? ":first-child" : ":last-child";
var sibling_func = direction == "down" ? "next" : "prev";
var active = menu.children("." + defaults.menu_option_active_class);
if (active.length == 0) {
active = menu.children(child_selector);
active.addClass(defaults.menu_option_active_class);
} else {
active.removeClass(defaults.menu_option_active_class);
active = active[sibling_func]().length > 0 ? active[sibling_func]() : active;
active.addClass(defaults.menu_option_active_class);
}
// Scroll inside menu if necessary
var i, options = menu.children();
var n = Math.floor($(menu).height() / $(options[0]).height()) - 1;
if ($(menu).height() % $(options[0]).height() > 0)
n -= 1; // don't scroll if bottom row is only partially visible
// Iterate to visible option
for (i = 0; i < options.length && $(options[i]).html() != $(active).html(); i++) {};
if (i > n && (i - n) >= 0 && (i - n) < options.length)
menu.scrollTo(options[i - n]);
}
// TODO: Fix this so that it works.
function init_hiliter(textarea) {
textarea.css("background", "transparent");
var container = $(textarea).wrap("<div class='tagmate-container'/>");
// Set up highlighter div
var hiliter = $("<pre class='tagmate-hiliter'></pre>");
hiliter.css("height", textarea.height() + "px");
hiliter.css("width", textarea.width() + "px");
hiliter.css("border", "1px solid #FFF");
//hiliter.css("position", "inherit");
//hiliter.css("top", "-" + textarea.outerHeight() + "px");
hiliter.css("margin", "0");
//hiliter.css("top", "0");
//hiliter.css("left", "0");
hiliter.css("padding-top", textarea.css("padding-top"));
hiliter.css("padding-bottom", textarea.css("padding-bottom"));
hiliter.css("padding-left", textarea.css("padding-left"));
hiliter.css("padding-right", textarea.css("padding-right"));
hiliter.css("color", "#FFF");
hiliter.css("z-index", "-1");
hiliter.css("background", "#FFF");
hiliter.css("font-family", textarea.css("font-family"));
hiliter.css("font-size", textarea.css("font-size"));
// Enable text wrapping in <pre>
hiliter.css("white-space", "pre-wrap");
hiliter.css("white-space", "-moz-pre-wrap !important");
hiliter.css("white-space", "-pre-wrap");
hiliter.css("white-space", "-o-pre-wrap");
hiliter.css("word-wrap", "break-word");
textarea.before(hiliter);
textarea.css("margin-top", "-" + textarea.outerHeight() + "px");
return hiliter;
}
// TODO: Fix this so that it works.
function update_hiliter(textarea, hiliter) {
var html = textarea.val();
var sources = textarea.data("_tagmate_sources");
var tags = parse_tags(textarea, defaults.exprs, sources);
for (var i = 0; i < tags.length; i++) {
var expr = tags[i], tok = tags[i][0], term = tags[i].substr(1);
if (sources && sources[tok]) {
for (var j = 0; j < sources[tok].length; j++) {
var option = sources[tok][j];
if (option.value == term) {
expr = tok + option.label;
break;
}
}
}
// Wrap tags in highlighter span
var re = new RegExp(regex_escape(expr), "g");
var span = "<span class='" + defaults.highlight_class + "'>" + expr + "</span>";
html = html.replace(re, span);
}
hiliter.html(html);
}
return this.each(function() {
if (options)
$.extend(defaults, options);
var textarea = $(this);
// Optionally enable the hiliter
var hiliter = null;
if (defaults.highlight_tags)
hiliter = init_hiliter(textarea);
textarea.data("_tagmate_exprs", defaults.exprs);
// Initialize static lists of sources
var sources_holder = {};
for (var tok in defaults.sources)
sources_holder[tok] = [];
textarea.data("_tagmate_sources", sources_holder);
// Set up the menu
var menu = $("<div class=" + defaults.menu_class + "></div>");
textarea.after(menu);
var pos = textarea.offset();
menu.css("position", "absolute");
menu.hide();
// Activate menu and fire callbacks if cursor enters a tag
function tag_check() {
menu.hide();
// Check for user tag
var tag = parse_tag(textarea);
if (tag) {
// Make sure cursor is within token
var tok = tag[0], term = tag.substr(1);
var sel = textarea.getSelection();
var pos = prev_tok(textarea.val(), tok, sel.start);
if ((sel.start - pos) <= tag.length) {
(function(done) {
if (typeof defaults.sources[tok] === 'object')
done(Tagmate.filterOptions(defaults.sources[tok], term));
else if (typeof defaults.sources[tok] === 'function')
defaults.sources[tok]({term:term}, done);
else if (typeof defaults.sources[tok] === 'string')
$.getJSON(defaults.sources[tok], {term:term}, function(res) {
done(res.options);
});
else
done();
})(function(options) {
if (options && options.length > 0) {
// Update and show the menu
update_menu(menu, options);
menu.css("top", (textarea.outerHeight() - 1) + "px");
menu.show();
// Store for parse_tags()
var _sources = textarea.data("_tagmate_sources");
for (var i = 0; i < options.length; i++) {
var found = false;
for (var j = 0; !found && j < _sources[tok].length; j++)
found = _sources[tok][j].value == options[i].value;
if (!found)
_sources[tok].push(options[i]);
}
}
// Fire callback if available
if (tag && defaults.capture_tag)
defaults.capture_tag(tag);
});
}
}
}
var ignore_keyup = false;
// Check for tags on keyup, focus and click
$(textarea)
.unbind('.tagmate')
.bind('focus.tagmate', function(e) {
tag_check();
})
.bind('blur.tagmate', function(e) {
// blur on textarea fires before mouse menu click
setTimeout(function() { menu.hide(); }, 300);
})
.bind('click.tagmate', function(e) {
tag_check();
})
.bind('keydown.tagmate', function(e) {
if (menu.is(":visible")) {
if (e.keyCode == 40) { // down
scroll_menu(menu, "down");
ignore_keyup = true;
return false;
} else if (e.keyCode == 38) { // up
scroll_menu(menu, "up");
ignore_keyup = true;
return false;
} else if (e.keyCode == 13) { // enter
var value = menu.children("." + defaults.menu_option_active_class).text();
var tag = parse_tag(textarea);
if (tag && value) {
replace_tag(textarea, tag, value);
menu.hide();
ignore_keyup = true;
return false;
}
} else if (e.keyCode == 27) { // escape
menu.hide();
ignore_keyup = true;
return false;
}
}
})
.bind('keyup.tagmate', function(e) {
if (ignore_keyup) {
ignore_keyup = false;
return true;
}
tag_check();
if (hiliter)
update_hiliter(textarea, hiliter);
});
// Mouse menu activation
//menu.find("." + defaults.menu_option_class) // Doesn't work
$("." + defaults.menu_class + " ." + defaults.menu_option_class)
.die("click.tagmate")
.live("click.tagmate", function() {
var value = $(this).text();
var tag = parse_tag(textarea);
replace_tag(textarea, tag, value);
textarea.keyup();
});
});
}
});
})(jQuery);