]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - tpl/default/js/shaarli.js
Merge pull request #939 from ArthurHoaro/hotfix/firefox-social-title
[github/shaarli/Shaarli.git] / tpl / default / js / shaarli.js
index e19e90010b08cafd30de1d95919663c8ca5a3a39..1c66ebbdd708f033c5ae26e747e3f313137edee7 100644 (file)
@@ -401,14 +401,14 @@ window.onload = function () {
 
             var message = 'Are you sure you want to delete '+ links.length +' links?\n';
             message += 'This action is IRREVERSIBLE!\n\nTitles:\n';
-            var ids = '';
+            var ids = [];
             links.forEach(function(item) {
                 message += '  - '+ item['title'] +'\n';
-                ids += item['id'] +'+';
+                ids.push(item['id']);
             });
 
             if (window.confirm(message)) {
-                window.location = '?delete_link&lf_linkdate='+ ids +'&token='+ token.value;
+                window.location = '?delete_link&lf_linkdate='+ ids.join('+') +'&token='+ token.value;
             }
         });
     }
@@ -418,6 +418,10 @@ window.onload = function () {
      *
      * TODO: support error code in the backend for AJAX requests
      */
+    var tagList = document.querySelector('input[name="taglist"]');
+    var existingTags = tagList ? tagList.value.split(' ') : [];
+    var awesomepletes = [];
+
     // Display/Hide rename form
     var renameTagButtons = document.querySelectorAll('.rename-tag');
     [].forEach.call(renameTagButtons, function(rename) {
@@ -425,7 +429,12 @@ window.onload = function () {
             event.preventDefault();
             var block = findParent(event.target, 'div', {'class': 'tag-list-item'});
             var form = block.querySelector('.rename-tag-form');
-            form.style.display = form.style.display == 'none' ? 'block' : 'none';
+            if (form.style.display == 'none' || form.style.display == '') {
+                form.style.display = 'block';
+            } else {
+                form.style.display = 'none';
+            }
+            block.querySelector('input').focus();
         });
     });
 
@@ -454,10 +463,18 @@ window.onload = function () {
                     block.setAttribute('data-tag', totag);
                     input.setAttribute('name', totag);
                     input.setAttribute('value', totag);
-                    input.parentNode.style.display = 'none';
+                    findParent(input, 'div', {'class': 'rename-tag-form'}).style.display = 'none';
                     block.querySelector('a.tag-link').innerHTML = htmlEntities(totag);
                     block.querySelector('a.tag-link').setAttribute('href', '?searchtags='+ encodeURIComponent(totag));
                     block.querySelector('a.rename-tag').setAttribute('href', '?do=changetag&fromtag='+ encodeURIComponent(totag));
+
+                    // Refresh awesomplete values
+                    for (var key in existingTags) {
+                        if (existingTags[key] == fromtag) {
+                            existingTags[key] = totag;
+                        }
+                    }
+                    awesomepletes = updateAwesompleteList('.rename-tag-input', existingTags, awesomepletes);
                 }
             };
             xhr.send('renametag=1&fromtag='+ encodeURIComponent(fromtag) +'&totag='+ encodeURIComponent(totag) +'&token='+ token);
@@ -468,6 +485,7 @@ window.onload = function () {
     // Validate input with enter key
     var renameTagInputs = document.querySelectorAll('.rename-tag-input');
     [].forEach.call(renameTagInputs, function(rename) {
+
         rename.addEventListener('keypress', function(event) {
             if (event.keyCode === 13) { // enter
                 findParent(event.target, 'div', {'class': 'tag-list-item'}).querySelector('.validate-rename-tag').click();
@@ -497,8 +515,19 @@ window.onload = function () {
             }
         });
     });
+
+    updateAwesompleteList('.rename-tag-input', existingTags, awesomepletes);
 };
 
+/**
+ * Find a parent element according to its tag and its attributes
+ *
+ * @param element    Element where to start the search
+ * @param tagName    Expected parent tag name
+ * @param attributes Associative array of expected attributes (name=>value).
+ *
+ * @returns Found element or null.
+ */
 function findParent(element, tagName, attributes)
 {
     while (element) {
@@ -522,6 +551,9 @@ function findParent(element, tagName, attributes)
     return null;
 }
 
+/**
+ * Ajax request to refresh the CSRF token.
+ */
 function refreshToken()
 {
     var xhr = new XMLHttpRequest();
@@ -533,6 +565,33 @@ function refreshToken()
     xhr.send();
 }
 
+/**
+ * Update awesomplete list of tag for all elements matching the given selector
+ *
+ * @param selector  CSS selector
+ * @param tags      Array of tags
+ * @param instances List of existing awesomplete instances
+ */
+function updateAwesompleteList(selector, tags, instances)
+{
+    // First load: create Awesomplete instances
+    if (instances.length == 0) {
+        var elements = document.querySelectorAll(selector);
+        [].forEach.call(elements, function (element) {
+            instances.push(new Awesomplete(
+                element,
+                {'list': tags}
+            ));
+        });
+    } else {
+        // Update awesomplete tag list
+        for (var key in instances) {
+            instances[key].list = tags;
+        }
+    }
+    return instances;
+}
+
 /**
  * html_entities in JS
  *
@@ -547,11 +606,12 @@ function htmlEntities(str)
 
 function activateFirefoxSocial(node) {
     var loc = location.href;
-    var baseURL = loc.substring(0, loc.lastIndexOf("/"));
+    var baseURL = loc.substring(0, loc.lastIndexOf("/") + 1);
+    var title = document.title;
 
     // Keeping the data separated (ie. not in the DOM) so that it's maintainable and diffable.
     var data = {
-        name: "{$shaarlititle}",
+        name: title,
         description: "The personal, minimalist, super-fast, database free, bookmarking service by the Shaarli community.",
         author: "Shaarli",
         version: "1.0.0",
@@ -560,7 +620,7 @@ function activateFirefoxSocial(node) {
         icon32URL: baseURL + "/images/favicon.ico",
         icon64URL: baseURL + "/images/favicon.ico",
 
-        shareURL: baseURL + "{noparse}?post=%{url}&title=%{title}&description=%{text}&source=firefoxsocialapi{/noparse}",
+        shareURL: baseURL + "?post=%{url}&title=%{title}&description=%{text}&source=firefoxsocialapi",
         homepageURL: baseURL
     };
     node.setAttribute("data-service", JSON.stringify(data));