-
Notifications
You must be signed in to change notification settings - Fork 144
/
mention.js
166 lines (150 loc) · 6.91 KB
/
mention.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
/*jslint forin: true */
;(function($) {
$.fn.extend({
mention: function(options) {
this.opts = {
users: [],
delimiter: '@',
sensitive: true,
emptyQuery: false,
queryBy: ['name', 'username'],
typeaheadOpts: {}
};
var settings = $.extend({}, this.opts, options),
_checkDependencies = function() {
if (typeof $ == 'undefined') {
throw new Error("jQuery is Required");
}
else {
if (typeof $.fn.typeahead == 'undefined') {
throw new Error("Typeahead is Required");
}
}
return true;
},
_extractCurrentQuery = function(query, caratPos) {
var i;
for (i = caratPos; i >= 0; i--) {
if (query[i] == settings.delimiter) {
break;
}
}
return query.substring(i, caratPos);
},
_matcher = function(itemProps) {
var i;
if(settings.emptyQuery){
var q = (this.query.toLowerCase()),
caratPos = this.$element[0].selectionStart,
lastChar = q.slice(caratPos-1,caratPos);
if(lastChar==settings.delimiter){
return true;
}
}
for (i in settings.queryBy) {
if (itemProps[settings.queryBy[i]]) {
var item = itemProps[settings.queryBy[i]].toLowerCase(),
usernames = (this.query.toLowerCase()).match(new RegExp(settings.delimiter + '\\w+', "g")),
j;
if ( !! usernames) {
for (j = 0; j < usernames.length; j++) {
var username = (usernames[j].substring(1)).toLowerCase(),
re = new RegExp(settings.delimiter + item, "g"),
used = ((this.query.toLowerCase()).match(re));
if (item.indexOf(username) != -1 && used === null) {
return true;
}
}
}
}
}
},
_updater = function(item) {
var data = this.query,
caratPos = this.$element[0].selectionStart,
i;
for (i = caratPos; i >= 0; i--) {
if (data[i] == settings.delimiter) {
break;
}
}
var replace = data.substring(i, caratPos),
textBefore = data.substring(0, i),
textAfter = data.substring(caratPos),
data = textBefore + settings.delimiter + item + textAfter;
this.tempQuery = data;
return data;
},
_sorter = function(items) {
if (items.length && settings.sensitive) {
var currentUser = _extractCurrentQuery(this.query, this.$element[0].selectionStart).substring(1),
i, len = items.length,
priorities = {
highest: [],
high: [],
med: [],
low: []
}, finals = [];
if (currentUser.length == 1) {
for (i = 0; i < len; i++) {
var currentRes = items[i];
if ((currentRes.username[0] == currentUser)) {
priorities.highest.push(currentRes);
}
else if ((currentRes.username[0].toLowerCase() == currentUser.toLowerCase())) {
priorities.high.push(currentRes);
}
else if (currentRes.username.indexOf(currentUser) != -1) {
priorities.med.push(currentRes);
}
else {
priorities.low.push(currentRes);
}
}
for (i in priorities) {
var j;
for (j in priorities[i]) {
finals.push(priorities[i][j]);
}
}
return finals;
}
}
return items;
},
_render = function(items) {
var that = this;
items = $(items).map(function(i, item) {
i = $(that.options.item).attr('data-value', item.username);
var _linkHtml = $('<div />');
if (item.image) {
_linkHtml.append('<img class="mention_image" src="' + item.image + '">');
}
if (item.name) {
_linkHtml.append('<b class="mention_name">' + item.name + '</b>');
}
if (item.username) {
_linkHtml.append('<span class="mention_username"> ' + settings.delimiter + item.username + '</span>');
}
i.find('a').html(that.highlighter(_linkHtml.html()));
return i[0];
});
items.first().addClass('active');
this.$menu.html(items);
return this;
};
$.fn.typeahead.Constructor.prototype.render = _render;
return this.each(function() {
var _this = $(this);
if (_checkDependencies()) {
_this.typeahead($.extend({
source: settings.users,
matcher: _matcher,
updater: _updater,
sorter: _sorter
}, settings.typeaheadOpts));
}
});
}
});
})(jQuery);