PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/jbone/0.0.6/jbone.js

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