inputmask.dependencyLib.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. Input Mask plugin dependencyLib
  3. http://github.com/RobinHerbots/jquery.inputmask
  4. Copyright (c) 2010 - Robin Herbots
  5. Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. */
  7. (function (factory) {
  8. if (typeof define === "function" && define.amd) {
  9. define(factory);
  10. } else if (typeof exports === "object") {
  11. module.exports = factory();
  12. } else {
  13. factory();
  14. }
  15. }
  16. (function () {
  17. //helper functions
  18. // Use a stripped-down indexOf as it's faster than native
  19. // http://jsperf.com/thor-indexof-vs-for/5
  20. function indexOf(list, elem) {
  21. var i = 0,
  22. len = list.length;
  23. for (; i < len; i++) {
  24. if (list[i] === elem) {
  25. return i;
  26. }
  27. }
  28. return -1;
  29. }
  30. var class2type = {},
  31. classTypes = "Boolean Number String Function Array Date RegExp Object Error".split(" ");
  32. for (var nameNdx = 0; nameNdx < classTypes.length; nameNdx++) {
  33. class2type["[object " + classTypes[nameNdx] + "]"] = classTypes[nameNdx].toLowerCase();
  34. }
  35. function type(obj) {
  36. if (obj == null) {
  37. return obj + "";
  38. }
  39. // Support: Android<4.0, iOS<6 (functionish RegExp)
  40. return typeof obj === "object" || typeof obj === "function" ?
  41. class2type[class2type.toString.call(obj)] || "object" :
  42. typeof obj;
  43. }
  44. function isWindow(obj) {
  45. return obj != null && obj === obj.window;
  46. }
  47. function isArraylike(obj) {
  48. // Support: iOS 8.2 (not reproducible in simulator)
  49. // `in` check used to prevent JIT error (gh-2145)
  50. // hasOwn isn't used here due to false negatives
  51. // regarding Nodelist length in IE
  52. var length = "length" in obj && obj.length,
  53. ltype = type(obj);
  54. if (ltype === "function" || isWindow(obj)) {
  55. return false;
  56. }
  57. if (obj.nodeType === 1 && length) {
  58. return true;
  59. }
  60. return ltype === "array" || length === 0 ||
  61. typeof length === "number" && length > 0 && (length - 1) in obj;
  62. }
  63. function isValidElement(elem) {
  64. return elem instanceof Element;
  65. }
  66. function DependencyLib(elem) {
  67. if (elem instanceof DependencyLib) {
  68. return elem;
  69. }
  70. if (!(this instanceof DependencyLib)) {
  71. return new DependencyLib(elem);
  72. }
  73. if (elem !== undefined && elem !== null && elem !== window) {
  74. this[0] = elem.nodeName ? elem : (elem[0] !== undefined && elem[0].nodeName ? elem[0] : document.querySelector(elem));
  75. if (this[0] !== undefined && this[0] !== null) {
  76. this[0].eventRegistry = this[0].eventRegistry || {};
  77. }
  78. }
  79. }
  80. DependencyLib.prototype = {
  81. on: function (events, handler) {
  82. if (isValidElement(this[0])) {
  83. var eventRegistry = this[0].eventRegistry,
  84. elem = this[0];
  85. function addEvent(ev, namespace) {
  86. //register domevent
  87. if (elem.addEventListener) { // all browsers except IE before version 9
  88. elem.addEventListener(ev, handler, false);
  89. } else if (elem.attachEvent) { // IE before version 9
  90. elem.attachEvent("on" + ev, handler);
  91. }
  92. eventRegistry[ev] = eventRegistry[ev] || {};
  93. eventRegistry[ev][namespace] = eventRegistry[ev][namespace] || [];
  94. eventRegistry[ev][namespace].push(handler);
  95. }
  96. var _events = events.split(" ");
  97. for (var endx = 0; endx < _events.length; endx++) {
  98. var nsEvent = _events[endx].split("."),
  99. ev = nsEvent[0],
  100. namespace = nsEvent[1] || "global";
  101. addEvent(ev, namespace);
  102. }
  103. }
  104. return this;
  105. },
  106. off: function (events, handler) {
  107. if (isValidElement(this[0])) {
  108. var eventRegistry = this[0].eventRegistry,
  109. elem = this[0];
  110. function removeEvent(ev, namespace, handler) {
  111. if (ev in eventRegistry === true) {
  112. //unbind to dom events
  113. if (elem.removeEventListener) { // all browsers except IE before version 9
  114. elem.removeEventListener(ev, handler, false);
  115. } else if (elem.detachEvent) { // IE before version 9
  116. elem.detachEvent("on" + ev, handler);
  117. }
  118. if (namespace === "global") {
  119. for (var nmsp in eventRegistry[ev]) {
  120. eventRegistry[ev][nmsp].splice(eventRegistry[ev][nmsp].indexOf(handler), 1);
  121. }
  122. } else {
  123. eventRegistry[ev][namespace].splice(eventRegistry[ev][namespace].indexOf(handler), 1);
  124. }
  125. }
  126. }
  127. function resolveNamespace(ev, namespace) {
  128. var evts = [],
  129. hndx, hndL;
  130. if (ev.length > 0) {
  131. if (handler === undefined) {
  132. for (hndx = 0, hndL = eventRegistry[ev][namespace].length; hndx < hndL; hndx++) {
  133. evts.push({
  134. ev: ev,
  135. namespace: namespace && namespace.length > 0 ? namespace : "global",
  136. handler: eventRegistry[ev][namespace][hndx]
  137. });
  138. }
  139. } else {
  140. evts.push({
  141. ev: ev,
  142. namespace: namespace && namespace.length > 0 ? namespace : "global",
  143. handler: handler
  144. });
  145. }
  146. } else if (namespace.length > 0) {
  147. for (var evNdx in eventRegistry) {
  148. for (var nmsp in eventRegistry[evNdx]) {
  149. if (nmsp === namespace) {
  150. if (handler === undefined) {
  151. for (hndx = 0, hndL = eventRegistry[evNdx][nmsp].length; hndx < hndL; hndx++) {
  152. evts.push({
  153. ev: evNdx,
  154. namespace: nmsp,
  155. handler: eventRegistry[evNdx][nmsp][hndx]
  156. });
  157. }
  158. } else {
  159. evts.push({
  160. ev: evNdx,
  161. namespace: nmsp,
  162. handler: handler
  163. });
  164. }
  165. }
  166. }
  167. }
  168. }
  169. return evts;
  170. }
  171. var _events = events.split(" ");
  172. for (var endx = 0; endx < _events.length; endx++) {
  173. var nsEvent = _events[endx].split("."),
  174. offEvents = resolveNamespace(nsEvent[0], nsEvent[1]);
  175. for (var i = 0, offEventsL = offEvents.length; i < offEventsL; i++) {
  176. removeEvent(offEvents[i].ev, offEvents[i].namespace, offEvents[i].handler);
  177. }
  178. }
  179. }
  180. return this;
  181. },
  182. trigger: function (events /* , args... */) {
  183. if (isValidElement(this[0])) {
  184. var eventRegistry = this[0].eventRegistry,
  185. elem = this[0];
  186. var _events = typeof events === "string" ? events.split(" ") : [events.type];
  187. for (var endx = 0; endx < _events.length; endx++) {
  188. var nsEvent = _events[endx].split("."),
  189. ev = nsEvent[0],
  190. namespace = nsEvent[1] || "global";
  191. if (document !== undefined && namespace === "global") {
  192. //trigger domevent
  193. var evnt, i, params = {
  194. bubbles: false,
  195. cancelable: true,
  196. detail: Array.prototype.slice.call(arguments, 1)
  197. };
  198. // The custom event that will be created
  199. if (document.createEvent) {
  200. try {
  201. evnt = new CustomEvent(ev, params);
  202. } catch (e) {
  203. evnt = document.createEvent("CustomEvent");
  204. evnt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
  205. }
  206. if (events.type) DependencyLib.extend(evnt, events);
  207. elem.dispatchEvent(evnt);
  208. } else {
  209. evnt = document.createEventObject();
  210. evnt.eventType = ev;
  211. if (events.type) DependencyLib.extend(evnt, events);
  212. elem.fireEvent("on" + evnt.eventType, evnt);
  213. }
  214. } else if (eventRegistry[ev] !== undefined) {
  215. arguments[0] = arguments[0].type ? arguments[0] : DependencyLib.Event(arguments[0]);
  216. if (namespace === "global") {
  217. for (var nmsp in eventRegistry[ev]) {
  218. for (i = 0; i < eventRegistry[ev][nmsp].length; i++) {
  219. eventRegistry[ev][nmsp][i].apply(elem, arguments);
  220. }
  221. }
  222. } else {
  223. for (i = 0; i < eventRegistry[ev][namespace].length; i++) {
  224. eventRegistry[ev][namespace][i].apply(elem, arguments);
  225. }
  226. }
  227. }
  228. }
  229. }
  230. return this;
  231. }
  232. };
  233. //static
  234. DependencyLib.isFunction = function (obj) {
  235. return type(obj) === "function";
  236. };
  237. DependencyLib.noop = function () {
  238. };
  239. DependencyLib.isArray = Array.isArray;
  240. DependencyLib.inArray = function (elem, arr, i) {
  241. return arr == null ? -1 : indexOf(arr, elem, i);
  242. };
  243. DependencyLib.valHooks = undefined;
  244. DependencyLib.isPlainObject = function (obj) {
  245. // Not plain objects:
  246. // - Any object or value whose internal [[Class]] property is not "[object Object]"
  247. // - DOM nodes
  248. // - window
  249. if (type(obj) !== "object" || obj.nodeType || isWindow(obj)) {
  250. return false;
  251. }
  252. if (obj.constructor && !class2type.hasOwnProperty.call(obj.constructor.prototype, "isPrototypeOf")) {
  253. return false;
  254. }
  255. // If the function hasn't returned already, we're confident that
  256. // |obj| is a plain object, created by {} or constructed with new Object
  257. return true;
  258. };
  259. DependencyLib.extend = function () {
  260. var options, name, src, copy, copyIsArray, clone,
  261. target = arguments[0] || {},
  262. i = 1,
  263. length = arguments.length,
  264. deep = false;
  265. // Handle a deep copy situation
  266. if (typeof target === "boolean") {
  267. deep = target;
  268. // Skip the boolean and the target
  269. target = arguments[i] || {};
  270. i++;
  271. }
  272. // Handle case when target is a string or something (possible in deep copy)
  273. if (typeof target !== "object" && !DependencyLib.isFunction(target)) {
  274. target = {};
  275. }
  276. // Extend jQuery itself if only one argument is passed
  277. if (i === length) {
  278. target = this;
  279. i--;
  280. }
  281. for (; i < length; i++) {
  282. // Only deal with non-null/undefined values
  283. if ((options = arguments[i]) != null) {
  284. // Extend the base object
  285. for (name in options) {
  286. src = target[name];
  287. copy = options[name];
  288. // Prevent never-ending loop
  289. if (target === copy) {
  290. continue;
  291. }
  292. // Recurse if we're merging plain objects or arrays
  293. if (deep && copy && (DependencyLib.isPlainObject(copy) || (copyIsArray = DependencyLib.isArray(copy)))) {
  294. if (copyIsArray) {
  295. copyIsArray = false;
  296. clone = src && DependencyLib.isArray(src) ? src : [];
  297. } else {
  298. clone = src && DependencyLib.isPlainObject(src) ? src : {};
  299. }
  300. // Never move original objects, clone them
  301. target[name] = DependencyLib.extend(deep, clone, copy);
  302. // Don't bring in undefined values
  303. } else if (copy !== undefined) {
  304. target[name] = copy;
  305. }
  306. }
  307. }
  308. }
  309. // Return the modified object
  310. return target;
  311. };
  312. DependencyLib.each = function (obj, callback) {
  313. var value, i = 0;
  314. if (isArraylike(obj)) {
  315. for (var length = obj.length; i < length; i++) {
  316. value = callback.call(obj[i], i, obj[i]);
  317. if (value === false) {
  318. break;
  319. }
  320. }
  321. } else {
  322. for (i in obj) {
  323. value = callback.call(obj[i], i, obj[i]);
  324. if (value === false) {
  325. break;
  326. }
  327. }
  328. }
  329. return obj;
  330. };
  331. DependencyLib.map = function (elems, callback) {
  332. var value,
  333. i = 0,
  334. length = elems.length,
  335. isArray = isArraylike(elems),
  336. ret = [];
  337. // Go through the array, translating each of the items to their new values
  338. if (isArray) {
  339. for (; i < length; i++) {
  340. value = callback(elems[i], i);
  341. if (value != null) {
  342. ret.push(value);
  343. }
  344. }
  345. // Go through every key on the object,
  346. } else {
  347. for (i in elems) {
  348. value = callback(elems[i], i);
  349. if (value != null) {
  350. ret.push(value);
  351. }
  352. }
  353. }
  354. // Flatten any nested arrays
  355. return [].concat(ret);
  356. };
  357. DependencyLib.data = function (owner, key, value) {
  358. if (value === undefined) {
  359. return owner.__data ? owner.__data[key] : null;
  360. } else {
  361. owner.__data = owner.__data || {};
  362. owner.__data[key] = value;
  363. }
  364. };
  365. DependencyLib.Event = function CustomEvent(event, params) {
  366. params = params || {
  367. bubbles: false,
  368. cancelable: false,
  369. detail: undefined
  370. };
  371. var evt = document.createEvent("CustomEvent");
  372. evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
  373. return evt;
  374. }
  375. DependencyLib.Event.prototype = window.Event.prototype;
  376. window.dependencyLib = DependencyLib;
  377. return DependencyLib;
  378. }));