PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/hammerjs/src/utils.js

https://gitlab.com/maudebo/tp-maudebaillyotis
JavaScript | 371 lines | 196 code | 35 blank | 140 comment | 52 complexity | 84c415c46e63a98d65024bd40106ba69 MD5 | raw file
  1. var VENDOR_PREFIXES = ['', 'webkit', 'Moz', 'MS', 'ms', 'o'];
  2. var TEST_ELEMENT = document.createElement('div');
  3. var TYPE_FUNCTION = 'function';
  4. var round = Math.round;
  5. var abs = Math.abs;
  6. var now = Date.now;
  7. /**
  8. * set a timeout with a given scope
  9. * @param {Function} fn
  10. * @param {Number} timeout
  11. * @param {Object} context
  12. * @returns {number}
  13. */
  14. function setTimeoutContext(fn, timeout, context) {
  15. return setTimeout(bindFn(fn, context), timeout);
  16. }
  17. /**
  18. * if the argument is an array, we want to execute the fn on each entry
  19. * if it aint an array we don't want to do a thing.
  20. * this is used by all the methods that accept a single and array argument.
  21. * @param {*|Array} arg
  22. * @param {String} fn
  23. * @param {Object} [context]
  24. * @returns {Boolean}
  25. */
  26. function invokeArrayArg(arg, fn, context) {
  27. if (Array.isArray(arg)) {
  28. each(arg, context[fn], context);
  29. return true;
  30. }
  31. return false;
  32. }
  33. /**
  34. * walk objects and arrays
  35. * @param {Object} obj
  36. * @param {Function} iterator
  37. * @param {Object} context
  38. */
  39. function each(obj, iterator, context) {
  40. var i;
  41. if (!obj) {
  42. return;
  43. }
  44. if (obj.forEach) {
  45. obj.forEach(iterator, context);
  46. } else if (obj.length !== undefined) {
  47. i = 0;
  48. while (i < obj.length) {
  49. iterator.call(context, obj[i], i, obj);
  50. i++;
  51. }
  52. } else {
  53. for (i in obj) {
  54. obj.hasOwnProperty(i) && iterator.call(context, obj[i], i, obj);
  55. }
  56. }
  57. }
  58. /**
  59. * wrap a method with a deprecation warning and stack trace
  60. * @param {Function} method
  61. * @param {String} name
  62. * @param {String} message
  63. * @returns {Function} A new function wrapping the supplied method.
  64. */
  65. function deprecate(method, name, message) {
  66. var deprecationMessage = 'DEPRECATED METHOD: ' + name + '\n' + message + ' AT \n';
  67. return function() {
  68. var e = new Error('get-stack-trace');
  69. var stack = e && e.stack ? e.stack.replace(/^[^\(]+?[\n$]/gm, '')
  70. .replace(/^\s+at\s+/gm, '')
  71. .replace(/^Object.<anonymous>\s*\(/gm, '{anonymous}()@') : 'Unknown Stack Trace';
  72. var log = window.console && (window.console.warn || window.console.log);
  73. if (log) {
  74. log.call(window.console, deprecationMessage, stack);
  75. }
  76. return method.apply(this, arguments);
  77. };
  78. }
  79. /**
  80. * extend object.
  81. * means that properties in dest will be overwritten by the ones in src.
  82. * @param {Object} target
  83. * @param {...Object} objects_to_assign
  84. * @returns {Object} target
  85. */
  86. var assign;
  87. if (typeof Object.assign !== 'function') {
  88. assign = function assign(target) {
  89. if (target === undefined || target === null) {
  90. throw new TypeError('Cannot convert undefined or null to object');
  91. }
  92. var output = Object(target);
  93. for (var index = 1; index < arguments.length; index++) {
  94. var source = arguments[index];
  95. if (source !== undefined && source !== null) {
  96. for (var nextKey in source) {
  97. if (source.hasOwnProperty(nextKey)) {
  98. output[nextKey] = source[nextKey];
  99. }
  100. }
  101. }
  102. }
  103. return output;
  104. };
  105. } else {
  106. assign = Object.assign;
  107. }
  108. /**
  109. * extend object.
  110. * means that properties in dest will be overwritten by the ones in src.
  111. * @param {Object} dest
  112. * @param {Object} src
  113. * @param {Boolean} [merge=false]
  114. * @returns {Object} dest
  115. */
  116. var extend = deprecate(function extend(dest, src, merge) {
  117. var keys = Object.keys(src);
  118. var i = 0;
  119. while (i < keys.length) {
  120. if (!merge || (merge && dest[keys[i]] === undefined)) {
  121. dest[keys[i]] = src[keys[i]];
  122. }
  123. i++;
  124. }
  125. return dest;
  126. }, 'extend', 'Use `assign`.');
  127. /**
  128. * merge the values from src in the dest.
  129. * means that properties that exist in dest will not be overwritten by src
  130. * @param {Object} dest
  131. * @param {Object} src
  132. * @returns {Object} dest
  133. */
  134. var merge = deprecate(function merge(dest, src) {
  135. return extend(dest, src, true);
  136. }, 'merge', 'Use `assign`.');
  137. /**
  138. * simple class inheritance
  139. * @param {Function} child
  140. * @param {Function} base
  141. * @param {Object} [properties]
  142. */
  143. function inherit(child, base, properties) {
  144. var baseP = base.prototype,
  145. childP;
  146. childP = child.prototype = Object.create(baseP);
  147. childP.constructor = child;
  148. childP._super = baseP;
  149. if (properties) {
  150. assign(childP, properties);
  151. }
  152. }
  153. /**
  154. * simple function bind
  155. * @param {Function} fn
  156. * @param {Object} context
  157. * @returns {Function}
  158. */
  159. function bindFn(fn, context) {
  160. return function boundFn() {
  161. return fn.apply(context, arguments);
  162. };
  163. }
  164. /**
  165. * let a boolean value also be a function that must return a boolean
  166. * this first item in args will be used as the context
  167. * @param {Boolean|Function} val
  168. * @param {Array} [args]
  169. * @returns {Boolean}
  170. */
  171. function boolOrFn(val, args) {
  172. if (typeof val == TYPE_FUNCTION) {
  173. return val.apply(args ? args[0] || undefined : undefined, args);
  174. }
  175. return val;
  176. }
  177. /**
  178. * use the val2 when val1 is undefined
  179. * @param {*} val1
  180. * @param {*} val2
  181. * @returns {*}
  182. */
  183. function ifUndefined(val1, val2) {
  184. return (val1 === undefined) ? val2 : val1;
  185. }
  186. /**
  187. * addEventListener with multiple events at once
  188. * @param {EventTarget} target
  189. * @param {String} types
  190. * @param {Function} handler
  191. */
  192. function addEventListeners(target, types, handler) {
  193. each(splitStr(types), function(type) {
  194. target.addEventListener(type, handler, false);
  195. });
  196. }
  197. /**
  198. * removeEventListener with multiple events at once
  199. * @param {EventTarget} target
  200. * @param {String} types
  201. * @param {Function} handler
  202. */
  203. function removeEventListeners(target, types, handler) {
  204. each(splitStr(types), function(type) {
  205. target.removeEventListener(type, handler, false);
  206. });
  207. }
  208. /**
  209. * find if a node is in the given parent
  210. * @method hasParent
  211. * @param {HTMLElement} node
  212. * @param {HTMLElement} parent
  213. * @return {Boolean} found
  214. */
  215. function hasParent(node, parent) {
  216. while (node) {
  217. if (node == parent) {
  218. return true;
  219. }
  220. node = node.parentNode;
  221. }
  222. return false;
  223. }
  224. /**
  225. * small indexOf wrapper
  226. * @param {String} str
  227. * @param {String} find
  228. * @returns {Boolean} found
  229. */
  230. function inStr(str, find) {
  231. return str.indexOf(find) > -1;
  232. }
  233. /**
  234. * split string on whitespace
  235. * @param {String} str
  236. * @returns {Array} words
  237. */
  238. function splitStr(str) {
  239. return str.trim().split(/\s+/g);
  240. }
  241. /**
  242. * find if a array contains the object using indexOf or a simple polyFill
  243. * @param {Array} src
  244. * @param {String} find
  245. * @param {String} [findByKey]
  246. * @return {Boolean|Number} false when not found, or the index
  247. */
  248. function inArray(src, find, findByKey) {
  249. if (src.indexOf && !findByKey) {
  250. return src.indexOf(find);
  251. } else {
  252. var i = 0;
  253. while (i < src.length) {
  254. if ((findByKey && src[i][findByKey] == find) || (!findByKey && src[i] === find)) {
  255. return i;
  256. }
  257. i++;
  258. }
  259. return -1;
  260. }
  261. }
  262. /**
  263. * convert array-like objects to real arrays
  264. * @param {Object} obj
  265. * @returns {Array}
  266. */
  267. function toArray(obj) {
  268. return Array.prototype.slice.call(obj, 0);
  269. }
  270. /**
  271. * unique array with objects based on a key (like 'id') or just by the array's value
  272. * @param {Array} src [{id:1},{id:2},{id:1}]
  273. * @param {String} [key]
  274. * @param {Boolean} [sort=False]
  275. * @returns {Array} [{id:1},{id:2}]
  276. */
  277. function uniqueArray(src, key, sort) {
  278. var results = [];
  279. var values = [];
  280. var i = 0;
  281. while (i < src.length) {
  282. var val = key ? src[i][key] : src[i];
  283. if (inArray(values, val) < 0) {
  284. results.push(src[i]);
  285. }
  286. values[i] = val;
  287. i++;
  288. }
  289. if (sort) {
  290. if (!key) {
  291. results = results.sort();
  292. } else {
  293. results = results.sort(function sortUniqueArray(a, b) {
  294. return a[key] > b[key];
  295. });
  296. }
  297. }
  298. return results;
  299. }
  300. /**
  301. * get the prefixed property
  302. * @param {Object} obj
  303. * @param {String} property
  304. * @returns {String|Undefined} prefixed
  305. */
  306. function prefixed(obj, property) {
  307. var prefix, prop;
  308. var camelProp = property[0].toUpperCase() + property.slice(1);
  309. var i = 0;
  310. while (i < VENDOR_PREFIXES.length) {
  311. prefix = VENDOR_PREFIXES[i];
  312. prop = (prefix) ? prefix + camelProp : property;
  313. if (prop in obj) {
  314. return prop;
  315. }
  316. i++;
  317. }
  318. return undefined;
  319. }
  320. /**
  321. * get a unique id
  322. * @returns {number} uniqueId
  323. */
  324. var _uniqueId = 1;
  325. function uniqueId() {
  326. return _uniqueId++;
  327. }
  328. /**
  329. * get the window object of an element
  330. * @param {HTMLElement} element
  331. * @returns {DocumentView|Window}
  332. */
  333. function getWindowForElement(element) {
  334. var doc = element.ownerDocument || element;
  335. return (doc.defaultView || doc.parentWindow || window);
  336. }