X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=js%2Futil.js;h=b645e8b0298aa1dc0d5e218acac82f979f02aa6e;hb=51ef894a472cc9cc3bef66983f100926f57f6d07;hp=793626ed25bfd580374415bdd980d1e3ebceeae6;hpb=44455bae2a458bd4fb076323c1c5d3ae54478671;p=quix0rs-gnu-social.git diff --git a/js/util.js b/js/util.js index 793626ed25..b645e8b029 100644 --- a/js/util.js +++ b/js/util.js @@ -32,7 +32,6 @@ var SN = { // StatusNet MaxLength: 140, PatternUsername: /^[0-9a-zA-Z\-_.]*$/, HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307], - NoticeFormMaster: null // to be cloned from the one at top }, /** @@ -54,10 +53,20 @@ var SN = { // StatusNet NoticeDataGeo: 'notice_data-geo', NoticeDataGeoCookie: 'NoticeDataGeo', NoticeDataGeoSelected: 'notice_data-geo_selected', - StatusNetInstance: 'StatusNetInstance' } }, + V: { // Variables + // These get set on runtime via inline scripting, so don't put anything here. + }, + + /** + * list of callbacks, categorized into _callbacks['event_name'] = [ callback_function_1, callback_function_2 ] + * + * @access private + */ + _callbacks: {}, + /** * Map of localized message strings exported to script from the PHP * side via Action::getScriptMessages(). @@ -94,12 +103,12 @@ var SN = { // StatusNet * @access private */ FormNoticeEnhancements: function (form) { - if (jQuery.data(form[0], 'ElementData') === undefined) { + if ($.data(form[0], 'ElementData') === undefined) { var MaxLength = form.find('.count').text(); if (MaxLength === undefined) { MaxLength = SN.C.I.MaxLength; } - jQuery.data(form[0], 'ElementData', {MaxLength: MaxLength}); + $.data(form[0], 'ElementData', {MaxLength: MaxLength}); SN.U.Counter(form); @@ -122,7 +131,7 @@ var SN = { // StatusNet NDT.on('cut', delayedUpdate) .on('paste', delayedUpdate); } else { - form.find('.count').text(jQuery.data(form[0], 'ElementData').MaxLength); + form.find('.count').text($.data(form[0], 'ElementData').MaxLength); } }, @@ -143,7 +152,7 @@ var SN = { // StatusNet Counter: function (form) { SN.C.I.FormNoticeCurrent = form; - var MaxLength = jQuery.data(form[0], 'ElementData').MaxLength; + var MaxLength = $.data(form[0], 'ElementData').MaxLength; if (MaxLength <= 0) { return; @@ -217,6 +226,27 @@ var SN = { // StatusNet return url; }, + FormNoticeUniqueID: function (form) { + var oldId = form.attr('id'); + var newId = 'form_notice_' + Math.floor(Math.random()*999999999); + var attrs = ['name', 'for', 'id']; + for (var key in attrs) { + if (form.attr(attrs[key]) === undefined) { + continue; + } + form.attr(attrs[key], form.attr(attrs[key]).replace(oldId, newId)); + } + for (var key in attrs) { + form.find("[" + attrs[key] + "*='" + oldId + "']").each(function () { + if ($(this).attr(attrs[key]) === undefined) { + return; // since we're inside the each(function () { ... }); + } + var newAttr = $(this).attr(attrs[key]).replace(oldId, newId); + $(this).attr(attrs[key], newAttr); + }); + } + }, + /** * Grabs form data and submits it asynchronously, with 'ajax=1' * parameter added to the rest. @@ -320,21 +350,6 @@ var SN = { // StatusNet // Make sure we don't have a mixed HTTP/HTTPS submission... 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( - $('
') - .addClass(cls) - .text(text) - ); - }; - /** * Hide the previous response feedback, if any. */ @@ -344,7 +359,7 @@ var SN = { // StatusNet form.ajaxForm({ dataType: 'xml', - timeout: '60000', + timeout: SN.V.xhrTimeout, beforeSend: function (formData) { if (form.find('.notice_data-text:first').val() == '') { form.addClass(SN.C.S.Warning); @@ -369,20 +384,20 @@ var SN = { // StatusNet 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.'); + 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 { 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) { + if (parseInt(xhr.status) === 0 || $.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.'); + 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.'); } } } @@ -391,61 +406,9 @@ var SN = { // StatusNet removeFeedback(); var errorResult = $('#' + SN.C.S.Error, data); if (errorResult.length > 0) { - showFeedback('error', errorResult.text()); + SN.U.showFeedback(form, 'error', errorResult.text()); } else { - var commandResult = $('#' + SN.C.S.CommandResult, data); - 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 placeholder = list.find('.notice-reply-placeholder'); - replyItem.remove(); - - var id = $(notice).attr('id'); - if ($('#' + id).length == 0) { - $(notice).insertBefore(placeholder); - } // else Realtime came through before us... - - // ...and show the placeholder form. - placeholder.show(); - } 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('
' + file.name + '
'));
var img = $('')
.attr('title', tooltip)
.attr('alt', tooltip)
.attr('src', url)
.attr('style', 'height: 120px');
- form.find('.attach-status').append(img);
+ fileentry.append(img);
+ form.find('.attach-status').append(fileentry);
});
} else {
var img = $('').text(tooltip);
@@ -1043,7 +1009,7 @@ var SN = { // StatusNet
function removeNoticeDataGeo(error) {
label
- .attr('title', jQuery.trim(label.text()))
+ .attr('title', $.trim(label.text()))
.removeClass('checked');
form.find('[name=lat]').val('');
@@ -1120,12 +1086,12 @@ var SN = { // StatusNet
label.attr('title', label.text());
check.change(function () {
- if (check.prop('checked') === true || $.cookie(SN.C.S.NoticeDataGeoCookie) === null) {
+ if (check.prop('checked') === true || $.cookie(SN.C.S.NoticeDataGeoCookie) === undefined) {
label
.attr('title', NoticeDataGeo_text.ShareDisable)
.addClass('checked');
- if ($.cookie(SN.C.S.NoticeDataGeoCookie) === null || $.cookie(SN.C.S.NoticeDataGeoCookie) == 'disabled') {
+ if ($.cookie(SN.C.S.NoticeDataGeoCookie) === undefined || $.cookie(SN.C.S.NoticeDataGeoCookie) == 'disabled') {
if (navigator.geolocation) {
SN.U.NoticeGeoStatus(form, 'Requesting location from browser...');
navigator.geolocation.getCurrentPosition(
@@ -1290,56 +1256,6 @@ var SN = { // StatusNet
return date;
},
- /**
- * Some sort of object interface for storing some structured
- * information in a cookie.
- *
- * Appears to be used to save the last-used login nickname?
- * That's something that browsers usually take care of for us
- * these days, do we really need to do it? Does anything else
- * use this interface?
- *
- * @fixme what is this?
- * @fixme should this use non-cookie local storage when available?
- */
- StatusNetInstance: {
- /**
- * @fixme what is this?
- */
- Set: function (value) {
- var SNI = SN.U.StatusNetInstance.Get();
- if (SNI !== null) {
- value = $.extend(SNI, value);
- }
-
- $.cookie(
- SN.C.S.StatusNetInstance,
- JSON.stringify(value),
- {
- path: '/',
- expires: SN.U.GetFullYear(2029, 0, 1)
- });
- },
-
- /**
- * @fixme what is this?
- */
- Get: function () {
- var cookieValue = $.cookie(SN.C.S.StatusNetInstance);
- if (cookieValue !== undefined) {
- return JSON.parse(cookieValue);
- }
- return null;
- },
-
- /**
- * @fixme what is this?
- */
- Delete: function () {
- $.cookie(SN.C.S.StatusNetInstance, null);
- }
- },
-
/**
* Check if the current page is a timeline where the current user's
* posts should be displayed immediately on success.
@@ -1382,7 +1298,8 @@ var SN = { // StatusNet
*
* @param {String} tag
*/
- switchInputFormTab: function (tag) {
+ switchInputFormTab: function (tag, setFocus) {
+ if (typeof setFocus === 'undefined') { setFocus = true; }
// The one that's current isn't current anymore
$('.input_form_nav_tab.current').removeClass('current');
if (tag != null) {
@@ -1403,13 +1320,15 @@ var SN = { // StatusNet
return false;
}
- $('#input_form_' + tag)
+ var noticeForm = $('#input_form_' + tag)
.addClass('current')
.find('.ajax-notice').each(function () {
var form = $(this);
SN.Init.NoticeFormSetup(form);
- })
- .find('.notice_data-text').focus();
+ });
+ if (setFocus) {
+ noticeForm.find('.notice_data-text').focus();
+ }
return false;
},
@@ -1420,9 +1339,106 @@ var SN = { // StatusNet
var extended = $(selector);
extended.removeClass('extended_menu');
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(
+ $('')
+ .addClass(cls)
+ .text(text)
+ );
+ },
+
+ addCallback: function (ename, callback) {
+ // initialize to array if it's undefined
+ if (typeof SN._callbacks[ename] === 'undefined') {
+ SN._callbacks[ename] = [];
+ }
+ SN._callbacks[ename].push(callback);
+ },
+
+ runCallbacks: function (ename, data) {
+ if (typeof SN._callbacks[ename] === 'undefined') {
+ return;
+ }
+ for (cbname in SN._callbacks[ename]) {
+ SN._callbacks[ename][cbname](data);
+ }
}
},
+ 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('