comparison gcalendar/static/js/gcalendar.js @ 1099:3f0a7e918c05

Prepare to detach V3 post box from GCalendar.
author Brian Neal <bgneal@gmail.com>
date Mon, 27 Jun 2016 20:11:17 -0500
parents d9cd3180c12c
children
comparison
equal deleted inserted replaced
1098:c245caccbbb3 1099:3f0a7e918c05
1 function editorReplaceText(textArea, i, j, newText) {
2 textArea.value = textArea.value.substring(0, i) + newText +
3 textArea.value.substring(j);
4 }
5
6 function editorWrapSelection(textArea, wrapText) {
7 if (wrapText.length == 0) return;
8 var i = textArea.selectionStart;
9 var j = textArea.selectionEnd;
10 if (i == j) return;
11 var selection = textArea.value.substring(i, j);
12 var newText = wrapText + selection + wrapText;
13 editorReplaceText(textArea, i, j, newText);
14 textArea.focus();
15 textArea.setSelectionRange(i, j + 2 * wrapText.length);
16 }
17
18 function editorPrependLines(textArea, s) {
19 if (s.length == 0) return;
20 var i = textArea.selectionStart;
21 var j = textArea.selectionEnd;
22 var newText = s;
23 var selection = textArea.value.substring(i, j);
24 newText += selection.replace(/\n/gm, '\n' + s);
25 editorReplaceText(textArea, i, j, newText);
26 }
27
28 function editorLink(textArea) {
29 var i = textArea.selectionStart;
30 var j = textArea.selectionEnd;
31 var url = window.prompt("Please enter a URL:", "http://");
32 if (!url) return;
33 var link;
34 if (i == j) {
35 link = '[Link](' + url + ')';
36 } else {
37 var selection = textArea.value.substring(i, j);
38 link = '[' + selection + '](' + url + ')';
39 }
40 editorReplaceText(textArea, i, j, link);
41 textArea.focus();
42 }
43
44 $(document).ready(function() { 1 $(document).ready(function() {
45 $('#id_start_date').datepicker({constrainInput: true, 2 $('#id_start_date').datepicker({constrainInput: true,
46 dateFormat: 'mm/dd/yy', 3 dateFormat: 'mm/dd/yy',
47 onClose: function () { 4 onClose: function () {
48 var end = $('#id_end_date'); 5 var end = $('#id_end_date');
66 $('.all-day-hide').hide(); 23 $('.all-day-hide').hide();
67 } 24 }
68 $('#id_all_day').click(function () { 25 $('#id_all_day').click(function () {
69 $('.all-day-hide').toggle(); 26 $('.all-day-hide').toggle();
70 }); 27 });
71
72 var editorPanel = $('#editor-panel textarea');
73 var previewPanel = $('#editor-preview-panel');
74
75 $('#preview-modal').on('open.zf.reveal', function() {
76 previewPanel.html('');
77 previewPanel.scrollTop(0);
78 // TODO:
79 // Clear preview panel and/or show spinner
80 // If there is any text in editor panel {
81 $.ajax({
82 url: '/comments/markdown/v3/',
83 method: 'POST',
84 data: {'data': editorPanel[0].value},
85 dataType: 'html',
86 success: function(data, textStatus) {
87 previewPanel.html(data);
88 },
89 error: function (xhr, textStatus, ex) {
90 alert('Oops, an error occurred: ' + xhr.statusText + ' - ' +
91 xhr.responseText);
92 }
93 });
94 });
95
96 // Editor stuff
97 var $postBox;
98 $('.v3-editor').each(function (index) {
99 var $this = $(this);
100 var textArea = $this.find('textarea')[0];
101 $this.find('.editor-bold').click(function () {
102 editorWrapSelection(textArea, '**');
103 return false;
104 });
105 $this.find('.editor-italic').click(function () {
106 editorWrapSelection(textArea, '_');
107 return false;
108 });
109 $this.find('.editor-strike').click(function () {
110 editorWrapSelection(textArea, '---');
111 return false;
112 });
113 $this.find('.editor-link').click(function () {
114 editorLink(textArea);
115 return false;
116 });
117 $this.find('.editor-quote').click(function () {
118 editorPrependLines(textArea, '> ');
119 return false;
120 });
121 $this.find('.editor-code').click(function () {
122 editorPrependLines(textArea, ' ');
123 return false;
124 });
125 $this.find('.editor-bullet').click(function () {
126 editorPrependLines(textArea, '* ');
127 return false;
128 });
129 $this.find('.editor-number').click(function () {
130 editorPrependLines(textArea, '1. ');
131 return false;
132 });
133 $this.find('.editor-hot-link').click(function () {
134 $postBox = $(textArea);
135 });
136 $this.find('.editor-upload').click(function () {
137 $postBox = $(textArea);
138 });
139 });
140
141 var $hotLinkForm = $('#hot-link-form');
142 if ($hotLinkForm.length) {
143 var $hotLinkFormSubmit = $('#hot-link-form-submit');
144 var $hotLinkStatus = $('#hot-link-status');
145
146 $('#hot-link-modal').on('open.zf.reveal', function() {
147 $hotLinkForm.clearForm();
148 });
149 $('#hot-link-modal').on('closed.zf.reveal', function() {
150 $hotLinkStatus.hide();
151 });
152
153 $hotLinkForm.ajaxForm({
154 dataType: 'json',
155 beforeSubmit: function(arr, $form, options) {
156 var url = null;
157 $.each(arr, function(index, val) {
158 if (val.name == 'url') {
159 url = val.value;
160 }
161 });
162 if (!url) {
163 $hotLinkStatus.removeClass('success').addClass('alert');
164 $hotLinkStatus.html("Please enter a link to an image.");
165 $hotLinkStatus.fadeIn(700);
166 return false;
167 } else {
168 $hotLinkStatus.hide();
169 }
170 $hotLinkFormSubmit.attr('disabled', 'disabled').val('Retrieving...');
171 $hotLinkFormSubmit.addClass('disabled');
172 return true;
173 },
174 success: function(resp, statusText, xhr, $form) {
175 $postBox.val($postBox.val() + '\n![image](' + resp.url + ')');
176 $hotLinkStatus.removeClass('alert').addClass('success');
177 $hotLinkStatus.html("Success! The image code was added to your post.");
178 $hotLinkStatus.fadeIn(700);
179 $hotLinkForm.clearForm();
180 },
181 complete: function(xhr) {
182 $hotLinkFormSubmit.removeAttr('disabled').val('Hot Link');
183 $hotLinkFormSubmit.removeClass('disabled');
184 },
185 error: function(xhr, textStatus, ex) {
186 $hotLinkStatus.removeClass('success').addClass('alert');
187 var resp = $.parseJSON(xhr.responseText);
188 $hotLinkStatus.html("Error: " + ex + ' - ' + resp.error_msg);
189 $hotLinkStatus.fadeIn(700);
190 }
191 });
192 }
193 var $photoForm = $('#photo-upload-form');
194 if ($photoForm.length) {
195 var $photoUploadSubmit = $('#photo-upload-submit');
196 var $photoStatus = $('#photo-status');
197
198 $('#upload-modal').on('open.zf.reveal', function() {
199 $photoForm.clearForm();
200 });
201 $('#upload-modal').on('closed.zf.reveal', function() {
202 $photoStatus.hide();
203 });
204
205 $photoForm.ajaxForm({
206 dataType: 'json',
207 beforeSubmit: function(arr, $form, options) {
208 var fileObj = null;
209 $.each(arr, function(index, val) {
210 if (val.name == 'image_file') {
211 fileObj = val.value;
212 }
213 });
214 if (!fileObj) {
215 $photoStatus.removeClass('success').addClass('alert');
216 $photoStatus.html("Please choose a file to upload.");
217 $photoStatus.fadeIn(700);
218 return false;
219 } else {
220 $photoStatus.hide();
221 }
222 $photoUploadSubmit.attr('disabled', 'disabled').val('Uploading...');
223 $photoUploadSubmit.addClass('disabled');
224 return true;
225 },
226 success: function(resp, statusText, xhr, $form) {
227 $postBox.val($postBox.val() + '\n![image](' + resp.url + ')');
228 $photoStatus.removeClass('alert').addClass('success');
229 $photoStatus.html("Success! The image code was added to your post.");
230 $photoStatus.fadeIn(700);
231 $photoStatus.clearForm();
232 },
233 complete: function(xhr) {
234 $photoUploadSubmit.removeAttr('disabled').val('Upload photo');
235 $photoUploadSubmit.removeClass('disabled');
236 },
237 error: function(xhr, textStatus, ex) {
238 $photoStatus.removeClass('success').addClass('alert');
239 var resp = $.parseJSON(xhr.responseText);
240 $photoStatus.html("Error: " + ex + ' - ' + resp.error_msg);
241 $photoStatus.fadeIn(700);
242 }
243 });
244 }
245 }); 28 });