JAVASCRIPT   20

tomcat js

Guest on 7th May 2022 03:49:59 PM

  1. /*
  2. * Copyright  The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *     http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16.  
  17. /** adds a live event handler akin to jQuery's on() */
  18. function addLiveEventListeners(selector, event, handler){
  19.     document.querySelector("body").addEventListener(
  20.          event
  21.         ,function(evt){
  22.             var target = evt.target;
  23.             while (target != null){
  24.                 // isMatch is a patch for IE 9 - 11; once support for IE11
  25.                 // is not required we can test with target.matches(selector)
  26.                 var isMatch = target.matches ? target.matches(selector) : target.msMatchesSelector(selector);
  27.                 if (isMatch){
  28.                     handler(evt);
  29.                     return;
  30.                 }
  31.                 target = target.parentElement;
  32.             }
  33.         }
  34.         ,true
  35.     );
  36. }
  37.  
  38.  
  39. /** adds event listener to the elements that match the selector */
  40. function addEventListeners(selector, event, handler, useCapture){
  41.     [].forEach.call(
  42.         document.querySelectorAll(selector)
  43.        ,function(el, ix) {
  44.           el.addEventListener(event, handler, useCapture || false);
  45.         }
  46.     );
  47. }
  48.  
  49.  
  50. /** toggles a class for all of the nodes that match a selector akin to jQuery's toggleClass() */
  51. function toggleClass(selector, className){
  52.     var nodes = document.querySelectorAll(selector);
  53.     [].forEach.call(nodes, function(node) {
  54.         if (node.classList.contains(className))
  55.             node.classList.remove(className);
  56.         else
  57.             node.classList.add(className);
  58.     });
  59. }
  60.  
  61.  
  62. function toggleMenu(){
  63.     toggleClass("#mainLeft", "opened");
  64.     toggleClass(".menu-toggler", "opened");
  65. }
  66.  
  67.  
  68. addEventListeners(".menu-toggler", "click", function(evt){
  69.     toggleMenu();
  70. });
  71.  
  72.  
  73. addEventListeners(".menu-toggler", "keyup", function(evt){
  74.     if (evt.which == 13)
  75.         toggleMenu();
  76. });

Raw Paste


Login or Register to edit or fork this paste. It's free.