+ },
+
+ AnswerFormSetup: function(form) {
+ console.log("AnswerFormSetup");
+ $('input[type=submit]').live('click', function() {
+ console.log("AnswerFormSetup click");
+ QnA.FormAnswerXHR(form);
+ });
+ },
+
+
+ /**
+ * Setup function -- DOES NOT trigger actions immediately.
+ *
+ * Sets up event handlers for special-cased async submission of the
+ * notice-posting form, including some pre-post validation.
+ *
+ * Unlike FormXHR() this does NOT submit the form immediately!
+ * It sets up event handlers so that any method of submitting the
+ * form (click on submit button, enter, submit() etc) will trigger
+ * it properly.
+ *
+ * Also unlike FormXHR(), this system will use a hidden iframe
+ * automatically to handle file uploads via <input type="file">
+ * controls.
+ *
+ * @fixme tl;dr
+ * @fixme vast swaths of duplicate code and really long variable names clutter this function up real bad
+ * @fixme error handling is unreliable
+ * @fixme cookieValue is a global variable, but probably shouldn't be
+ * @fixme saving the location cache cookies should be split out
+ * @fixme some error messages are hardcoded english: needs i18n
+ * @fixme special-case for bookmarklet is confusing and uses a global var "self". Is this ok?
+ *
+ * @param {jQuery} form: jQuery object whose first element is a form
+ *
+ * @access public
+ */
+ FormAnswerXHR: function(form) {
+ console.log("FormAanwerXHR - begin");
+ //SN.C.I.NoticeDataGeo = {};
+ form.append('<input type="hidden" name="ajax" value="1"/>');
+ console.log("appended ajax flag");
+
+ // Make sure we don't have a mixed HTTP/HTTPS submission...
+ form.attr('action', SN.U.RewriteAjaxAction(form.attr('action')));
+ console.log("rewrote action");
+
+ /**
+ * Show a response feedback bit under the new-notice dialog.
+ *
+ * @param {String} cls: CSS class name to use ('error' or 'success')
+ * @param {String} text
+ * @access private
+ */
+ var showFeedback = function(cls, text) {
+ form.append(
+ $('<p class="form_response"></p>')
+ .addClass(cls)
+ .text(text)
+ );
+ };
+
+ /**
+ * Hide the previous response feedback, if any.
+ */
+ var removeFeedback = function() {
+ form.find('.form_response').remove();
+ };
+
+ console.log("doing ajax");
+
+ form.ajaxForm({
+ dataType: 'xml',
+ timeout: '60000',
+ beforeSend: function(formData) {
+ console.log("beforeSend");
+ if (form.find('.notice_data-text:first').val() == '') {
+ form.addClass(SN.C.S.Warning);
+ return false;
+ }
+ form
+ .addClass(SN.C.S.Processing)
+ .find('.submit')
+ .addClass(SN.C.S.Disabled)
+ .attr(SN.C.S.Disabled, SN.C.S.Disabled);
+
+ SN.U.normalizeGeoData(form);
+
+ return true;
+ },
+ error: function (xhr, textStatus, errorThrown) {
+ form
+ .removeClass(SN.C.S.Processing)
+ .find('.submit')
+ .removeClass(SN.C.S.Disabled)
+ .removeAttr(SN.C.S.Disabled, SN.C.S.Disabled);
+ removeFeedback();
+ if (textStatus == 'timeout') {
+ // @fixme i18n
+ 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.');
+ }
+ else {
+ var response = SN.U.GetResponseXML(xhr);
+ if ($('.'+SN.C.S.Error, response).length > 0) {
+ form.append(document._importNode($('.'+SN.C.S.Error, response)[0], true));
+ }
+ else {
+ if (parseInt(xhr.status) === 0 || jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) >= 0) {
+ form
+ .resetForm()
+ .find('.attach-status').remove();
+ SN.U.FormNoticeEnhancements(form);
+ }
+ else {
+ // @fixme i18n
+ 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.');
+ }
+ }
+ }
+ },
+ success: function(data, textStatus) {
+ console.log("FormAnswerHXR success");
+ removeFeedback();
+ var errorResult = $('#'+SN.C.S.Error, data);
+ if (errorResult.length > 0) {
+ showFeedback('error', errorResult.text());
+ }
+ else {
+
+ // New notice post was successful. If on our timeline, show it!
+ var notice = document._importNode($('li', data)[0], true);
+
+ var notices = $('#notices_primary .notices:first');
+ var replyItem = form.closest('li.notice-reply');
+
+ if (replyItem.length > 0) {
+ console.log("I found a reply li to append to");
+ // If this is an inline reply, remove the form...
+ var list = form.closest('.threaded-replies');
+ var placeholder = list.find('.notice-answer-placeholder');
+ replyItem.remove();
+
+ var id = $(notice).attr('id');
+ console.log("got notice id " + id);
+ if ($("#"+id).length == 0) {
+ console.log("inserting before placeholder");
+ $(notice).insertBefore(placeholder);
+ } else {
+ // Realtime came through before us...
+ }
+
+ // ...and show the placeholder form.
+ placeholder.show();
+ console.log('qqqq made it this far')
+ } else if (notices.length > 0 && SN.U.belongsOnTimeline(notice)) {
+ // Not a reply. If on our timeline, show it at the top!
+ if ($('#'+notice.id).length === 0) {
+ console.log("couldn't find a notice id for " + notice.id);
+ var notice_irt_value = form.find('#inreplyto').val();
+ var notice_irt = '#notices_primary #notice-'+notice_irt_value;
+ console.log("notice_irt value = " + notice_irt_value);
+ if($('body')[0].id == 'conversation') {
+ console.log("found conversation");
+ if(notice_irt_value.length > 0 && $(notice_irt+' .notices').length < 1) {
+ $(notice_irt).append('<ul class="notices"></ul>');
+ }
+ $($(notice_irt+' .notices')[0]).append(notice);
+ }
+ else {
+ console.log("prepending notice")
+ notices.prepend(notice);
+ }
+ $('#'+notice.id)
+ .css({display:'none'})
+ .fadeIn(2500);
+ }
+ } else {
+ // Not on a timeline that this belongs on?
+ // Just show a success message.
+ // @fixme inline
+ showFeedback('success', $('title', data).text());
+ }
+
+ //form.resetForm();
+ //SN.U.FormNoticeEnhancements(form);
+ }
+ },
+ complete: function(xhr, textStatus) {
+ form
+ .removeClass(SN.C.S.Processing)
+ .find('.submit')
+ .removeAttr(SN.C.S.Disabled)
+ .removeClass(SN.C.S.Disabled);
+
+ form.find('[name=lat]').val(SN.C.I.NoticeDataGeo.NLat);
+ form.find('[name=lon]').val(SN.C.I.NoticeDataGeo.NLon);
+ form.find('[name=location_ns]').val(SN.C.I.NoticeDataGeo.NLNS);
+ form.find('[name=location_id]').val(SN.C.I.NoticeDataGeo.NLID);
+ form.find('[name=notice_data-geo]').attr('checked', SN.C.I.NoticeDataGeo.NDG);
+ }
+ });