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