/
jquery.atwho.js
707 lines (642 loc) · 21.4 KB
/
jquery.atwho.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/*
Implement Github like autocomplete mentions
http://ichord.github.com/At.js
Copyright (c) 2013 chord.luo@gmail.com
Licensed under the MIT license.
*/
(function() {
(function(factory) {
if (typeof define === 'function' && define.amd) {
return define(['jquery'], factory);
} else {
return factory(window.jQuery);
}
})(function($) {
var Controller, DEFAULT_CALLBACKS, DEFAULT_TPL, KEY_CODE, Mirror, View;
Mirror = (function() {
Mirror.prototype.css_attr = ["overflowY", "height", "width", "paddingTop", "paddingLeft", "paddingRight", "paddingBottom", "marginTop", "marginLeft", "marginRight", "marginBottom", "fontFamily", "borderStyle", "borderWidth", "wordWrap", "fontSize", "lineHeight", "overflowX", "text-align"];
function Mirror($inputor) {
this.$inputor = $inputor;
}
Mirror.prototype.copy_inputor_css = function() {
var css,
_this = this;
css = {
position: 'absolute',
left: -9999,
top: 0,
zIndex: -20000,
'white-space': 'pre-wrap'
};
$.each(this.css_attr, function(i, p) {
return css[p] = _this.$inputor.css(p);
});
return css;
};
Mirror.prototype.create = function(html) {
this.$mirror = $('<div></div>');
this.$mirror.css(this.copy_inputor_css());
this.$mirror.html(html);
this.$inputor.after(this.$mirror);
return this;
};
Mirror.prototype.get_flag_rect = function() {
var $flag, pos, rect;
$flag = this.$mirror.find("span#flag");
pos = $flag.position();
rect = {
left: pos.left,
top: pos.top,
bottom: $flag.height() + pos.top
};
this.$mirror.remove();
return rect;
};
return Mirror;
})();
KEY_CODE = {
DOWN: 40,
UP: 38,
ESC: 27,
TAB: 9,
ENTER: 13
};
DEFAULT_CALLBACKS = {
data_refactor: function(data) {
if (!$.isArray(data)) {
return data;
}
return $.map(data, function(item, k) {
if (!$.isPlainObject(item)) {
item = {
name: item
};
}
return item;
});
},
matcher: function(flag, subtext) {
var match, matched, regexp;
regexp = new RegExp(flag + '([A-Za-z0-9_\+\-]*)$|' + flag + '([^\\x00-\\xff]*)$', 'gi');
match = regexp.exec(subtext);
matched = null;
if (match) {
matched = match[2] ? match[2] : match[1];
}
return matched;
},
filter: function(query, data, search_key) {
var _this = this;
return $.map(data, function(item, i) {
var name;
name = $.isPlainObject(item) ? item[search_key] : item;
if (name.toLowerCase().indexOf(query) >= 0) {
return item;
}
});
},
remote_filter: function(params, url, render_view) {
return $.ajax(url, {
data: params,
success: function(data) {
return render_view(data);
}
});
},
sorter: function(query, items, search_key) {
var item, results, text, _i, _len;
if (!query) {
return items.sort(function(a, b) {
if (a[search_key].toLowerCase() > b[search_key].toLowerCase()) {
return 1;
} else {
return -1;
}
});
}
results = [];
for (_i = 0, _len = items.length; _i < _len; _i++) {
item = items[_i];
text = item[search_key];
item.order = text.toLowerCase().indexOf(query);
results.push(item);
}
return results.sort(function(a, b) {
return a.order - b.order;
});
},
tpl_eval: function(tpl, map) {
var el;
try {
return el = tpl.replace(/\$\{([^\}]*)\}/g, function(tag, key, pos) {
return map[key];
});
} catch (error) {
return "";
}
},
highlighter: function(li, query) {
if (!query) {
return li;
}
return li.replace(new RegExp(">\\s*(\\w*)(" + query.replace("+", "\\+") + ")(\\w*)\\s*<", 'ig'), function(str, $1, $2, $3) {
return '> ' + $1 + '<strong>' + $2 + '</strong>' + $3 + ' <';
});
},
selector: function($li) {
if ($li.length > 0) {
return this.replace_str($li.data("value") || "");
}
}
};
Controller = (function() {
function Controller(inputor) {
this.settings = {};
this.common_settings = {};
this.pos = 0;
this.flags = null;
this.current_flag = null;
this.query = null;
this.$inputor = $(inputor);
this.mirror = new Mirror(this.$inputor);
this.common_settings = $.extend({}, $.fn.atwho["default"]);
this.view = new View(this, this.$el);
this.listen();
}
Controller.prototype.listen = function() {
var _this = this;
return this.$inputor.on('keyup.atwho', function(e) {
return _this.on_keyup(e);
}).on('keydown.atwho', function(e) {
return _this.on_keydown(e);
}).on('scroll.atwho', function(e) {
return _this.view.hide();
}).on('blur.atwho', function(e) {
return _this.view.hide(_this.get_opt("display_timeout"));
});
};
Controller.prototype.reg = function(flag, settings) {
var current_settings, data;
current_settings = {};
current_settings = $.isPlainObject(flag) ? this.common_settings = $.extend({}, this.common_settings, flag) : !this.settings[flag] ? this.settings[flag] = $.extend({}, settings) : this.settings[flag] = $.extend({}, this.settings[flag], settings);
data = current_settings["data"];
current_settings["data"] = this.callbacks("data_refactor").call(this, data);
return this;
};
Controller.prototype.trigger = function(name, data) {
data || (data = []);
data.push(this);
return this.$inputor.trigger("" + name + ".atwho", data);
};
Controller.prototype.data = function() {
return this.get_opt("data");
};
Controller.prototype.callbacks = function(func_name) {
var func;
if (!(func = this.get_opt("callbacks", {})[func_name])) {
func = this.common_settings["callbacks"][func_name];
}
return func;
};
Controller.prototype.get_opt = function(key, default_value) {
var value;
try {
if (this.current_flag) {
value = this.settings[this.current_flag][key];
}
if (value === void 0) {
value = this.common_settings[key];
}
return value = value === void 0 ? default_value : value;
} catch (e) {
return value = default_value === void 0 ? null : default_value;
}
};
Controller.prototype.rect = function() {
var $inputor, Sel, at_rect, bottom, format, html, offset, start_range, x, y;
$inputor = this.$inputor;
if (document.selection) {
Sel = document.selection.createRange();
x = Sel.boundingLeft + $inputor.scrollLeft();
y = Sel.boundingTop + $(window).scrollTop() + $inputor.scrollTop();
bottom = y + Sel.boundingHeight;
return {
top: y - 2,
left: x - 2,
bottom: bottom - 2
};
}
format = function(value) {
return value.replace(/</g, '<').replace(/>/g, '>').replace(/`/g, '`').replace(/"/g, '"').replace(/\r\n|\r|\n/g, "<br />");
};
/* 克隆完inputor后将原来的文本内容根据
@的位置进行分块,以获取@块在inputor(输入框)里的position
*/
start_range = $inputor.val().slice(0, this.pos - 1);
html = "<span>" + format(start_range) + "</span>";
html += "<span id='flag'>?</span>";
/*
将inputor的 offset(相对于document)
和@在inputor里的position相加
就得到了@相对于document的offset.
当然,还要加上行高和滚动条的偏移量.
*/
offset = $inputor.offset();
at_rect = this.mirror.create(html).get_flag_rect();
x = offset.left + at_rect.left - $inputor.scrollLeft();
y = offset.top - $inputor.scrollTop();
bottom = y + at_rect.bottom;
y += at_rect.top;
return {
top: y,
left: x,
bottom: bottom + 2
};
};
Controller.prototype.catch_query = function() {
var caret_pos, content, end, query, start, subtext,
_this = this;
content = this.$inputor.val();
caret_pos = this.$inputor.caretPos();
/* 向在插入符前的的文本进行正则匹配
* 考虑会有多个 @ 的存在, 匹配离插入符最近的一个
*/
subtext = content.slice(0, caret_pos);
query = null;
$.each(this.settings, function(flag, settings) {
query = _this.callbacks("matcher").call(_this, flag, subtext);
if (query != null) {
_this.current_flag = flag;
return false;
}
});
if (typeof query === "string" && query.length <= 20) {
start = caret_pos - query.length;
end = start + query.length;
this.pos = start;
query = {
'text': query.toLowerCase(),
'head_pos': start,
'end_pos': end
};
this.trigger("matched", [this.current_flag, query.text]);
} else {
this.view.hide();
}
return this.query = query;
};
Controller.prototype.replace_str = function(str) {
var $inputor, flag_len, source, start_str, text;
$inputor = this.$inputor;
source = $inputor.val();
flag_len = this.get_opt("display_flag") ? 0 : this.current_flag.length;
start_str = source.slice(0, (this.query['head_pos'] || 0) - flag_len);
text = "" + start_str + str + " " + (source.slice(this.query['end_pos'] || 0));
$inputor.val(text);
$inputor.caretPos(start_str.length + str.length + 1);
return $inputor.change();
};
Controller.prototype.on_keyup = function(e) {
switch (e.keyCode) {
case KEY_CODE.ESC:
e.preventDefault();
this.view.hide();
break;
case KEY_CODE.DOWN:
case KEY_CODE.UP:
$.noop();
break;
default:
this.look_up();
}
return e.stopPropagation();
};
Controller.prototype.on_keydown = function(e) {
if (!this.view.visible()) {
return;
}
switch (e.keyCode) {
case KEY_CODE.ESC:
e.preventDefault();
this.view.hide();
break;
case KEY_CODE.UP:
e.preventDefault();
this.view.prev();
break;
case KEY_CODE.DOWN:
e.preventDefault();
this.view.next();
break;
case KEY_CODE.TAB:
case KEY_CODE.ENTER:
if (!this.view.visible()) {
return;
}
e.preventDefault();
this.view.choose();
break;
default:
$.noop();
}
return e.stopPropagation();
};
Controller.prototype.render_view = function(data) {
var search_key;
search_key = this.get_opt("search_key");
data = this.callbacks("sorter").call(this, this.query.text, data, search_key);
data = data.splice(0, this.get_opt('limit'));
return this.view.render(data);
};
Controller.prototype.remote_call = function(data, query) {
var params, _callback;
params = {
q: query.text,
limit: this.get_opt("limit")
};
_callback = function(data) {
this.reg(this.current_flag, {
data: data
});
return this.render_view(this.data());
};
_callback = $.proxy(_callback, this);
return this.callbacks('remote_filter').call(this, params, data, _callback);
};
Controller.prototype.look_up = function() {
var data, query, search_key;
query = this.catch_query();
if (!query) {
return false;
}
data = this.data();
search_key = this.get_opt("search_key");
if (typeof data === "string") {
this.remote_call(data, query);
} else if ((data = this.callbacks('filter').call(this, query.text, data, search_key))) {
this.render_view(data);
} else {
this.view.hide();
}
return $.noop();
};
return Controller;
})();
View = (function() {
function View(controller) {
this.controller = controller;
this.id = this.controller.get_opt("view_id", "at-view");
this.timeout_id = null;
this.$el = $("#" + this.id);
this.create_view();
}
View.prototype.create_view = function() {
var $menu, tpl,
_this = this;
if (this.exist()) {
return;
}
tpl = "<div id='" + this.id + "' class='at-view'><ul id='" + this.id + "-ul'></ul></div>";
$("body").append(tpl);
this.$el = $("#" + this.id);
$menu = this.$el.find('ul');
return $menu.on('mouseenter.view', 'li', function(e) {
$menu.find('.cur').removeClass('cur');
return $(e.currentTarget).addClass('cur');
}).on('click', function(e) {
e.stopPropagation();
e.preventDefault();
return _this.$el.data("_view").choose();
});
};
View.prototype.exist = function() {
return $("#" + this.id).length > 0;
};
View.prototype.visible = function() {
return this.$el.is(":visible");
};
View.prototype.choose = function() {
var $li;
$li = this.$el.find(".cur");
this.controller.callbacks("selector").call(this.controller, $li);
this.controller.trigger("choose", [$li]);
return this.hide();
};
View.prototype.reposition = function() {
var offset, rect;
rect = this.controller.rect();
if (rect.bottom + this.$el.height() - $(window).scrollTop() > $(window).height()) {
rect.bottom = rect.top - this.$el.height();
}
offset = {
left: rect.left,
top: rect.bottom
};
this.$el.offset(offset);
return this.controller.trigger("reposition", [offset]);
};
View.prototype.next = function() {
var cur, next;
cur = this.$el.find('.cur').removeClass('cur');
next = cur.next();
if (!next.length) {
next = $(this.$el.find('li')[0]);
}
return next.addClass('cur');
};
View.prototype.prev = function() {
var cur, prev;
cur = this.$el.find('.cur').removeClass('cur');
prev = cur.prev();
if (!prev.length) {
prev = this.$el.find('li').last();
}
return prev.addClass('cur');
};
View.prototype.show = function() {
if (!this.visible()) {
this.$el.show();
}
return this.reposition();
};
View.prototype.hide = function(time) {
var callback,
_this = this;
if (isNaN(time)) {
if (this.visible()) {
return this.$el.hide();
}
} else {
callback = function() {
return _this.hide();
};
clearTimeout(this.timeout_id);
return this.timeout_id = setTimeout(callback, time);
}
};
View.prototype.clear = function() {
return this.$el.find('ul').empty();
};
View.prototype.render = function(list) {
var $ul, tpl,
_this = this;
if (!$.isArray(list)) {
return false;
}
if (list.length <= 0) {
this.hide();
return true;
}
this.clear();
this.$el.data("_view", this);
$ul = this.$el.find('ul');
tpl = this.controller.get_opt('tpl', DEFAULT_TPL);
$.each(list, function(i, item) {
var $li, li;
li = _this.controller.callbacks("tpl_eval").call(_this.controller, tpl, item);
$li = $(_this.controller.callbacks("highlighter").call(_this.controller, li, _this.controller.query.text));
$li.data("info", item);
return $ul.append($li);
});
this.show();
return $ul.find("li:eq(0)").addClass("cur");
};
return View;
})();
DEFAULT_TPL = "<li data-value='${name}'>${name}</li>";
$.fn.atwho = function(flag, options) {
return this.filter('textarea, input').each(function() {
var $this, data;
$this = $(this);
data = $this.data("atwho");
if (!data) {
$this.data('atwho', (data = new Controller(this)));
}
return data.reg(flag, options);
});
};
$.fn.atwho.Controller = Controller;
$.fn.atwho.View = View;
$.fn.atwho.Mirror = Mirror;
return $.fn.atwho["default"] = {
data: null,
search_key: "name",
callbacks: DEFAULT_CALLBACKS,
limit: 5,
display_flag: true,
display_timeout: 300,
tpl: DEFAULT_TPL
};
});
}).call(this);
/*
Implement Github like autocomplete mentions
http://ichord.github.com/At.js
Copyright (c) 2013 chord.luo@gmail.com
Licensed under the MIT license.
*/
/*
本插件操作 textarea 或者 input 内的插入符
只实现了获得插入符在文本框中的位置,我设置
插入符的位置.
*/
(function() {
(function(factory) {
if (typeof exports === 'object') {
return factory(require('jquery'));
} else if (typeof define === 'function' && define.amd) {
return define(['jquery']);
} else {
return factory(window.jQuery);
}
})(function($) {
var getCaretPos, setCaretPos;
getCaretPos = function(inputor) {
var end, endRange, len, normalizedValue, pos, range, start, textInputRange;
if (document.selection) {
/*
#assume we select "HATE" in the inputor such as textarea -> { }.
* start end-point.
* /
* < I really [HATE] IE > between the brackets is the selection range.
* \
* end end-point.
*/
range = document.selection.createRange();
pos = 0;
if (range && range.parentElement() === inputor) {
normalizedValue = inputor.value.replace(/\r\n/g, "\n");
/* SOMETIME !!!
"/r/n" is counted as two char.
one line is two, two will be four. balalala.
so we have to using the normalized one's length.;
*/
len = normalizedValue.length;
/*
<[ I really HATE IE ]>:
the whole content in the inputor will be the textInputRange.
*/
textInputRange = inputor.createTextRange();
/* _here must be the position of bookmark.
/
<[ I really [HATE] IE ]>
[---------->[ ] : this is what moveToBookmark do.
< I really [[HATE] IE ]> : here is result.
\ two brackets in should be in line.
*/
textInputRange.moveToBookmark(range.getBookmark());
endRange = inputor.createTextRange();
/* [--------------------->[] : if set false all end-point goto end.
< I really [[HATE] IE []]>
*/
endRange.collapse(false);
/*
___VS____
/ \
< I really [[HATE] IE []]>
\_endRange end-point.
" > -1" mean the start end-point will be the same or right to the end end-point
* simplelly, all in the end.
*/
if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
start = end = len;
} else {
/*
I really |HATE] IE ]>
<-|
I really[ [HATE] IE ]>
<-[
I reall[y [HATE] IE ]>
will return how many unit have moved.
*/
start = -textInputRange.moveStart("character", -len);
end = -textInputRange.moveEnd("character", -len);
}
}
} else {
start = inputor.selectionStart;
}
return start;
};
setCaretPos = function(inputor, pos) {
var range;
if (document.selection) {
range = inputor.createTextRange();
range.move("character", pos);
return range.select();
} else {
return inputor.setSelectionRange(pos, pos);
}
};
return $.fn.caretPos = function(pos) {
var inputor;
inputor = this[0];
inputor.focus();
if (pos) {
return setCaretPos(inputor, pos);
} else {
return getCaretPos(inputor);
}
};
});
}).call(this);