]> git.immae.eu Git - github/shaarli/Shaarli.git/blobdiff - tpl/default/js/shaarli.js
Fix delete buttons issues
[github/shaarli/Shaarli.git] / tpl / default / js / shaarli.js
index 3afec7f32babcad4d0eef6984636b7bf221dd461..a3d40b0ddf32647006a8b5f3fcd4045d23594f84 100644 (file)
@@ -53,71 +53,90 @@ function getParentByClass(el, className) {
     window.addEventListener(WINDOW_CHANGE_EVENT, closeMenu);
 })(this, this.document);
 
-
-/**
- * Expend search fields on focus.
- */
-var searchInputs = document.querySelectorAll('#search input[type="text"]');
-[].forEach.call(searchInputs, function(searchInput) {
-    searchInput.addEventListener('focus', function(event) {
-        event.target.style.width = '250px';
-    });
-    searchInput.addEventListener('blur', function(event) {
-        event.target.style.width = '140px';
-    });
-});
-
 /**
  * Fold/Expand shaares description and thumbnail.
  */
-var foldButtons = document.querySelectorAll('.fold-button');
-[].forEach.call(foldButtons, function(foldButton) {
-    // Retrieve description
-    var description = null;
-    var thumbnail = null;
-    var linklistItem = getParentByClass(foldButton, 'linklist-item');
-    if (linklistItem != null) {
-        description = linklistItem.querySelector('.linklist-item-description');
-        thumbnail = linklistItem.querySelector('.linklist-item-thumbnail');
-        if (description != null || thumbnail != null) {
-            foldButton.style.display = 'inline';
+(function (window, document) {
+    var foldAllButtons = document.getElementsByClassName('fold-all');
+    var foldButtons = document.getElementsByClassName('fold-button');
+
+    [].forEach.call(foldButtons, function (foldButton) {
+        // Retrieve description
+        var description = null;
+        var thumbnail = null;
+        var linklistItem = getParentByClass(foldButton, 'linklist-item');
+        if (linklistItem != null) {
+            description = linklistItem.querySelector('.linklist-item-description');
+            thumbnail = linklistItem.querySelector('.linklist-item-thumbnail');
+            if (description != null || thumbnail != null) {
+                foldButton.style.display = 'inline';
+            }
         }
-    }
 
-    foldButton.addEventListener('click', function(event) {
-        event.preventDefault();
+        foldButton.addEventListener('click', function (event) {
+            event.preventDefault();
+            toggleFold(event.target, description, thumbnail);
+        });
+    });
+
+    if (foldAllButtons != null) {
+        [].forEach.call(foldAllButtons, function (foldAllButton) {
+            foldAllButton.addEventListener('click', function (event) {
+                event.preventDefault();
+                [].forEach.call(foldButtons, function (foldButton) {
+                    // Retrieve description
+                    var description = null;
+                    var thumbnail = null;
+                    var linklistItem = getParentByClass(foldButton, 'linklist-item');
+                    if (linklistItem != null) {
+                        description = linklistItem.querySelector('.linklist-item-description');
+                        thumbnail = linklistItem.querySelector('.linklist-item-thumbnail');
+                        if (description != null || thumbnail != null) {
+                            foldButton.style.display = 'inline';
+                        }
+                    }
+
+                    toggleFold(foldButton.firstElementChild, description, thumbnail);
+                });
+                foldAllButton.firstElementChild.classList.toggle('fa-chevron-down');
+                foldAllButton.firstElementChild.classList.toggle('fa-chevron-up');
+            });
+        });
+    }
+})(this, this.document);
 
-        // Switch fold/expand - up = fold
-        if (event.target.classList.contains('fa-chevron-up')) {
-            event.target.title = 'Expand';
-            if (description != null) {
-                description.style.display = 'none';
-            }
-            if (thumbnail != null) {
-                thumbnail.style.display = 'none';
-            }
+function toggleFold(button, description, thumb)
+{
+    // Switch fold/expand - up = fold
+    if (button.classList.contains('fa-chevron-up')) {
+        button.title = 'Expand';
+        if (description != null) {
+            description.style.display = 'none';
         }
-        else {
-            event.target.title = 'Fold';
-            if (description != null) {
-                description.style.display = 'block';
-            }
-            if (thumbnail != null) {
-                thumbnail.style.display = 'block';
-            }
+        if (thumb != null) {
+            thumb.style.display = 'none';
         }
-        event.target.classList.toggle('fa-chevron-down');
-        event.target.classList.toggle('fa-chevron-up');
-    });
-});
+    }
+    else {
+        button.title = 'Fold';
+        if (description != null) {
+            description.style.display = 'block';
+        }
+        if (thumb != null) {
+            thumb.style.display = 'block';
+        }
+    }
+    button.classList.toggle('fa-chevron-down');
+    button.classList.toggle('fa-chevron-up');
+}
 
 /**
  * Confirmation message before deletion.
  */
-var deleteLinks = document.querySelectorAll('.delete-link');
+var deleteLinks = document.querySelectorAll('.confirm-delete');
 [].forEach.call(deleteLinks, function(deleteLink) {
     deleteLink.addEventListener('click', function(event) {
-        if(!confirm('Are you sure you want to delete this link ?')) {
+        if(! confirm('Are you sure you want to delete this link ?')) {
             event.preventDefault();
         }
     });
@@ -152,27 +171,6 @@ if (newVersionDismiss != null) {
     });
 }
 
-/**
- * Login button
- */
-var loginButton = document.getElementById('login-button');
-var loginBlock = document.getElementById('header-login-form');
-
-if (loginButton != null) {
-    loginButton.addEventListener('click', function(event) {
-        event.preventDefault();
-        loginBlock.classList.toggle('open');
-        document.getElementById('content').style.boxShadow = 'none';
-    });
-}
-
-// Focus on login field.
-if (loginBlock != null) {
-    loginBlock.addEventListener('transitionend', function () {
-        loginBlock.firstElementChild.focus();
-    });
-}
-
 var hiddenReturnurl = document.getElementsByName('returnurl');
 if (hiddenReturnurl != null) {
     hiddenReturnurl.value = window.location.href;
@@ -184,4 +182,43 @@ if (hiddenReturnurl != null) {
 var autofocusElements = document.querySelector('.autofocus');
 if (autofocusElements != null) {
     autofocusElements.focus();
-}
\ No newline at end of file
+}
+
+/**
+ * Handle sub menus/forms
+ */
+var openers = document.getElementsByClassName('subheader-opener');
+if (openers != null) {
+    [].forEach.call(openers, function(opener) {
+         opener.addEventListener('click', function(event) {
+             event.preventDefault();
+
+             var id = opener.getAttribute('data-open-id');
+             var sub = document.getElementById(id);
+
+             if (sub != null) {
+                [].forEach.call(document.getElementsByClassName('subheader-form'), function (element) {
+                    if (element != sub) {
+                        removeClass(element, 'open')
+                    }
+                 });
+
+                 sub.classList.toggle('open');
+             }
+         });
+    });
+}
+
+function removeClass(element, classname) {
+    element.className = element.className.replace(new RegExp('(?:^|\\s)'+ classname + '(?:\\s|$)'), ' ');
+}
+
+/**
+ * Remove CSS target padding (for fixed bar)
+ */
+var anchor = document.querySelector('.anchor:target');
+if (anchor != null) {
+    var padsize = anchor.clientHeight;
+    this.window.scroll(0, this.window.scrollY - padsize);
+    anchor.style.paddingTop = 0;
+}