-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathselection-share.js
287 lines (253 loc) · 10.9 KB
/
selection-share.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
/* global jQuery */
jQuery(document).ready(function ($) {
$("html").highlighter({ "selector": ".holder" });
$('.holder').mousedown(function () {
return false;
});
$('#copySelectionShare').click(function () {
sel = window.getSelection();
selText = sel.toString();
$("#copySelectionShare").attr('data-clipboard-text', selText + ' - ' + window.location.href);
var $el = $("#copySelectionShare"),
x = 300
$el.css("background", "#75FF55");
setTimeout(function () {
$el.removeAttr('style');
}, x);
});
$('#emailSelectionShare').click(function () {
sel = window.getSelection();
selText = sel.toString();
$("#emailSelectionShare").attr('href', 'mailto:?body=' + encodeURIComponent(selText.trim()) + '%20-%20' + window.location.protocol + "//" + window.location.host)
.click();
});
$('#twitterSelectionShare').click(function () {
sel = window.getSelection();
selText = sel.toString();
url = "";
url = window.location.protocol + "//" + window.location.host;
usr = "";
usr = "@p_wueth";
if (selText.length > (137 - (url.length + usr.length))) {
var atest = selText.split(' ');
var selText = '';
var i = 0;
var maxlength = (128 - (url.length + usr.length));
do {
selText = selText + ' ' + atest[i];
i += 1;
} while (atest.length >= i && selText.length < maxlength);
selText = selText + '...';
}
$("#twitterSelectionShare").attr('href', 'https://twitter.com/intent/tweet?text=' + encodeURIComponent(selText.trim()) + '%20'+ usr +'%20-%20' + url)
.click();
});
});
(function ($) {
/*
* Code for triple click from
* http://css-tricks.com/snippets/jquery/triple-click-event/
*/
$.event.special.tripleclick = {
setup: function (data, namespaces) {
var elem = this,
$elem = jQuery(elem);
$elem.bind('click', jQuery.event.special.tripleclick.handler);
},
teardown: function (namespaces) {
var elem = this,
$elem = jQuery(elem);
$elem.unbind('click', jQuery.event.special.tripleclick.handler);
},
handler: function (event) {
var elem = this,
$elem = jQuery(elem),
clicks = $elem.data('clicks') || 0;
clicks += 1;
if (clicks === 3) {
clicks = 0;
// set event type to "tripleclick"
event.type = "tripleclick";
// let jQuery handle the triggering of "tripleclick" event handlers
jQuery.event.dispatch.apply(this, arguments);
}
$elem.data('clicks', clicks);
}
};
/*
* Attempt to get the previous sibling
* of a container in the event of a triple
* click.
*
* Adapted from http://stackoverflow.com/a/574922
*/
function get_previoussibling(n) {
var y = n, x;
try {
x = n.previousSibling;
while (x && x.nodeType != 1) {
y = x;
x = x.previousSibling;
}
} catch (err) {
console.log(err);
topOffset = -15;
return y;
}
return x ? x : y;
}
var methods = {
init: function (options) {
var settings = $.extend({
'selector': '.highlighter-container',
'minWords': 0,
'complete': function () { }
}, options);
var numClicks = 0;
var topOffset = 0;
var leftOffset = 0;
var isDown = false;
var selText;
return this.each(function () {
/*
* Insert an html <span> after a user selects text.
* We then use the X-Y coordinates of that span
* to place our tooltip.
* Thanks to http://stackoverflow.com/a/3599599 for
* some inspiration.
*/
function insertSpanAfterSelection(clicks) {
var html = "<span class='dummy'><span>";
topOffset = 0;
leftOffset = 0;
if (numClicks !== clicks) return;
var isIE = (navigator.appName === "Microsoft Internet Explorer");
var sel, range, expandedSelRange, node;
var position;
if (window.getSelection) {
sel = window.getSelection();
selText = sel.toString();
if ($.trim(selText) === '' || selText.split(' ').length < settings.minWords) return;
if (sel.getRangeAt && sel.rangeCount) {
range = window.getSelection().getRangeAt(0);
expandedSelRange = range.cloneRange();
expandedSelRange.collapse(false);
// Range.createContextualFragment() would be useful here but is
// non-standard and not supported in all browsers (IE9, for one)
var el = document.createElement("div");
el.innerHTML = html;
var dummy = document.createElement("span");
if (range.startOffset === 0 && range.endOffset === 0) {
var cont = expandedSelRange.startContainer;
var prev = get_previoussibling(cont);
try {
expandedSelRange.selectNode(prev.lastChild);
} catch (err) {
leftOffset = 40;
topOffset = -15;
expandedSelRange.selectNode(prev);
}
// console.log(expandedSelRange);
expandedSelRange.collapse(false);
} else if (range.endOffset === 0) {
topOffset = -25;
leftOffset = 40;
}
if (numClicks !== clicks) return;
$(settings.selector).hide();
if (!isIE && $.trim(selText) === $.trim(expandedSelRange.startContainer.innerText)) {
expandedSelRange.startContainer.innerHTML += "<span class='dummy'> </span>";
position = $(".dummy").offset();
$(".dummy").remove();
} else if (!isIE && $.trim(selText) === $.trim(expandedSelRange.endContainer.innerText)) {
expandedSelRange.endContainer.innerHTML += "<span class='dummy'> </span>";
position = $(".dummy").offset();
$(".dummy").remove();
} else {
expandedSelRange.insertNode(dummy);
position = $(dummy).offset();
dummy.parentNode.removeChild(dummy);
}
}
} else if (document.selection && document.selection.createRange) {
range = document.selection.createRange();
expandedSelRange = range.duplicate();
selText = expandedSelRange.text;
if ($.trim(selText) === '' || selText.split(' ').length < settings.minWords) return;
range.collapse(false);
range.pasteHTML(html);
expandedSelRange.setEndPoint("EndToEnd", range);
expandedSelRange.select();
position = $(".dummy").offset();
$(".dummy").remove();
}
$(settings.selector).css("top", position.top + topOffset + "px");
$(settings.selector).css("left", position.left + leftOffset + "px");
$(settings.selector).show(300, function () {
settings.complete(selText);
});
}
$(settings.selector).hide();
$(settings.selector).css("position", "absolute");
$(document).bind('mouseup.highlighter', function (e) {
if (isDown) {
numClicks = 1;
clicks = 0;
setTimeout(function () {
insertSpanAfterSelection(1);
}, 300);
isDown = false;
}
});
$(this).bind('mouseup.highlighter', function (e) {
numClicks = 1;
clicks = 0;
setTimeout(function () {
insertSpanAfterSelection(1);
}, 300);
});
$(this).bind('tripleclick.highlighter', function (e) {
numClicks = 3;
setTimeout(function () {
insertSpanAfterSelection(3);
}, 200);
});
$(this).bind('dblclick.highlighter', function (e) {
numClicks = 2;
setTimeout(function () {
insertSpanAfterSelection(2);
}, 300);
});
$(this).bind('mousedown.highlighter', function (e) {
$(settings.selector).hide();
isDown = true;
});
});
},
destroy: function (content) {
return this.each(function () {
$(document).unbind('mouseup.highlighter');
$(this).unbind('mouseup.highlighter');
$(this).unbind('tripleclick.highlighter');
$(this).unbind('dblclick.highlighter');
$(this).unbind('mousedown.highlighter');
});
}
};
/*
* Method calling logic taken from the
* jQuery article on best practices for
* plugins.
*
* http://docs.jquery.com/Plugins/Authoring
*/
$.fn.highlighter = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.highlighter');
}
};
})(jQuery);