3 // @fixme: Should use ID
4 close: function (form, best) {
5 var notice = $(form).closest('li.hentry.notice.question');
7 notice.find('input#qna-best-answer,#qna-question-close').hide();
8 notice.find('textarea').hide();
10 var list = notice.find('ul');
12 notice.find('ul > li.notice-answer-placeholder').remove();
13 notice.find('ul > li.notice-answer').remove();
16 var p = notice.parent().find('div.question-description > form > fieldset > p');
18 p.append($('<span class="question-closed">This question is closed.</span>'));
24 QnA.NoticeInlineAnswerSetup();
26 $(document).on('submit', 'form.form_question_show', function () {
29 $(document).on('submit', 'form.form_answer_show', function () {
30 QnA.close(this, true);
35 * Open up a question's inline answer box.
37 * @param {jQuery} notice: jQuery object containing one notice
39 NoticeInlineAnswerTrigger: function (notice) {
40 // Find the notice we're replying to...
41 var id = $($('.notice_id', notice)[0]).text();
42 var parentNotice = notice;
44 // Find the threaded replies view we'll be adding to...
45 var list = notice.closest('.notices');
46 if (list.hasClass('threaded-replies')) {
48 // We're replying to a reply; use reply form on the end of this list.
49 // We'll add our form at the end of this; grab the root notice.
50 parentNotice = list.closest('.notice');
54 // We're replying to a parent notice; pull its threaded list
55 // and we'll add on the end of it. Will add if needed.
56 list = $('ul.threaded-replies', notice);
59 // See if the form's already open...
60 var answerForm = $('.qna_answer_form', list);
62 var hideReplyPlaceholders = function (notice) {
63 // Do we still have a dummy answer placeholder? If so get rid of
64 // reply place holders for this question. If the current user hasn't
65 // answered the question we want to direct her to providing an
66 // answer. She can still reply by hitting the reply button if she
68 var dummyAnswer = $('ul.qna-dummy', notice);
69 if (dummyAnswer.length > 0) {
70 notice.find('li.notice-reply-placeholder').hide();
74 var nextStep = function () {
75 var dummyAnswer = $('ul.qna-dummy', notice);
79 var text = answerForm.find('textarea');
81 if (text.length == 0) {
87 $('body').click(function (e) {
88 var dummyAnswer = $('ul.qna-dummy', notice);
89 var style = dummyAnswer.attr('style');
90 var ans = $(notice).find('li.hentry.notice.anwer', notice)
92 hideReplyPlaceholders(notice);
95 var openAnswers = $('li.notice-answer');
96 if (openAnswers.length > 0) {
97 var target = $(e.target);
98 openAnswers.each(function () {
100 // Did we click outside this one?
101 var answerItem = $(this);
102 var parentNotice = answerItem.closest('li.notice');
104 if (answerItem.has(e.target).length == 0) {
105 var textarea = answerItem.find('.notice_data-text:first');
106 var cur = $.trim(textarea.val());
107 // Only close if there's been no edit.
108 if (cur == '' || cur == textarea.data('initialText')) {
118 // See if the form's already open...
119 if (answerForm.length > 0 ) {
122 var placeholder = list.find('li.qna-dummy-placeholder').hide();
124 // Create the answer form entry at the end
125 var answerItem = $('li.notice-answer', list);
127 if (answerItem.length == 0) {
128 answerItem = $('<li class="notice-answer"></li>');
129 var intermediateStep = function (formMaster) {
130 // @todo cache the form if we can (worth it?)
131 var formEl = document._importNode(formMaster, true);
132 $(formEl).data('NoticeFormSetup', true);
133 answerItem.append(formEl);
134 list.prepend(answerItem); // *before* the placeholder
135 var form = answerForm = $(formEl);
136 QnA.AnswerFormSetup(form);
140 if (QnA.AnswerFormMaster) {
141 // @todo if we had a cached for here's where we'd use it'
142 intermediateStep(QnA.AnswerFormMaster);
144 // Fetch a fresh copy of the answer form over AJAX.
145 // Warning: this can have a delay, which looks bad.
146 // @fixme this fallback may or may not work
147 var url = $('#answer-action').attr('value');
148 $.get(url, {ajax: 1}, function (data, textStatus, xhr) {
149 intermediateStep($('form', data)[0]);
157 * Setup function -- DOES NOT apply immediately.
159 * Sets up event handlers for inline reply mini-form placeholders.
160 * Uses 'live' rather than 'bind', so applies to future as well as present items.
162 NoticeInlineAnswerSetup: function () {
164 $(document).on('focus',
165 'li.qna-dummy-placeholder input.placeholder',
167 var notice = $(this).closest('li.notice');
168 QnA.NoticeInlineAnswerTrigger(notice);
174 AnswerFormSetup: function (form) {
176 form.find('textarea').focus();
178 if (!form.data('NoticeFormSetup')) {
182 if (!form.data('AnswerFormSetup')) {
183 //SN.U.NoticeLocationAttach(form);
184 QnA.FormAnswerXHR(form);
185 //SN.U.FormNoticeEnhancements(form);
186 //SN.U.NoticeDataAttach(form);
187 form.data('NoticeFormSetup', true);
192 * Setup function -- DOES NOT trigger actions immediately.
194 * Sets up event handlers for special-cased async submission of the
195 * answer-posting form, including some pre-post validation.
198 * @fixme refactor and unify with FormNoticeXHR in util.js
200 * @param {jQuery} form: jQuery object whose first element is a form
204 FormAnswerXHR: function (form) {
206 //SN.C.I.NoticeDataGeo = {};
207 form.append('<input type="hidden" name="ajax" value="1"/>');
209 // Make sure we don't have a mixed HTTP/HTTPS submission...
210 form.attr('action', SN.U.RewriteAjaxAction(form.attr('action')));
213 * Show a response feedback bit under the new-notice dialog.
215 * @param {String} cls: CSS class name to use ('error' or 'success')
216 * @param {String} text
219 var showFeedback = function (cls, text) {
221 $('<p class="form_response"></p>')
228 * Hide the previous response feedback, if any.
230 var removeFeedback = function () {
231 form.find('.form_response').remove();
238 beforeSend: function (formData) {
240 if (form.find('.notice_data-text:first').val() == '') {
241 form.addClass(SN.C.S.Warning);
245 .addClass(SN.C.S.Processing)
247 .addClass(SN.C.S.Disabled)
248 .attr(SN.C.S.Disabled, SN.C.S.Disabled);
251 error: function (xhr, textStatus, errorThrown) {
253 .removeClass(SN.C.S.Processing)
255 .removeClass(SN.C.S.Disabled)
256 .removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
258 if (textStatus == 'timeout') {
260 showFeedback('error', 'Sorry! We had trouble sending your notice. The servers are overloaded. Please try again, and contact the site administrator if this problem persists.');
263 var response = SN.U.GetResponseXML(xhr);
264 if ($('.'+SN.C.S.Error, response).length > 0) {
265 form.append(document._importNode($('.'+SN.C.S.Error, response)[0], true));
268 if (parseInt(xhr.status) === 0 || jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) >= 0) {
271 .find('.attach-status').remove();
272 SN.U.FormNoticeEnhancements(form);
276 showFeedback('error', '(Sorry! We had trouble sending your notice ('+xhr.status+' '+xhr.statusText+'). Please report the problem to the site administrator if this happens again.');
281 success: function (data, textStatus) {
284 var errorResult = $('#'+SN.C.S.Error, data);
285 if (errorResult.length > 0) {
286 showFeedback('error', errorResult.text());
290 // New notice post was successful. If on our timeline, show it!
291 var notice = document._importNode($('li', data)[0], true);
292 var notices = $('#notices_primary .notices:first');
293 var answerItem = form.closest('li.notice-answer');
294 var questionItem = form.closest('li.question');
296 var dummyAnswer = form.find('ul.qna-dummy', questionItem).remove();
297 if (answerItem.length > 0) {
299 // If this is an inline answer, remove the form...
300 var list = form.closest('.threaded-replies');
302 // if the inserted notice's parent question needs it give it a placeholder
303 var ans = questionItem.find('ul > li.hentry.notice.answer');
304 if (ans.length == 0) {
305 SN.U.NoticeInlineReplyPlaceholder(questionItem);
308 var id = $(notice).attr('id');
309 if ($("#"+id).length == 0) {
310 $(notice).insertBefore(answerItem);
314 // Realtime came through before us...
317 } else if (notices.length > 0 && SN.U.belongsOnTimeline(notice)) {
319 // Not a reply. If on our timeline, show it at the
320 if ($('#'+notice.id).length === 0) {
321 var notice_irt_value = form.find('#inreplyto').val();
322 var notice_irt = '#notices_primary #notice-'+notice_irt_value;
323 if($('body')[0].id == 'conversation') {
324 if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
325 $(notice_irt).append('<ul class="notices"></ul>');
327 $($(notice_irt+' .notices')[0]).append(notice);
330 notices.prepend(notice);
333 .css({display:'none'})
337 // realtime injected the notice first
340 // Not on a timeline that this belongs on?
341 // Just show a success message.
343 showFeedback('success', $('title', data).text());
347 complete: function (xhr, textStatus) {
349 .removeClass(SN.C.S.Processing)
351 .removeAttr(SN.C.S.Disabled)
352 .removeClass(SN.C.S.Disabled);
358 $(document).ready(function () {