-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.js
90 lines (82 loc) · 2.42 KB
/
interface.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
function createQuoteLink() {
var button = $(document.createElement('button'))
.addClass('update-info')
.addClass('os-quote-button')
.addClass('quote-post')
.text('Quote')
.click(makeQuote);
var li = $(document.createElement('li'))
.addClass('update-info')
.append(button);
return li;
}
function createCopyLink() {
var button = $(document.createElement('button'))
.addClass('update-info')
.addClass('os-copy-button')
.addClass('copy-post')
.text('Copy')
.click(makeCopy);
var li = $(document.createElement('li'))
.addClass('update-info')
.append(button);
return li;
}
function makeQuote() {
var post = $(this).parents('h2.post');
if (post.length == 0) {
post = $(this).parents('li.post.reply');
}
insertQuote(post.OpenStudyQuoteText());
}
function makeCopy() {
var post = $(this).parents('h2.post');
if (post.length == 0) {
post = $(this).parents('li.post.reply');
}
insertCopy(post.OpenStudyCopyText());
}
function insertQuote(body) {
$('textarea#reply-body').insertAtCaret(body).fireEvent('keyup');
}
function insertCopy(body) {
$('textarea#reply-body').insertAtCaret(body).fireEvent('keyup');
}
$.fn.extend({
insertAtCaret: function(myValue){
return this.each(function(i) {
if (document.selection) {
//For browsers like Internet Explorer
this.focus();
sel = document.selection.createRange();
sel.text = myValue;
this.focus();
} else if (this.selectionStart || this.selectionStart == '0') {
//For browsers like Firefox and Webkit based
var startPos = this.selectionStart;
var endPos = this.selectionEnd;
var scrollTop = this.scrollTop;
this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
this.focus();
this.selectionStart = startPos + myValue.length;
this.selectionEnd = startPos + myValue.length;
this.scrollTop = scrollTop;
} else {
this.value += myValue;
this.focus();
}
});
},
fireEvent: function (event) {
return this.each(function(i) {
if (document.createEventObject) {
var evt = document.createEventObject();
return this.fireEvent("on" + event, evt);
} else {
var evt = document.createEvent("HTMLEvents");
evt.initEvent(event, true, true);
return !this.dispatchEvent(evt);
}
});
}
});