##// END OF EJS Templates
Refactoring of the scripts accessing textarea
neko259 -
r1769:30b6f53c default
parent child Browse files
Show More
@@ -1,190 +1,194 b''
1 1 var ITEM_FILE_SOURCE = 'fileSource';
2 2 var URL_STICKERS = '/api/stickers';
3 3 var MIN_INPUT_LENGTH = 3;
4 4 var URL_DELIMITER = '\n';
5 5
6 6 $('input[name=image]').wrap($('<div class="file_wrap"></div>'));
7 7
8 8 $('body').on('change', 'input[name=image]', function(event) {
9 9 var file = event.target.files[0];
10 10
11 11 if(file.type.match('image.*')) {
12 12 var fileReader = new FileReader();
13 13
14 14 fileReader.addEventListener("load", function(event) {
15 15 var wrapper = $('.file_wrap');
16 16
17 17 wrapper.find('.file-thumb').remove();
18 18 wrapper.append(
19 19 $('<div class="file-thumb" style="background-image: url('+event.target.result+')"></div>')
20 20 );
21 21 });
22 22
23 23 fileReader.readAsDataURL(file);
24 24 }
25 25 });
26 26
27 27 var form = $('#form');
28 28 $('textarea').keypress(function(event) {
29 29 if ((event.which == 10 || event.which == 13) && event.ctrlKey) {
30 30 form.find('input[type=submit]').click();
31 31 }
32 32 });
33 33
34 34 $('#preview-button').click(function() {
35 35 var data = {
36 36 raw_text: $('textarea#id_text').val()
37 37 }
38 38
39 39 var diffUrl = '/api/preview/';
40 40
41 41 $.post(diffUrl,
42 42 data,
43 43 function(data) {
44 44 var previewTextBlock = $('#preview-text');
45 45 previewTextBlock.html(data);
46 46 previewTextBlock.show();
47 47
48 48 addScriptsToPost(previewTextBlock);
49 49 })
50 50 });
51 51
52 52 /**
53 53 * Show text in the errors row of the form.
54 54 * @param form
55 55 * @param text
56 56 */
57 57 function showAsErrors(form, text) {
58 58 form.children('.form-errors').remove();
59 59
60 60 if (text.length > 0) {
61 61 var errorList = $('<div class="form-errors">' + text + '<div>');
62 62 errorList.appendTo(form);
63 63 }
64 64 }
65 65
66 66 function addHiddenInput(form, name, value) {
67 67 form.find('input[name=' + name + ']').val(value);
68 68 }
69 69
70 70 function selectFileChoice() {
71 71 var file_input = $('#id_file');
72 72 var url_input = $('#id_file_url');
73 73
74 74 var file_input_row = file_input.parent().parent();
75 75 var url_input_row = url_input.parent().parent();
76 76
77 77 file_input_row.toggle();
78 78 url_input_row.toggle();
79 79 url_input.val('');
80 80 file_input.val('');
81 81
82 82 var source;
83 83 if (file_input_row.is(':visible')) {
84 84 source = 'file';
85 85 } else {
86 86 source = 'url';
87 87 }
88 88 localStorage.setItem(ITEM_FILE_SOURCE, source);
89 89 }
90 90
91 function getPostTextarea() {
92 return $('textarea#id_text');
93 }
94
91 95 $(document).ready(function() {
92 96 var powDifficulty = parseInt($('body').attr('data-pow-difficulty'));
93 97 if (powDifficulty > 0 && typeof SharedWorker != 'undefined') {
94 98 var worker = new SharedWorker($('.post-form').attr('data-pow-script'));
95 99 worker.port.onmessage = function(e) {
96 100 var form = $('#form');
97 101 addHiddenInput(form, 'timestamp', e.data.timestamp);
98 102 addHiddenInput(form, 'iteration', e.data.iteration);
99 103 addHiddenInput(form, 'guess', e.data.guess);
100 104
101 105 form.submit();
102 106 $('.post-form-w').unblock();
103 107 };
104 108 worker.onerror = function(event){
105 109 throw new Error(event.message + " (" + event.filename + ":" + event.lineno + ")");
106 110 };
107 111 worker.port.start();
108 112
109 113 var form = $('#form');
110 114 var submitButton = form.find('input[type=submit]');
111 115 submitButton.click(function() {
112 116 showAsErrors(form, gettext('Computing PoW...'));
113 117 $('.post-form-w').block({ message: gettext('Computing PoW...') })
114 118
115 119 var msg = $('textarea#id_text').val().trim();
116 120
117 121 var data = {
118 122 msg: msg,
119 123 difficulty: parseInt($('body').attr('data-pow-difficulty')),
120 124 hasher: $('.post-form').attr('data-hasher')
121 125 };
122 126 worker.port.postMessage(data);
123 127
124 128 return false;
125 129 });
126 130 }
127 131
128 132 var $fileSourceButton = $('#file-source-button');
129 133 if (window.localStorage) {
130 134 var source = localStorage.getItem(ITEM_FILE_SOURCE);
131 135 if (source == null) {
132 136 source = 'file';
133 137 }
134 138 if (source == 'file') {
135 139 $('#id_file_url').parent().parent().hide();
136 140 } else {
137 141 $('#id_file').parent().parent().hide();
138 142 }
139 143
140 144 $fileSourceButton.click(function() {
141 145 selectFileChoice();
142 146 });
143 147 } else {
144 148 $fileSourceButton.hide();
145 149 }
146 150
147 151 // Stickers autocomplete
148 152 function split( val ) {
149 153 return val.split(URL_DELIMITER);
150 154 }
151 155
152 156 function extractLast( term ) {
153 157 return split(term).pop();
154 158 }
155 159
156 160 $('#id_file_1').autocomplete({
157 161 source: function( request, response ) {
158 162 $.getJSON(URL_STICKERS, {
159 163 term: extractLast( request.term )
160 164 }, response);
161 165 },
162 166 search: function() {
163 167 // custom minLength
164 168 var term = extractLast( this.value );
165 169 if (term.length < MIN_INPUT_LENGTH) {
166 170 return false;
167 171 }
168 172 },
169 173 focus: function() {
170 174 // prevent value inserted on focus
171 175 return false;
172 176 },
173 177 select: function( event, ui ) {
174 178 var terms = split( this.value );
175 179 // remove the current input
176 180 terms.pop();
177 181 // add the selected item
178 182 terms.push( ui.item.alias );
179 183 // add placeholder to get the comma-and-space at the end
180 184 terms.push("");
181 185 this.value = terms.join(URL_DELIMITER);
182 186 return false;
183 187 }
184 188 })
185 189 .autocomplete( "instance" )._renderItem = function( ul, item ) {
186 190 return $( "<li>" )
187 191 .append( "<div>" + '<img src="' + item.thumb + '">' + '<br />' + item.alias + "</div>" )
188 192 .appendTo( ul );
189 193 };
190 194 });
@@ -1,60 +1,60 b''
1 1 /*
2 2 @licstart The following is the entire license notice for the
3 3 JavaScript code in this page.
4 4
5 5
6 6 Copyright (C) 2013 neko259
7 7
8 8 The JavaScript code in this page is free software: you can
9 9 redistribute it and/or modify it under the terms of the GNU
10 10 General Public License (GNU GPL) as published by the Free Software
11 11 Foundation, either version 3 of the License, or (at your option)
12 12 any later version. The code is distributed WITHOUT ANY WARRANTY;
13 13 without even the implied warranty of MERCHANTABILITY or FITNESS
14 14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
15 15
16 16 As additional permission under GNU GPL version 3 section 7, you
17 17 may distribute non-source (e.g., minimized or compacted) forms of
18 18 that code without the copy of the GNU GPL normally required by
19 19 section 4, provided you include this license notice and a URL
20 20 through which recipients can access the Corresponding Source.
21 21
22 22 @licend The above is the entire license notice
23 23 for the JavaScript code in this page.
24 24 */
25 25
26 26 /**
27 27 * Add the desired characters to the start and end of selection.
28 28 *
29 29 * @param start Start (left) text
30 30 * @param end End (right) text
31 31 */
32 32 function addMarkToMsg(start, end) {
33 var textareas = $('textarea#id_text');
33 var textareas = getPostTextarea();
34 34
35 35 for (var i = 0; i < textareas.length; i++) {
36 36 var textarea = textareas[i];
37 37
38 38 if (document.selection) {
39 39 textarea.focus();
40 40
41 41 var sel = document.selection.createRange();
42 42 sel.text = start + sel.text + end;
43 43 } else if (textarea.selectionStart || textarea.selectionStart == '0') {
44 44 textarea.focus();
45 45
46 46 var startPos = textarea.selectionStart;
47 47 var endPos = textarea.selectionEnd;
48 48
49 49 var oldValue = textarea.value;
50 50 textarea.value = oldValue.substring(0, startPos) + start +
51 51 oldValue.substring(startPos, endPos) + end +
52 52 oldValue.substring(endPos, oldValue.length);
53 53 } else {
54 54 textarea.value += start + end;
55 55 }
56 56 }
57 57
58 58 return false;
59 59 }
60 60
@@ -1,168 +1,168 b''
1 1 /*
2 2 @licstart The following is the entire license notice for the
3 3 JavaScript code in this page.
4 4
5 5
6 6 Copyright (C) 2013 neko259
7 7
8 8 The JavaScript code in this page is free software: you can
9 9 redistribute it and/or modify it under the terms of the GNU
10 10 General Public License (GNU GPL) as published by the Free Software
11 11 Foundation, either version 3 of the License, or (at your option)
12 12 any later version. The code is distributed WITHOUT ANY WARRANTY;
13 13 without even the implied warranty of MERCHANTABILITY or FITNESS
14 14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
15 15
16 16 As additional permission under GNU GPL version 3 section 7, you
17 17 may distribute non-source (e.g., minimized or compacted) forms of
18 18 that code without the copy of the GNU GPL normally required by
19 19 section 4, provided you include this license notice and a URL
20 20 through which recipients can access the Corresponding Source.
21 21
22 22 @licend The above is the entire license notice
23 23 for the JavaScript code in this page.
24 24 */
25 25
26 26 var CLOSE_BUTTON = '#form-close-button';
27 27 var REPLY_TO_MSG = '.reply-to-message';
28 28 var REPLY_TO_MSG_ID = '#reply-to-message-id';
29 29
30 30 var $html = $("html, body");
31 31
32 32 function moveCaretToEnd(el) {
33 33 if (typeof el.selectionStart == "number") {
34 34 el.selectionStart = el.selectionEnd = el.value.length;
35 35 } else if (typeof el.createTextRange != "undefined") {
36 36 el.focus();
37 37 var range = el.createTextRange();
38 38 range.collapse(false);
39 39 range.select();
40 40 }
41 41 }
42 42
43 43 function getForm() {
44 44 return $('.post-form-w');
45 45 }
46 46
47 47 function resetFormPosition() {
48 48 var form = getForm();
49 49 form.insertAfter($('.thread'));
50 50
51 51 $(CLOSE_BUTTON).hide();
52 52 $(REPLY_TO_MSG).hide();
53 53 }
54 54
55 55 function showFormAfter(blockToInsertAfter) {
56 56 var form = getForm();
57 57 form.insertAfter(blockToInsertAfter);
58 58
59 59 $(CLOSE_BUTTON).show();
60 60 form.show();
61 61 $(REPLY_TO_MSG_ID).text(blockToInsertAfter.attr('id'));
62 62 $(REPLY_TO_MSG).show();
63 63 }
64 64
65 65 function addQuickReply(postId) {
66 66 // If we click "reply" on the same post, it means "cancel"
67 67 if (getForm().prev().attr('id') == postId) {
68 68 resetFormPosition();
69 69 } else {
70 70 var blockToInsert = null;
71 var textAreaJq = $('textarea#id_text');
71 var textAreaJq = getPostTextarea();
72 72 var postLinkRaw = '[post]' + postId + '[/post]'
73 73 var textToAdd = '';
74 74
75 75 if (postId != null) {
76 76 var post = $('#' + postId);
77 77
78 78 // If this is not OP, add reflink to the post. If there already is
79 79 // the same reflink, don't add it again.
80 80 var postText = textAreaJq.val();
81 81 if (!post.is(':first-child') && postText.indexOf(postLinkRaw) < 0) {
82 82 // Insert line break if none is present.
83 83 if (postText.length > 0 && !postText.endsWith('\n') && !postText.endsWith('\r')) {
84 84 textToAdd += '\n';
85 85 }
86 86 textToAdd += postLinkRaw + '\n';
87 87 }
88 88
89 89 textAreaJq.val(textAreaJq.val()+ textToAdd);
90 90 blockToInsert = post;
91 91 } else {
92 92 blockToInsert = $('.thread');
93 93 }
94 94 showFormAfter(blockToInsert);
95 95
96 96 textAreaJq.focus();
97 97
98 98 var textarea = document.getElementsByTagName('textarea')[0];
99 99 moveCaretToEnd(textarea);
100 100 }
101 101 }
102 102
103 103 function addQuickQuote() {
104 var textAreaJq = $('textarea#id_text');
104 var textAreaJq = getPostTextarea();
105 105
106 106 var quoteButton = $("#quote-button");
107 107 var postId = quoteButton.attr('data-post-id');
108 108 if (postId != null && getForm().prev().attr('id') != postId) {
109 109 addQuickReply(postId);
110 110 }
111 111
112 112 var textToAdd = '';
113 113 var selection = window.getSelection().toString();
114 114 if (selection.length == 0) {
115 115 selection = quoteButton.attr('data-text');
116 116 }
117 117 if (selection.length > 0) {
118 118 textToAdd += '[quote]' + selection + '[/quote]\n';
119 119 }
120 120
121 121 textAreaJq.val(textAreaJq.val() + textToAdd);
122 122
123 123 textAreaJq.focus();
124 124
125 125 var textarea = document.getElementsByTagName('textarea')[0];
126 126 moveCaretToEnd(textarea);
127 127 }
128 128
129 129 function scrollToBottom() {
130 130 $html.animate({scrollTop: $html.height()}, "fast");
131 131 }
132 132
133 133 function showQuoteButton() {
134 134 var selection = window.getSelection().getRangeAt(0).getBoundingClientRect();
135 135 var quoteButton = $("#quote-button");
136 136 if (selection.width > 0) {
137 137 // quoteButton.offset({ top: selection.top - selection.height, left: selection.left });
138 138 quoteButton.css({top: selection.top + $(window).scrollTop() - 30, left: selection.left});
139 139 quoteButton.show();
140 140
141 141 var text = window.getSelection().toString();
142 142 quoteButton.attr('data-text', text);
143 143
144 144 var rect = window.getSelection().getRangeAt(0).getBoundingClientRect();
145 145 var element = $(document.elementFromPoint(rect.x, rect.y));
146 146 var postId = null;
147 147 if (element.hasClass('post')) {
148 148 postId = element.attr('id');
149 149 } else {
150 150 var postParent = element.parents('.post');
151 151 if (postParent.length > 0) {
152 152 postId = postParent.attr('id');
153 153 }
154 154 }
155 155 quoteButton.attr('data-post-id', postId);
156 156 } else {
157 157 quoteButton.hide();
158 158 }
159 159 }
160 160
161 161 $(document).ready(function() {
162 162 $('body').on('mouseup', function() {
163 163 showQuoteButton();
164 164 });
165 165 $("#quote-button").click(function() {
166 166 addQuickQuote();
167 167 })
168 168 });
General Comments 0
You need to be logged in to leave comments. Login now