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