|
| 1 | +jQuery(document).ready(function($) { |
| 2 | + |
| 3 | + // Reveal the textarea and hide previews. |
| 4 | + $(document).on('click', 'a.WriteButton', function() { |
| 5 | + if ($(this).hasClass('WriteButton')) { |
| 6 | + var frm = $(this).parents('.MessageForm').find('form'); |
| 7 | + frm.trigger('WriteButtonClick', [frm]); |
| 8 | + |
| 9 | + // Reveal the "Preview" button and hide this one |
| 10 | + $(this).parents('.DiscussionForm').find('.PreviewButton').show(); |
| 11 | + $(this).addClass('Hidden'); |
| 12 | + } |
| 13 | + resetDiscussionForm(this); |
| 14 | + return false; |
| 15 | + }); |
| 16 | + |
| 17 | + function resetDiscussionForm(sender) { |
| 18 | + var parent = $(sender).parents('.DiscussionForm, .EditDiscussionForm'); |
| 19 | + $(parent).find('.Preview').remove(); |
| 20 | + $(parent).find('.bodybox-wrap .TextBoxWrapper').show(); |
| 21 | + } |
| 22 | + |
| 23 | + // Hijack comment form button clicks |
| 24 | + $('#CommentForm :submit').click(function() { |
| 25 | + var btn = this; |
| 26 | + var frm = $(btn).parents('form').get(0); |
| 27 | + |
| 28 | + // Handler before submitting |
| 29 | + $(frm).triggerHandler('BeforeCommentSubmit', [frm, btn]); |
| 30 | + |
| 31 | + var textbox = $(frm).find('textarea'); |
| 32 | + var inpCommentID = $(frm).find('input:hidden[name$=CommentID]'); |
| 33 | + var inpDraftID = $(frm).find('input:hidden[name$=DraftID]'); |
| 34 | + var preview = $(btn).attr('name') == $('#Form_Preview').attr('name') ? true : false; |
| 35 | + var draft = $(btn).attr('name') == $('#Form_SaveDraft').attr('name') ? true : false; |
| 36 | + var postValues = $(frm).serialize(); |
| 37 | + postValues += '&DeliveryType=VIEW&DeliveryMethod=JSON'; // DELIVERY_TYPE_VIEW |
| 38 | + postValues += '&' + btn.name + '=' + btn.value; |
| 39 | + var discussionID = $(frm).find('[name$=DiscussionID]').val(); |
| 40 | + var action = $(frm).attr('action') + '/' + discussionID; |
| 41 | + gdn.disable(btn); |
| 42 | + |
| 43 | + $.ajax({ |
| 44 | + type: "POST", |
| 45 | + url: action, |
| 46 | + data: postValues, |
| 47 | + dataType: 'json', |
| 48 | + error: function(XMLHttpRequest, textStatus, errorThrown) { |
| 49 | + // Remove any old popups |
| 50 | + $('div.Popup').remove(); |
| 51 | + // Add new popup with error |
| 52 | + $.popup({}, XMLHttpRequest.responseText); |
| 53 | + }, |
| 54 | + success: function(json) { |
| 55 | + // Remove any old popups if not saving as a draft |
| 56 | + if (!draft) |
| 57 | + $('div.Popup').remove(); |
| 58 | + |
| 59 | + // Assign the comment id to the form if it was defined |
| 60 | + if (json.CommentID != null && json.CommentID != '') { |
| 61 | + $(inpCommentID).val(json.CommentID); |
| 62 | + gdn.definition('LastCommentID', json.CommentID, true); |
| 63 | + } |
| 64 | + |
| 65 | + if (json.DraftID != null && json.DraftID != '') |
| 66 | + $(inpDraftID).val(json.DraftID); |
| 67 | + |
| 68 | + // Remove any old errors from the form |
| 69 | + $(frm).find('div.Errors').remove(); |
| 70 | + |
| 71 | + if (json.FormSaved == false) { |
| 72 | + $(frm).prepend(json.ErrorMessages); |
| 73 | + json.ErrorMessages = null; |
| 74 | + } else if (preview) { |
| 75 | + // Pop up the new preview. |
| 76 | + $.popup({}, json.Data); |
| 77 | + } else if (!draft && json.DiscussionUrl != null) { |
| 78 | + $(frm).triggerHandler('complete'); |
| 79 | + // Redirect to the discussion |
| 80 | + document.location = json.DiscussionUrl; |
| 81 | + } |
| 82 | + gdn.inform(json); |
| 83 | + }, |
| 84 | + complete: function(XMLHttpRequest, textStatus) { |
| 85 | + gdn.enable(btn); |
| 86 | + } |
| 87 | + }); |
| 88 | + $(frm).triggerHandler('submit'); |
| 89 | + return false; |
| 90 | + }); |
| 91 | + |
| 92 | + // Hijack discussion form button clicks |
| 93 | + //$('#DiscussionForm :submit').live('click', function() { |
| 94 | + |
| 95 | + // Jan28, 2014 jQuery upgrade to 1.10.2, as live() removed in 1.7. |
| 96 | + $(document).on('click', '#DiscussionForm :submit', function() { |
| 97 | + var btn = this; |
| 98 | + var frm = $(btn).parents('form').get(0); |
| 99 | + |
| 100 | + // Handler before submitting |
| 101 | + $(frm).triggerHandler('BeforeDiscussionSubmit', [frm, btn]); |
| 102 | + |
| 103 | + var inpDiscussionID = $(frm).find(':hidden[name$=DiscussionID]'); |
| 104 | + var inpDraftID = $(frm).find(':hidden[name$=DraftID]'); |
| 105 | + var preview = $(btn).attr('name') == $('#Form_Preview').attr('name') ? true : false; |
| 106 | + var draft = $(btn).attr('name') == $('#Form_SaveDraft').attr('name') ? true : false; |
| 107 | + var postValues = $(frm).serialize(); |
| 108 | + postValues += '&DeliveryType=VIEW&DeliveryMethod=JSON'; // DELIVERY_TYPE_VIEW |
| 109 | + postValues += '&' + btn.name + '=' + btn.value; |
| 110 | + gdn.disable(btn); |
| 111 | + |
| 112 | + $.ajax({ |
| 113 | + type: "POST", |
| 114 | + url: $(frm).attr('action'), |
| 115 | + data: postValues, |
| 116 | + dataType: 'json', |
| 117 | + error: function(XMLHttpRequest, textStatus, errorThrown) { |
| 118 | + $('div.Popup').remove(); |
| 119 | + $.popup({}, XMLHttpRequest.responseText); |
| 120 | + }, |
| 121 | + success: function(json) { |
| 122 | + // Remove any old popups if not saving as a draft |
| 123 | + if (!draft) |
| 124 | + $('div.Popup').remove(); |
| 125 | + |
| 126 | + // Assign the discussion id to the form if it was defined |
| 127 | + if (json.DiscussionID != null) |
| 128 | + $(inpDiscussionID).val(json.DiscussionID); |
| 129 | + |
| 130 | + if (json.DraftID != null) |
| 131 | + $(inpDraftID).val(json.DraftID); |
| 132 | + |
| 133 | + // Remove any old errors from the form |
| 134 | + $(frm).find('div.Errors').remove(); |
| 135 | + |
| 136 | + if (json.MyDrafts != null && json.CountDrafts != null) { |
| 137 | + var countMyDraftsHtml = '<span aria-hidden="true" class="Sprite SpMyDrafts"></span> My Drafts'; |
| 138 | + if(json.CountDrafts > 0) { |
| 139 | + countMyDraftsHtml += '<span class="Aside"><span class="Count">' + json.CountDrafts + '</span></span>'; |
| 140 | + $('li#MyDrafts').removeClass('hidden'); |
| 141 | + } else { |
| 142 | + $('li#MyDrafts').addClass('hidden'); |
| 143 | + } |
| 144 | + $('li#MyDrafts a').html(countMyDraftsHtml); |
| 145 | + } |
| 146 | + |
| 147 | + if (json.FormSaved == false) { |
| 148 | + $(frm).prepend(json.ErrorMessages); |
| 149 | + json.ErrorMessages = null; |
| 150 | + } else if (preview) { |
| 151 | + // Reveal the "Edit" button and hide this one |
| 152 | + $(btn).hide(); |
| 153 | + $(frm).find('.WriteButton').removeClass('Hidden'); |
| 154 | + |
| 155 | + $(frm).find('.bodybox-wrap .TextBoxWrapper').hide().after(json.Data); |
| 156 | + $(frm).trigger('PreviewLoaded', [frm]); |
| 157 | + } else if (!draft) { |
| 158 | + if (json.RedirectTo) { |
| 159 | + $(frm).triggerHandler('complete'); |
| 160 | + // Redirect to the new discussion |
| 161 | + document.location = json.RedirectTo; |
| 162 | + } else { |
| 163 | + var contentContainer = $("#Content"); |
| 164 | + |
| 165 | + if (contentContainer.length === 1) { |
| 166 | + contentContainer.html(json.Data); |
| 167 | + } else { |
| 168 | + // Hack to emulate a content container. |
| 169 | + contentContainer = $(document.createElement("div")); |
| 170 | + contentContainer.html(json.Data); |
| 171 | + $(frm).replaceWith(contentContainer); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + gdn.inform(json); |
| 176 | + }, |
| 177 | + complete: function(XMLHttpRequest, textStatus) { |
| 178 | + gdn.enable(btn); |
| 179 | + } |
| 180 | + }); |
| 181 | + $(frm).triggerHandler('submit'); |
| 182 | + return false; |
| 183 | + }); |
| 184 | + |
| 185 | + // Autosave |
| 186 | + if ($.fn.autosave) { |
| 187 | + var btn = $('#Form_SaveDraft'); |
| 188 | + |
| 189 | + $('#CommentForm textarea').autosave({ |
| 190 | + button: btn |
| 191 | + }); |
| 192 | + $('#DiscussionForm textarea').autosave({ |
| 193 | + button: btn |
| 194 | + }); |
| 195 | + } |
| 196 | +}); |
0 commit comments