]> git.mxchange.org Git - friendica.git/blobdiff - view/js/main.js
We now store the state in a cookie
[friendica.git] / view / js / main.js
index 4ccbc8044ebb20b636b8cb824635c8fc75de910d..c6cf0a85b337b2f60e0a518df6a43770c1b19464 100644 (file)
@@ -1,3 +1,21 @@
+// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPLv3-or-later
+
+// https://developer.mozilla.org/en-US/docs/Web/API/Element/matches#Polyfill
+if (!Element.prototype.matches) {
+       Element.prototype.matches =
+               Element.prototype.matchesSelector ||
+               Element.prototype.mozMatchesSelector ||
+               Element.prototype.msMatchesSelector ||
+               Element.prototype.oMatchesSelector ||
+               Element.prototype.webkitMatchesSelector ||
+               function(s) {
+                       var matches = (this.document || this.ownerDocument).querySelectorAll(s),
+                               i = matches.length;
+                       while (--i >= 0 && matches.item(i) !== this) {}
+                       return i > -1;
+               };
+}
+
 function resizeIframe(obj) {
        _resizeIframe(obj, 0);
 }
@@ -15,26 +33,75 @@ function _resizeIframe(obj, desth) {
        setTimeout(_resizeIframe, 100, obj, ch);
 }
 
-function openClose(theID) {
-       if (document.getElementById(theID).style.display == "block") {
-               document.getElementById(theID).style.display = "none"
+function getCookie(name) {
+       var value = "; " + document.cookie;
+       var parts = value.split("; " + name + "=");
+
+       if (parts.length == 2) {
+               return parts.pop().split(";").shift();
+       }
+}
+
+function initWidget(inflated, deflated) {
+       var elInf = document.getElementById(inflated);
+       var elDef = document.getElementById(deflated);
+
+       if (!elInf || !elDef) {
+               return;
+       }
+       if (getCookie(window.location.pathname + ":" + inflated) != "none") {
+               elInf.style.display = "block";
+               elDef.style.display = "none";
        } else {
-               document.getElementById(theID).style.display = "block"
+               elInf.style.display = "none";
+               elDef.style.display = "block";
        }
 }
 
-function openMenu(theID) {
-       var el = document.getElementById(theID)
+function openCloseWidget(inflated, deflated) {
+       var elInf = document.getElementById(inflated);
+       var elDef = document.getElementById(deflated);
 
+       if (!elInf || !elDef) {
+               return;
+       }
+
+       if (window.getComputedStyle(elInf).display === "none") {
+               elInf.style.display = "block";
+               elDef.style.display = "none";
+               document.cookie = window.location.pathname + ":" + inflated + "=block";
+       } else {
+               elInf.style.display = "none";
+               elDef.style.display = "block";
+               document.cookie = window.location.pathname + ":" + inflated + "=none";
+       }
+}
+
+function openClose(theID) {
+       var el = document.getElementById(theID);
        if (el) {
-               el.style.display = "block";
+               if (window.getComputedStyle(el).display === "none") {
+                       openMenu(theID);
+               } else {
+                       closeMenu(theID);
+               }
        }
 }
 
-function closeMenu(theID) {
-       var el = document.getElementById(theID)
+function openMenu(theID) {
+       var el = document.getElementById(theID);
+       if (el) {
+               if (!el.dataset.display) {
+                       el.dataset.display = 'block';
+               }
+               el.style.display = el.dataset.display;
+       }
+}
 
+function closeMenu(theID) {
+       var el = document.getElementById(theID);
        if (el) {
+               el.dataset.display = window.getComputedStyle(el).display;
                el.style.display = "none";
        }
 }
@@ -63,6 +130,8 @@ var last_popup_menu = null;
 var last_popup_button = null;
 var lockLoadContent = false;
 
+const urlRegex = /^(?:https?:\/\/|\s)[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})(?:\/+[a-z0-9_.:;-]*)*(?:\?[&%|+a-z0-9_=,.:;-]*)?(?:[&%|+&a-z0-9_=,:;.-]*)(?:[!#\/&%|+a-z0-9_=,:;.-]*)}*$/i;
+
 $(function() {
        $.ajaxSetup({cache: false});
 
@@ -81,6 +150,11 @@ $(function() {
                        Dialog.doImageBrowser("comment", id);
                        return;
                }
+
+               if (bbcode == "imgprv") {
+                       bbcode = "img";
+               }
+
                insertFormatting(bbcode, id);
        });
 
@@ -376,6 +450,61 @@ $(function() {
        }
 });
 
+/**
+ * Inserts a BBCode tag in the comment textarea identified by id
+ *
+ * @param {string} BBCode
+ * @param {int} id
+ * @returns {boolean}
+ */
+function insertFormatting(BBCode, id) {
+       let textarea = document.getElementById('comment-edit-text-' + id);
+
+       if (textarea.value === '') {
+               $(textarea)
+                       .addClass("comment-edit-text-full")
+                       .removeClass("comment-edit-text-empty");
+               closeMenu("comment-fake-form-" + id);
+               openMenu("item-comments-" + id);
+       }
+
+       insertBBCodeInTextarea(BBCode, textarea);
+
+       return true;
+}
+
+/**
+ * Inserts a BBCode tag in the provided textarea element, wrapping the currently selected text.
+ * For URL BBCode, it discriminates between link text and non-link text to determine where to insert the selected text.
+ *
+ * @param {string} BBCode
+ * @param {HTMLTextAreaElement} textarea
+ */
+function insertBBCodeInTextarea(BBCode, textarea) {
+       let selectionStart = textarea.selectionStart;
+       let selectionEnd = textarea.selectionEnd;
+       let selectedText = textarea.value.substring(selectionStart, selectionEnd);
+       let openingTag = '[' + BBCode + ']';
+       let closingTag = '[/' + BBCode + ']';
+       let cursorPosition = selectionStart + openingTag.length + selectedText.length;
+
+       if (BBCode === 'url') {
+               if (urlRegex.test(selectedText)) {
+                       openingTag = '[' + BBCode + '=' + selectedText + ']';
+                       selectedText = '';
+                       cursorPosition = selectionStart + openingTag.length;
+               } else {
+                       openingTag = '[' + BBCode + '=]';
+                       cursorPosition = selectionStart + openingTag.length - 1;
+               }
+       }
+
+       textarea.value = textarea.value.substring(0, selectionStart) + openingTag + selectedText + closingTag + textarea.value.substring(selectionEnd, textarea.value.length);
+       textarea.setSelectionRange(cursorPosition, cursorPosition);
+       textarea.dispatchEvent(new Event('change'));
+       textarea.focus();
+}
+
 function NavUpdate() {
        if (!stopped) {
                var pingCmd = 'ping?format=json' + ((localUser != 0) ? '&f=&uid=' + localUser : '');
@@ -385,7 +514,7 @@ function NavUpdate() {
                                $('nav').trigger('nav-update', data.result);
 
                                // start live update
-                               ['network', 'profile', 'community', 'notes', 'display', 'contacts'].forEach(function (src) {
+                               ['network', 'profile', 'community', 'notes', 'display', 'contact'].forEach(function (src) {
                                        if ($('#live-' + src).length) {
                                                liveUpdate(src);
                                        }
@@ -438,7 +567,7 @@ function updateConvItems(data) {
                $('body').css('cursor', 'auto');
        }
        /* autocomplete @nicknames */
-       $(".comment-edit-form  textarea").editor_autocomplete(baseurl+"/acl");
+       $(".comment-edit-form  textarea").editor_autocomplete(baseurl + '/search/acl');
        /* autocomplete bbcode */
        $(".comment-edit-form  textarea").bbco_autocomplete('bbcode');
 }
@@ -480,14 +609,12 @@ function liveUpdate(src) {
                $('.wall-item-body', data).imagesLoaded(function() {
                        updateConvItems(data);
 
+                       document.dispatchEvent(new Event('postprocess_liveupdate'));
+
                        // Update the scroll position.
                        $(window).scrollTop($(window).scrollTop() + $("section").height() - orgHeight);
                });
-
-               callAddonHooks("postprocess_liveupdate");
-
        });
-
 }
 
 function imgbright(node) {
@@ -543,18 +670,39 @@ function dostar(ident) {
        });
 }
 
-function doignore(ident) {
+function dopin(ident) {
        ident = ident.toString();
        $('#like-rotator-' + ident).show();
-       $.get('ignored/' + ident, function(data) {
+       $.get('pinned/' + ident, function(data) {
                if (data.match(/1/)) {
-                       $('#ignored-' + ident).addClass('ignored');
-                       $('#ignored-' + ident).removeClass('unignored');
+                       $('#pinned-' + ident).addClass('pinned');
+                       $('#pinned-' + ident).removeClass('unpinned');
+                       $('#pin-' + ident).addClass('hidden');
+                       $('#unpin-' + ident).removeClass('hidden');
+               } else {
+                       $('#pinned-' + ident).addClass('unpinned');
+                       $('#pinned-' + ident).removeClass('pinned');
+                       $('#pin-' + ident).removeClass('hidden');
+                       $('#unpin-' + ident).addClass('hidden');
+               }
+               $('#like-rotator-' + ident).hide();
+       });
+}
+
+function doignore(ident) {
+       ident = ident.toString();
+       $('#like-rotator-' + ident).show();
+       $.get('item/ignore/' + ident, function(data) {
+               if (data === 1) {
+                       $('#ignored-' + ident)
+                               .addClass('ignored')
+                               .removeClass('unignored');
                        $('#ignore-' + ident).addClass('hidden');
                        $('#unignore-' + ident).removeClass('hidden');
                } else {
-                       $('#ignored-' + ident).addClass('unignored');
-                       $('#ignored-' + ident).removeClass('ignored');
+                       $('#ignored-' + ident)
+                               .addClass('unignored')
+                               .removeClass('ignored');
                        $('#ignore-' + ident).removeClass('hidden');
                        $('#unignore-' + ident).addClass('hidden');
                }
@@ -606,7 +754,6 @@ function post_comment(id) {
        unpause();
        commentBusy = true;
        $('body').css('cursor', 'wait');
-       $("#comment-preview-inp-" + id).val("0");
        $.post(
                "item",
                $("#comment-edit-form-" + id).serialize(),
@@ -635,11 +782,10 @@ function post_comment(id) {
 }
 
 function preview_comment(id) {
-       $("#comment-preview-inp-" + id).val("1");
        $("#comment-edit-preview-" + id).show();
        $.post(
                "item",
-               $("#comment-edit-form-" + id).serialize(),
+               $("#comment-edit-form-" + id).serialize() + '&preview=1',
                function(data) {
                        if (data.preview) {
                                $("#comment-edit-preview-" + id).html(data.preview);
@@ -652,30 +798,31 @@ function preview_comment(id) {
 }
 
 function showHideComments(id) {
-       if ($("#collapsed-comments-" + id).is(":visible")) {
-               $("#collapsed-comments-" + id).hide();
-               $("#hide-comments-" + id).html(window.showMore);
+       if ($('#collapsed-comments-' + id).is(':visible')) {
+               $('#collapsed-comments-' + id).slideUp();
+               $('#hide-comments-' + id).hide();
+               $('#hide-comments-total-' + id).show();
        } else {
-               $("#collapsed-comments-" + id).show();
-               $("#hide-comments-" + id).html(window.showFewer);
+               $('#collapsed-comments-' + id).slideDown();
+               $('#hide-comments-' + id).show();
+               $('#hide-comments-total-' + id).hide();
        }
 }
 
 function preview_post() {
-       $("#jot-preview").val("1");
        $("#jot-preview-content").show();
        $.post(
                "item",
-               $("#profile-jot-form").serialize(),
+               $("#profile-jot-form").serialize() + '&preview=1',
                function(data) {
                        if (data.preview) {
                                $("#jot-preview-content").html(data.preview);
                                $("#jot-preview-content" + " a").click(function() {return false;});
+                               document.dispatchEvent(new Event('postprocess_liveupdate'));
                        }
                },
                "json"
        );
-       $("#jot-preview").val("0");
        return true;
 }
 
@@ -729,7 +876,7 @@ function loadScrollContent() {
 
        // get the raw content from the next page and insert this content
        // right before "#conversation-end"
-       $.get('network?mode=raw' + infinite_scroll.reload_uri + '&last_received=' + received + '&last_commented=' + commented + '&last_created=' + created + '&last_id=' + id + '&page=' + infinite_scroll.pageno, function(data) {
+       $.get(infinite_scroll.reload_uri + '&mode=raw&last_received=' + received + '&last_commented=' + commented + '&last_created=' + created + '&last_id=' + id + '&page=' + infinite_scroll.pageno, function(data) {
                $("#scroll-loader").hide();
                if ($(data).length > 0) {
                        $(data).insertBefore('#conversation-end');
@@ -737,6 +884,8 @@ function loadScrollContent() {
                } else {
                        $("#scroll-end").fadeIn('normal');
                }
+
+               document.dispatchEvent(new Event('postprocess_liveupdate'));
        });
 }
 
@@ -779,11 +928,25 @@ function profChangeMember(gid,cid) {
        });
 }
 
-function contactgroupChangeMember(gid,cid) {
+function contactgroupChangeMember(checkbox, gid, cid) {
+       let url;
+       // checkbox.checked is the checkbox state after the click
+       if (checkbox.checked) {
+               url = 'group/' + gid + '/add/' + cid;
+       } else {
+               url = 'group/' + gid + '/remove/' + cid;
+       }
        $('body').css('cursor', 'wait');
-       $.get('contactgroup/' + gid + '/' + cid, function(data) {
-                       $('body').css('cursor', 'auto');
+       $.post(url)
+       .error(function () {
+               // Restores previous state in case of error
+               checkbox.checked = !checkbox.checked;
+       })
+       .always(function() {
+               $('body').css('cursor', 'auto');
        });
+
+       return true;
 }
 
 function checkboxhighlight(box) {
@@ -916,3 +1079,4 @@ var Dialog = {
                };
        }
 }
+// @license-end