Grille des programmes de Riff en HTML CSS JS.

util.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Utility function
  2. function Util () {};
  3. /*
  4. class manipulation functions
  5. */
  6. Util.hasClass = function(el, className) {
  7. if (el.classList) return el.classList.contains(className);
  8. else return !!el.className.match(new RegExp('(\\s|^)' + className + '(\\s|$)'));
  9. };
  10. Util.addClass = function(el, className) {
  11. var classList = className.split(' ');
  12. if (el.classList) el.classList.add(classList[0]);
  13. else if (!Util.hasClass(el, classList[0])) el.className += " " + classList[0];
  14. if (classList.length > 1) Util.addClass(el, classList.slice(1).join(' '));
  15. };
  16. Util.removeClass = function(el, className) {
  17. var classList = className.split(' ');
  18. if (el.classList) el.classList.remove(classList[0]);
  19. else if(Util.hasClass(el, classList[0])) {
  20. var reg = new RegExp('(\\s|^)' + classList[0] + '(\\s|$)');
  21. el.className=el.className.replace(reg, ' ');
  22. }
  23. if (classList.length > 1) Util.removeClass(el, classList.slice(1).join(' '));
  24. };
  25. Util.toggleClass = function(el, className, bool) {
  26. if(bool) Util.addClass(el, className);
  27. else Util.removeClass(el, className);
  28. };
  29. Util.setAttributes = function(el, attrs) {
  30. for(var key in attrs) {
  31. el.setAttribute(key, attrs[key]);
  32. }
  33. };
  34. /*
  35. DOM manipulation
  36. */
  37. Util.getChildrenByClassName = function(el, className) {
  38. var children = el.children,
  39. childrenByClass = [];
  40. for (var i = 0; i < el.children.length; i++) {
  41. if (Util.hasClass(el.children[i], className)) childrenByClass.push(el.children[i]);
  42. }
  43. return childrenByClass;
  44. };
  45. /*
  46. Animate height of an element
  47. */
  48. Util.setHeight = function(start, to, element, duration, cb) {
  49. var change = to - start,
  50. currentTime = null;
  51. var animateHeight = function(timestamp){
  52. if (!currentTime) currentTime = timestamp;
  53. var progress = timestamp - currentTime;
  54. var val = parseInt((progress/duration)*change + start);
  55. element.setAttribute("style", "height:"+val+"px;");
  56. if(progress < duration) {
  57. window.requestAnimationFrame(animateHeight);
  58. } else {
  59. cb();
  60. }
  61. };
  62. //set the height of the element before starting animation -> fix bug on Safari
  63. element.setAttribute("style", "height:"+start+"px;");
  64. window.requestAnimationFrame(animateHeight);
  65. };
  66. /*
  67. Smooth Scroll
  68. */
  69. Util.scrollTo = function(final, duration, cb) {
  70. var start = window.scrollY || document.documentElement.scrollTop,
  71. currentTime = null;
  72. var animateScroll = function(timestamp){
  73. if (!currentTime) currentTime = timestamp;
  74. var progress = timestamp - currentTime;
  75. if(progress > duration) progress = duration;
  76. var val = Math.easeInOutQuad(progress, start, final-start, duration);
  77. window.scrollTo(0, val);
  78. if(progress < duration) {
  79. window.requestAnimationFrame(animateScroll);
  80. } else {
  81. cb && cb();
  82. }
  83. };
  84. window.requestAnimationFrame(animateScroll);
  85. };
  86. /*
  87. Focus utility classes
  88. */
  89. //Move focus to an element
  90. Util.moveFocus = function (element) {
  91. if( !element ) element = document.getElementsByTagName("body")[0];
  92. element.focus();
  93. if (document.activeElement !== element) {
  94. element.setAttribute('tabindex','-1');
  95. element.focus();
  96. }
  97. };
  98. /*
  99. Misc
  100. */
  101. Util.getIndexInArray = function(array, el) {
  102. return Array.prototype.indexOf.call(array, el);
  103. };
  104. Util.cssSupports = function(property, value) {
  105. if('CSS' in window) {
  106. return CSS.supports(property, value);
  107. } else {
  108. var jsProperty = property.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase();});
  109. return jsProperty in document.body.style;
  110. }
  111. };
  112. /*
  113. Polyfills
  114. */
  115. //Closest() method
  116. if (!Element.prototype.matches) {
  117. Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
  118. }
  119. if (!Element.prototype.closest) {
  120. Element.prototype.closest = function(s) {
  121. var el = this;
  122. if (!document.documentElement.contains(el)) return null;
  123. do {
  124. if (el.matches(s)) return el;
  125. el = el.parentElement || el.parentNode;
  126. } while (el !== null && el.nodeType === 1);
  127. return null;
  128. };
  129. }
  130. //Custom Event() constructor
  131. if ( typeof window.CustomEvent !== "function" ) {
  132. function CustomEvent ( event, params ) {
  133. params = params || { bubbles: false, cancelable: false, detail: undefined };
  134. var evt = document.createEvent( 'CustomEvent' );
  135. evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
  136. return evt;
  137. }
  138. CustomEvent.prototype = window.Event.prototype;
  139. window.CustomEvent = CustomEvent;
  140. }
  141. /*
  142. Animation curves
  143. */
  144. Math.easeInOutQuad = function (t, b, c, d) {
  145. t /= d/2;
  146. if (t < 1) return c/2*t*t + b;
  147. t--;
  148. return -c/2 * (t*(t-2) - 1) + b;
  149. };