comparison gcalendar/static/js/gcalendar.js @ 1096:d9cd3180c12c

More GCalendar V3 conversion in progress. Built a brand new post editor. It is hardcoded into GCalendar right now. We will make it more general in the future.
author Brian Neal <bgneal@gmail.com>
date Tue, 14 Jun 2016 21:16:09 -0500
parents 110bbc78a482
children 3f0a7e918c05
comparison
equal deleted inserted replaced
1095:74d7e56a9cfb 1096:d9cd3180c12c
68 $('#id_all_day').click(function () { 68 $('#id_all_day').click(function () {
69 $('.all-day-hide').toggle(); 69 $('.all-day-hide').toggle();
70 }); 70 });
71 71
72 var editorPanel = $('#editor-panel textarea'); 72 var editorPanel = $('#editor-panel textarea');
73 var previewPanel = $('#preview-panel'); 73 var previewPanel = $('#editor-preview-panel');
74 previewPanel.css('height', editorPanel.css('height')); 74
75 var observer = new MutationObserver(function(mutations) { 75 $('#preview-modal').on('open.zf.reveal', function() {
76 if (previewPanel.is(':visible')) { 76 previewPanel.html('');
77 alert(editorPanel[0].value); 77 previewPanel.scrollTop(0);
78 $.ajax({ 78 // TODO:
79 url: '/comments/markdown/v3/', 79 // Clear preview panel and/or show spinner
80 method: 'POST', 80 // If there is any text in editor panel {
81 data: {'data': editorPanel[0].value}, 81 $.ajax({
82 dataType: 'html', 82 url: '/comments/markdown/v3/',
83 success: function(data, textStatus) { 83 method: 'POST',
84 alert(data); 84 data: {'data': editorPanel[0].value},
85 previewPanel.html(data); 85 dataType: 'html',
86 }, 86 success: function(data, textStatus) {
87 error: function (xhr, textStatus, ex) { 87 previewPanel.html(data);
88 alert('Oops, an error occurred: ' + xhr.statusText + ' - ' + 88 },
89 xhr.responseText); 89 error: function (xhr, textStatus, ex) {
90 } 90 alert('Oops, an error occurred: ' + xhr.statusText + ' - ' +
91 }); 91 xhr.responseText);
92 } 92 }
93 });
93 }); 94 });
94 var target = document.getElementById('preview-panel');
95 observer.observe(target, {attributes: true});
96 95
97 // Editor stuff 96 // Editor stuff
97 var $postBox;
98 $('.v3-editor').each(function (index) { 98 $('.v3-editor').each(function (index) {
99 var $this = $(this); 99 var $this = $(this);
100 var textArea = $this.find('textarea')[0]; 100 var textArea = $this.find('textarea')[0];
101 $this.find('.editor-bold').click(function () { 101 $this.find('.editor-bold').click(function () {
102 editorWrapSelection(textArea, '**'); 102 editorWrapSelection(textArea, '**');
128 }); 128 });
129 $this.find('.editor-number').click(function () { 129 $this.find('.editor-number').click(function () {
130 editorPrependLines(textArea, '1. '); 130 editorPrependLines(textArea, '1. ');
131 return false; 131 return false;
132 }); 132 });
133 $this.find('.editor-hot-link').click(function () {
134 $postBox = $(textArea);
135 });
136 $this.find('.editor-upload').click(function () {
137 $postBox = $(textArea);
138 });
133 }); 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 }
134 }); 245 });