bgneal@1094: function editorReplaceText(textArea, i, j, newText) { bgneal@1094: textArea.value = textArea.value.substring(0, i) + newText + bgneal@1094: textArea.value.substring(j); bgneal@1094: } bgneal@1094: bgneal@1094: function editorWrapSelection(textArea, wrapText) { bgneal@1094: if (wrapText.length == 0) return; bgneal@1094: var i = textArea.selectionStart; bgneal@1094: var j = textArea.selectionEnd; bgneal@1094: if (i == j) return; bgneal@1094: var selection = textArea.value.substring(i, j); bgneal@1094: var newText = wrapText + selection + wrapText; bgneal@1094: editorReplaceText(textArea, i, j, newText); bgneal@1094: textArea.focus(); bgneal@1094: textArea.setSelectionRange(i, j + 2 * wrapText.length); bgneal@1094: } bgneal@1094: bgneal@1094: function editorPrependLines(textArea, s) { bgneal@1094: if (s.length == 0) return; bgneal@1094: var i = textArea.selectionStart; bgneal@1094: var j = textArea.selectionEnd; bgneal@1094: var newText = s; bgneal@1094: var selection = textArea.value.substring(i, j); bgneal@1094: newText += selection.replace(/\n/gm, '\n' + s); bgneal@1094: editorReplaceText(textArea, i, j, newText); bgneal@1094: } bgneal@1094: bgneal@1094: function editorLink(textArea) { bgneal@1094: var i = textArea.selectionStart; bgneal@1094: var j = textArea.selectionEnd; bgneal@1094: var url = window.prompt("Please enter a URL:", "http://"); bgneal@1094: if (!url) return; bgneal@1094: var link; bgneal@1094: if (i == j) { bgneal@1094: link = '[Link](' + url + ')'; bgneal@1094: } else { bgneal@1094: var selection = textArea.value.substring(i, j); bgneal@1094: link = '[' + selection + '](' + url + ')'; bgneal@1094: } bgneal@1094: editorReplaceText(textArea, i, j, link); bgneal@1094: textArea.focus(); bgneal@1094: } bgneal@1094: bgneal@312: $(document).ready(function() { bgneal@312: $('#id_start_date').datepicker({constrainInput: true, bgneal@312: dateFormat: 'mm/dd/yy', bgneal@312: onClose: function () { bgneal@312: var end = $('#id_end_date'); bgneal@312: if (this.value > end.val()) bgneal@312: { bgneal@312: end.val(this.value); bgneal@312: } bgneal@312: } bgneal@312: }); bgneal@312: $('#id_end_date').datepicker({constrainInput: true, bgneal@312: dateFormat: 'mm/dd/yy', bgneal@312: onClose: function () { bgneal@312: var start = $('#id_start_date'); bgneal@312: if (this.value < start.val()) bgneal@312: { bgneal@312: start.val(this.value); bgneal@312: } bgneal@312: } bgneal@312: }); bgneal@1094: if ($('#id_all_day:checked').length) { bgneal@1094: $('.all-day-hide').hide(); bgneal@312: } bgneal@312: $('#id_all_day').click(function () { bgneal@1094: $('.all-day-hide').toggle(); bgneal@1094: }); bgneal@1094: bgneal@1094: var editorPanel = $('#editor-panel textarea'); bgneal@1096: var previewPanel = $('#editor-preview-panel'); bgneal@1096: bgneal@1096: $('#preview-modal').on('open.zf.reveal', function() { bgneal@1096: previewPanel.html(''); bgneal@1096: previewPanel.scrollTop(0); bgneal@1096: // TODO: bgneal@1096: // Clear preview panel and/or show spinner bgneal@1096: // If there is any text in editor panel { bgneal@1096: $.ajax({ bgneal@1096: url: '/comments/markdown/v3/', bgneal@1096: method: 'POST', bgneal@1096: data: {'data': editorPanel[0].value}, bgneal@1096: dataType: 'html', bgneal@1096: success: function(data, textStatus) { bgneal@1096: previewPanel.html(data); bgneal@1096: }, bgneal@1096: error: function (xhr, textStatus, ex) { bgneal@1096: alert('Oops, an error occurred: ' + xhr.statusText + ' - ' + bgneal@1096: xhr.responseText); bgneal@1096: } bgneal@1096: }); bgneal@1094: }); bgneal@1094: bgneal@1094: // Editor stuff bgneal@1096: var $postBox; bgneal@1094: $('.v3-editor').each(function (index) { bgneal@1094: var $this = $(this); bgneal@1094: var textArea = $this.find('textarea')[0]; bgneal@1094: $this.find('.editor-bold').click(function () { bgneal@1094: editorWrapSelection(textArea, '**'); bgneal@1094: return false; bgneal@1094: }); bgneal@1094: $this.find('.editor-italic').click(function () { bgneal@1094: editorWrapSelection(textArea, '_'); bgneal@1094: return false; bgneal@1094: }); bgneal@1094: $this.find('.editor-strike').click(function () { bgneal@1094: editorWrapSelection(textArea, '---'); bgneal@1094: return false; bgneal@1094: }); bgneal@1094: $this.find('.editor-link').click(function () { bgneal@1094: editorLink(textArea); bgneal@1094: return false; bgneal@1094: }); bgneal@1094: $this.find('.editor-quote').click(function () { bgneal@1094: editorPrependLines(textArea, '> '); bgneal@1094: return false; bgneal@1094: }); bgneal@1094: $this.find('.editor-code').click(function () { bgneal@1094: editorPrependLines(textArea, ' '); bgneal@1094: return false; bgneal@1094: }); bgneal@1094: $this.find('.editor-bullet').click(function () { bgneal@1094: editorPrependLines(textArea, '* '); bgneal@1094: return false; bgneal@1094: }); bgneal@1094: $this.find('.editor-number').click(function () { bgneal@1094: editorPrependLines(textArea, '1. '); bgneal@1094: return false; bgneal@1094: }); bgneal@1096: $this.find('.editor-hot-link').click(function () { bgneal@1096: $postBox = $(textArea); bgneal@1096: }); bgneal@1096: $this.find('.editor-upload').click(function () { bgneal@1096: $postBox = $(textArea); bgneal@1096: }); bgneal@1094: }); bgneal@1096: bgneal@1096: var $hotLinkForm = $('#hot-link-form'); bgneal@1096: if ($hotLinkForm.length) { bgneal@1096: var $hotLinkFormSubmit = $('#hot-link-form-submit'); bgneal@1096: var $hotLinkStatus = $('#hot-link-status'); bgneal@1096: bgneal@1096: $('#hot-link-modal').on('open.zf.reveal', function() { bgneal@1096: $hotLinkForm.clearForm(); bgneal@1096: }); bgneal@1096: $('#hot-link-modal').on('closed.zf.reveal', function() { bgneal@1096: $hotLinkStatus.hide(); bgneal@1096: }); bgneal@1096: bgneal@1096: $hotLinkForm.ajaxForm({ bgneal@1096: dataType: 'json', bgneal@1096: beforeSubmit: function(arr, $form, options) { bgneal@1096: var url = null; bgneal@1096: $.each(arr, function(index, val) { bgneal@1096: if (val.name == 'url') { bgneal@1096: url = val.value; bgneal@1096: } bgneal@1096: }); bgneal@1096: if (!url) { bgneal@1096: $hotLinkStatus.removeClass('success').addClass('alert'); bgneal@1096: $hotLinkStatus.html("Please enter a link to an image."); bgneal@1096: $hotLinkStatus.fadeIn(700); bgneal@1096: return false; bgneal@1096: } else { bgneal@1096: $hotLinkStatus.hide(); bgneal@1096: } bgneal@1096: $hotLinkFormSubmit.attr('disabled', 'disabled').val('Retrieving...'); bgneal@1096: $hotLinkFormSubmit.addClass('disabled'); bgneal@1096: return true; bgneal@1096: }, bgneal@1096: success: function(resp, statusText, xhr, $form) { bgneal@1096: $postBox.val($postBox.val() + '\n![image](' + resp.url + ')'); bgneal@1096: $hotLinkStatus.removeClass('alert').addClass('success'); bgneal@1096: $hotLinkStatus.html("Success! The image code was added to your post."); bgneal@1096: $hotLinkStatus.fadeIn(700); bgneal@1096: $hotLinkForm.clearForm(); bgneal@1096: }, bgneal@1096: complete: function(xhr) { bgneal@1096: $hotLinkFormSubmit.removeAttr('disabled').val('Hot Link'); bgneal@1096: $hotLinkFormSubmit.removeClass('disabled'); bgneal@1096: }, bgneal@1096: error: function(xhr, textStatus, ex) { bgneal@1096: $hotLinkStatus.removeClass('success').addClass('alert'); bgneal@1096: var resp = $.parseJSON(xhr.responseText); bgneal@1096: $hotLinkStatus.html("Error: " + ex + ' - ' + resp.error_msg); bgneal@1096: $hotLinkStatus.fadeIn(700); bgneal@1096: } bgneal@1096: }); bgneal@1096: } bgneal@1096: var $photoForm = $('#photo-upload-form'); bgneal@1096: if ($photoForm.length) { bgneal@1096: var $photoUploadSubmit = $('#photo-upload-submit'); bgneal@1096: var $photoStatus = $('#photo-status'); bgneal@1096: bgneal@1096: $('#upload-modal').on('open.zf.reveal', function() { bgneal@1096: $photoForm.clearForm(); bgneal@1096: }); bgneal@1096: $('#upload-modal').on('closed.zf.reveal', function() { bgneal@1096: $photoStatus.hide(); bgneal@1096: }); bgneal@1096: bgneal@1096: $photoForm.ajaxForm({ bgneal@1096: dataType: 'json', bgneal@1096: beforeSubmit: function(arr, $form, options) { bgneal@1096: var fileObj = null; bgneal@1096: $.each(arr, function(index, val) { bgneal@1096: if (val.name == 'image_file') { bgneal@1096: fileObj = val.value; bgneal@1096: } bgneal@1096: }); bgneal@1096: if (!fileObj) { bgneal@1096: $photoStatus.removeClass('success').addClass('alert'); bgneal@1096: $photoStatus.html("Please choose a file to upload."); bgneal@1096: $photoStatus.fadeIn(700); bgneal@1096: return false; bgneal@1096: } else { bgneal@1096: $photoStatus.hide(); bgneal@1096: } bgneal@1096: $photoUploadSubmit.attr('disabled', 'disabled').val('Uploading...'); bgneal@1096: $photoUploadSubmit.addClass('disabled'); bgneal@1096: return true; bgneal@1096: }, bgneal@1096: success: function(resp, statusText, xhr, $form) { bgneal@1096: $postBox.val($postBox.val() + '\n![image](' + resp.url + ')'); bgneal@1096: $photoStatus.removeClass('alert').addClass('success'); bgneal@1096: $photoStatus.html("Success! The image code was added to your post."); bgneal@1096: $photoStatus.fadeIn(700); bgneal@1096: $photoStatus.clearForm(); bgneal@1096: }, bgneal@1096: complete: function(xhr) { bgneal@1096: $photoUploadSubmit.removeAttr('disabled').val('Upload photo'); bgneal@1096: $photoUploadSubmit.removeClass('disabled'); bgneal@1096: }, bgneal@1096: error: function(xhr, textStatus, ex) { bgneal@1096: $photoStatus.removeClass('success').addClass('alert'); bgneal@1096: var resp = $.parseJSON(xhr.responseText); bgneal@1096: $photoStatus.html("Error: " + ex + ' - ' + resp.error_msg); bgneal@1096: $photoStatus.fadeIn(700); bgneal@1096: } bgneal@1096: }); bgneal@1096: } bgneal@312: });