PageRenderTime 107ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/jbone/0.0.8/jbone.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 343 lines | 334 code | 1 blank | 8 comment | 80 complexity | d2a75cbdc9d24245e7059226bc8cb90f MD5 | raw file
  1. /*!
  2. * jBone v0.0.8 - 2013-11-13 - Library for DOM manipulation
  3. *
  4. * https://github.com/kupriyanenko/jbone
  5. *
  6. * Copyright 2013 Alexey Kupriyanenko
  7. * Released under the MIT license.
  8. */
  9. (function(exports, global) {
  10. global["true"] = exports;
  11. var rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>|)$/;
  12. var rquickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/;
  13. function jBone(element, data) {
  14. if (this instanceof jBone) {
  15. return init.call(this, element, data);
  16. } else {
  17. return new jBone(element, data);
  18. }
  19. }
  20. function init(element, data) {
  21. var elements;
  22. if (element instanceof jBone) {
  23. return element;
  24. } else if (Array.isArray(element)) {
  25. elements = element.map(function(el) {
  26. return getElement(el, data);
  27. });
  28. } else if (element) {
  29. elements = getElement(element, data);
  30. }
  31. elements = Array.isArray(elements) ? elements : [ elements ];
  32. jBone.merge(this, elements);
  33. if (data) {
  34. this.attr(data);
  35. }
  36. return this;
  37. }
  38. function getElement(element) {
  39. var tag, wraper;
  40. if (typeof element === "string" && (tag = rsingleTag.exec(element))) {
  41. return document.createElement(tag[1]);
  42. } else if (typeof element === "string" && (tag = rquickExpr.exec(element)) && tag[1]) {
  43. wraper = document.createElement("div");
  44. wraper.innerHTML = element;
  45. return [].slice.call(wraper.childNodes);
  46. } else if (typeof element === "string") {
  47. return [].slice.call(document.querySelectorAll(element));
  48. }
  49. return element;
  50. }
  51. jBone.setId = function(el) {
  52. var jid = el.jid || undefined;
  53. if (el === window) {
  54. jid = "window";
  55. } else if (!el.jid) {
  56. jid = ++jBone._cache.jid;
  57. el.jid = jid;
  58. }
  59. if (!jBone._cache.events[jid]) {
  60. jBone._cache.events[jid] = {};
  61. }
  62. };
  63. jBone.getData = function(el) {
  64. el = el instanceof jBone ? el[0] : el;
  65. var jid = el === window ? "window" : el.jid;
  66. return {
  67. jid: jid,
  68. events: jBone._cache.events[jid]
  69. };
  70. };
  71. jBone.merge = function(first, second) {
  72. var l = second.length, i = first.length, j = 0;
  73. if (typeof l === "number") {
  74. while (j < l) {
  75. first[i++] = second[j];
  76. j++;
  77. }
  78. } else {
  79. while (second[j] !== undefined) {
  80. first[i++] = second[j++];
  81. }
  82. }
  83. first.length = i;
  84. return first;
  85. };
  86. jBone._cache = {
  87. events: {},
  88. jid: 0
  89. };
  90. jBone.fn = jBone.prototype = [];
  91. window.jBone = window.$ = jBone;
  92. jBone.fn.on = function() {
  93. var event = arguments[0], callback, target, namespace, fn, events;
  94. if (arguments.length === 2) {
  95. callback = arguments[1];
  96. } else {
  97. target = arguments[1], callback = arguments[2];
  98. }
  99. this.forEach(function(el) {
  100. jBone.setId(el);
  101. events = jBone.getData(el).events;
  102. event.split(" ").forEach(function(event) {
  103. namespace = event.split(".")[1];
  104. event = event.split(".")[0];
  105. events[event] = events[event] ? events[event] : [];
  106. fn = function(e) {
  107. if (e.namespace && e.namespace !== namespace) {
  108. return;
  109. }
  110. if (!target) {
  111. callback.call(el, e);
  112. } else {
  113. if (~jBone(el).find(target).indexOf(e.target)) {
  114. callback.call(el, e);
  115. }
  116. }
  117. };
  118. events[event].push({
  119. namespace: namespace,
  120. fn: fn,
  121. originfn: callback
  122. });
  123. if (el.addEventListener) {
  124. el.addEventListener(event, fn, false);
  125. }
  126. });
  127. });
  128. return this;
  129. };
  130. jBone.fn.one = function() {
  131. var event = arguments[0], callback, target;
  132. if (arguments.length === 2) {
  133. callback = arguments[1];
  134. } else {
  135. target = arguments[1], callback = arguments[2];
  136. }
  137. this.forEach(function(el) {
  138. event.split(" ").forEach(function(event) {
  139. var fn = function(e) {
  140. callback.call(el, e);
  141. jBone(el).off(event, fn);
  142. };
  143. if (arguments.length === 2) {
  144. jBone(el).on(event, fn);
  145. } else {
  146. jBone(el).on(event, target, fn);
  147. }
  148. });
  149. });
  150. return this;
  151. };
  152. jBone.fn.trigger = function(eventName) {
  153. if (!eventName || !eventName.split(".")[0]) {
  154. return this;
  155. }
  156. var namespace, event;
  157. this.forEach(function(el) {
  158. eventName.split(" ").forEach(function(eventName) {
  159. namespace = eventName.split(".")[1];
  160. eventName = eventName.split(".")[0];
  161. if ("CustomEvent" in window) {
  162. event = document.createEvent("CustomEvent");
  163. event.initCustomEvent(eventName, true, true, null);
  164. } else {
  165. event = document.createEvent("Event");
  166. event.initEvent(eventName, true, true);
  167. }
  168. event.namespace = namespace;
  169. if (el.dispatchEvent) {
  170. el.dispatchEvent(event);
  171. }
  172. });
  173. });
  174. return this;
  175. };
  176. jBone.fn.off = function(event, fn) {
  177. var events, callback, namespace, getCallback = function(e) {
  178. if (fn && e.originfn === fn) {
  179. return e.fn;
  180. } else if (!fn) {
  181. return e.fn;
  182. }
  183. };
  184. this.forEach(function(el) {
  185. events = jBone.getData(el).events;
  186. event.split(" ").forEach(function(event) {
  187. namespace = event.split(".")[1];
  188. event = event.split(".")[0];
  189. if (events && events[event]) {
  190. events[event].forEach(function(e) {
  191. callback = getCallback(e);
  192. if (namespace) {
  193. if (e.namespace === namespace) {
  194. el.removeEventListener(event, callback);
  195. }
  196. } else if (!namespace) {
  197. el.removeEventListener(event, callback);
  198. }
  199. });
  200. } else if (namespace) {
  201. Object.keys(events).forEach(function(key) {
  202. events[key].forEach(function(e) {
  203. callback = getCallback(e);
  204. if (e.namespace === namespace) {
  205. el.removeEventListener(key, callback);
  206. }
  207. });
  208. });
  209. }
  210. });
  211. });
  212. return this;
  213. };
  214. jBone.fn.is = function() {
  215. var args = arguments;
  216. return this.some(function(el) {
  217. return el.tagName.toLowerCase() === args[0];
  218. });
  219. };
  220. jBone.fn.has = function() {
  221. var args = arguments;
  222. return this.some(function(el) {
  223. return el.querySelectorAll(args[0]).length;
  224. });
  225. };
  226. jBone.fn.attr = function() {
  227. var args = arguments;
  228. if (typeof args[0] === "string" && args.length === 1) {
  229. return this[0].getAttribute(args[0]);
  230. } else if (typeof args[0] === "string" && args.length > 1) {
  231. this.forEach(function(el) {
  232. el.setAttribute(args[0], args[1]);
  233. });
  234. } else if (args[0] instanceof Object) {
  235. this.forEach(function(el) {
  236. Object.keys(args[0]).forEach(function(key) {
  237. el.setAttribute(key, args[0][key]);
  238. });
  239. });
  240. }
  241. return this;
  242. };
  243. jBone.fn.val = function(value) {
  244. if (typeof value === "string") {
  245. this.forEach(function(el) {
  246. el.value = value;
  247. });
  248. } else {
  249. return this[0].value;
  250. }
  251. return this;
  252. };
  253. jBone.fn.css = function() {
  254. var args = arguments;
  255. if (typeof args[0] === "string" && args.length === 2) {
  256. this.forEach(function(el) {
  257. el.style[args[0]] = args[1];
  258. });
  259. } else if (args[0] instanceof Object) {
  260. this.forEach(function(el) {
  261. Object.keys(args[0]).forEach(function(key) {
  262. el.style[key] = args[0][key];
  263. });
  264. });
  265. }
  266. return this;
  267. };
  268. jBone.fn.find = function(selector) {
  269. var results = [];
  270. this.forEach(function(el) {
  271. [].forEach.call(el.querySelectorAll(selector), function(finded) {
  272. results.push(finded);
  273. });
  274. });
  275. return jBone(results);
  276. };
  277. jBone.fn.get = function(index) {
  278. return this[index];
  279. };
  280. jBone.fn.eq = function(index) {
  281. return jBone(this[index]);
  282. };
  283. jBone.fn.html = function() {
  284. var value = arguments[0], result;
  285. if (value !== undefined) {
  286. this.empty.call(this);
  287. this.append.call(this, value);
  288. return this;
  289. } else {
  290. result = [];
  291. this.forEach(function(el) {
  292. if (el instanceof HTMLElement) {
  293. result.push(el.innerHTML);
  294. }
  295. });
  296. return result.length ? result.join("") : null;
  297. }
  298. };
  299. jBone.fn.append = function(appended) {
  300. if (typeof appended === "string") {
  301. appended = jBone(appended);
  302. }
  303. if (appended instanceof jBone) {
  304. this.forEach(function(el, i) {
  305. appended.forEach(function(jel) {
  306. if (!i) {
  307. el.appendChild(jel);
  308. } else {
  309. el.appendChild(jel.cloneNode());
  310. }
  311. });
  312. });
  313. } else if (appended instanceof HTMLElement || appended instanceof DocumentFragment) {
  314. this.forEach(function(el) {
  315. el.appendChild(appended);
  316. });
  317. }
  318. return this;
  319. };
  320. jBone.fn.appendTo = function(to) {
  321. jBone(to).append(this);
  322. return this;
  323. };
  324. jBone.fn.empty = function() {
  325. this.forEach(function(el) {
  326. while (el.hasChildNodes()) {
  327. el.removeChild(el.lastChild);
  328. }
  329. });
  330. return this;
  331. };
  332. jBone.fn.remove = function() {
  333. this.forEach(function(el) {
  334. if (el.parentNode) {
  335. el.parentNode.removeChild(el);
  336. }
  337. });
  338. return this;
  339. };
  340. })({}, function() {
  341. return this;
  342. }());