]>
Commit | Line | Data |
---|---|---|
7093e9ba MG |
1 | /*! |
2 | * ki.js - jQuery-like API super-tiny JavaScript library | |
3 | * Copyright (c) 2014 Denis Ciccale (@tdecs) | |
4 | * Released under MIT license | |
5 | */ | |
6 | !function (b, c, d, e) { | |
7 | ||
8 | /* | |
9 | * init function (internal use) | |
10 | * a = selector, dom element or function | |
11 | */ | |
12 | function i(a) { | |
13 | c.push.apply(this, a && a.nodeType ? [a] : '' + a === a ? b.querySelectorAll(a) : e) | |
14 | } | |
15 | ||
16 | /* | |
17 | * $ main function | |
18 | * a = css selector, dom object, or function | |
19 | * http://www.dustindiaz.com/smallest-domready-ever | |
20 | * returns instance or executes function on ready | |
21 | */ | |
22 | $ = function (a) { | |
23 | return /^f/.test(typeof a) ? /c/.test(b.readyState) ? a() : $(b).on('DOMContentLoaded', a) : new i(a) | |
24 | } | |
25 | ||
26 | // set ki prototype | |
27 | $[d] = i[d] = { | |
28 | ||
29 | // default length | |
30 | length: 0, | |
31 | ||
32 | /* | |
33 | * on method | |
34 | * a = string event type i.e 'click' | |
35 | * b = function | |
36 | * return this | |
37 | */ | |
38 | on: function (a, b) { | |
39 | return this.each(function (c) { | |
40 | c.addEventListener(a, b) | |
41 | }) | |
42 | }, | |
43 | ||
44 | /* | |
45 | * off method | |
46 | * a = string event type i.e 'click' | |
47 | * b = function | |
48 | * return this | |
49 | */ | |
50 | off: function (a, b) { | |
51 | return this.each(function (c) { | |
52 | c.removeEventListener(a, b) | |
53 | }) | |
54 | }, | |
55 | ||
56 | /* | |
57 | * each method | |
58 | * use native forEach to iterate collection | |
59 | * a = the function to call on each iteration | |
60 | * b = the this value for that function | |
61 | */ | |
62 | each: function (a, b) { | |
63 | c.forEach.call(this, a, b) | |
64 | return this | |
65 | }, | |
66 | ||
67 | // for some reason is needed to get an array-like | |
68 | // representation instead of an object | |
69 | splice: c.splice | |
70 | } | |
71 | }(document, [], 'prototype'); |