github.com/fragmenta/fragmenta-cms@v1.5.5/src/app/assets/scripts/_dom.js (about)

     1  // Package DOM provides functions to replace the use of jquery in 1.4KB of js
     2  // See http://youmightnotneedjquery.com/ for more if required
     3  var DOM = (function() {
     4      return {
     5          // Apply a function on document ready
     6          Ready: function(f) {
     7              if (document.readyState != 'loading') {
     8                  f();
     9              } else {
    10                  document.addEventListener('DOMContentLoaded', f);
    11              }
    12          },
    13  
    14          // Return a NodeList of nearest elements matching selector, 
    15          // checking children, siblings or parents of el
    16          Nearest: function(el, s) {
    17  
    18              // Start with this element, then walk up the tree till 
    19              // we find a child which matches selector or we run out of elements
    20              while (el !== undefined && el !== null) {
    21                  var nearest = el.querySelectorAll(s);
    22                  if (nearest.length > 0) {
    23                      return nearest;
    24                  }
    25                  el = el.parentNode;
    26              }
    27  
    28              return []; // return empty array
    29          },
    30  
    31          // FIXME - perhaps adjust all to operate on either selector or an element?
    32  
    33          // Attribute returns either an attribute value or an empty string (if null)
    34          Attribute: function(el, a) {
    35              if (el.getAttribute(a) === null) {
    36                  return ''
    37              }
    38              return el.getAttribute(a)
    39          },
    40  
    41          // HasClass returns true if this element has this className
    42          HasClass: function(el, c) {
    43              var regexp = new RegExp("\\b" + c + "\\b", 'gi');
    44              return regexp.test(el.className);
    45          },
    46  
    47          // AddClass Adds the given className from el.className
    48          AddClass: function(s, c) {
    49              if (typeof s === "string") {
    50                  DOM.Each(s, function(el, i) {
    51                      if (!DOM.HasClass(el, c)) {
    52                          el.className = el.className + ' ' + c;
    53                      }
    54                  });
    55              } else {
    56                  if (!DOM.HasClass(s, c)) {
    57                      s.className = s.className + ' ' + c;
    58                  }
    59              }
    60          },
    61  
    62          // RemoveClass removes the given className from el.className
    63          RemoveClass: function(s, c) {
    64              var regexp = new RegExp("\\b" + c + "\\b", 'gi');
    65              if (typeof s === "string") {
    66                  DOM.Each(s, function(el, i) {
    67                      el.className = el.className.replace(regexp, '')
    68                  });
    69              } else {
    70                  s.className = s.className.replace(regexp, '')
    71              }
    72          },
    73  
    74          // Format returns the format string with the indexed arguments substituted
    75          // Formats are of the form - "{0} {1}" which uses variables 0 and 1 respectively
    76          Format: function(f) {
    77              for (var i = 1; i < arguments.length; i++) {
    78                  var regexp = new RegExp('\\{' + (i - 1) + '\\}', 'gi');
    79                  f = f.replace(regexp, arguments[i]);
    80              }
    81              return f;
    82          },
    83  
    84  
    85          // Apply a function to elements of an array
    86          ForEach: function(a, f) {
    87              Array.prototype.forEach.call(a, f);
    88          },
    89  
    90  
    91          // Return true if any element match selector
    92          Exists: function(s) {
    93              return (document.querySelector(s) !== null);
    94          },
    95  
    96          // Return a NodeList of elements matching selector
    97          All: function(s) {
    98              return document.querySelectorAll(s);
    99          },
   100  
   101  
   102          // Return the first in the NodeList of elements matching selector - may return nil
   103          First: function(s) {
   104              return DOM.All(s)[0];
   105          },
   106  
   107          // Apply a function to elements matching selector, return true to break
   108          Each: function(s, f) {
   109              var a = DOM.All(s);
   110              for (i = 0; i < a.length; ++i) {
   111                f(a[i], i);
   112              }
   113          },
   114  
   115  
   116          // Hidden returns true if this element is hidden
   117          Hidden: function(s) {
   118              if (typeof s === "string") {
   119                  return (DOM.First(s).style.display == 'none');
   120              } else {
   121                  return s.style.display == 'none';
   122              }
   123  
   124          },
   125  
   126          // Hide elements matching selector 
   127          Hide: function(s) {
   128              if (typeof s === "string") {
   129                  DOM.Each(s, function(el, i) {
   130                      el.style.display = 'none';
   131                  });
   132              } else {
   133                  s.style.display = 'none';
   134              }
   135          },
   136  
   137          // Show elements matching selector
   138          Show: function(s) {
   139              if (typeof s === "string") {
   140                  DOM.Each(s, function(el, i) {
   141                      el.style.display = '';
   142                  });
   143              } else {
   144                  s.style.display = '';
   145              }
   146          },
   147  
   148          // Toggle the Shown or Hidden value of elements matching selector
   149          ShowHide: function(s) {
   150              if (typeof s === "string") {
   151                  DOM.Each(s, function(el, i) {
   152                      if (el.style.display != 'none') {
   153                          el.style.display = 'none';
   154                      } else {
   155                          el.style.display = '';
   156                      }
   157                  });
   158              } else {
   159                  if (s.style.display != 'none') {
   160                      s.style.display = 'none';
   161                  } else {
   162                      s.style.display = '';
   163                  }
   164              }
   165          },
   166  
   167          // Attach event handlers to all matches for a selector 
   168          On: function(s, b, f) {
   169              DOM.Each(s, function(el, i) {
   170                  el.addEventListener(b, f);
   171              });
   172          },
   173  
   174  
   175          // Ajax - Send to url u the data d, call fs for success, ff for failures
   176          Post: function(u, d, fs, fe) {
   177              var request = new XMLHttpRequest();
   178              request.open('POST', u, true);
   179              request.onerror = fe;
   180              request.onload = function() {
   181                  if (request.status >= 200 && request.status < 400) {
   182                      fs(request);
   183                  } else {
   184                      fe(request);
   185                  }
   186              };
   187              request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
   188              request.send(d);
   189          },
   190  
   191          // Ajax - Get the data from url u, call fs for success, ff for failures
   192          Get: function(u, fs, fe) {
   193              var request = new XMLHttpRequest();
   194              request.open('GET', u, true);
   195              request.onload = function() {
   196                  if (request.status >= 200 && request.status < 400) {
   197                      fs(request);
   198                  } else {
   199                      fe();
   200                  }
   201              };
   202              request.onerror = fe;
   203              request.send();
   204          }
   205  
   206      };
   207  
   208  }());