utils.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * Pico's Default Theme - JavaScript helper
  3. *
  4. * Pico is a stupidly simple, blazing fast, flat file CMS.
  5. *
  6. * @author Daniel Rudolf
  7. * @link http://picocms.org
  8. * @license http://opensource.org/licenses/MIT The MIT License
  9. * @version 1.1
  10. */
  11. utils = {};
  12. /**
  13. * Iterates through an iterable object (e.g. plain objects, arrays, NodeList)
  14. *
  15. * @param object object the object to iterate through
  16. * @param function callback function to call on every item; the key is passed
  17. * as first, the value as second parameter; the callback may return FALSE
  18. * to stop the iteration
  19. * @return void
  20. */
  21. utils.forEach = function (object, callback)
  22. {
  23. var i = 0,
  24. keys = Object.keys(object),
  25. length = keys.length;
  26. for (; i < length; i++) {
  27. if (callback(keys[i], object[keys[i]]) === false) {
  28. return;
  29. }
  30. }
  31. };
  32. /**
  33. * Checks whether the client's browser is able to slide elements or not
  34. *
  35. * @return boolean TRUE when the browser supports sliding, FALSE otherwise
  36. */
  37. utils.canSlide = function ()
  38. {
  39. return (Modernizr.classlist && Modernizr.requestanimationframe && Modernizr.csstransitions);
  40. };
  41. /**
  42. * Slides a element up (i.e. hide a element by changing its height to 0px)
  43. *
  44. * @param HTMLElement element the element to slide up
  45. * @param function finishCallback function to call when the animation has
  46. * been finished (i.e. the element is hidden)
  47. * @param function startCallback function to call when the animation starts
  48. * @return void
  49. */
  50. utils.slideUp = function (element, finishCallback, startCallback)
  51. {
  52. if (!utils.canSlide()) {
  53. if (startCallback) startCallback();
  54. element.className += (element.className !== '') ? ' hidden' : 'hidden';
  55. if (finishCallback) window.requestAnimationFrame(finishCallback);
  56. return;
  57. }
  58. element.style.height = element.clientHeight + 'px';
  59. var slideId = parseInt(element.getAttribute('data-slide-id')) || 0;
  60. element.setAttribute('data-slide-id', ++slideId);
  61. window.requestAnimationFrame(function () {
  62. element.classList.add('slide');
  63. window.requestAnimationFrame(function () {
  64. element.style.height = '0px';
  65. if (startCallback) {
  66. startCallback();
  67. }
  68. window.setTimeout(function () {
  69. if (parseInt(element.getAttribute('data-slide-id')) !== slideId) return;
  70. element.classList.add('hidden');
  71. element.classList.remove('slide');
  72. element.style.height = null;
  73. if (finishCallback) {
  74. window.requestAnimationFrame(finishCallback);
  75. }
  76. }, 500);
  77. });
  78. });
  79. };
  80. /**
  81. * Slides a element down (i.e. show a hidden element)
  82. *
  83. * @param HTMLElement element the element to slide down
  84. * @param function finishCallback function to call when the animation has
  85. * been finished (i.e. the element is visible)
  86. * @param function startCallback function to call when the animation starts
  87. * @return void
  88. */
  89. utils.slideDown = function (element, finishCallback, startCallback)
  90. {
  91. if (!utils.canSlide()) {
  92. if (startCallback) startCallback();
  93. element.className = element.className.replace(/\bhidden\b */g, '');
  94. if (finishCallback) window.requestAnimationFrame(finishCallback);
  95. return;
  96. }
  97. var cssRuleOriginalValue = element.clientHeight + 'px',
  98. slideId = parseInt(element.getAttribute('data-slide-id')) || 0;
  99. element.setAttribute('data-slide-id', ++slideId);
  100. element.style.height = null;
  101. element.classList.remove('hidden');
  102. element.classList.remove('slide');
  103. var cssRuleValue = element.clientHeight + 'px';
  104. element.style.height = cssRuleOriginalValue;
  105. window.requestAnimationFrame(function () {
  106. element.classList.add('slide');
  107. window.requestAnimationFrame(function () {
  108. element.style.height = cssRuleValue;
  109. if (startCallback) {
  110. startCallback();
  111. }
  112. window.setTimeout(function () {
  113. if (parseInt(element.getAttribute('data-slide-id')) !== slideId) return;
  114. element.classList.remove('slide');
  115. element.style.height = null;
  116. if (finishCallback) {
  117. window.requestAnimationFrame(finishCallback);
  118. }
  119. }, 500);
  120. });
  121. });
  122. };
  123. /**
  124. * Checks whether a element is visible or not
  125. *
  126. * @param HTMLElement element the element to check
  127. * @return boolean TRUE when the element is visible, FALSE otherwise
  128. */
  129. utils.isElementVisible = function (element)
  130. {
  131. return !!(element.offsetWidth || element.offsetHeight || element.getClientRects().length);
  132. };