UNPKG

12.6 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.$LG = exports.lgQuery = void 0;
4function initLgPolyfills() {
5 (function () {
6 if (typeof window.CustomEvent === 'function')
7 return false;
8 function CustomEvent(event, params) {
9 params = params || {
10 bubbles: false,
11 cancelable: false,
12 detail: null,
13 };
14 var evt = document.createEvent('CustomEvent');
15 evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
16 return evt;
17 }
18 window.CustomEvent = CustomEvent;
19 })();
20 (function () {
21 if (!Element.prototype.matches) {
22 Element.prototype.matches =
23 Element.prototype.msMatchesSelector ||
24 Element.prototype.webkitMatchesSelector;
25 }
26 })();
27}
28var lgQuery = /** @class */ (function () {
29 function lgQuery(selector) {
30 this.cssVenderPrefixes = [
31 'TransitionDuration',
32 'TransitionTimingFunction',
33 'Transform',
34 'Transition',
35 ];
36 this.selector = this._getSelector(selector);
37 this.firstElement = this._getFirstEl();
38 return this;
39 }
40 lgQuery.generateUUID = function () {
41 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
42 var r = (Math.random() * 16) | 0, v = c == 'x' ? r : (r & 0x3) | 0x8;
43 return v.toString(16);
44 });
45 };
46 lgQuery.prototype._getSelector = function (selector, context) {
47 if (context === void 0) { context = document; }
48 if (typeof selector !== 'string') {
49 return selector;
50 }
51 context = context || document;
52 var fl = selector.substring(0, 1);
53 if (fl === '#') {
54 return context.querySelector(selector);
55 }
56 else {
57 return context.querySelectorAll(selector);
58 }
59 };
60 lgQuery.prototype._each = function (func) {
61 if (!this.selector) {
62 return this;
63 }
64 if (this.selector.length !== undefined) {
65 [].forEach.call(this.selector, func);
66 }
67 else {
68 func(this.selector, 0);
69 }
70 return this;
71 };
72 lgQuery.prototype._setCssVendorPrefix = function (el, cssProperty, value) {
73 // prettier-ignore
74 var property = cssProperty.replace(/-([a-z])/gi, function (s, group1) {
75 return group1.toUpperCase();
76 });
77 if (this.cssVenderPrefixes.indexOf(property) !== -1) {
78 el.style[property.charAt(0).toLowerCase() + property.slice(1)] = value;
79 el.style['webkit' + property] = value;
80 el.style['moz' + property] = value;
81 el.style['ms' + property] = value;
82 el.style['o' + property] = value;
83 }
84 else {
85 el.style[property] = value;
86 }
87 };
88 lgQuery.prototype._getFirstEl = function () {
89 if (this.selector && this.selector.length !== undefined) {
90 return this.selector[0];
91 }
92 else {
93 return this.selector;
94 }
95 };
96 lgQuery.prototype.isEventMatched = function (event, eventName) {
97 var eventNamespace = eventName.split('.');
98 return event
99 .split('.')
100 .filter(function (e) { return e; })
101 .every(function (e) {
102 return eventNamespace.indexOf(e) !== -1;
103 });
104 };
105 lgQuery.prototype.attr = function (attr, value) {
106 if (value === undefined) {
107 if (!this.firstElement) {
108 return '';
109 }
110 return this.firstElement.getAttribute(attr);
111 }
112 this._each(function (el) {
113 el.setAttribute(attr, value);
114 });
115 return this;
116 };
117 lgQuery.prototype.find = function (selector) {
118 return $LG(this._getSelector(selector, this.selector));
119 };
120 lgQuery.prototype.first = function () {
121 if (this.selector && this.selector.length !== undefined) {
122 return $LG(this.selector[0]);
123 }
124 else {
125 return $LG(this.selector);
126 }
127 };
128 lgQuery.prototype.eq = function (index) {
129 return $LG(this.selector[index]);
130 };
131 lgQuery.prototype.parent = function () {
132 return $LG(this.selector.parentElement);
133 };
134 lgQuery.prototype.get = function () {
135 return this._getFirstEl();
136 };
137 lgQuery.prototype.removeAttr = function (attributes) {
138 var attrs = attributes.split(' ');
139 this._each(function (el) {
140 attrs.forEach(function (attr) { return el.removeAttribute(attr); });
141 });
142 return this;
143 };
144 lgQuery.prototype.wrap = function (className) {
145 if (!this.firstElement) {
146 return this;
147 }
148 var wrapper = document.createElement('div');
149 wrapper.className = className;
150 this.firstElement.parentNode.insertBefore(wrapper, this.firstElement);
151 this.firstElement.parentNode.removeChild(this.firstElement);
152 wrapper.appendChild(this.firstElement);
153 return this;
154 };
155 lgQuery.prototype.addClass = function (classNames) {
156 if (classNames === void 0) { classNames = ''; }
157 this._each(function (el) {
158 // IE doesn't support multiple arguments
159 classNames.split(' ').forEach(function (className) {
160 if (className) {
161 el.classList.add(className);
162 }
163 });
164 });
165 return this;
166 };
167 lgQuery.prototype.removeClass = function (classNames) {
168 this._each(function (el) {
169 // IE doesn't support multiple arguments
170 classNames.split(' ').forEach(function (className) {
171 if (className) {
172 el.classList.remove(className);
173 }
174 });
175 });
176 return this;
177 };
178 lgQuery.prototype.hasClass = function (className) {
179 if (!this.firstElement) {
180 return false;
181 }
182 return this.firstElement.classList.contains(className);
183 };
184 lgQuery.prototype.hasAttribute = function (attribute) {
185 if (!this.firstElement) {
186 return false;
187 }
188 return this.firstElement.hasAttribute(attribute);
189 };
190 lgQuery.prototype.toggleClass = function (className) {
191 if (!this.firstElement) {
192 return this;
193 }
194 if (this.hasClass(className)) {
195 this.removeClass(className);
196 }
197 else {
198 this.addClass(className);
199 }
200 return this;
201 };
202 lgQuery.prototype.css = function (property, value) {
203 var _this = this;
204 this._each(function (el) {
205 _this._setCssVendorPrefix(el, property, value);
206 });
207 return this;
208 };
209 // Need to pass separate namespaces for separate elements
210 lgQuery.prototype.on = function (events, listener) {
211 var _this = this;
212 if (!this.selector) {
213 return this;
214 }
215 events.split(' ').forEach(function (event) {
216 if (!Array.isArray(lgQuery.eventListeners[event])) {
217 lgQuery.eventListeners[event] = [];
218 }
219 lgQuery.eventListeners[event].push(listener);
220 _this.selector.addEventListener(event.split('.')[0], listener);
221 });
222 return this;
223 };
224 // @todo - test this
225 lgQuery.prototype.once = function (event, listener) {
226 var _this = this;
227 this.on(event, function () {
228 _this.off(event);
229 listener(event);
230 });
231 return this;
232 };
233 lgQuery.prototype.off = function (event) {
234 var _this = this;
235 if (!this.selector) {
236 return this;
237 }
238 Object.keys(lgQuery.eventListeners).forEach(function (eventName) {
239 if (_this.isEventMatched(event, eventName)) {
240 lgQuery.eventListeners[eventName].forEach(function (listener) {
241 _this.selector.removeEventListener(eventName.split('.')[0], listener);
242 });
243 lgQuery.eventListeners[eventName] = [];
244 }
245 });
246 return this;
247 };
248 lgQuery.prototype.trigger = function (event, detail) {
249 if (!this.firstElement) {
250 return this;
251 }
252 var customEvent = new CustomEvent(event.split('.')[0], {
253 detail: detail || null,
254 });
255 this.firstElement.dispatchEvent(customEvent);
256 return this;
257 };
258 // Does not support IE
259 lgQuery.prototype.load = function (url) {
260 var _this = this;
261 fetch(url)
262 .then(function (res) { return res.text(); })
263 .then(function (html) {
264 _this.selector.innerHTML = html;
265 });
266 return this;
267 };
268 lgQuery.prototype.html = function (html) {
269 if (html === undefined) {
270 if (!this.firstElement) {
271 return '';
272 }
273 return this.firstElement.innerHTML;
274 }
275 this._each(function (el) {
276 el.innerHTML = html;
277 });
278 return this;
279 };
280 lgQuery.prototype.append = function (html) {
281 this._each(function (el) {
282 if (typeof html === 'string') {
283 el.insertAdjacentHTML('beforeend', html);
284 }
285 else {
286 el.appendChild(html);
287 }
288 });
289 return this;
290 };
291 lgQuery.prototype.prepend = function (html) {
292 this._each(function (el) {
293 el.insertAdjacentHTML('afterbegin', html);
294 });
295 return this;
296 };
297 lgQuery.prototype.remove = function () {
298 this._each(function (el) {
299 el.parentNode.removeChild(el);
300 });
301 return this;
302 };
303 lgQuery.prototype.empty = function () {
304 this._each(function (el) {
305 el.innerHTML = '';
306 });
307 return this;
308 };
309 lgQuery.prototype.scrollTop = function (scrollTop) {
310 if (scrollTop !== undefined) {
311 document.body.scrollTop = scrollTop;
312 document.documentElement.scrollTop = scrollTop;
313 return this;
314 }
315 else {
316 return (window.pageYOffset ||
317 document.documentElement.scrollTop ||
318 document.body.scrollTop ||
319 0);
320 }
321 };
322 lgQuery.prototype.scrollLeft = function (scrollLeft) {
323 if (scrollLeft !== undefined) {
324 document.body.scrollLeft = scrollLeft;
325 document.documentElement.scrollLeft = scrollLeft;
326 return this;
327 }
328 else {
329 return (window.pageXOffset ||
330 document.documentElement.scrollLeft ||
331 document.body.scrollLeft ||
332 0);
333 }
334 };
335 lgQuery.prototype.offset = function () {
336 if (!this.firstElement) {
337 return {
338 left: 0,
339 top: 0,
340 };
341 }
342 var rect = this.firstElement.getBoundingClientRect();
343 var bodyMarginLeft = $LG('body').style().marginLeft;
344 // Minus body margin - https://stackoverflow.com/questions/30711548/is-getboundingclientrect-left-returning-a-wrong-value
345 return {
346 left: rect.left - parseFloat(bodyMarginLeft) + this.scrollLeft(),
347 top: rect.top + this.scrollTop(),
348 };
349 };
350 lgQuery.prototype.style = function () {
351 if (!this.firstElement) {
352 return {};
353 }
354 return (this.firstElement.currentStyle ||
355 window.getComputedStyle(this.firstElement));
356 };
357 // Width without padding and border even if box-sizing is used.
358 lgQuery.prototype.width = function () {
359 var style = this.style();
360 return (this.firstElement.clientWidth -
361 parseFloat(style.paddingLeft) -
362 parseFloat(style.paddingRight));
363 };
364 // Height without padding and border even if box-sizing is used.
365 lgQuery.prototype.height = function () {
366 var style = this.style();
367 return (this.firstElement.clientHeight -
368 parseFloat(style.paddingTop) -
369 parseFloat(style.paddingBottom));
370 };
371 lgQuery.eventListeners = {};
372 return lgQuery;
373}());
374exports.lgQuery = lgQuery;
375function $LG(selector) {
376 initLgPolyfills();
377 return new lgQuery(selector);
378}
379exports.$LG = $LG;
380//# sourceMappingURL=lgQuery.js.map
\No newline at end of file