X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=js%2Futil.js;h=190c1e4f43647724d313ddc417cce8851bcc7bff;hb=abfa0528611018ab3e4b377f6ae9f6f40e07e29b;hp=9001a401c2b373183c1697c06a912db2f4bb4bf1;hpb=0da75125974b19c4a5bd2173fd57927b1e1b8e07;p=quix0rs-gnu-social.git diff --git a/js/util.js b/js/util.js index 9001a401c2..190c1e4f43 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 }, /** @@ -58,6 +57,9 @@ var SN = { // StatusNet } }, + V: { // Variables + }, + /** * Map of localized message strings exported to script from the PHP * side via Action::getScriptMessages(). @@ -94,12 +96,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 +124,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 +145,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 +219,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. @@ -375,7 +398,7 @@ var SN = { // StatusNet 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(); @@ -405,16 +428,14 @@ var SN = { // StatusNet 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); + $(notice).insertBefore(replyItem); } // else Realtime came through before us... - // ...and show the placeholder form. - placeholder.show(); + replyItem.remove(); + } else if (notices.length > 0 && SN.U.belongsOnTimeline(notice)) { // Not a reply. If on our timeline, show it at the top! @@ -578,6 +599,44 @@ var SN = { // StatusNet } }, + /** + * Setup function -- DOES NOT trigger actions immediately. + * + * Sets up event handlers on all visible notice's option elements + * with the "popup" class so they behave as expected with AJAX. + * + * (without javascript the link goes to a page that expects you to verify + * the action through a form) + * + * @access private + */ + NoticeOptionsAjax: function () { + $(document).on('click', '.notice-options > a.popup', function (e) { + e.preventDefault(); + var noticeEl = $(this).closest('.notice'); + $.ajax({ + url: $(this).attr('href'), + data: {ajax: 1}, + success: function (data, textStatus, xhr) { + SN.U.NoticeOptionPopup(data, noticeEl); + }, + }); + return false; + }); + }, + + NoticeOptionPopup: function (data, noticeEl) { + title = $('head > title', data).text(); + body = $('body', data).html(); + dialog = $(body).dialog({ + height: "auto", + width: "auto", + modal: true, + resizable: true, + title: title, + }); + }, + /** * Setup function -- DOES NOT trigger actions immediately. * @@ -616,22 +675,18 @@ var SN = { // StatusNet NoticeInlineReplyTrigger: function (notice, initialText) { // Find the notice we're replying to... var id = $($('.notice_id', notice)[0]).text(); - var replyForm, placeholder; + var replyForm; var parentNotice = notice; var stripForm = true; // strip a couple things out of reply forms that are inline - var list = notice.closest('.notices'); - if (list.hasClass('threaded-replies')) { - // We're replying to a reply; use reply form on the end of this list. - } else { - // We're replying to a parent notice; pull its threaded list - // and we'll add on the end of it. Will add the threaded list if needed. - var list = $('ul.threaded-replies', notice); - if (list.length == 0) { - list = $(''); - notice.append(list); - list = notice.find('ul.threaded-replies'); - } + var list = notice.find('.threaded-replies'); + if (list.length == 0) { + list = notice.closest('.threaded-replies'); + } + if (list.length == 0) { + list = $(''); + notice.append(list); + list = notice.find('.threaded-replies'); } var nextStep = function () { @@ -644,6 +699,7 @@ var SN = { // StatusNet replyForm.find('label[for=notice_to]').hide(); replyForm.find('label[for=notice_private]').hide(); } + replyItem.show(); // Set focus... var text = replyForm.find('textarea'); @@ -663,43 +719,42 @@ var SN = { // StatusNet } }; - // See if the form's already open... - replyForm = $('.notice-reply-form', list); - if (replyForm.length > 0) { - // Update the existing form... - nextStep(); - } else { - // Create the reply form entry at the end - var replyItem = $('li.notice-reply > form', list); - if (replyItem.length == 0) { - replyItem = $('
  • '); - - var intermediateStep = function (formMaster) { - var formEl = document._importNode(formMaster, true); - replyItem.append(formEl); - list.append(replyItem); // *after* the placeholder - - var form = $(formEl); - replyForm = form; - SN.Init.NoticeFormSetup(form); - - nextStep(); - }; - if (SN.C.I.NoticeFormMaster) { - // We've already saved a master copy of the form. - // Clone it in! - intermediateStep(SN.C.I.NoticeFormMaster); - } else { - // Fetch a fresh copy of the notice form over AJAX. - // Warning: this can have a delay, which looks bad. - // @fixme this fallback may or may not work - var url = $('#form_notice').attr('action'); - $.get(url, {ajax: 1}, function (data, textStatus, xhr) { - intermediateStep($('form', data)[0]); - }); - } + // Create the reply form entry + var replyItem = $('li.notice-reply', list); + if (replyItem.length == 0) { + replyItem = $('
  • '); + } + replyForm = replyItem.children('form'); + if (replyForm.length == 0) { + // Let's try another trick to avoid fetching by URL + var noticeForm = $('#input_form_status > form'); + if (noticeForm.length == 0) { + // No notice form found on the page, so let's just + // fetch a fresh copy of the notice form over AJAX. + $.ajax({ + url: SN.V.urlNewNotice, + data: {ajax: 1, inreplyto: id}, + success: function (data, textStatus, xhr) { + var formEl = document._importNode($('form', data)[0], true); + replyForm = $(formEl); + replyItem.append(replyForm); + list.append(replyItem); + + SN.Init.NoticeFormSetup(replyForm); + nextStep(); + }, + }); + // We do everything relevant in 'success' above + return; } + replyForm = noticeForm.clone(); + SN.Init.NoticeFormSetup(replyForm); + replyItem.append(replyForm); + list.append(replyItem); } + // replyForm is set, we're not fetching by URL... + // Next setp is to configure in-reply-to etc. + nextStep(); }, /** @@ -712,11 +767,15 @@ var SN = { // StatusNet $(document).on('click', 'li.notice-reply-comments a', function () { var url = $(this).attr('href'); var area = $(this).closest('.threaded-replies'); - $.get(url, {ajax: 1}, function (data, textStatus, xhr) { - var replies = $('.threaded-replies', data); - if (replies.length) { - area.replaceWith(document._importNode(replies[0], true)); - } + $.ajax({ + url: url, + data: {ajax: 1}, + success: function (data, textStatus, xhr) { + var replies = $('.threaded-replies', data); + if (replies.length) { + area.replaceWith(document._importNode(replies[0], true)); + } + }, }); return false; }); @@ -1003,7 +1062,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(''); @@ -1420,16 +1479,13 @@ var SN = { // StatusNet // Only close if there's been no edit. if (cur == '' || cur == textarea.data('initialText')) { var parentNotice = replyItem.closest('li.notice'); - replyItem.remove(); + replyItem.hide(); parentNotice.find('li.notice-reply-placeholder').show(); } } }); } }); - - // Infield labels for notice form inputs. - $('.input_forms fieldset fieldset label').inFieldLabels({ fadeOpacity:0 }); } }, @@ -1441,13 +1497,15 @@ var SN = { // StatusNet * @param {jQuery} form */ NoticeFormSetup: function (form) { - if (!form.data('NoticeFormSetup')) { - SN.U.NoticeLocationAttach(form); - SN.U.FormNoticeXHR(form); - SN.U.FormNoticeEnhancements(form); - SN.U.NoticeDataAttach(form); - form.data('NoticeFormSetup', true); + if (form.data('NoticeFormSetup')) { + return false; } + SN.U.NoticeLocationAttach(form); + SN.U.FormNoticeUniqueID(form); + SN.U.FormNoticeXHR(form); + SN.U.FormNoticeEnhancements(form); + SN.U.NoticeDataAttach(form); + form.data('NoticeFormSetup', true); }, /** @@ -1458,13 +1516,10 @@ var SN = { // StatusNet */ Notices: function () { if ($('body.user_in').length > 0) { - var masterForm = $('.form_notice:first'); - if (masterForm.length > 0) { - SN.C.I.NoticeFormMaster = document._importNode(masterForm[0], true); - } SN.U.NoticeRepeat(); SN.U.NoticeReply(); SN.U.NoticeInlineReplySetup(); + SN.U.NoticeOptionsAjax(); } SN.U.NoticeAttachments();