utils.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* global Fluid, CONFIG */
  2. window.requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame;
  3. Fluid.utils = {
  4. listenScroll: function(callback) {
  5. var dbc = new Debouncer(callback);
  6. window.addEventListener('scroll', dbc, false);
  7. dbc.handleEvent();
  8. return dbc;
  9. },
  10. unlistenScroll: function(callback) {
  11. window.removeEventListener('scroll', callback);
  12. },
  13. listenDOMLoaded(callback) {
  14. if (document.readyState !== 'loading') {
  15. callback();
  16. } else {
  17. document.addEventListener('DOMContentLoaded', function () {
  18. callback();
  19. });
  20. }
  21. },
  22. scrollToElement: function(target, offset) {
  23. var of = jQuery(target).offset();
  24. if (of) {
  25. jQuery('html,body').animate({
  26. scrollTop: of.top + (offset || 0),
  27. easing : 'swing'
  28. });
  29. }
  30. },
  31. elementVisible: function(element, offsetFactor) {
  32. offsetFactor = offsetFactor && offsetFactor >= 0 ? offsetFactor : 0;
  33. var rect = element.getBoundingClientRect();
  34. const viewportHeight = window.innerHeight || document.documentElement.clientHeight;
  35. return (
  36. (rect.top >= 0 && rect.top <= viewportHeight * (1 + offsetFactor) + rect.height / 2) ||
  37. (rect.bottom >= 0 && rect.bottom <= viewportHeight * (1 + offsetFactor) + rect.height / 2)
  38. );
  39. },
  40. waitElementVisible: function(selectorOrElement, callback, offsetFactor) {
  41. var runningOnBrowser = typeof window !== 'undefined';
  42. var isBot = (runningOnBrowser && !('onscroll' in window))
  43. || (typeof navigator !== 'undefined' && /(gle|ing|ro|msn)bot|crawl|spider|yand|duckgo/i.test(navigator.userAgent));
  44. if (!runningOnBrowser || isBot) {
  45. return;
  46. }
  47. offsetFactor = offsetFactor && offsetFactor >= 0 ? offsetFactor : 0;
  48. function waitInViewport(element) {
  49. Fluid.utils.listenDOMLoaded(function() {
  50. if (Fluid.utils.elementVisible(element, offsetFactor)) {
  51. callback();
  52. return;
  53. }
  54. if ('IntersectionObserver' in window) {
  55. var io = new IntersectionObserver(function(entries, ob) {
  56. if (entries[0].isIntersecting) {
  57. callback();
  58. ob.disconnect();
  59. }
  60. }, {
  61. threshold : [0],
  62. rootMargin: (window.innerHeight || document.documentElement.clientHeight) * offsetFactor + 'px'
  63. });
  64. io.observe(element);
  65. } else {
  66. var wrapper = Fluid.utils.listenScroll(function() {
  67. if (Fluid.utils.elementVisible(element, offsetFactor)) {
  68. Fluid.utils.unlistenScroll(wrapper);
  69. callback();
  70. }
  71. });
  72. }
  73. });
  74. }
  75. if (typeof selectorOrElement === 'string') {
  76. this.waitElementLoaded(selectorOrElement, function(element) {
  77. waitInViewport(element);
  78. });
  79. } else {
  80. waitInViewport(selectorOrElement);
  81. }
  82. },
  83. waitElementLoaded: function(selector, callback) {
  84. var runningOnBrowser = typeof window !== 'undefined';
  85. var isBot = (runningOnBrowser && !('onscroll' in window))
  86. || (typeof navigator !== 'undefined' && /(gle|ing|ro|msn)bot|crawl|spider|yand|duckgo/i.test(navigator.userAgent));
  87. if (!runningOnBrowser || isBot) {
  88. return;
  89. }
  90. if ('MutationObserver' in window) {
  91. var mo = new MutationObserver(function(records, ob) {
  92. var ele = document.querySelector(selector);
  93. if (ele) {
  94. callback(ele);
  95. ob.disconnect();
  96. }
  97. });
  98. mo.observe(document, { childList: true, subtree: true });
  99. } else {
  100. Fluid.utils.listenDOMLoaded(function() {
  101. var waitLoop = function() {
  102. var ele = document.querySelector(selector);
  103. if (ele) {
  104. callback(ele);
  105. } else {
  106. setTimeout(waitLoop, 100);
  107. }
  108. };
  109. waitLoop();
  110. });
  111. }
  112. },
  113. createScript: function(url, onload) {
  114. var s = document.createElement('script');
  115. s.setAttribute('src', url);
  116. s.setAttribute('type', 'text/javascript');
  117. s.setAttribute('charset', 'UTF-8');
  118. s.async = false;
  119. if (typeof onload === 'function') {
  120. if (window.attachEvent) {
  121. s.onreadystatechange = function() {
  122. var e = s.readyState;
  123. if (e === 'loaded' || e === 'complete') {
  124. s.onreadystatechange = null;
  125. onload();
  126. }
  127. };
  128. } else {
  129. s.onload = onload;
  130. }
  131. }
  132. var ss = document.getElementsByTagName('script');
  133. var e = ss.length > 0 ? ss[ss.length - 1] : document.head || document.documentElement;
  134. e.parentNode.insertBefore(s, e.nextSibling);
  135. },
  136. createCssLink: function(url) {
  137. var l = document.createElement('link');
  138. l.setAttribute('rel', 'stylesheet');
  139. l.setAttribute('type', 'text/css');
  140. l.setAttribute('href', url);
  141. var e = document.getElementsByTagName('link')[0]
  142. || document.getElementsByTagName('head')[0]
  143. || document.head || document.documentElement;
  144. e.parentNode.insertBefore(l, e);
  145. },
  146. loadComments: function(selector, loadFunc) {
  147. var ele = document.querySelector('#comments[lazyload]');
  148. if (ele) {
  149. var callback = function() {
  150. loadFunc();
  151. ele.removeAttribute('lazyload');
  152. };
  153. Fluid.utils.waitElementVisible(selector, callback, CONFIG.lazyload.offset_factor);
  154. } else {
  155. loadFunc();
  156. }
  157. },
  158. getBackgroundLightness(selectorOrElement) {
  159. var ele = selectorOrElement;
  160. if (typeof selectorOrElement === 'string') {
  161. ele = document.querySelector(selectorOrElement);
  162. }
  163. var view = ele.ownerDocument.defaultView;
  164. if (!view) {
  165. view = window;
  166. }
  167. var rgbArr = view.getComputedStyle(ele).backgroundColor.replace(/rgba*\(/, '').replace(')', '').split(/,\s*/);
  168. if (rgbArr.length < 3) {
  169. return 0;
  170. }
  171. var colorCast = (0.213 * rgbArr[0]) + (0.715 * rgbArr[1]) + (0.072 * rgbArr[2]);
  172. return colorCast === 0 || colorCast > 255 / 2 ? 1 : -1;
  173. },
  174. retry(handler, interval, times) {
  175. if (times <= 0) {
  176. return;
  177. }
  178. var next = function() {
  179. if (--times >= 0 && !handler()) {
  180. setTimeout(next, interval);
  181. }
  182. };
  183. setTimeout(next, interval);
  184. }
  185. };
  186. /**
  187. * Handles debouncing of events via requestAnimationFrame
  188. * @see http://www.html5rocks.com/en/tutorials/speed/animations/
  189. * @param {Function} callback The callback to handle whichever event
  190. */
  191. function Debouncer(callback) {
  192. this.callback = callback;
  193. this.ticking = false;
  194. }
  195. Debouncer.prototype = {
  196. constructor: Debouncer,
  197. /**
  198. * dispatches the event to the supplied callback
  199. * @private
  200. */
  201. update: function() {
  202. this.callback && this.callback();
  203. this.ticking = false;
  204. },
  205. /**
  206. * ensures events don't get stacked
  207. * @private
  208. */
  209. requestTick: function() {
  210. if (!this.ticking) {
  211. requestAnimationFrame(this.rafCallback || (this.rafCallback = this.update.bind(this)));
  212. this.ticking = true;
  213. }
  214. },
  215. /**
  216. * Attach this as the event listeners
  217. */
  218. handleEvent: function() {
  219. this.requestTick();
  220. }
  221. };