-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgplus.quick_mention.oven.js
268 lines (234 loc) · 8.53 KB
/
gplus.quick_mention.oven.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
/*
Google+ Quick Mention => Mention G+ users quickly.
Copyright (C) 2013 Jingqin Lynn
Includes jQuery
Copyright 2011, John Resig
Dual licensed under the MIT or GPL Version 2 licenses.
http://jquery.org/license
Includes Sizzle.js
http://sizzlejs.com/
Copyright 2011, The Dojo Foundation
Released under the MIT, BSD, and GPL Licenses.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
OVEN::name org.quietmusic.project.gplus.quick_mention
OVEN::display Google+ Quick Mention
OVEN::require jquery.gplus https://github.com/quietlynn/oven-gplus/raw/master/jquery.gplus.oven.js
OVEN::optional org.quietmusic.project.gplus.keyboard
*/
(function () {
'use strict';
var $ = window.jQuery;
var insertMention = function (node, offset, mentionDetails, contenteditable) {
var doc = node.ownerDocument;
var button = null;
switch (contenteditable) {
case 'plaintext-only':
button = $('<button/>').attr({
'contenteditable' : 'false',
'tabindex' : -1,
'class' : $.gplus.selectors.asClass('mentionButton'),
'oid' : mentionDetails.id,
'data-token-entity' : '@' + mentionDetails.id,
'data-sbxm' : 1
});
button.text(mentionDetails.name);
var prefix = $('<span/>')
.addClass($.gplus.selectors.asClass('mentionPrefix'));
prefix.text('+');
button.prepend(prefix);
break;
default:
button = $('<input/>').attr({
'type' : 'button',
'tabindex' : -1,
'class' : $.gplus.selectors.asClass('mentionButton'),
'oid' : mentionDetails.id,
'data-token-entity' : '@' + mentionDetails.id,
'data-sbxm' : 1,
'value' : '+' + mentionDetails.name
});
}
if (node instanceof Text) {
var p = node.parentElement;
var text = node.data || '';
node.data = ' ' + text.substr(offset).trimLeft();
p.insertBefore(button[0], node);
var beforeText = null;
if (offset > 0 || p.firstChild != node) {
var t = doc.createTextNode(text.substr(0, offset).trimRight() + ' ');
p.insertBefore(t, button[0]);
}
} else {
if (node.lastChild != null && node.lastChild.tagName === 'BR') {
node.removeChild(node.lastChild);
}
node.appendChild(button[0]);
var br = doc.createElement('br');
node.appendChild(br);
var t = doc.createTextNode(' ');
node.insertBefore(t, br);
node = t;
}
var sel = doc.defaultView.getSelection();
var r = doc.createRange();
r.setStart(node, 1);
sel.removeAllRanges();
sel.addRange(r);
};
var scrollIntoViewIfNeeded = function (j, delta) {
var win = $(j[0].ownerDocument.defaultView);
var viewTop = win.scrollTop();
var viewBottom = viewTop + win.height();
var elemTop = j.offset().top;
var elemBottom = elemTop + j.height();
if (viewTop > elemTop) {
j[0].scrollIntoView(true);
win.scrollTop(win.scrollTop() - delta);
} else if (viewBottom < elemBottom) {
j[0].scrollIntoView(false);
win.scrollTop(win.scrollTop() + delta);
}
};
var insertMentionToNewComment = function (details, newComment) {
newComment.openEditor(function (editor) {
var scrollTo = editor;
var win = editor[0].ownerDocument.defaultView;
if (win != newComment[0].ownerDocument.defaultView) {
var frame = win.frameElement;
if (frame) {
frame.focus();
scrollTo = $(frame);
}
}
scrollIntoViewIfNeeded(scrollTo, 200);
insertMention(editor[0], 0, details, editor.attr('contenteditable'));
newComment.removeData('ext-quick-mention-expanded');
var submit = newComment.find('newCommentSubmit');
var it = setInterval(function () {
editor.doKeypress();
if (submit.attr('aria-disabled') != 'false') {
clearInterval(it);
};
}, 100);
});
};
var extractMentionDetailsFrom = function (el) {
var id = el.attr('oid') || el.attr('o');
var name = el.attr('title') || el.contents().last().text() || id;
return {
id : id,
name : name
};
};
var profileLinkContextMenu = function (e) {
if (e.shiftKey) return;
e.stopPropagation();
e.preventDefault();
var el = $(e.currentTarget);
var details = extractMentionDetailsFrom(el);
var sel = window.getSelection();
var node = $.gplus.wrap(sel.anchorNode || el);
var editor = node.closest('contentEditor');
if (editor.length === 0) {
node = $.gplus.wrap(el);
editor = node.find('contentEditor');
if (editor.length === 0) {
editor = $.gplus.page().find('contentEditor').filter(function (_, ed) {
return $.gplus.wrap(ed).closest(
$.gplus.selector.combine('closedNewUpdate',
'[aria-hidden="true"]')).length === 0;
}).last();
if (editor.length > 0) node = editor;
}
}
if (editor.length > 0) {
scrollIntoViewIfNeeded(editor, 200);
var frame = editor.find('iframe');
if (frame.length > 0) {
frame[0].focus();
sel = frame[0].contentWindow.getSelection();
if (sel.anchorNode) {
node = $.gplus.wrap(sel.anchorNode);
editor = $.gplus.wrap(frame[0].contentDocument.body);
}
}
}
if (editor.length === 0) {
var newComment = node.closest('update').find('newComment');
if (newComment.length === 0) return;
insertMentionToNewComment(details, newComment);
} else {
insertMention(node[0], sel.anchorOffset, details,
editor.attr('contenteditable'));
editor.doKeypress();
}
};
var selector = '[oid], [o]';
var newCommentContextMenu = function (e) {
if (e.shiftKey) return;
e.preventDefault();
e.stopPropagation();
var newComment = $.gplus.wrap(e.currentTarget, 'newComment');
if (newComment.data('ext-quick-mention-expanded')) return;
newComment.data('ext-quick-mention-expanded', 'true');
newComment.removeAttr('event-cancel-click', 1);
var el = newComment.closest('update').find('authorInfo').find(selector);
insertMentionToNewComment(extractMentionDetailsFrom(el), newComment);
};
$.gplus.page().dynamicSelect(selector, function (profileLink) {
profileLink.bind('contextmenu', profileLinkContextMenu);
});
$.gplus.page().dynamicSelect('newComment', function (newComment) {
newComment.bind('contextmenu', newCommentContextMenu);
});
if (oven.manager.has('org.quietmusic.project.gplus.keyboard')) {
$.gplus.keyboard.registerKey('r', function () {
var update = $.gplus.page().find('activeUpdate').first();
if (update.length > 0) {
var comment = update.getActiveComment();
if (comment.length > 0) {
comment.find('authorProfileLink').contextmenu();
} else {
update.find('authorInfo').find('authorProfileLink').contextmenu();
}
} else {
return false;
}
});
} else {
var editorKeyDown = function (e) {
// Ctrl+Enter = Submit
// Shift+Enter = Submit
// (Copied from gplus.keyboard.oven.js)
if ((e.ctrlKey || e.shiftKey) && (e.keyCode === 10 || e.keyCode === 13)) {
e.preventDefault();
e.stopPropagation();
var update = $.gplus.wrap(e.currentTarget).closest(
$.gplus.selectors.combine('update', 'newUpdate')
);
if (update.length > 0) {
if (update.is('newUpdate')) {
update.find($.gplus.selectors.combine('newUpdateSubmit',
'postEditSubmit')).doClick();
} else {
update.find('newCommentSubmit').doClick();
}
}
}
};
$.gplus.page().dynamicSelect('contentEditor', function (editor) {
editor.keydown(editorKeyDown);
});
}
})();