Move some javascript out to an E (Event) object

This commit is contained in:
Mikael Nordfeldth 2015-06-14 21:57:39 +02:00
parent a03249dd99
commit 01cbe744b6

View File

@ -343,21 +343,6 @@ var SN = { // StatusNet
// Make sure we don't have a mixed HTTP/HTTPS submission... // Make sure we don't have a mixed HTTP/HTTPS submission...
form.attr('action', SN.U.RewriteAjaxAction(form.attr('action'))); form.attr('action', SN.U.RewriteAjaxAction(form.attr('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. * Hide the previous response feedback, if any.
*/ */
@ -392,7 +377,7 @@ var SN = { // StatusNet
removeFeedback(); removeFeedback();
if (textStatus == 'timeout') { if (textStatus == 'timeout') {
// @fixme i18n // @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.'); SN.U.showFeedback(form, '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 { } else {
var response = SN.U.GetResponseXML(xhr); var response = SN.U.GetResponseXML(xhr);
if ($('.' + SN.C.S.Error, response).length > 0) { if ($('.' + SN.C.S.Error, response).length > 0) {
@ -405,7 +390,7 @@ var SN = { // StatusNet
SN.U.FormNoticeEnhancements(form); SN.U.FormNoticeEnhancements(form);
} else { } else {
// @fixme i18n // @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.'); SN.U.showFeedback(form, 'error', '(Sorry! We had trouble sending your notice (' + xhr.status + ' ' + xhr.statusText + '). Please report the problem to the site administrator if this happens again.');
} }
} }
} }
@ -414,59 +399,9 @@ var SN = { // StatusNet
removeFeedback(); removeFeedback();
var errorResult = $('#' + SN.C.S.Error, data); var errorResult = $('#' + SN.C.S.Error, data);
if (errorResult.length > 0) { if (errorResult.length > 0) {
showFeedback('error', errorResult.text()); SN.U.showFeedback(form, 'error', errorResult.text());
} else { } else {
var commandResult = $('#' + SN.C.S.CommandResult, data); SN.E.ajaxNoticePosted(form, data, textStatus);
if (commandResult.length > 0) {
showFeedback('success', commandResult.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) {
// If this is an inline reply, remove the form...
var list = form.closest('.threaded-replies');
var id = $(notice).attr('id');
if ($('#' + id).length == 0) {
$(notice).insertBefore(replyItem);
} // else Realtime came through before us...
replyItem.remove();
} 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) {
var notice_irt_value = form.find('[name=inreplyto]').val();
var notice_irt = '#notices_primary #notice-' + notice_irt_value;
if ($('body')[0].id == '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 {
notices.prepend(notice);
}
$('#' + notice.id)
.css({display: 'none'})
.fadeIn(2500);
SN.U.NoticeWithAttachment($('#' + notice.id));
SN.U.switchInputFormTab(null);
}
} else {
// Not on a timeline that this belongs on?
// Just show a success message.
// @fixme inline
showFeedback('success', $('title', data).text());
}
}
form.resetForm();
form.find('[name=inreplyto]').val('');
form.find('.attach-status').remove();
SN.U.FormNoticeEnhancements(form);
} }
}, },
complete: function (xhr, textStatus) { complete: function (xhr, textStatus) {
@ -1442,9 +1377,88 @@ var SN = { // StatusNet
var extended = $(selector); var extended = $(selector);
extended.removeClass('extended_menu'); extended.removeClass('extended_menu');
return void(0); return void(0);
} },
/**
* Show a response feedback bit under a form.
*
* @param {Element} form: the new-notice form usually
* @param {String} cls: CSS class name to use ('error' or 'success')
* @param {String} text
* @access public
*/
showFeedback: function (form, cls, text) {
form.append(
$('<p class="form_response"></p>')
.addClass(cls)
.text(text)
);
},
}, },
E: { /* Events */
/* SN.E.ajaxNoticePosted, called when a notice has been posted successfully via an AJAX form
@param form the originating form element
@param data data from success() callback
@param textStatus textStatus from success() callback
*/
ajaxNoticePosted: function (form, data, textStatus) {
var commandResult = $('#' + SN.C.S.CommandResult, data);
if (commandResult.length > 0) {
SN.U.showFeedback(form, 'success', commandResult.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) {
// If this is an inline reply, remove the form...
var list = form.closest('.threaded-replies');
var id = $(notice).attr('id');
if ($('#' + id).length == 0) {
$(notice).insertBefore(replyItem);
} // else Realtime came through before us...
replyItem.remove();
} 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) {
var notice_irt_value = form.find('[name=inreplyto]').val();
var notice_irt = '#notices_primary #notice-' + notice_irt_value;
if ($('body')[0].id == '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 {
notices.prepend(notice);
}
$('#' + notice.id)
.css({display: 'none'})
.fadeIn(2500);
SN.U.NoticeWithAttachment($('#' + notice.id));
SN.U.switchInputFormTab(null);
}
} else {
// Not on a timeline that this belongs on?
// Just show a success message.
// @fixme inline
SN.U.showFeedback(form, 'success', $('title', data).text());
}
}
form.resetForm();
form.find('[name=inreplyto]').val('');
form.find('.attach-status').remove();
SN.U.FormNoticeEnhancements(form);
alert('notice posted: ' + $(notice).attr('id'));
},
},
Init: { Init: {
/** /**
* If user is logged in, run setup code for the new notice form: * If user is logged in, run setup code for the new notice form: