Show More
@@ -1,226 +1,232 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 | var pastedImages = []; |
|
7 | 7 | |
|
8 | 8 | $('input[name=image]').wrap($('<div class="file_wrap"></div>')); |
|
9 | 9 | |
|
10 | 10 | $('body').on('change', 'input[name=image]', function(event) { |
|
11 | 11 | var file = event.target.files[0]; |
|
12 | 12 | |
|
13 | 13 | if(file.type.match('image.*')) { |
|
14 | 14 | var fileReader = new FileReader(); |
|
15 | 15 | |
|
16 | 16 | fileReader.addEventListener("load", function(event) { |
|
17 | 17 | var wrapper = $('.file_wrap'); |
|
18 | 18 | |
|
19 | 19 | wrapper.find('.file-thumb').remove(); |
|
20 | 20 | wrapper.append( |
|
21 | 21 | $('<div class="file-thumb" style="background-image: url('+event.target.result+')"></div>') |
|
22 | 22 | ); |
|
23 | 23 | }); |
|
24 | 24 | |
|
25 | 25 | fileReader.readAsDataURL(file); |
|
26 | 26 | } |
|
27 | 27 | }); |
|
28 | 28 | |
|
29 | 29 | var form = $('#form'); |
|
30 | 30 | $('textarea').keypress(function(event) { |
|
31 | 31 | if ((event.which == 10 || event.which == 13) && event.ctrlKey) { |
|
32 | 32 | form.find('input[type=submit]').click(); |
|
33 | 33 | } |
|
34 | 34 | }); |
|
35 | 35 | |
|
36 | 36 | $('#preview-button').click(function() { |
|
37 | 37 | var data = { |
|
38 | 38 | raw_text: $('textarea#id_text').val() |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | var diffUrl = '/api/preview/'; |
|
42 | 42 | |
|
43 | 43 | $.post(diffUrl, |
|
44 | 44 | data, |
|
45 | 45 | function(data) { |
|
46 | 46 | var previewTextBlock = $('#preview-text'); |
|
47 | 47 | previewTextBlock.html(data); |
|
48 | 48 | previewTextBlock.show(); |
|
49 | 49 | |
|
50 | 50 | addScriptsToPost(previewTextBlock); |
|
51 | 51 | }) |
|
52 | 52 | }); |
|
53 | 53 | |
|
54 | 54 | /** |
|
55 | 55 | * Show text in the errors row of the form. |
|
56 | 56 | * @param form |
|
57 | 57 | * @param text |
|
58 | 58 | */ |
|
59 | 59 | function showAsErrors(form, text) { |
|
60 | 60 | form.children('.form-errors').remove(); |
|
61 | 61 | |
|
62 | 62 | if (text.length > 0) { |
|
63 | 63 | var errorList = $('<div class="form-errors">' + text + '<div>'); |
|
64 | 64 | errorList.appendTo(form); |
|
65 | 65 | } |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | function addHiddenInput(form, name, value) { |
|
69 | 69 | form.find('input[name=' + name + ']').val(value); |
|
70 | 70 | } |
|
71 | 71 | |
|
72 | 72 | function selectFileChoice() { |
|
73 | 73 | var file_input = $('#id_file'); |
|
74 | 74 | var url_input = $('#id_file_url'); |
|
75 | 75 | |
|
76 | 76 | var file_input_row = file_input.parent().parent(); |
|
77 | 77 | var url_input_row = url_input.parent().parent(); |
|
78 | 78 | |
|
79 | 79 | file_input_row.toggle(); |
|
80 | 80 | url_input_row.toggle(); |
|
81 | 81 | url_input.val(''); |
|
82 | 82 | file_input.val(''); |
|
83 | 83 | |
|
84 | 84 | var source; |
|
85 | 85 | if (file_input_row.is(':visible')) { |
|
86 | 86 | source = 'file'; |
|
87 | 87 | } else { |
|
88 | 88 | source = 'url'; |
|
89 | 89 | } |
|
90 | 90 | localStorage.setItem(ITEM_FILE_SOURCE, source); |
|
91 | 91 | } |
|
92 | 92 | |
|
93 | 93 | function getPostTextarea() { |
|
94 | 94 | return $('textarea#id_text'); |
|
95 | 95 | } |
|
96 | 96 | |
|
97 | 97 | function addOnImagePaste() { |
|
98 | 98 | $('#id_file_1').on('paste', function(event) { |
|
99 | 99 | var items = (event.clipboardData || event.originalEvent.clipboardData).items; |
|
100 | 100 | for (index in items) { |
|
101 | 101 | var item = items[index]; |
|
102 | 102 | if (item.kind === 'file') { |
|
103 | 103 | var blob = item.getAsFile(); |
|
104 | 104 | |
|
105 | 105 | pastedImages.push(blob); |
|
106 | 106 | |
|
107 | 107 | var pastedImagesList = $('#pasted-images'); |
|
108 | 108 | if (pastedImagesList.length === 0) { |
|
109 | 109 | pastedImagesList = $('<div id="pasted-images" />'); |
|
110 | 110 | $('#id_file_1').parent().append(pastedImagesList); |
|
111 | 111 | } |
|
112 | 112 | |
|
113 | 113 | var fr = new FileReader(); |
|
114 | 114 | fr.onload = function () { |
|
115 | 115 | var img = $('<img class="image-preview" />'); |
|
116 | 116 | img.attr('src', fr.result); |
|
117 | 117 | pastedImagesList.append(img); |
|
118 | img.on("click", function() { | |
|
119 | // Remove the image from all lists | |
|
120 | var itemIndex = $(this).index(); | |
|
121 | pastedImages.splice(itemIndex, 1); | |
|
122 | $(this).remove(); | |
|
123 | }); | |
|
118 | 124 | }; |
|
119 | 125 | fr.readAsDataURL(blob); |
|
120 | 126 | } |
|
121 | 127 | } |
|
122 | 128 | }); |
|
123 | 129 | } |
|
124 | 130 | |
|
125 | 131 | $(document).ready(function() { |
|
126 | 132 | var powDifficulty = parseInt($('body').attr('data-pow-difficulty')); |
|
127 | 133 | if (powDifficulty > 0 && typeof SharedWorker != 'undefined') { |
|
128 | 134 | var worker = new SharedWorker($('.post-form').attr('data-pow-script')); |
|
129 | 135 | worker.port.onmessage = function(e) { |
|
130 | 136 | var form = $('#form'); |
|
131 | 137 | addHiddenInput(form, 'timestamp', e.data.timestamp); |
|
132 | 138 | addHiddenInput(form, 'iteration', e.data.iteration); |
|
133 | 139 | addHiddenInput(form, 'guess', e.data.guess); |
|
134 | 140 | |
|
135 | 141 | form.submit(); |
|
136 | 142 | $('.post-form-w').unblock(); |
|
137 | 143 | }; |
|
138 | 144 | worker.onerror = function(event){ |
|
139 | 145 | throw new Error(event.message + " (" + event.filename + ":" + event.lineno + ")"); |
|
140 | 146 | }; |
|
141 | 147 | worker.port.start(); |
|
142 | 148 | |
|
143 | 149 | var form = $('#form'); |
|
144 | 150 | var submitButton = form.find('input[type=submit]'); |
|
145 | 151 | submitButton.click(function() { |
|
146 | 152 | showAsErrors(form, gettext('Computing PoW...')); |
|
147 | 153 | $('.post-form-w').block({ message: gettext('Computing PoW...') }) |
|
148 | 154 | |
|
149 | 155 | var msg = $('textarea#id_text').val().trim(); |
|
150 | 156 | |
|
151 | 157 | var data = { |
|
152 | 158 | msg: msg, |
|
153 | 159 | difficulty: parseInt($('body').attr('data-pow-difficulty')), |
|
154 | 160 | hasher: $('.post-form').attr('data-hasher') |
|
155 | 161 | }; |
|
156 | 162 | worker.port.postMessage(data); |
|
157 | 163 | |
|
158 | 164 | return false; |
|
159 | 165 | }); |
|
160 | 166 | } |
|
161 | 167 | |
|
162 | 168 | var $fileSourceButton = $('#file-source-button'); |
|
163 | 169 | if (window.localStorage) { |
|
164 | 170 | var source = localStorage.getItem(ITEM_FILE_SOURCE); |
|
165 | 171 | if (source == null) { |
|
166 | 172 | source = 'file'; |
|
167 | 173 | } |
|
168 | 174 | if (source == 'file') { |
|
169 | 175 | $('#id_file_url').parent().parent().hide(); |
|
170 | 176 | } else { |
|
171 | 177 | $('#id_file').parent().parent().hide(); |
|
172 | 178 | } |
|
173 | 179 | |
|
174 | 180 | $fileSourceButton.click(function() { |
|
175 | 181 | selectFileChoice(); |
|
176 | 182 | }); |
|
177 | 183 | } else { |
|
178 | 184 | $fileSourceButton.hide(); |
|
179 | 185 | } |
|
180 | 186 | |
|
181 | 187 | addOnImagePaste(); |
|
182 | 188 | |
|
183 | 189 | // Stickers autocomplete |
|
184 | 190 | function split( val ) { |
|
185 | 191 | return val.split(URL_DELIMITER); |
|
186 | 192 | } |
|
187 | 193 | |
|
188 | 194 | function extractLast( term ) { |
|
189 | 195 | return split(term).pop(); |
|
190 | 196 | } |
|
191 | 197 | |
|
192 | 198 | $('#id_file_1').autocomplete({ |
|
193 | 199 | source: function( request, response ) { |
|
194 | 200 | $.getJSON(URL_STICKERS, { |
|
195 | 201 | term: extractLast( request.term ) |
|
196 | 202 | }, response); |
|
197 | 203 | }, |
|
198 | 204 | search: function() { |
|
199 | 205 | // custom minLength |
|
200 | 206 | var term = extractLast( this.value ); |
|
201 | 207 | if (term.length < MIN_INPUT_LENGTH) { |
|
202 | 208 | return false; |
|
203 | 209 | } |
|
204 | 210 | }, |
|
205 | 211 | focus: function() { |
|
206 | 212 | // prevent value inserted on focus |
|
207 | 213 | return false; |
|
208 | 214 | }, |
|
209 | 215 | select: function( event, ui ) { |
|
210 | 216 | var terms = split( this.value ); |
|
211 | 217 | // remove the current input |
|
212 | 218 | terms.pop(); |
|
213 | 219 | // add the selected item |
|
214 | 220 | terms.push( ui.item.alias ); |
|
215 | 221 | // add placeholder to get the comma-and-space at the end |
|
216 | 222 | terms.push(""); |
|
217 | 223 | this.value = terms.join(URL_DELIMITER); |
|
218 | 224 | return false; |
|
219 | 225 | } |
|
220 | 226 | }) |
|
221 | 227 | .autocomplete( "instance" )._renderItem = function( ul, item ) { |
|
222 | 228 | return $( "<li>" ) |
|
223 | 229 | .append( "<div>" + '<img src="' + item.thumb + '">' + '<br />' + item.alias + "</div>" ) |
|
224 | 230 | .appendTo( ul ); |
|
225 | 231 | }; |
|
226 | 232 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now