X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=js%2Futil.js;h=e6806ce5386d1c9f93dfe77e4eb5b415355dd3ba;hb=3c5383b11c2ea99b64c395a9703def8db0426b8d;hp=ae9a67dbb36dad6d8a10130c6b775374882565ed;hpb=a4d04d24de59f2694aaeb65dda50afe40a58b931;p=quix0rs-gnu-social.git diff --git a/js/util.js b/js/util.js index ae9a67dbb3..e6806ce538 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 }, /** @@ -94,12 +93,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 +121,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 +142,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; @@ -308,7 +307,6 @@ var SN = { // StatusNet * @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 * @@ -376,7 +374,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(); @@ -394,11 +392,6 @@ var SN = { // StatusNet if (errorResult.length > 0) { showFeedback('error', errorResult.text()); } else { - if ($('body')[0].id == 'bookmarklet') { - // @fixme self is not referenced anywhere? - self.close(); - } - var commandResult = $('#' + SN.C.S.CommandResult, data); if (commandResult.length > 0) { showFeedback('success', commandResult.text()); @@ -411,16 +404,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! @@ -439,7 +430,7 @@ var SN = { // StatusNet .css({display: 'none'}) .fadeIn(2500); SN.U.NoticeWithAttachment($('#' + notice.id)); - SN.U.switchInputFormTab("placeholder"); + SN.U.switchInputFormTab(null); } } else { // Not on a timeline that this belongs on? @@ -546,7 +537,7 @@ var SN = { // StatusNet var cookieValue = $.cookie(SN.C.S.NoticeDataGeoCookie); - if (cookieValue !== null && cookieValue != 'disabled') { + if (cookieValue !== undefined && cookieValue != 'disabled') { cookieValue = JSON.parse(cookieValue); SN.C.I.NoticeDataGeo.NLat = form.find('[name=lat]').val(cookieValue.NLat).val(); SN.C.I.NoticeDataGeo.NLon = form.find('[name=lon]').val(cookieValue.NLon).val(); @@ -584,6 +575,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. * @@ -622,43 +651,14 @@ 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 - // Find the threaded replies view we'll be adding to... - var list = notice.closest('.notices'); - if (list.closest('.old-school').length) { - // We're replying to an old-school conversation thread; - // use the old-style ping into the top form. - SN.U.switchInputFormTab("status"); - replyForm = $('#input_form_status').find('form'); - stripForm = false; - } else if (list.hasClass('threaded-replies')) { - // We're replying to a reply; use reply form on the end of this list. - // We'll add our form at the end of this; grab the root notice. - parentNotice = list.closest('.notice'); - - // See if the form's already open... - replyForm = $('.notice-reply-form', list); - } else { - // We're replying to a parent notice; pull its threaded list - // and we'll add on the end of it. Will add if needed. - list = $('ul.threaded-replies', notice); - if (list.length == 0) { - SN.U.NoticeInlineReplyPlaceholder(notice); - list = $('ul.threaded-replies', notice); - } else { - placeholder = $('li.notice-reply-placeholder', notice); - if (placeholder.length == 0) { - SN.U.NoticeInlineReplyPlaceholder(notice); - } - } - - // See if the form's already open... - replyForm = $('.notice-reply-form', list); + var list = notice.find('.threaded-replies'); + if (list.length == 0) { + list = notice.closest('.threaded-replies'); } - var nextStep = function () { // Override...? replyForm.find('input[name=inreplyto]').val(id); @@ -669,6 +669,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'); @@ -687,83 +688,53 @@ var SN = { // StatusNet text[0].setSelectionRange(len, len); } }; - if (replyForm.length > 0) { - // Update the existing form... - nextStep(); - } else { - // Hide the placeholder... - placeholder = list.find('li.notice-reply-placeholder').hide(); - // Create the reply form entry at the end - var replyItem = $('li.notice-reply', list); - if (replyItem.length == 0) { - replyItem = $('
  • '); + // Create the reply form entry + var replyItem = $('li.notice-reply', list); + if (replyItem.length == 0) { + replyItem = $('
  • '); - var intermediateStep = function (formMaster) { - var formEl = document._importNode(formMaster, true); + // Fetch a fresh copy of the notice form over AJAX. + var url = $('#input_form_status > form').attr('action'); + $.ajax({ + url: url, + data: {ajax: 1, inreplyto: id}, + success: function (data, textStatus, xhr) { + var formEl = document._importNode($('form', data)[0], true); replyItem.append(formEl); - list.append(replyItem); // *after* the placeholder - - var form = $(formEl); - replyForm = form; - SN.Init.NoticeFormSetup(form); + list.append(replyItem); + replyForm = $(formEl); + SN.Init.NoticeFormSetup(replyForm); 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]); - }); - } - } - } - }, - - NoticeInlineReplyPlaceholder: function (notice) { - var list = notice.find('ul.threaded-replies'); - if (list.length == 0) { - list = $(''); - notice.append(list); - list = notice.find('ul.threaded-replies'); + }, + }); + } else { + replyForm = replyItem.children('form'); + SN.Init.NoticeFormSetup(replyForm); + nextStep(); } - var placeholder = $('
  • ' + - '' + - '
  • '); - placeholder.find('input') - .val(SN.msg('reply_placeholder')); - list.append(placeholder); }, /** * Setup function -- DOES NOT apply immediately. * - * Sets up event handlers for inline reply mini-form placeholders. * Uses 'on' rather than 'live' or 'bind', so applies to future as well as present items. */ NoticeInlineReplySetup: function () { - $('li.notice-reply-placeholder input') - .on('focus', function () { - var notice = $(this).closest('li.notice'); - SN.U.NoticeInlineReplyTrigger(notice); - return false; - }); - $('li.notice-reply-comments a') - .on('click', function () { + // Expand conversation links + $(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; }); @@ -779,7 +750,7 @@ var SN = { // StatusNet * */ NoticeRepeat: function () { - $('.form_repeat').on('click', function (e) { + $('body').on('click', '.form_repeat', function (e) { e.preventDefault(); SN.U.NoticeRepeatConfirmation($(this)); @@ -863,18 +834,16 @@ var SN = { // StatusNet return; } - var attachment_more = notice.find('.attachment.more'); - if (attachment_more.length > 0) { - $(attachment_more[0]).click(function () { - var m = $(this); - m.addClass(SN.C.S.Processing); - $.get(m.attr('href') + '/ajax', null, function (data) { - m.parent('.entry-content').html($(data).find('#attachment_view .entry-content').html()); - }); + $(document).on('click','.attachment.more',function () { + var m = $(this); + m.addClass(SN.C.S.Processing); + $.get(m.attr('href'), {ajax: 1}, function (data) { + m.parent('.e-content').html($(data).find('#attachment_view .e-content').html()); + }); + + return false; + }); - return false; - }).attr('title', SN.msg('showmore_tooltip')); - } }, /** @@ -1052,7 +1021,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(''); @@ -1183,18 +1152,22 @@ var SN = { // StatusNet } } } else { - var cookieValue = JSON.parse($.cookie(SN.C.S.NoticeDataGeoCookie)); - - form.find('[name=lat]').val(cookieValue.NLat); - form.find('[name=lon]').val(cookieValue.NLon); - form.find('[name=location_ns]').val(cookieValue.NLNS); - form.find('[name=location_id]').val(cookieValue.NLID); - form.find('[name=notice_data-geo]').prop('checked', cookieValue.NDG); - - SN.U.NoticeGeoStatus(form, cookieValue.NLN, cookieValue.NLat, cookieValue.NLon, cookieValue.NLNU); - label - .attr('title', NoticeDataGeo_text.ShareDisable + ' (' + cookieValue.NLN + ')') - .addClass('checked'); + try { + var cookieValue = JSON.parse($.cookie(SN.C.S.NoticeDataGeoCookie)); + + form.find('[name=lat]').val(cookieValue.NLat); + form.find('[name=lon]').val(cookieValue.NLon); + form.find('[name=location_ns]').val(cookieValue.NLNS); + form.find('[name=location_id]').val(cookieValue.NLID); + form.find('[name=notice_data-geo]').prop('checked', cookieValue.NDG); + + SN.U.NoticeGeoStatus(form, cookieValue.NLN, cookieValue.NLat, cookieValue.NLon, cookieValue.NLNU); + label + .attr('title', NoticeDataGeo_text.ShareDisable + ' (' + cookieValue.NLN + ')') + .addClass('checked'); + } catch (e) { + console.log('Parsing error:', e); + } } } else { removeNoticeDataGeo(); @@ -1331,7 +1304,7 @@ var SN = { // StatusNet */ Get: function () { var cookieValue = $.cookie(SN.C.S.StatusNetInstance); - if (cookieValue !== null) { + if (cookieValue !== undefined) { return JSON.parse(cookieValue); } return null; @@ -1363,7 +1336,7 @@ var SN = { // StatusNet var profileLink = $('#nav_profile a').attr('href'); if (profileLink) { - var authorUrl = $(notice).find('.vcard.author a.url').attr('href'); + var authorUrl = $(notice).find('.h-card.p-author').attr('href'); if (authorUrl == profileLink) { if (action == 'all' || action == 'showstream') { // Posts always show on your own friends and profile streams. @@ -1376,7 +1349,7 @@ var SN = { // StatusNet // Mismatch between id-based and name-based user/group links currently complicates // the lookup, since all our inline mentions contain the absolute links but the // UI links currently on the page use malleable names. - + return false; }, @@ -1387,14 +1360,11 @@ var SN = { // StatusNet * * @param {String} tag */ - switchInputFormTab: function (tag) { - // The one that's current isn't current anymore - $('.input_form_nav_tab.current').removeClass('current'); - if (tag == 'placeholder') { - // Hack: when showing the placeholder, mark the tab - // as current for 'Status'. - $('#input_form_nav_status').addClass('current'); - } else { + 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) { $('#input_form_nav_' + tag).addClass('current'); } @@ -1406,15 +1376,24 @@ var SN = { // StatusNet return; } - $('.input_form.current').removeClass('current'); - $('#input_form_' + tag) - .addClass('current') - .find('.ajax-notice').each(function () { - var form = $(this); - SN.Init.NoticeFormSetup(form); - }) - .find('.notice_data-text').focus(); - }, + $('.input_form.current').removeClass('current'); + if (tag == null) { + // we're done here, no new inputform to focus on + return false; + } + + var noticeForm = $('#input_form_' + tag) + .addClass('current') + .find('.ajax-notice').each(function () { + var form = $(this); + SN.Init.NoticeFormSetup(form); + }); + if (setFocus) { + noticeForm.find('.notice_data-text').focus(); + } + + return false; + }, showMoreMenuItems: function (menuid) { $('#' + menuid + ' .more_link').remove(); @@ -1439,28 +1418,14 @@ var SN = { // StatusNet // SN.Init.NoticeFormSetup() will get run // when forms get displayed for the first time... - // Hack to initialize the placeholder at top - $('#input_form_placeholder input.placeholder').focus(function () { - SN.U.switchInputFormTab("status"); + // Initialize the input form field + $('#input_form_nav .input_form_nav_tab.current').each(function () { + current_tab_id = $(this).attr('id').substring('input_form_nav_'.length); + SN.U.switchInputFormTab(current_tab_id, false); }); // Make inline reply forms self-close when clicking out. $('body').on('click', function (e) { - var currentForm = $('#content .input_forms div.current'); - if (currentForm.length > 0) { - if ($('#content .input_forms').has(e.target).length == 0) { - // If all fields are empty, switch back to the placeholder. - var fields = currentForm.find('textarea, input[type=text], input[type=""]'); - var anything = false; - fields.each(function () { - anything = anything || $(this).val(); - }); - if (!anything) { - SN.U.switchInputFormTab("placeholder"); - } - } - } - var openReplies = $('li.notice-reply'); if (openReplies.length > 0) { var target = $(e.target); @@ -1473,17 +1438,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 }); - } }, @@ -1495,13 +1456,14 @@ 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.FormNoticeXHR(form); + SN.U.FormNoticeEnhancements(form); + SN.U.NoticeDataAttach(form); + form.data('NoticeFormSetup', true); }, /** @@ -1512,13 +1474,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(); @@ -1576,60 +1535,6 @@ var SN = { // StatusNet }); }, - /** - * Called when a people tag edit box is shown in the interface - * - * - loads the jQuery UI autocomplete plugin - * - sets event handlers for tag completion - * - */ - PeopletagAutocomplete: function (txtBox) { - var split = function (val) { - return val.split( /\s+/ ); - } - var extractLast = function (term) { - return split(term).pop(); - } - - // don't navigate away from the field on tab when selecting an item - txtBox.on( "keydown", function ( event ) { - if ( event.keyCode === $.ui.keyCode.TAB && - $(this).data( "autocomplete" ).menu.active ) { - event.preventDefault(); - } - }).autocomplete({ - minLength: 0, - source: function (request, response) { - // delegate back to autocomplete, but extract the last term - response($.ui.autocomplete.filter( - SN.C.PtagACData, extractLast(request.term))); - }, - focus: function () { - return false; - }, - select: function (event, ui) { - var terms = split(this.value); - terms.pop(); - terms.push(ui.item.value); - terms.push(""); - this.value = terms.join(" "); - return false; - } - }).data('autocomplete')._renderItem = function (ul, item) { - // FIXME: with jQuery UI you cannot have it highlight the match - var _l = '
    ' + item.tag - + ' ' + item.mode + '' - + '' + item.freq + '' - - return $("
  • ") - .addClass('mode-' + item.mode) - .addClass('ptag-ac-line') - .data("item.autocomplete", item) - .append(_l) - .appendTo(ul); - } - }, - /** * Run setup for the ajax people tags editor * @@ -1658,7 +1563,6 @@ var SN = { // StatusNet } SN.C.PtagACData = data; - SN.Init.PeopletagAutocomplete(form.find('#tags')); } });