Guest User

Untitled

a guest
Jul 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.57 KB | None | 0 0
  1.  
  2. (function() {
  3. var doc = document;
  4. var disableBuilds = true;
  5.  
  6. var ctr = 0;
  7. var spaces = /\s+/, a1 = [''];
  8.  
  9. var toArray = function(list) {
  10. return Array.prototype.slice.call(list || [], 0);
  11. };
  12.  
  13. var byId = function(id) {
  14. if (typeof id == 'string') { return doc.getElementById(id); }
  15. return id;
  16. };
  17.  
  18. var query = function(query, root) {
  19. if (!query) { return []; }
  20. if (typeof query != 'string') { return toArray(query); }
  21. if (typeof root == 'string') {
  22. root = byId(root);
  23. if(!root){ return []; }
  24. }
  25.  
  26. root = root || document;
  27. var rootIsDoc = (root.nodeType == 9);
  28. var doc = rootIsDoc ? root : (root.ownerDocument || document);
  29.  
  30. // rewrite the query to be ID rooted
  31. if (!rootIsDoc || ('>~+'.indexOf(query.charAt(0)) >= 0)) {
  32. root.id = root.id || ('qUnique' + (ctr++));
  33. query = '#' + root.id + ' ' + query;
  34. }
  35. // don't choke on something like ".yada.yada >"
  36. if ('>~+'.indexOf(query.slice(-1)) >= 0) { query += ' *'; }
  37.  
  38. return toArray(doc.querySelectorAll(query));
  39. };
  40.  
  41. var strToArray = function(s) {
  42. if (typeof s == 'string' || s instanceof String) {
  43. if (s.indexOf(' ') < 0) {
  44. a1[0] = s;
  45. return a1;
  46. } else {
  47. return s.split(spaces);
  48. }
  49. }
  50. return s;
  51. };
  52.  
  53. var addClass = function(node, classStr) {
  54. classStr = strToArray(classStr);
  55. var cls = ' ' + node.className + ' ';
  56. for (var i = 0, len = classStr.length, c; i < len; ++i) {
  57. c = classStr[i];
  58. if (c && cls.indexOf(' ' + c + ' ') < 0) {
  59. cls += c + ' ';
  60. }
  61. }
  62. node.className = cls.trim();
  63. };
  64.  
  65. var removeClass = function(node, classStr) {
  66. var cls;
  67. if (classStr !== undefined) {
  68. classStr = strToArray(classStr);
  69. cls = ' ' + node.className + ' ';
  70. for (var i = 0, len = classStr.length; i < len; ++i) {
  71. cls = cls.replace(' ' + classStr[i] + ' ', ' ');
  72. }
  73. cls = cls.trim();
  74. } else {
  75. cls = '';
  76. }
  77. if (node.className != cls) {
  78. node.className = cls;
  79. }
  80. };
  81.  
  82. var toggleClass = function(node, classStr) {
  83. var cls = ' ' + node.className + ' ';
  84. if (cls.indexOf(' ' + classStr.trim() + ' ') >= 0) {
  85. removeClass(node, classStr);
  86. } else {
  87. addClass(node, classStr);
  88. }
  89. };
  90.  
  91. var ua = navigator.userAgent;
  92. var isFF = parseFloat(ua.split('Firefox/')[1]) || undefined;
  93. var isWK = parseFloat(ua.split('WebKit/')[1]) || undefined;
  94. var isOpera = parseFloat(ua.split('Opera/')[1]) || undefined;
  95.  
  96. var canTransition = (function() {
  97. var ver = parseFloat(ua.split('Version/')[1]) || undefined;
  98. // test to determine if this browser can handle CSS transitions.
  99. var cachedCanTransition =
  100. (isWK || (isFF && isFF > 3.6 ) || (isOpera && ver >= 10.5));
  101. return function() { return cachedCanTransition; }
  102. })();
  103.  
  104. //
  105. // Slide class
  106. //
  107. var Slide = function(node, idx) {
  108. this._node = node;
  109. if (idx >= 0) {
  110. this._count = idx + 1;
  111. }
  112. if (this._node) {
  113. addClass(this._node, 'slide distant-slide');
  114. }
  115. this._makeCounter();
  116. this._makeBuildList();
  117. };
  118.  
  119. Slide.prototype = {
  120. _node: null,
  121. _count: 0,
  122. _buildList: [],
  123. _visited: false,
  124. _currentState: '',
  125. _states: [ 'distant-slide', 'far-past',
  126. 'past', 'current', 'future',
  127. 'far-future', 'distant-slide' ],
  128. setState: function(state) {
  129. if (typeof state != 'string') {
  130. state = this._states[state];
  131. }
  132. if (state == 'current' && !this._visited) {
  133. this._visited = true;
  134. this._makeBuildList();
  135. }
  136. removeClass(this._node, this._states);
  137. addClass(this._node, state);
  138. this._currentState = state;
  139.  
  140. // delay first auto run. Really wish this were in CSS.
  141. /*
  142. this._runAutos();
  143. */
  144. var _t = this;
  145. setTimeout(function(){ _t._runAutos(); } , 400);
  146. },
  147. _makeCounter: function() {
  148. if(!this._count || !this._node) { return; }
  149. var c = doc.createElement('span');
  150. c.innerHTML = this._count;
  151. c.className = 'counter';
  152. this._node.appendChild(c);
  153. },
  154. _makeBuildList: function() {
  155. this._buildList = [];
  156. if (disableBuilds) { return; }
  157. if (this._node) {
  158. this._buildList = query('[data-build] > *', this._node);
  159. }
  160. this._buildList.forEach(function(el) {
  161. addClass(el, 'to-build');
  162. });
  163. },
  164. _runAutos: function() {
  165. if (this._currentState != 'current') {
  166. return;
  167. }
  168. // find the next auto, slice it out of the list, and run it
  169. var idx = -1;
  170. this._buildList.some(function(n, i) {
  171. if (n.hasAttribute('data-auto')) {
  172. idx = i;
  173. return true;
  174. }
  175. return false;
  176. });
  177. if (idx >= 0) {
  178. var elem = this._buildList.splice(idx, 1)[0];
  179. var transitionEnd = isWK ? 'webkitTransitionEnd' : (isFF ? 'mozTransitionEnd' : 'oTransitionEnd');
  180. var _t = this;
  181. if (canTransition()) {
  182. var l = function(evt) {
  183. elem.parentNode.removeEventListener(transitionEnd, l, false);
  184. _t._runAutos();
  185. };
  186. elem.parentNode.addEventListener(transitionEnd, l, false);
  187. removeClass(elem, 'to-build');
  188. } else {
  189. setTimeout(function() {
  190. removeClass(elem, 'to-build');
  191. _t._runAutos();
  192. }, 400);
  193. }
  194. }
  195. },
  196. buildNext: function() {
  197. if (!this._buildList.length) {
  198. return false;
  199. }
  200. removeClass(this._buildList.shift(), 'to-build');
  201. return true;
  202. },
  203. };
  204.  
  205. //
  206. // SlideShow class
  207. //
  208. var SlideShow = function(slides) {
  209. this._slides = (slides || []).map(function(el, idx) {
  210. return new Slide(el, idx);
  211. });
  212.  
  213. var h = window.location.hash;
  214. try {
  215. this.current = parseInt(h.split('#slide')[1], 10);
  216. }catch (e) { /* squeltch */ }
  217. this.current = isNaN(this.current) ? 1 : this.current;
  218. var _t = this;
  219. doc.addEventListener('keydown',
  220. function(e) { _t.handleKeys(e); }, false);
  221. doc.addEventListener('mousewheel',
  222. function(e) { _t.handleWheel(e); }, false);
  223. doc.addEventListener('DOMMouseScroll',
  224. function(e) { _t.handleWheel(e); }, false);
  225. doc.addEventListener('touchstart',
  226. function(e) { _t.handleTouchStart(e); }, false);
  227. doc.addEventListener('touchend',
  228. function(e) { _t.handleTouchEnd(e); }, false);
  229. window.addEventListener('popstate',
  230. function(e) { _t.go(e.state); }, false);
  231. this._update();
  232. };
  233.  
  234. SlideShow.prototype = {
  235. _slides: [],
  236. _update: function(dontPush) {
  237. document.querySelector('#presentation-counter').innerText = this.current;
  238. if (history.pushState) {
  239. if (!dontPush) {
  240. history.pushState(this.current, 'Slide ' + this.current, '#slide' + this.current);
  241. }
  242. } else {
  243. window.location.hash = 'slide' + this.current;
  244. }
  245. for (var x = this.current-1; x < this.current + 7; x++) {
  246. if (this._slides[x-4]) {
  247. this._slides[x-4].setState(Math.max(0, x-this.current));
  248. }
  249. }
  250. },
  251.  
  252. current: 0,
  253. next: function() {
  254. if (!this._slides[this.current-1].buildNext()) {
  255. this.current = Math.min(this.current + 1, this._slides.length);
  256. this._update();
  257. }
  258. },
  259. prev: function() {
  260. this.current = Math.max(this.current-1, 1);
  261. this._update();
  262. },
  263. go: function(num) {
  264. if (history.pushState && this.current != num) {
  265. history.replaceState(this.current, 'Slide ' + this.current, '#slide' + this.current);
  266. }
  267. this.current = num;
  268. this._update(true);
  269. },
  270.  
  271. _notesOn: false,
  272. showNotes: function() {
  273. var isOn = this._notesOn = !this._notesOn;
  274. query('.notes').forEach(function(el) {
  275. el.style.display = (notesOn) ? 'block' : 'none';
  276. });
  277. },
  278. switch3D: function() {
  279. toggleClass(document.body, 'three-d');
  280. },
  281. handleWheel: function(e) {
  282. var delta = 0;
  283. if (e.wheelDelta) {
  284. delta = e.wheelDelta/120;
  285. if (isOpera) {
  286. delta = -delta;
  287. }
  288. } else if (e.detail) {
  289. delta = -e.detail/3;
  290. }
  291.  
  292. if (delta > 0 ) {
  293. this.prev();
  294. return;
  295. }
  296. if (delta < 0 ) {
  297. this.next();
  298. return;
  299. }
  300. },
  301. handleKeys: function(e) {
  302.  
  303. if (/^(input|textarea)$/i.test(e.target.nodeName)) return;
  304.  
  305. switch (e.keyCode) {
  306. case 37: // left arrow
  307. this.prev(); break;
  308. case 39: // right arrow
  309. case 32: // space
  310. this.next(); break;
  311. case 50: // 2
  312. this.showNotes(); break;
  313. case 51: // 3
  314. this.switch3D(); break;
  315. }
  316. },
  317. _touchStartX: 0,
  318. handleTouchStart: function(e) {
  319. this._touchStartX = e.touches[0].pageX;
  320. },
  321. handleTouchEnd: function(e) {
  322. var delta = this._touchStartX - e.changedTouches[0].pageX;
  323. var SWIPE_SIZE = 150;
  324. if (delta > SWIPE_SIZE) {
  325. this.next();
  326. } else if (delta< -SWIPE_SIZE) {
  327. this.prev();
  328. }
  329. },
  330. };
  331.  
  332. // Initialize
  333. var slideshow = new SlideShow(query('.slide'));
  334.  
  335.  
  336.  
  337.  
  338.  
  339. document.querySelector('#toggle-counter').addEventListener('click', toggleCounter, false);
  340. document.querySelector('#toggle-size').addEventListener('click', toggleSize, false);
  341. document.querySelector('#toggle-transitions').addEventListener('click', toggleTransitions, false);
  342. document.querySelector('#toggle-gradients').addEventListener('click', toggleGradients, false);
  343.  
  344.  
  345. var counters = document.querySelectorAll('.counter');
  346. var slides = document.querySelectorAll('.slide');
  347.  
  348. function toggleCounter() {
  349. toArray(counters).forEach(function(el) {
  350. el.style.display = (el.offsetHeight) ? 'none' : 'block';
  351. });
  352. }
  353.  
  354. function toggleSize() {
  355. toArray(slides).forEach(function(el) {
  356. if (!/reduced/.test(el.className)) {
  357. addClass(el, 'reduced');
  358. }
  359. else {
  360. removeClass(el, 'reduced');
  361. }
  362. });
  363. }
  364.  
  365. function toggleTransitions() {
  366. toArray(slides).forEach(function(el) {
  367. if (!/no-transitions/.test(el.className)) {
  368. addClass(el, 'no-transitions');
  369. }
  370. else {
  371. removeClass(el, 'no-transitions');
  372. }
  373. });
  374. }
  375.  
  376. function toggleGradients() {
  377. toArray(slides).forEach(function(el) {
  378. if (!/no-gradients/.test(el.className)) {
  379. addClass(el, 'no-gradients');
  380. }
  381. else {
  382. removeClass(el, 'no-gradients');
  383. }
  384. });
  385. }
  386.  
  387.  
  388.  
  389.  
  390.  
  391. })();
Add Comment
Please, Sign In to add comment