|
1 | | -/*! Tiny.js v0.3.0 **/ |
| 1 | +/*! |
| 2 | + * Tiny.js v0.4.0 |
| 3 | + * https://github.com/UserScript/tiny.js |
| 4 | + */ |
2 | 5 |
|
3 | | -//namespace |
4 | | -var $ = function (s, eWrapper) { |
5 | | - return (eWrapper || document).querySelectorAll(s); |
6 | | -}; |
| 6 | +void function (root) { |
| 7 | + 'use strict' |
7 | 8 |
|
8 | | -//utilties - array-like |
9 | | -$.each = function (a, fn, context) { |
10 | | - for (var i = 0, l = a.length; i < l; ++i) { |
11 | | - fn.call(context || window, a[i], i, a); |
| 9 | + //namespace |
| 10 | + var $ = function (s, eWrapper) { |
| 11 | + return (eWrapper || document).querySelectorAll(s) |
12 | 12 | } |
13 | | -}; |
14 | 13 |
|
15 | | -//utilties - str |
16 | | -$.strstr = function (so, s) { |
17 | | - return so.indexOf(s) > -1; |
18 | | -}; |
| 14 | + //type |
| 15 | + $.isArray = function (arr) { |
| 16 | + return Array.isArray(arr) |
| 17 | + } |
19 | 18 |
|
20 | | -//dom query |
21 | | -$.id = function (s) { |
22 | | - return document.getElementById(s); |
23 | | -}; |
24 | | -$.class = function (s, eWrapper) { |
25 | | - return (eWrapper || document).getElementsByClassName(s); |
26 | | -}; |
27 | | -$.tag = function (s, eWrapper) { |
28 | | - return (eWrapper || document).getElementsByTagName(s); |
29 | | -}; |
| 19 | + //data collection |
| 20 | + //TODO |
| 21 | + //$.extend() |
| 22 | + $.each = function (arr, fn, context) { |
| 23 | + //todo: object |
| 24 | + for (var i = 0, l = arr.length; i < l; ++i) { |
| 25 | + fn.call(context || window, arr[i], i, arr) |
| 26 | + } |
| 27 | + } |
| 28 | + $.inArray = function (arr, item) { |
| 29 | + if (!$.isArray(arr)) return false |
| 30 | + return arr.indexOf(item) > -1 |
| 31 | + } |
30 | 32 |
|
31 | | -//creat |
32 | | -$.crElem = function (s) { |
33 | | - return document.createElement(s); |
34 | | -}; |
35 | | -$.crText = function (s) { |
36 | | - return document.createTextNode(s); |
37 | | -}; |
| 33 | + //str |
| 34 | + $.str = {} |
| 35 | + $.str.include = function (so, s) { |
| 36 | + return so.indexOf(s) > -1 |
| 37 | + } |
| 38 | + //TODO |
| 39 | + //$.str.endsWith() |
| 40 | + $.str.startsWith = function (so, s) { |
| 41 | + return so.indexOf(s) === 0 |
| 42 | + } |
38 | 43 |
|
39 | | -//mod dom |
40 | | -$.insertBefore = function (e, eTarget) { |
41 | | - eTarget.parentNode.insertBefore(e, eTarget); |
42 | | -}; |
43 | | -$.before = function (eTarget, e) { |
44 | | - $.insBefore(e, eTarget); |
45 | | -}; |
46 | | -$.append = function (eWrapper, e) { |
47 | | - eWrapper.appendChild(e); |
48 | | -} |
49 | | -$.appendTo = function (e, eWrapper) { |
50 | | - $.append(eWrapper, e); |
51 | | -} |
52 | | -$.remove = function (e) { |
53 | | - e.parentNode.removeChild(e); |
54 | | -}; |
| 44 | + //dom query |
| 45 | + $.id = function (s) { |
| 46 | + return document.getElementById(s) |
| 47 | + } |
| 48 | + $.cls = function (s, eWrapper) { |
| 49 | + return (eWrapper || document).getElementsByClassName(s) |
| 50 | + } |
| 51 | + $.tag = function (s, eWrapper) { |
| 52 | + return (eWrapper || document).getElementsByTagName(s) |
| 53 | + } |
55 | 54 |
|
56 | | -//classname |
57 | | -$.hasClass = function (e, s) { |
58 | | - return $.strstr(' ' + e.className + ' ', ' ' + s + ' '); |
59 | | -}; |
60 | | -$.addClass = function (e, s) { |
61 | | - var so = e.className; |
62 | | - if (!$.hasClass(so, s)) { |
63 | | - e.className += (' ' + s); |
64 | | - } |
65 | | -}; |
66 | | -$.removeClass = function (e,s) { |
67 | | - var so = e.className; |
68 | | - if ($.hasClass(so, s)) { |
69 | | - e.className = (' ' + so + ' ').replace(' ' + s + ' ', ' ').trim(); |
70 | | - } |
71 | | -}; |
| 55 | + //create |
| 56 | + $.createElem = function (s) { |
| 57 | + return document.createElement(s) |
| 58 | + } |
| 59 | + $.createText = function (s) { |
| 60 | + return document.createTextNode(s) |
| 61 | + } |
72 | 62 |
|
73 | | -//style |
74 | | -$.visible = function (e, b) { |
75 | | - var val = b ? 'visible' : 'hidden'; |
76 | | - e.style.visibility = val; |
77 | | -}; |
78 | | -$.hide = function (e) { |
79 | | - e.style.display = 'none'; |
80 | | -}; |
81 | | -$.show = function (e) { |
82 | | - e.style.display = ''; |
83 | | -}; |
84 | | -$.setStyle = function (e, prop, val) { |
85 | | - if (v) { |
86 | | - e.style[prop] = val; |
87 | | - } else { |
88 | | - e.style.cssText = prop; |
89 | | - } |
90 | | -}; |
91 | | -$.css = function (s) { |
92 | | - var e = $.crElem('style'); |
93 | | - var cssText = s || $.cssText; |
94 | | - if (cssText) { |
95 | | - e.innerHTML = cssText; |
96 | | - //console.log(css); |
97 | | - $.tag('head')[0].appendChild(e); |
98 | | - } |
99 | | -}; |
100 | | -$.cssText = ''; |
| 63 | + //mod dom |
| 64 | + //TODO |
| 65 | + //$.after() |
| 66 | + //$.insertAfter() |
| 67 | + $.insertBefore = function (e, eTarget) { |
| 68 | + eTarget.parentNode.insertBefore(e, eTarget) |
| 69 | + } |
| 70 | + $.before = function (eTarget, e) { |
| 71 | + eTarget.parentNode.insertBefore(e, eTarget) |
| 72 | + } |
| 73 | + //TODO |
| 74 | + //$.prepend() |
| 75 | + //$.prependTo() |
| 76 | + $.append = function (eWrapper, e) { |
| 77 | + eWrapper.appendChild(e) |
| 78 | + } |
| 79 | + $.appendTo = function (e, eWrapper) { |
| 80 | + eWrapper.appendChild(e) |
| 81 | + } |
| 82 | + $.remove = function (e) { |
| 83 | + e.parentNode.removeChild(e) |
| 84 | + } |
| 85 | + |
| 86 | + //class name |
| 87 | + $.hasClass = function (e, s) { |
| 88 | + return $.str.include(' ' + e.className + ' ', ' ' + s + ' ') |
| 89 | + } |
| 90 | + $.addClass = function (e, s) { |
| 91 | + if (!$.hasClass(e, s)) { |
| 92 | + e.className += (' ' + s) |
| 93 | + } |
| 94 | + } |
| 95 | + $.removeClass = function (e, s) { |
| 96 | + if ($.hasClass(e, s)) { |
| 97 | + e.className = (' ' + e.className + ' ').replace(' ' + s + ' ', ' ').trim() |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + //style |
| 102 | + $.hide = function (e) { |
| 103 | + e.style.display = 'none' |
| 104 | + } |
| 105 | + $.show = function (e) { |
| 106 | + e.style.display = '' |
| 107 | + } |
| 108 | + $.css = function (e, prop, val) { |
| 109 | + if (arguments.length === 3) { |
| 110 | + e.style[prop] = val |
| 111 | + } else { |
| 112 | + e.style.cssText = prop |
| 113 | + } |
| 114 | + } |
| 115 | + $.insertCSS = function (s) { |
| 116 | + if (!s) return false |
| 117 | + var e = $.createElem('style') |
| 118 | + e.innerHTML = s |
| 119 | + $.tag('head')[0].appendChild(e) |
| 120 | + return e |
| 121 | + } |
| 122 | + |
| 123 | + //event |
| 124 | + $.on = function (e, sEvent, fn) { |
| 125 | + e.addEventListener(sEvent, fn, false) |
| 126 | + } |
| 127 | + $.off = function (e, sEvent, fn) { |
| 128 | + e.removeEventListener(sEvent, fn, false) |
| 129 | + } |
101 | 130 |
|
102 | | -//event |
103 | | -$.on = function (e, sEvent, fn) { |
104 | | - e.addEventListener(sEvent, fn, false); |
105 | | -}; |
106 | | -$.off = function (e, sEvent, fn) { |
107 | | - e.removeEventListener(sEvent, fn, false); |
108 | | -}; |
| 131 | + //exports |
| 132 | + root.$ = $ |
109 | 133 |
|
| 134 | +}(this) |
0 commit comments