In this repo i store all my websites, each in a different branch
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
9.1 KiB

  1. /**
  2. * @name ityped
  3. * @description Dead simple Animated Typing with no dependencies
  4. * @author Luis Vinícius
  5. * @email luis@uilabs.me
  6. */
  7. (function (global, factory) {
  8. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  9. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  10. (factory((global.ityped = global.ityped || {})));
  11. }(this, (function (exports) { 'use strict';
  12. /**
  13. * async foreach
  14. * https://www.npmjs.com/package/async-foreach
  15. */
  16. var forEach = function forEach(a, b, c) {
  17. var d = -1,
  18. e = a.length >>> 0;
  19. (function f(g) {
  20. var h,
  21. j = g === !1;
  22. do {
  23. ++d;
  24. } while (!(d in a) && d !== e);
  25. if (j || d === e) {
  26. c && c(!j, a);
  27. return;
  28. }
  29. g = b.call({
  30. async: function async() {
  31. return h = !0, f;
  32. }
  33. }, a[d], d, a), h || f(g);
  34. })();
  35. };
  36. /**
  37. * el is the element
  38. */
  39. var selectedElement = void 0;
  40. var props = void 0;
  41. var cursor = document.createElement('span');
  42. cursor.classList.add('ityped-cursor');
  43. cursor.textContent = '|';
  44. /**
  45. * @name setProps
  46. * @description Set the ityped properties configuration
  47. * @param {Object} config The configuration properties
  48. * @return {Promise}
  49. */
  50. function setProps(config) {
  51. var props = config;
  52. props.strings = config.strings || ['Put your string here...', 'and Enjoy!'];
  53. props.typeSpeed = config.typeSpeed || 100;
  54. props.backSpeed = config.backSpeed || 75;
  55. props.backDelay = config.backDelay || 2000;
  56. props.startDelay = config.startDelay || 750;
  57. props.showCursor = config.showCursor;
  58. props.loop = config.loop || false;
  59. if (props.showCursor === undefined) props.showCursor = true;
  60. return Promise.resolve(props);
  61. }
  62. /**
  63. * @name init
  64. * @param { String || Element } element The element that will receive the strings
  65. * @param {Object} config The initial configuration
  66. */
  67. function init(element, config) {
  68. typeof element === 'string' ? element = document.querySelector(element) : element = element;
  69. setProps(config).then(function (properties) {
  70. props = properties;
  71. element._props = props;
  72. // init cursor if needed
  73. if (props.showCursor) {
  74. initCursorOn(element, props.cursorChar || '|');
  75. }
  76. loopingOnWords(element);
  77. });
  78. }
  79. function initCursorOn(element, cursorChar) {
  80. var newCursor = cursor.cloneNode();
  81. element.insertAdjacentElement('afterend', newCursor);
  82. newCursor.textContent = cursorChar;
  83. }
  84. /**
  85. * @name loopingOnWords
  86. * @description Loop on each string passed
  87. * @param {HTMLElement} element The element to handle the animation on
  88. * @param {Array} words The array that contain the words
  89. */
  90. function loopingOnWords(element) {
  91. forEach(element._props.strings, function (word, index, arr) {
  92. var time = element._props.typeSpeed * word.length - 1;
  93. /**
  94. * set the correct time
  95. * with the differences of type and back
  96. * speed
  97. */
  98. if (element._props.backSpeed < element._props.typeSpeed) {
  99. time -= (element._props.typeSpeed - element._props.backSpeed) * word.length;
  100. } else if (element._props.backSpeed > element._props.typeSpeed) {
  101. time += (element._props.backSpeed - element._props.typeSpeed) * word.length;
  102. }
  103. var done = this.async();
  104. var len = element._props.strings.length;
  105. iterateWords(element, word, index, len).then(function () {
  106. setTimeout(function () {
  107. done();
  108. }, time);
  109. });
  110. }, function () {
  111. if (element._props.loop) {
  112. loopingOnWords(element);
  113. }
  114. });
  115. }
  116. /**
  117. * @name increment
  118. * @description Increment each letter and append it on element
  119. * @param {Element} element The Element that will receive the letters
  120. * @param {String} word The string that will be looped to
  121. * get each letter
  122. * @return {Promise}
  123. */
  124. function increment(element, word) {
  125. return new Promise(function (resolve, reject) {
  126. var count = 0;
  127. var _loop = function _loop(i) {
  128. var wordIndex = i;
  129. var len = word.length;
  130. setTimeout(function (i) {
  131. appendWord(element, word.charAt(wordIndex));
  132. count++;
  133. if (count === len - 1) {
  134. resolve();
  135. }
  136. }, element._props.typeSpeed * i);
  137. };
  138. for (var i = 0; i < word.length; i++) {
  139. _loop(i);
  140. }
  141. });
  142. }
  143. /**
  144. * @name appendWord
  145. * @description Append each letter on Element
  146. * @param {Element} element The Element that will receive the letter
  147. * @param {String} word The string that will be appended
  148. */
  149. function appendWord(element, word) {
  150. element.innerHTML += word;
  151. }
  152. /**
  153. * @name iterateWords
  154. * @description Iterate on each word, incrementing and decrementing
  155. * @param {Element} element The Element that will receive the letters of word
  156. * @param {String} word The string that is the word
  157. * @param {Integer} index The index position of the words Array
  158. * @param {Integer} wordsLengthArray The length of words Array
  159. * @return {Promise}
  160. */
  161. function iterateWords(element, word, index, wordsLengthArray) {
  162. return new Promise(function (resolve, reject) {
  163. increment(element, word).then(function () {
  164. setTimeout(function () {
  165. decrement(element, word, index, wordsLengthArray).then(function () {
  166. setTimeout(function () {
  167. resolve();
  168. }, element._props.startDelay);
  169. });
  170. }, element._props.backDelay);
  171. });
  172. });
  173. }
  174. /**
  175. * @name iterateInsideDecrement
  176. * @description Iterate on each word, inside the decrement function for decrement the word
  177. * @param {Element} element The Element that will receive the letters of word
  178. * @param {String} word The string that is the word
  179. * @param {Integer} len The length of words Array
  180. * @param {Promise} resolve The Promise.resolve method that will be trigerred when
  181. * the decrement iteration are finished
  182. * @return {Promise}
  183. */
  184. function iterateInsideDecrement(element, word, len, resolve) {
  185. var _loop2 = function _loop2(i) {
  186. var iteratedI = i,
  187. count = len;
  188. setTimeout(function (i) {
  189. element.innerHTML = word.substring(0, len - iteratedI);
  190. count--;
  191. if (iteratedI === 1) {
  192. resolve();
  193. }
  194. }, element._props.backSpeed * i);
  195. };
  196. for (var i = len; i > 0; i--) {
  197. _loop2(i);
  198. }
  199. }
  200. /**
  201. * @name decrement
  202. * @description decrement the word in the correct case
  203. * @param {Element} element The Element that will receive the letters of word
  204. * @param {String} word The string that is the word
  205. * @param {Integer} index The index of the Array that contain the word
  206. * @param {Integer} lengthWords The length of words Array
  207. */
  208. function decrement(element, word, index, lengthWords) {
  209. return new Promise(function (resolve, reject) {
  210. var len = word.length;
  211. // if is the last letter and the last word and no loop
  212. if (index + 1 === lengthWords) {
  213. if (!element._props.loop) {
  214. // when the last word
  215. if (element._props.onFinished !== undefined && typeof element._props.onFinished === "function") {
  216. element._props.onFinished();
  217. }
  218. element.innerHTML = word;
  219. } else if (element._props.loop) {
  220. iterateInsideDecrement(element, word, len, resolve);
  221. }
  222. } else if (index + 1 !== lengthWords) {
  223. iterateInsideDecrement(element, word, len, resolve);
  224. }
  225. });
  226. }
  227. /**
  228. * @name destroy
  229. * @description destroy the onFinished function
  230. */
  231. function destroy(element) {
  232. element._props.onFinished = function () {
  233. return void 0;
  234. };
  235. }
  236. exports.init = init;
  237. exports.destroy = destroy;
  238. Object.defineProperty(exports, '__esModule', { value: true });
  239. })));
  240. //# sourceMappingURL=ityped.js.map