Difference between revisions of "MediaWiki:Common.js"

From Wiki
Jump to navigation Jump to search
m
 
(19 intermediate revisions by 2 users not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
+
/* Javascript filter
 +
---------------------------------*/
  
filterSelection("all")
+
// animate divs on start
function filterSelection(c) {
+
var items = document.querySelectorAll('.filter-sections li');
   var x, i;
+
animate(items);
   x = document.getElementsByClassName("ManualsfilterLi");
+
 
   if (c == "all") c = "";
+
// filter on click
  // Add the "show" class (display:block) to the filtered elements, and remove the "show" class from the elements that are not selected
+
each('.filter-links li', function(el) {
  for (i = 0; i < x.length; i++) {
+
   el.addEventListener('click', function(e) {
     w3RemoveClass(x[i], "Manualsshow");
+
    e.preventDefault();
     if (x[i].className.indexOf(c) > -1) w3AddClass(x[i], "Manualsshow");
+
    filterLinks(el);
 +
  });
 +
});
 +
 
 +
// filter links functions
 +
function filterLinks(element) {
 +
  // get text
 +
   var el = element.textContent,
 +
    // convert to lowercase
 +
//    linksTolowerCase = el.toLowerCase();
 +
    linksTolowerCase = el;
 +
  // if all remove all elements
 +
   if (el === 'All') {
 +
    // first show all view class
 +
    each('.view', function(e) {
 +
      e.classList.remove('view');
 +
    });
 +
     // no show init animation
 +
    animate(items);
 +
  } else {
 +
     // if not click all remove all elements
 +
    each('.view', function(e) {
 +
      e.classList.remove('view');
 +
    });
 
   }
 
   }
}
+
  // show animation for current elements
 +
  animate(document.querySelectorAll('.' + linksTolowerCase));
 +
};
 +
// forech arrays
 +
function each(el, callback) {
 +
  var allDivs = document.querySelectorAll(el),
 +
    alltoArr = Array.prototype.slice.call(allDivs);
 +
  Array.prototype.forEach.call(alltoArr, function(selector, index) {
 +
    if (callback) return callback(selector);
 +
  });
 +
};
 +
// animate function
 +
function animate(item) {
 +
  (function show(counter) {
 +
    setTimeout(function() {
 +
      item[counter].classList.add('view');
 +
      counter++;
 +
      if (counter < item.length) show(counter);
 +
    },2);
 +
  })(0);
 +
};
  
// Show filtered elements
+
        function openForm(evt, formName) {
function w3AddClass(element, name) {
+
          // Declare all variables
  var i, arr1, arr2;
+
          var i, tabcontent, tablinks;
  arr1 = element.className.split(" ");
+
          evt.preventDefault();
  arr2 = name.split(" ");
 
  for (i = 0; i < arr2.length; i++) {
 
    if (arr1.indexOf(arr2[i]) == -1) {
 
      element.className += " " + arr2[i];
 
    }
 
  }
 
}
 
  
// Hide elements that are not selected
+
          // Get all elements with class="tabcontent" and hide them
function w3RemoveClass(element, name) {
+
          //tabcontent = document.getElementsByClassName("contextxmlform");
  var i, arr1, arr2;
+
          //for (i = 0; i < tabcontent.length; i++) {
  arr1 = element.className.split(" ");
+
          //  tabcontent[i].style.display = "none";
  arr2 = name.split(" ");
+
          //}
  for (i = 0; i < arr2.length; i++) {
 
    while (arr1.indexOf(arr2[i]) > -1) {
 
      arr1.splice(arr1.indexOf(arr2[i]), 1);
 
    }
 
  }
 
  element.className = arr1.join(" ");
 
}
 
  
// Add active class to the current control button (highlight it)
+
          // Show the current tab, and add an "active" class to the link that opened the tab
var btnContainer = document.getElementById("myBtnContainer");
+
          if (evt.currentTarget.className.includes(' active')) {
var btns = btnContainer.getElementsByClassName("Manualsbtn");
+
              evt.currentTarget.className.replace(" active", "");
for (var i = 0; i < btns.length; i++) {
+
              // Get all elements with class="tablinks" and remove the class "active"
  btns[i].addEventListener("click", function() {
+
              tablinks = document.getElementsByClassName("tablinks");
    var current = document.getElementsByClassName("active");
+
              for (i = 0; i < tablinks.length; i++) {
    current[0].className = current[0].className.replace(" active", "");
+
                tablinks[i].className = tablinks[i].className.replace(" active", "");
    this.className += " active";
+
              }
  });
+
              document.getElementById(formName).style.display = "none";
}
+
          } else {
 +
              // Get all elements with class="tablinks" and remove the class "active"
 +
              tablinks = document.getElementsByClassName("tablinks");
 +
              for (i = 0; i < tablinks.length; i++) {
 +
                tablinks[i].className = tablinks[i].className.replace(" active", "");
 +
              }
 +
              document.getElementById(formName).style.display = "block";
 +
              evt.currentTarget.className += " active";
 +
          }
 +
        }

Latest revision as of 10:11, 25 August 2020

/*  Javascript filter
---------------------------------*/

// animate divs on start
var items = document.querySelectorAll('.filter-sections li');
animate(items);

// filter on click
each('.filter-links li', function(el) {
  el.addEventListener('click', function(e) {
    e.preventDefault();
    filterLinks(el);
  });
});

// filter links functions
function filterLinks(element) {
  // get text 
  var el = element.textContent,
    // convert to lowercase
//    linksTolowerCase = el.toLowerCase();
    linksTolowerCase = el;
  // if all remove all elements
  if (el === 'All') {
    // first show all view class
    each('.view', function(e) {
      e.classList.remove('view');
    });
    // no show init animation
    animate(items);
  } else {
    // if not click all remove all elements
    each('.view', function(e) {
      e.classList.remove('view');
    });
  }
  // show animation for current elements
  animate(document.querySelectorAll('.' + linksTolowerCase));
};
// forech arrays
function each(el, callback) {
  var allDivs = document.querySelectorAll(el),
    alltoArr = Array.prototype.slice.call(allDivs);
  Array.prototype.forEach.call(alltoArr, function(selector, index) {
    if (callback) return callback(selector);
  });
};
// animate function
function animate(item) {
  (function show(counter) {
    setTimeout(function() {
      item[counter].classList.add('view');
      counter++;
      if (counter < item.length) show(counter);
    },2);
  })(0);
};

        function openForm(evt, formName) {
          // Declare all variables
          var i, tabcontent, tablinks;
          evt.preventDefault();

          // Get all elements with class="tabcontent" and hide them
          //tabcontent = document.getElementsByClassName("contextxmlform");
          //for (i = 0; i < tabcontent.length; i++) {
          //  tabcontent[i].style.display = "none";
          //}

          // Show the current tab, and add an "active" class to the link that opened the tab
          if (evt.currentTarget.className.includes(' active')) {
              evt.currentTarget.className.replace(" active", "");
              // Get all elements with class="tablinks" and remove the class "active"
              tablinks = document.getElementsByClassName("tablinks");
              for (i = 0; i < tablinks.length; i++) {
                tablinks[i].className = tablinks[i].className.replace(" active", "");
              }
              document.getElementById(formName).style.display = "none";
          } else {
              // Get all elements with class="tablinks" and remove the class "active"
              tablinks = document.getElementsByClassName("tablinks");
              for (i = 0; i < tablinks.length; i++) {
                tablinks[i].className = tablinks[i].className.replace(" active", "");
              }
              document.getElementById(formName).style.display = "block";
              evt.currentTarget.className += " active";
          }
        }