+
+ /**
+ * Awesomplete trigger.
+ */
+ var tags = document.getElementById('lf_tags');
+ if (tags != null) {
+ awesompleteUniqueTag('#lf_tags');
+ }
+
+ /**
+ * bLazy trigger
+ */
+ var picwall = document.getElementById('picwall_container');
+ if (picwall != null) {
+ var bLazy = new Blazy();
+ }
+
+ /**
+ * Bookmarklet alert
+ */
+ var bookmarkletLinks = document.querySelectorAll('.bookmarklet-link');
+ var bkmMessage = document.getElementById('bookmarklet-alert');
+ [].forEach.call(bookmarkletLinks, function(link) {
+ link.addEventListener('click', function(event) {
+ event.preventDefault();
+ alert(bkmMessage.value);
+ });
+ });
+
+ /**
+ * Firefox Social
+ */
+ var ffButton = document.getElementById('ff-social-button');
+ if (ffButton != null) {
+ ffButton.addEventListener('click', function(event) {
+ activateFirefoxSocial(event.target);
+ });
+ }
+
+ /**
+ * Plugin admin order
+ */
+ var orderPA = document.querySelectorAll('.order');
+ [].forEach.call(orderPA, function(link) {
+ link.addEventListener('click', function(event) {
+ event.preventDefault();
+ if (event.target.classList.contains('order-up')) {
+ return orderUp(event.target.parentNode.parentNode.getAttribute('data-order'));
+ } else if (event.target.classList.contains('order-down')) {
+ return orderDown(event.target.parentNode.parentNode.getAttribute('data-order'));
+ }
+ });
+ });
+
+ var continent = document.getElementById('continent');
+ var city = document.getElementById('city');
+ if (continent != null && city != null) {
+ continent.addEventListener('change', function (event) {
+ hideTimezoneCities(city, continent.options[continent.selectedIndex].value, true);
+ });
+ hideTimezoneCities(city, continent.options[continent.selectedIndex].value, false);
+ }
+
+ /**
+ * Bulk actions
+ */
+ var linkCheckboxes = document.querySelectorAll('.delete-checkbox');
+ var bar = document.getElementById('actions');
+ [].forEach.call(linkCheckboxes, function(checkbox) {
+ checkbox.style.display = 'block';
+ checkbox.addEventListener('click', function(event) {
+ var count = 0;
+ var linkCheckedCheckboxes = document.querySelectorAll('.delete-checkbox:checked');
+ [].forEach.call(linkCheckedCheckboxes, function(checkbox) {
+ count++;
+ });
+ if (count == 0 && bar.classList.contains('open')) {
+ bar.classList.toggle('open');
+ } else if (count > 0 && ! bar.classList.contains('open')) {
+ bar.classList.toggle('open');
+ }
+ });
+ });
+
+ var deleteButton = document.getElementById('actions-delete');
+ var token = document.querySelector('input[type="hidden"][name="token"]');
+ if (deleteButton != null && token != null) {
+ deleteButton.addEventListener('click', function(event) {
+ event.preventDefault();
+
+ var links = [];
+ var linkCheckedCheckboxes = document.querySelectorAll('.delete-checkbox:checked');
+ [].forEach.call(linkCheckedCheckboxes, function(checkbox) {
+ links.push({
+ 'id': checkbox.value,
+ 'title': document.querySelector('.linklist-item[data-id="'+ checkbox.value +'"] .linklist-link').innerHTML
+ });
+ });
+
+ var message = 'Are you sure you want to delete '+ links.length +' links?\n';
+ message += 'This action is IRREVERSIBLE!\n\nTitles:\n';
+ var ids = [];
+ links.forEach(function(item) {
+ message += ' - '+ item['title'] +'\n';
+ ids.push(item['id']);
+ });
+
+ if (window.confirm(message)) {
+ window.location = '?delete_link&lf_linkdate='+ ids.join('+') +'&token='+ token.value;
+ }
+ });
+ }
+
+ /**
+ * Tag list operations
+ *
+ * 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) {
+ rename.addEventListener('click', function(event) {
+ event.preventDefault();
+ var block = findParent(event.target, 'div', {'class': 'tag-list-item'});
+ var form = block.querySelector('.rename-tag-form');
+ if (form.style.display == 'none' || form.style.display == '') {
+ form.style.display = 'block';
+ } else {
+ form.style.display = 'none';
+ }
+ block.querySelector('input').focus();
+ });
+ });
+
+ // Rename a tag with an AJAX request
+ var renameTagSubmits = document.querySelectorAll('.validate-rename-tag');
+ [].forEach.call(renameTagSubmits, function(rename) {
+ rename.addEventListener('click', function(event) {
+ event.preventDefault();
+ var block = findParent(event.target, 'div', {'class': 'tag-list-item'});
+ var input = block.querySelector('.rename-tag-input');
+ var totag = input.value.replace('/"/g', '\\"');
+ if (totag.trim() == '') {
+ return;
+ }
+ var fromtag = block.getAttribute('data-tag');
+ var token = document.getElementById('token').value;
+
+ xhr = new XMLHttpRequest();
+ xhr.open('POST', '?do=changetag');
+ xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
+ xhr.onload = function() {
+ if (xhr.status !== 200) {
+ alert('An error occurred. Return code: '+ xhr.status);
+ location.reload();
+ } else {
+ block.setAttribute('data-tag', totag);
+ input.setAttribute('name', totag);
+ input.setAttribute('value', totag);
+ 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);
+ refreshToken();
+ });
+ });
+
+ // 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();
+ }
+ });
+ });
+
+ // Delete a tag with an AJAX query (alert popup confirmation)
+ var deleteTagButtons = document.querySelectorAll('.delete-tag');
+ [].forEach.call(deleteTagButtons, function(rename) {
+ rename.style.display = 'inline';
+ rename.addEventListener('click', function(event) {
+ event.preventDefault();
+ var block = findParent(event.target, 'div', {'class': 'tag-list-item'});
+ var tag = block.getAttribute('data-tag');
+ var token = document.getElementById('token').value;
+
+ if (confirm('Are you sure you want to delete the tag "'+ tag +'"?')) {
+ xhr = new XMLHttpRequest();
+ xhr.open('POST', '?do=changetag');
+ xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
+ xhr.onload = function() {
+ block.remove();
+ };
+ xhr.send(encodeURI('deletetag=1&fromtag='+ tag +'&token='+ token));
+ refreshToken();
+ }
+ });
+ });
+
+ updateAwesompleteList('.rename-tag-input', existingTags, awesomepletes);