scratch – Blame information for rev 115
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
115 | office | 1 | /* |
2 | * blueimp helper JS |
||
3 | * https://github.com/blueimp/Gallery |
||
4 | * |
||
5 | * Copyright 2013, Sebastian Tschan |
||
6 | * https://blueimp.net |
||
7 | * |
||
8 | * Licensed under the MIT license: |
||
9 | * https://opensource.org/licenses/MIT |
||
10 | */ |
||
11 | |||
12 | /* global define, window, document */ |
||
13 | |||
14 | ;(function () { |
||
15 | 'use strict' |
||
16 | |||
17 | function extend (obj1, obj2) { |
||
18 | var prop |
||
19 | for (prop in obj2) { |
||
20 | if (obj2.hasOwnProperty(prop)) { |
||
21 | obj1[prop] = obj2[prop] |
||
22 | } |
||
23 | } |
||
24 | return obj1 |
||
25 | } |
||
26 | |||
27 | function Helper (query) { |
||
28 | if (!this || this.find !== Helper.prototype.find) { |
||
29 | // Called as function instead of as constructor, |
||
30 | // so we simply return a new instance: |
||
31 | return new Helper(query) |
||
32 | } |
||
33 | this.length = 0 |
||
34 | if (query) { |
||
35 | if (typeof query === 'string') { |
||
36 | query = this.find(query) |
||
37 | } |
||
38 | if (query.nodeType || query === query.window) { |
||
39 | // Single HTML element |
||
40 | this.length = 1 |
||
41 | this[0] = query |
||
42 | } else { |
||
43 | // HTML element collection |
||
44 | var i = query.length |
||
45 | this.length = i |
||
46 | while (i) { |
||
47 | i -= 1 |
||
48 | this[i] = query[i] |
||
49 | } |
||
50 | } |
||
51 | } |
||
52 | } |
||
53 | |||
54 | Helper.extend = extend |
||
55 | |||
56 | Helper.contains = function (container, element) { |
||
57 | do { |
||
58 | element = element.parentNode |
||
59 | if (element === container) { |
||
60 | return true |
||
61 | } |
||
62 | } while (element) |
||
63 | return false |
||
64 | } |
||
65 | |||
66 | Helper.parseJSON = function (string) { |
||
67 | return window.JSON && JSON.parse(string) |
||
68 | } |
||
69 | |||
70 | extend(Helper.prototype, { |
||
71 | find: function (query) { |
||
72 | var container = this[0] || document |
||
73 | if (typeof query === 'string') { |
||
74 | if (container.querySelectorAll) { |
||
75 | query = container.querySelectorAll(query) |
||
76 | } else if (query.charAt(0) === '#') { |
||
77 | query = container.getElementById(query.slice(1)) |
||
78 | } else { |
||
79 | query = container.getElementsByTagName(query) |
||
80 | } |
||
81 | } |
||
82 | return new Helper(query) |
||
83 | }, |
||
84 | |||
85 | hasClass: function (className) { |
||
86 | if (!this[0]) { |
||
87 | return false |
||
88 | } |
||
89 | return new RegExp('(^|\\s+)' + className + |
||
90 | '(\\s+|$)').test(this[0].className) |
||
91 | }, |
||
92 | |||
93 | addClass: function (className) { |
||
94 | var i = this.length |
||
95 | var element |
||
96 | while (i) { |
||
97 | i -= 1 |
||
98 | element = this[i] |
||
99 | if (!element.className) { |
||
100 | element.className = className |
||
101 | return this |
||
102 | } |
||
103 | if (this.hasClass(className)) { |
||
104 | return this |
||
105 | } |
||
106 | element.className += ' ' + className |
||
107 | } |
||
108 | return this |
||
109 | }, |
||
110 | |||
111 | removeClass: function (className) { |
||
112 | var regexp = new RegExp('(^|\\s+)' + className + '(\\s+|$)') |
||
113 | var i = this.length |
||
114 | var element |
||
115 | while (i) { |
||
116 | i -= 1 |
||
117 | element = this[i] |
||
118 | element.className = element.className.replace(regexp, ' ') |
||
119 | } |
||
120 | return this |
||
121 | }, |
||
122 | |||
123 | on: function (eventName, handler) { |
||
124 | var eventNames = eventName.split(/\s+/) |
||
125 | var i |
||
126 | var element |
||
127 | while (eventNames.length) { |
||
128 | eventName = eventNames.shift() |
||
129 | i = this.length |
||
130 | while (i) { |
||
131 | i -= 1 |
||
132 | element = this[i] |
||
133 | if (element.addEventListener) { |
||
134 | element.addEventListener(eventName, handler, false) |
||
135 | } else if (element.attachEvent) { |
||
136 | element.attachEvent('on' + eventName, handler) |
||
137 | } |
||
138 | } |
||
139 | } |
||
140 | return this |
||
141 | }, |
||
142 | |||
143 | off: function (eventName, handler) { |
||
144 | var eventNames = eventName.split(/\s+/) |
||
145 | var i |
||
146 | var element |
||
147 | while (eventNames.length) { |
||
148 | eventName = eventNames.shift() |
||
149 | i = this.length |
||
150 | while (i) { |
||
151 | i -= 1 |
||
152 | element = this[i] |
||
153 | if (element.removeEventListener) { |
||
154 | element.removeEventListener(eventName, handler, false) |
||
155 | } else if (element.detachEvent) { |
||
156 | element.detachEvent('on' + eventName, handler) |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | return this |
||
161 | }, |
||
162 | |||
163 | empty: function () { |
||
164 | var i = this.length |
||
165 | var element |
||
166 | while (i) { |
||
167 | i -= 1 |
||
168 | element = this[i] |
||
169 | while (element.hasChildNodes()) { |
||
170 | element.removeChild(element.lastChild) |
||
171 | } |
||
172 | } |
||
173 | return this |
||
174 | }, |
||
175 | |||
176 | first: function () { |
||
177 | return new Helper(this[0]) |
||
178 | } |
||
179 | |||
180 | }) |
||
181 | |||
182 | if (typeof define === 'function' && define.amd) { |
||
183 | define(function () { |
||
184 | return Helper |
||
185 | }) |
||
186 | } else { |
||
187 | window.blueimp = window.blueimp || {} |
||
188 | window.blueimp.helper = Helper |
||
189 | } |
||
190 | }()) |