Show More
@@ -1,136 +1,136 b'' | |||
|
1 | 1 | var ITEM_FILE_SOURCE = 'fileSource'; |
|
2 | 2 | |
|
3 | 3 | $('input[name=image]').wrap($('<div class="file_wrap"></div>')); |
|
4 | 4 | |
|
5 | 5 | $('body').on('change', 'input[name=image]', function(event) { |
|
6 | 6 | var file = event.target.files[0]; |
|
7 | 7 | |
|
8 | 8 | if(file.type.match('image.*')) { |
|
9 | 9 | var fileReader = new FileReader(); |
|
10 | 10 | |
|
11 | 11 | fileReader.addEventListener("load", function(event) { |
|
12 | 12 | var wrapper = $('.file_wrap'); |
|
13 | 13 | |
|
14 | 14 | wrapper.find('.file-thumb').remove(); |
|
15 | 15 | wrapper.append( |
|
16 | 16 | $('<div class="file-thumb" style="background-image: url('+event.target.result+')"></div>') |
|
17 | 17 | ); |
|
18 | 18 | }); |
|
19 | 19 | |
|
20 | 20 | fileReader.readAsDataURL(file); |
|
21 | 21 | } |
|
22 | 22 | }); |
|
23 | 23 | |
|
24 | 24 | var form = $('#form'); |
|
25 | 25 | $('textarea').keypress(function(event) { |
|
26 | if (event.which == 13 && event.ctrlKey) { | |
|
26 | if ((event.which == 10 || event.which == 13) && event.ctrlKey) { | |
|
27 | 27 | form.find('input[type=submit]').click(); |
|
28 | 28 | } |
|
29 | 29 | }); |
|
30 | 30 | |
|
31 | 31 | $('#preview-button').click(function() { |
|
32 | 32 | var data = { |
|
33 | 33 | raw_text: $('textarea').val() |
|
34 | 34 | } |
|
35 | 35 | |
|
36 | 36 | var diffUrl = '/api/preview/'; |
|
37 | 37 | |
|
38 | 38 | $.post(diffUrl, |
|
39 | 39 | data, |
|
40 | 40 | function(data) { |
|
41 | 41 | var previewTextBlock = $('#preview-text'); |
|
42 | 42 | previewTextBlock.html(data); |
|
43 | 43 | previewTextBlock.show(); |
|
44 | 44 | }) |
|
45 | 45 | }); |
|
46 | 46 | |
|
47 | 47 | /** |
|
48 | 48 | * Show text in the errors row of the form. |
|
49 | 49 | * @param form |
|
50 | 50 | * @param text |
|
51 | 51 | */ |
|
52 | 52 | function showAsErrors(form, text) { |
|
53 | 53 | form.children('.form-errors').remove(); |
|
54 | 54 | |
|
55 | 55 | if (text.length > 0) { |
|
56 | 56 | var errorList = $('<div class="form-errors">' + text + '<div>'); |
|
57 | 57 | errorList.appendTo(form); |
|
58 | 58 | } |
|
59 | 59 | } |
|
60 | 60 | |
|
61 | 61 | function addHiddenInput(form, name, value) { |
|
62 | 62 | form.find('input[name=' + name + ']').val(value); |
|
63 | 63 | } |
|
64 | 64 | |
|
65 | 65 | function selectFileChoice() { |
|
66 | 66 | var file_input = $('#id_file'); |
|
67 | 67 | var url_input = $('#id_file_url'); |
|
68 | 68 | |
|
69 | 69 | var file_input_row = file_input.parent().parent(); |
|
70 | 70 | var url_input_row = url_input.parent().parent(); |
|
71 | 71 | |
|
72 | 72 | file_input_row.toggle(); |
|
73 | 73 | url_input_row.toggle(); |
|
74 | 74 | url_input.val(''); |
|
75 | 75 | file_input.val(''); |
|
76 | 76 | |
|
77 | 77 | var source; |
|
78 | 78 | if (file_input_row.is(':visible')) { |
|
79 | 79 | source = 'file'; |
|
80 | 80 | } else { |
|
81 | 81 | source = 'url'; |
|
82 | 82 | } |
|
83 | 83 | localStorage.setItem(ITEM_FILE_SOURCE, source); |
|
84 | 84 | } |
|
85 | 85 | |
|
86 | 86 | $(document).ready(function() { |
|
87 | 87 | var powDifficulty = parseInt($('body').attr('data-pow-difficulty')); |
|
88 | 88 | if (powDifficulty > 0 && typeof SharedWorker != 'undefined') { |
|
89 | 89 | var worker = new SharedWorker($('.post-form').attr('data-pow-script')); |
|
90 | 90 | worker.port.onmessage = function(e) { |
|
91 | 91 | var form = $('#form'); |
|
92 | 92 | addHiddenInput(form, 'timestamp', e.data.timestamp); |
|
93 | 93 | addHiddenInput(form, 'iteration', e.data.iteration); |
|
94 | 94 | addHiddenInput(form, 'guess', e.data.guess); |
|
95 | 95 | |
|
96 | 96 | form.submit(); |
|
97 | 97 | $('.post-form-w').unblock(); |
|
98 | 98 | }; |
|
99 | 99 | worker.onerror = function(event){ |
|
100 | 100 | throw new Error(event.message + " (" + event.filename + ":" + event.lineno + ")"); |
|
101 | 101 | }; |
|
102 | 102 | worker.port.start(); |
|
103 | 103 | |
|
104 | 104 | var form = $('#form'); |
|
105 | 105 | var submitButton = form.find('input[type=submit]'); |
|
106 | 106 | submitButton.click(function() { |
|
107 | 107 | showAsErrors(form, gettext('Computing PoW...')); |
|
108 | 108 | $('.post-form-w').block({ message: gettext('Computing PoW...') }) |
|
109 | 109 | |
|
110 | 110 | var msg = $('textarea').val().trim(); |
|
111 | 111 | |
|
112 | 112 | var data = { |
|
113 | 113 | msg: msg, |
|
114 | 114 | difficulty: parseInt($('body').attr('data-pow-difficulty')), |
|
115 | 115 | hasher: $('.post-form').attr('data-hasher') |
|
116 | 116 | }; |
|
117 | 117 | worker.port.postMessage(data); |
|
118 | 118 | |
|
119 | 119 | return false; |
|
120 | 120 | }); |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | var source = localStorage.getItem(ITEM_FILE_SOURCE); |
|
124 | 124 | if (source == null) { |
|
125 | 125 | source = 'file'; |
|
126 | 126 | } |
|
127 | 127 | if (source == 'file') { |
|
128 | 128 | $('#id_file_url').parent().parent().hide(); |
|
129 | 129 | } else { |
|
130 | 130 | $('#id_file').parent().parent().hide(); |
|
131 | 131 | } |
|
132 | 132 | |
|
133 | 133 | $('#file-source-button').click(function() { |
|
134 | 134 | selectFileChoice(); |
|
135 | 135 | }); |
|
136 | 136 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now