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