##// END OF EJS Templates
Hide quote button when its click. Cache its selector
neko259 -
r2042:8f0c2e52 default
parent child Browse files
Show More
@@ -1,142 +1,143
1 /*
1 /*
2 @licstart The following is the entire license notice for the
2 @licstart The following is the entire license notice for the
3 JavaScript code in this page.
3 JavaScript code in this page.
4
4
5
5
6 Copyright (C) 2013 neko259
6 Copyright (C) 2013 neko259
7
7
8 The JavaScript code in this page is free software: you can
8 The JavaScript code in this page is free software: you can
9 redistribute it and/or modify it under the terms of the GNU
9 redistribute it and/or modify it under the terms of the GNU
10 General Public License (GNU GPL) as published by the Free Software
10 General Public License (GNU GPL) as published by the Free Software
11 Foundation, either version 3 of the License, or (at your option)
11 Foundation, either version 3 of the License, or (at your option)
12 any later version. The code is distributed WITHOUT ANY WARRANTY;
12 any later version. The code is distributed WITHOUT ANY WARRANTY;
13 without even the implied warranty of MERCHANTABILITY or FITNESS
13 without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
15
15
16 As additional permission under GNU GPL version 3 section 7, you
16 As additional permission under GNU GPL version 3 section 7, you
17 may distribute non-source (e.g., minimized or compacted) forms of
17 may distribute non-source (e.g., minimized or compacted) forms of
18 that code without the copy of the GNU GPL normally required by
18 that code without the copy of the GNU GPL normally required by
19 section 4, provided you include this license notice and a URL
19 section 4, provided you include this license notice and a URL
20 through which recipients can access the Corresponding Source.
20 through which recipients can access the Corresponding Source.
21
21
22 @licend The above is the entire license notice
22 @licend The above is the entire license notice
23 for the JavaScript code in this page.
23 for the JavaScript code in this page.
24 */
24 */
25
25
26 var REPLY_TO_MSG = '.reply-to-message';
26 var REPLY_TO_MSG = '.reply-to-message';
27 var REPLY_TO_MSG_ID = '#reply-to-message-id';
27 var REPLY_TO_MSG_ID = '#reply-to-message-id';
28
28
29 var $html = $("html, body");
29 var $html = $("html, body");
30 var $quoteButton = $("#quote-button");
30
31
31 function moveCaretToEnd(el) {
32 function moveCaretToEnd(el) {
32 var newPos = el.val().length;
33 var newPos = el.val().length;
33 el[0].setSelectionRange(newPos, newPos);
34 el[0].setSelectionRange(newPos, newPos);
34 }
35 }
35
36
36
37
37 function showFormAfter(blockToInsertAfter) {
38 function showFormAfter(blockToInsertAfter) {
38 var form = getForm();
39 var form = getForm();
39 form.insertAfter(blockToInsertAfter);
40 form.insertAfter(blockToInsertAfter);
40
41
41 form.show();
42 form.show();
42 $(REPLY_TO_MSG_ID).text(blockToInsertAfter.attr('id'));
43 $(REPLY_TO_MSG_ID).text(blockToInsertAfter.attr('id'));
43 $(REPLY_TO_MSG).show();
44 $(REPLY_TO_MSG).show();
44 }
45 }
45
46
46 function addQuickReply(postId) {
47 function addQuickReply(postId) {
47 var blockToInsert = null;
48 var blockToInsert = null;
48 var textAreaJq = getPostTextarea();
49 var textAreaJq = getPostTextarea();
49 var postLinkRaw = '[post]' + postId + '[/post]'
50 var postLinkRaw = '[post]' + postId + '[/post]'
50 var textToAdd = '';
51 var textToAdd = '';
51
52
52 if (postId != null) {
53 if (postId != null) {
53 var post = $('#' + postId);
54 var post = $('#' + postId);
54
55
55 // If this is not OP, add reflink to the post. If there already is
56 // If this is not OP, add reflink to the post. If there already is
56 // the same reflink, don't add it again.
57 // the same reflink, don't add it again.
57 var postText = textAreaJq.val();
58 var postText = textAreaJq.val();
58 if (!post.is(':first-child') && postText.indexOf(postLinkRaw) < 0) {
59 if (!post.is(':first-child') && postText.indexOf(postLinkRaw) < 0) {
59 // Insert line break if none is present.
60 // Insert line break if none is present.
60 if (postText.length > 0 && !postText.endsWith('\n') && !postText.endsWith('\r')) {
61 if (postText.length > 0 && !postText.endsWith('\n') && !postText.endsWith('\r')) {
61 textToAdd += '\n';
62 textToAdd += '\n';
62 }
63 }
63 textToAdd += postLinkRaw + '\n';
64 textToAdd += postLinkRaw + '\n';
64 }
65 }
65
66
66 textAreaJq.val(textAreaJq.val()+ textToAdd);
67 textAreaJq.val(textAreaJq.val()+ textToAdd);
67 blockToInsert = post;
68 blockToInsert = post;
68 } else {
69 } else {
69 blockToInsert = $('.thread');
70 blockToInsert = $('.thread');
70 }
71 }
71 showFormAfter(blockToInsert);
72 showFormAfter(blockToInsert);
72
73
73 textAreaJq.focus();
74 textAreaJq.focus();
74
75
75 moveCaretToEnd(textAreaJq);
76 moveCaretToEnd(textAreaJq);
76 }
77 }
77
78
78 function addQuickQuote() {
79 function addQuickQuote() {
79 var textAreaJq = getPostTextarea();
80 var textAreaJq = getPostTextarea();
80
81
81 var quoteButton = $("#quote-button");
82 var postId = $quoteButton.attr('data-post-id');
82 var postId = quoteButton.attr('data-post-id');
83 if (postId != null) {
83 if (postId != null) {
84 addQuickReply(postId);
84 addQuickReply(postId);
85 }
85 }
86
86
87 var textToAdd = '';
87 var textToAdd = '';
88 var selection = window.getSelection().toString();
88 var selection = window.getSelection().toString();
89 if (selection.length == 0) {
89 if (selection.length == 0) {
90 selection = quoteButton.attr('data-text');
90 selection = $quoteButton.attr('data-text');
91 }
91 }
92 if (selection.length > 0) {
92 if (selection.length > 0) {
93 textToAdd += '[quote]' + selection + '[/quote]\n';
93 textToAdd += '[quote]' + selection + '[/quote]\n';
94 }
94 }
95
95
96 textAreaJq.val(textAreaJq.val() + textToAdd);
96 textAreaJq.val(textAreaJq.val() + textToAdd);
97
97
98 textAreaJq.focus();
98 textAreaJq.focus();
99
99
100 moveCaretToEnd(textAreaJq);
100 moveCaretToEnd(textAreaJq);
101
102 $quoteButton.hide();
101 }
103 }
102
104
103 function scrollToBottom() {
105 function scrollToBottom() {
104 $html.animate({scrollTop: $html.height()}, "fast");
106 $html.animate({scrollTop: $html.height()}, "fast");
105 }
107 }
106
108
107 function showQuoteButton() {
109 function showQuoteButton() {
108 var selection = window.getSelection().getRangeAt(0).getBoundingClientRect();
110 var selection = window.getSelection().getRangeAt(0).getBoundingClientRect();
109 var quoteButton = $("#quote-button");
110 if (selection.width > 0) {
111 if (selection.width > 0) {
111 // quoteButton.offset({ top: selection.top - selection.height, left: selection.left });
112 // quoteButton.offset({ top: selection.top - selection.height, left: selection.left });
112 quoteButton.css({top: selection.top + $(window).scrollTop() - 30, left: selection.left});
113 $quoteButton.css({top: selection.top + $(window).scrollTop() - 30, left: selection.left});
113 quoteButton.show();
114 $quoteButton.show();
114
115
115 var text = window.getSelection().toString();
116 var text = window.getSelection().toString();
116 quoteButton.attr('data-text', text);
117 $quoteButton.attr('data-text', text);
117
118
118 var rect = window.getSelection().getRangeAt(0).getBoundingClientRect();
119 var rect = window.getSelection().getRangeAt(0).getBoundingClientRect();
119 var element = $(document.elementFromPoint(rect.x, rect.y));
120 var element = $(document.elementFromPoint(rect.x, rect.y));
120 var postId = null;
121 var postId = null;
121 if (element.hasClass('post')) {
122 if (element.hasClass('post')) {
122 postId = element.attr('id');
123 postId = element.attr('id');
123 } else {
124 } else {
124 var postParent = element.parents('.post');
125 var postParent = element.parents('.post');
125 if (postParent.length > 0) {
126 if (postParent.length > 0) {
126 postId = postParent.attr('id');
127 postId = postParent.attr('id');
127 }
128 }
128 }
129 }
129 quoteButton.attr('data-post-id', postId);
130 $quoteButton.attr('data-post-id', postId);
130 } else {
131 } else {
131 quoteButton.hide();
132 $quoteButton.hide();
132 }
133 }
133 }
134 }
134
135
135 $(document).ready(function() {
136 $(document).ready(function() {
136 $('body').on('mouseup', function() {
137 $('body').on('mouseup', function() {
137 showQuoteButton();
138 showQuoteButton();
138 });
139 });
139 $("#quote-button").click(function() {
140 $("#quote-button").click(function() {
140 addQuickQuote();
141 addQuickQuote();
141 })
142 })
142 });
143 });
General Comments 0
You need to be logged in to leave comments. Login now