scratch – Blame information for rev 117

Subversion Repositories:
Rev:
Rev Author Line No. Line
117 office 1 /*===========================
2 Dom7 Library
3 ===========================*/
4 var Dom7 = (function () {
5 var Dom7 = function (arr) {
6 var _this = this, i = 0;
7 // Create array-like object
8 for (i = 0; i < arr.length; i++) {
9 _this[i] = arr[i];
10 }
11 _this.length = arr.length;
12 // Return collection with methods
13 return this;
14 };
15 var $ = function (selector, context) {
16 var arr = [], i = 0;
17 if (selector && !context) {
18 if (selector instanceof Dom7) {
19 return selector;
20 }
21 }
22 if (selector) {
23 // String
24 if (typeof selector === 'string') {
25 var els, tempParent, html = selector.trim();
26 if (html.indexOf('<') >= 0 && html.indexOf('>') >= 0) {
27 var toCreate = 'div';
28 if (html.indexOf('<li') === 0) toCreate = 'ul';
29 if (html.indexOf('<tr') === 0) toCreate = 'tbody';
30 if (html.indexOf('<td') === 0 || html.indexOf('<th') === 0) toCreate = 'tr';
31 if (html.indexOf('<tbody') === 0) toCreate = 'table';
32 if (html.indexOf('<option') === 0) toCreate = 'select';
33 tempParent = document.createElement(toCreate);
34 tempParent.innerHTML = selector;
35 for (i = 0; i < tempParent.childNodes.length; i++) {
36 arr.push(tempParent.childNodes[i]);
37 }
38 }
39 else {
40 if (!context && selector[0] === '#' && !selector.match(/[ .<>:~]/)) {
41 <> // Pure ID selector
42 <> els = [document.getElementById(selector.split('#')[1])];
43 <> }
44 <> else {
45 <> // Other selectors
46 <> els = (context || document).querySelectorAll(selector);
47 <> }
48 <> for (i = 0; i < els.length; i++) {
49 <> if (els[i]) arr.push(els[i]);
50 <> }
51 <> }
52 <> }
53 <> // Node/element
54 <> else if (selector.nodeType || selector === window || selector === document) {
55 <> arr.push(selector);
56 <> }
57 <> //Array of elements or instance of Dom
58 <> else if (selector.length > 0 && selector[0].nodeType) {
59 <> for (i = 0; i < selector.length; i++) {
60 <> arr.push(selector[i]);
61 <> }
62 <> }
63 <> }
64 <> return new Dom7(arr);
65 <> };
66 <> Dom7.prototype = {
67 <> // Classes and attriutes
68 <> addClass: function (className) {
69 <> if (typeof className === 'undefined') {
70 <> return this;
71 <> }
72 <> var classes = className.split(' ');
73 <> for (var i = 0; i < classes.length; i++) {
74 <> for (var j = 0; j < this.length; j++) {
75 <> this[j].classList.add(classes[i]);
76 <> }
77 <> }
78 <> return this;
79 <> },
80 <> removeClass: function (className) {
81 <> var classes = className.split(' ');
82 <> for (var i = 0; i < classes.length; i++) {
83 <> for (var j = 0; j < this.length; j++) {
84 <> this[j].classList.remove(classes[i]);
85 <> }
86 <> }
87 <> return this;
88 <> },
89 <> hasClass: function (className) {
90 <> if (!this[0]) return false;
91 <> else return this[0].classList.contains(className);
92 <> },
93 <> toggleClass: function (className) {
94 <> var classes = className.split(' ');
95 <> for (var i = 0; i < classes.length; i++) {
96 <> for (var j = 0; j < this.length; j++) {
97 <> this[j].classList.toggle(classes[i]);
98 <> }
99 <> }
100 <> return this;
101 <> },
102 <> attr: function (attrs, value) {
103 <> if (arguments.length === 1 && typeof attrs === 'string') {
104 <> // Get attr
105 <> if (this[0]) return this[0].getAttribute(attrs);
106 <> else return undefined;
107 <> }
108 <> else {
109 <> // Set attrs
110 <> for (var i = 0; i < this.length; i++) {
111 <> if (arguments.length === 2) {
112 <> // String
113 <> this[i].setAttribute(attrs, value);
114 <> }
115 <> else {
116 <> // Object
117 <> for (var attrName in attrs) {
118 <> this[i][attrName] = attrs[attrName];
119 <> this[i].setAttribute(attrName, attrs[attrName]);
120 <> }
121 <> }
122 <> }
123 <> return this;
124 <> }
125 <> },
126 <> removeAttr: function (attr) {
127 <> for (var i = 0; i < this.length; i++) {
128 <> this[i].removeAttribute(attr);
129 <> }
130 <> return this;
131 <> },
132 <> data: function (key, value) {
133 <> if (typeof value === 'undefined') {
134 <> // Get value
135 <> if (this[0]) {
136 <> var dataKey = this[0].getAttribute('data-' + key);
137 <> if (dataKey) return dataKey;
138 <> else if (this[0].dom7ElementDataStorage && (key in this[0].dom7ElementDataStorage)) return this[0].dom7ElementDataStorage[key];
139 <> else return undefined;
140 <> }
141 <> else return undefined;
142 <> }
143 <> else {
144 <> // Set value
145 <> for (var i = 0; i < this.length; i++) {
146 <> var el = this[i];
147 <> if (!el.dom7ElementDataStorage) el.dom7ElementDataStorage = {};
148 <> el.dom7ElementDataStorage[key] = value;
149 <> }
150 <> return this;
151 <> }
152 <> },
153 <> // Transforms
154 <> transform : function (transform) {
155 <> for (var i = 0; i < this.length; i++) {
156 <> var elStyle = this[i].style;
157 <> elStyle.webkitTransform = elStyle.MsTransform = elStyle.msTransform = elStyle.MozTransform = elStyle.OTransform = elStyle.transform = transform;
158 <> }
159 <> return this;
160 <> },
161 <> transition: function (duration) {
162 <> if (typeof duration !== 'string') {
163 <> duration = duration + 'ms';
164 <> }
165 <> for (var i = 0; i < this.length; i++) {
166 <> var elStyle = this[i].style;
167 <> elStyle.webkitTransitionDuration = elStyle.MsTransitionDuration = elStyle.msTransitionDuration = elStyle.MozTransitionDuration = elStyle.OTransitionDuration = elStyle.transitionDuration = duration;
168 <> }
169 <> return this;
170 <> },
171 <> //Events
172 <> on: function (eventName, targetSelector, listener, capture) {
173 <> function handleLiveEvent(e) {
174 <> var target = e.target;
175 <> if ($(target).is(targetSelector)) listener.call(target, e);
176 <> else {
177 <> var parents = $(target).parents();
178 <> for (var k = 0; k < parents.length; k++) {
179 <> if ($(parents[k]).is(targetSelector)) listener.call(parents[k], e);
180 <> }
181 <> }
182 <> }
183 <> var events = eventName.split(' ');
184 <> var i, j;
185 <> for (i = 0; i < this.length; i++) {
186 <> if (typeof targetSelector === 'function' || targetSelector === false) {
187 <> // Usual events
188 <> if (typeof targetSelector === 'function') {
189 <> listener = arguments[1];
190 <> capture = arguments[2] || false;
191 <> }
192 <> for (j = 0; j < events.length; j++) {
193 <> this[i].addEventListener(events[j], listener, capture);
194 <> }
195 <> }
196 <> else {
197 <> //Live events
198 <> for (j = 0; j < events.length; j++) {
199 <> if (!this[i].dom7LiveListeners) this[i].dom7LiveListeners = [];
200 <> this[i].dom7LiveListeners.push({listener: listener, liveListener: handleLiveEvent});
201 <> this[i].addEventListener(events[j], handleLiveEvent, capture);
202 <> }
203 <> }
204 <> }
205  
206 <> return this;
207 <> },
208 <> off: function (eventName, targetSelector, listener, capture) {
209 <> var events = eventName.split(' ');
210 <> for (var i = 0; i < events.length; i++) {
211 <> for (var j = 0; j < this.length; j++) {
212 <> if (typeof targetSelector === 'function' || targetSelector === false) {
213 <> // Usual events
214 <> if (typeof targetSelector === 'function') {
215 <> listener = arguments[1];
216 <> capture = arguments[2] || false;
217 <> }
218 <> this[j].removeEventListener(events[i], listener, capture);
219 <> }
220 <> else {
221 <> // Live event
222 <> if (this[j].dom7LiveListeners) {
223 <> for (var k = 0; k < this[j].dom7LiveListeners.length; k++) {
224 <> if (this[j].dom7LiveListeners[k].listener === listener) {
225 <> this[j].removeEventListener(events[i], this[j].dom7LiveListeners[k].liveListener, capture);
226 <> }
227 <> }
228 <> }
229 <> }
230 <> }
231 <> }
232 <> return this;
233 <> },
234 <> once: function (eventName, targetSelector, listener, capture) {
235 <> var dom = this;
236 <> if (typeof targetSelector === 'function') {
237 <> targetSelector = false;
238 <> listener = arguments[1];
239 <> capture = arguments[2];
240 <> }
241 <> function proxy(e) {
242 <> listener(e);
243 <> dom.off(eventName, targetSelector, proxy, capture);
244 <> }
245 <> dom.on(eventName, targetSelector, proxy, capture);
246 <> },
247 <> trigger: function (eventName, eventData) {
248 <> for (var i = 0; i < this.length; i++) {
249 <> var evt;
250 <> try {
251 <> evt = new window.CustomEvent(eventName, {detail: eventData, bubbles: true, cancelable: true});
252 <> }
253 <> catch (e) {
254 <> evt = document.createEvent('Event');
255 <> evt.initEvent(eventName, true, true);
256 <> evt.detail = eventData;
257 <> }
258 <> this[i].dispatchEvent(evt);
259 <> }
260 <> return this;
261 <> },
262 <> transitionEnd: function (callback) {
263 <> var events = ['webkitTransitionEnd', 'transitionend', 'oTransitionEnd', 'MSTransitionEnd', 'msTransitionEnd'],
264 <> i, j, dom = this;
265 <> function fireCallBack(e) {
266 <> /*jshint validthis:true */
267 <> if (e.target !== this) return;
268 <> callback.call(this, e);
269 <> for (i = 0; i < events.length; i++) {
270 <> dom.off(events[i], fireCallBack);
271 <> }
272 <> }
273 <> if (callback) {
274 <> for (i = 0; i < events.length; i++) {
275 <> dom.on(events[i], fireCallBack);
276 <> }
277 <> }
278 <> return this;
279 <> },
280 <> // Sizing/Styles
281 <> width: function () {
282 <> if (this[0] === window) {
283 <> return window.innerWidth;
284 <> }
285 <> else {
286 <> if (this.length > 0) {
287 <> return parseFloat(this.css('width'));
288 <> }
289 <> else {
290 <> return null;
291 <> }
292 <> }
293 <> },
294 <> outerWidth: function (includeMargins) {
295 <> if (this.length > 0) {
296 <> if (includeMargins)
297 <> return this[0].offsetWidth + parseFloat(this.css('margin-right')) + parseFloat(this.css('margin-left'));
298 <> else
299 <> return this[0].offsetWidth;
300 <> }
301 <> else return null;
302 <> },
303 <> height: function () {
304 <> if (this[0] === window) {
305 <> return window.innerHeight;
306 <> }
307 <> else {
308 <> if (this.length > 0) {
309 <> return parseFloat(this.css('height'));
310 <> }
311 <> else {
312 <> return null;
313 <> }
314 <> }
315 <> },
316 <> outerHeight: function (includeMargins) {
317 <> if (this.length > 0) {
318 <> if (includeMargins)
319 <> return this[0].offsetHeight + parseFloat(this.css('margin-top')) + parseFloat(this.css('margin-bottom'));
320 <> else
321 <> return this[0].offsetHeight;
322 <> }
323 <> else return null;
324 <> },
325 <> offset: function () {
326 <> if (this.length > 0) {
327 <> var el = this[0];
328 <> var box = el.getBoundingClientRect();
329 <> var body = document.body;
330 <> var clientTop = el.clientTop || body.clientTop || 0;
331 <> var clientLeft = el.clientLeft || body.clientLeft || 0;
332 <> var scrollTop = window.pageYOffset || el.scrollTop;
333 <> var scrollLeft = window.pageXOffset || el.scrollLeft;
334 <> return {
335 <> top: box.top + scrollTop - clientTop,
336 <> left: box.left + scrollLeft - clientLeft
337 <> };
338 <> }
339 <> else {
340 <> return null;
341 <> }
342 <> },
343 <> css: function (props, value) {
344 <> var i;
345 <> if (arguments.length === 1) {
346 <> if (typeof props === 'string') {
347 <> if (this[0]) return window.getComputedStyle(this[0], null).getPropertyValue(props);
348 <> }
349 <> else {
350 <> for (i = 0; i < this.length; i++) {
351 <> for (var prop in props) {
352 <> this[i].style[prop] = props[prop];
353 <> }
354 <> }
355 <> return this;
356 <> }
357 <> }
358 <> if (arguments.length === 2 && typeof props === 'string') {
359 <> for (i = 0; i < this.length; i++) {
360 <> this[i].style[props] = value;
361 <> }
362 <> return this;
363 <> }
364 <> return this;
365 <> },
366  
367 <> //Dom manipulation
368 <> each: function (callback) {
369 <> for (var i = 0; i < this.length; i++) {
370 <> callback.call(this[i], i, this[i]);
371 <> }
372 <> return this;
373 <> },
374 <> html: function (html) {
375 <> if (typeof html === 'undefined') {
376 <> return this[0] ? this[0].innerHTML : undefined;
377 <> }
378 <> else {
379 <> for (var i = 0; i < this.length; i++) {
380 <> this[i].innerHTML = html;
381 <> }
382 <> return this;
383 <> }
384 <> },
385 <> text: function (text) {
386 <> if (typeof text === 'undefined') {
387 <> if (this[0]) {
388 <> return this[0].textContent.trim();
389 <> }
390 <> else return null;
391 <> }
392 <> else {
393 <> for (var i = 0; i < this.length; i++) {
394 <> this[i].textContent = text;
395 <> }
396 <> return this;
397 <> }
398 <> },
399 <> is: function (selector) {
400 <> if (!this[0]) return false;
401 <> var compareWith, i;
402 <> if (typeof selector === 'string') {
403 <> var el = this[0];
404 <> if (el === document) return selector === document;
405 <> if (el === window) return selector === window;
406  
407 <> if (el.matches) return el.matches(selector);
408 <> else if (el.webkitMatchesSelector) return el.webkitMatchesSelector(selector);
409 <> else if (el.mozMatchesSelector) return el.mozMatchesSelector(selector);
410 <> else if (el.msMatchesSelector) return el.msMatchesSelector(selector);
411 <> else {
412 <> compareWith = $(selector);
413 <> for (i = 0; i < compareWith.length; i++) {
414 <> if (compareWith[i] === this[0]) return true;
415 <> }
416 <> return false;
417 <> }
418 <> }
419 <> else if (selector === document) return this[0] === document;
420 <> else if (selector === window) return this[0] === window;
421 <> else {
422 <> if (selector.nodeType || selector instanceof Dom7) {
423 <> compareWith = selector.nodeType ? [selector] : selector;
424 <> for (i = 0; i < compareWith.length; i++) {
425 <> if (compareWith[i] === this[0]) return true;
426 <> }
427 <> return false;
428 <> }
429 <> return false;
430 <> }
431  
432 <> },
433 <> index: function () {
434 <> if (this[0]) {
435 <> var child = this[0];
436 <> var i = 0;
437 <> while ((child = child.previousSibling) !== null) {
438 <> if (child.nodeType === 1) i++;
439 <> }
440 <> return i;
441 <> }
442 <> else return undefined;
443 <> },
444 <> eq: function (index) {
445 <> if (typeof index === 'undefined') return this;
446 <> var length = this.length;
447 <> var returnIndex;
448 <> if (index > length - 1) {
449 <> return new Dom7([]);
450 <> }
451 <> if (index < 0) {
452 <> returnIndex = length + index;
453 <> if (returnIndex < 0) return new Dom7([]);
454 <> else return new Dom7([this[returnIndex]]);
455 <> }
456 <> return new Dom7([this[index]]);
457 <> },
458 <> append: function (newChild) {
459 <> var i, j;
460 <> for (i = 0; i < this.length; i++) {
461 <> if (typeof newChild === 'string') {
462 <> var tempDiv = document.createElement('div');
463 <> tempDiv.innerHTML = newChild;
464 <> while (tempDiv.firstChild) {
465 <> this[i].appendChild(tempDiv.firstChild);
466 <> }
467 <> }
468 <> else if (newChild instanceof Dom7) {
469 <> for (j = 0; j < newChild.length; j++) {
470 <> this[i].appendChild(newChild[j]);
471 <> }
472 <> }
473 <> else {
474 <> this[i].appendChild(newChild);
475 <> }
476 <> }
477 <> return this;
478 <> },
479 <> prepend: function (newChild) {
480 <> var i, j;
481 <> for (i = 0; i < this.length; i++) {
482 <> if (typeof newChild === 'string') {
483 <> var tempDiv = document.createElement('div');
484 <> tempDiv.innerHTML = newChild;
485 <> for (j = tempDiv.childNodes.length - 1; j >= 0; j--) {
486 <> this[i].insertBefore(tempDiv.childNodes[j], this[i].childNodes[0]);
487 <> }
488 <> // this[i].insertAdjacentHTML('afterbegin', newChild);
489 <> }
490 <> else if (newChild instanceof Dom7) {
491 <> for (j = 0; j < newChild.length; j++) {
492 <> this[i].insertBefore(newChild[j], this[i].childNodes[0]);
493 <> }
494 <> }
495 <> else {
496 <> this[i].insertBefore(newChild, this[i].childNodes[0]);
497 <> }
498 <> }
499 <> return this;
500 <> },
501 <> insertBefore: function (selector) {
502 <> var before = $(selector);
503 <> for (var i = 0; i < this.length; i++) {
504 <> if (before.length === 1) {
505 <> before[0].parentNode.insertBefore(this[i], before[0]);
506 <> }
507 <> else if (before.length > 1) {
508 <> for (var j = 0; j < before.length; j++) {
509 <> before[j].parentNode.insertBefore(this[i].cloneNode(true), before[j]);
510 <> }
511 <> }
512 <> }
513 <> },
514 <> insertAfter: function (selector) {
515 <> var after = $(selector);
516 <> for (var i = 0; i < this.length; i++) {
517 <> if (after.length === 1) {
518 <> after[0].parentNode.insertBefore(this[i], after[0].nextSibling);
519 <> }
520 <> else if (after.length > 1) {
521 <> for (var j = 0; j < after.length; j++) {
522 <> after[j].parentNode.insertBefore(this[i].cloneNode(true), after[j].nextSibling);
523 <> }
524 <> }
525 <> }
526 <> },
527 <> next: function (selector) {
528 <> if (this.length > 0) {
529 <> if (selector) {
530 <> if (this[0].nextElementSibling && $(this[0].nextElementSibling).is(selector)) return new Dom7([this[0].nextElementSibling]);
531 <> else return new Dom7([]);
532 <> }
533 <> else {
534 <> if (this[0].nextElementSibling) return new Dom7([this[0].nextElementSibling]);
535 <> else return new Dom7([]);
536 <> }
537 <> }
538 <> else return new Dom7([]);
539 <> },
540 <> nextAll: function (selector) {
541 <> var nextEls = [];
542 <> var el = this[0];
543 <> if (!el) return new Dom7([]);
544 <> while (el.nextElementSibling) {
545 <> var next = el.nextElementSibling;
546 <> if (selector) {
547 <> if($(next).is(selector)) nextEls.push(next);
548 <> }
549 <> else nextEls.push(next);
550 <> el = next;
551 <> }
552 <> return new Dom7(nextEls);
553 <> },
554 <> prev: function (selector) {
555 <> if (this.length > 0) {
556 <> if (selector) {
557 <> if (this[0].previousElementSibling && $(this[0].previousElementSibling).is(selector)) return new Dom7([this[0].previousElementSibling]);
558 <> else return new Dom7([]);
559 <> }
560 <> else {
561 <> if (this[0].previousElementSibling) return new Dom7([this[0].previousElementSibling]);
562 <> else return new Dom7([]);
563 <> }
564 <> }
565 <> else return new Dom7([]);
566 <> },
567 <> prevAll: function (selector) {
568 <> var prevEls = [];
569 <> var el = this[0];
570 <> if (!el) return new Dom7([]);
571 <> while (el.previousElementSibling) {
572 <> var prev = el.previousElementSibling;
573 <> if (selector) {
574 <> if($(prev).is(selector)) prevEls.push(prev);
575 <> }
576 <> else prevEls.push(prev);
577 <> el = prev;
578 <> }
579 <> return new Dom7(prevEls);
580 <> },
581 <> parent: function (selector) {
582 <> var parents = [];
583 <> for (var i = 0; i < this.length; i++) {
584 <> if (selector) {
585 <> if ($(this[i].parentNode).is(selector)) parents.push(this[i].parentNode);
586 <> }
587 <> else {
588 <> parents.push(this[i].parentNode);
589 <> }
590 <> }
591 <> return $($.unique(parents));
592 <> },
593 <> parents: function (selector) {
594 <> var parents = [];
595 <> for (var i = 0; i < this.length; i++) {
596 <> var parent = this[i].parentNode;
597 <> while (parent) {
598 <> if (selector) {
599 <> if ($(parent).is(selector)) parents.push(parent);
600 <> }
601 <> else {
602 <> parents.push(parent);
603 <> }
604 <> parent = parent.parentNode;
605 <> }
606 <> }
607 <> return $($.unique(parents));
608 <> },
609 <> find : function (selector) {
610 <> var foundElements = [];
611 <> for (var i = 0; i < this.length; i++) {
612 <> var found = this[i].querySelectorAll(selector);
613 <> for (var j = 0; j < found.length; j++) {
614 <> foundElements.push(found[j]);
615 <> }
616 <> }
617 <> return new Dom7(foundElements);
618 <> },
619 <> children: function (selector) {
620 <> var children = [];
621 <> for (var i = 0; i < this.length; i++) {
622 <> var childNodes = this[i].childNodes;
623  
624 <> for (var j = 0; j < childNodes.length; j++) {
625 <> if (!selector) {
626 <> if (childNodes[j].nodeType === 1) children.push(childNodes[j]);
627 <> }
628 <> else {
629 <> if (childNodes[j].nodeType === 1 && $(childNodes[j]).is(selector)) children.push(childNodes[j]);
630 <> }
631 <> }
632 <> }
633 <> return new Dom7($.unique(children));
634 <> },
635 <> remove: function () {
636 <> for (var i = 0; i < this.length; i++) {
637 <> if (this[i].parentNode) this[i].parentNode.removeChild(this[i]);
638 <> }
639 <> return this;
640 <> },
641 <> add: function () {
642 <> var dom = this;
643 <> var i, j;
644 <> for (i = 0; i < arguments.length; i++) {
645 <> var toAdd = $(arguments[i]);
646 <> for (j = 0; j < toAdd.length; j++) {
647 <> dom[dom.length] = toAdd[j];
648 <> dom.length++;
649 <> }
650 <> }
651 <> return dom;
652 <> }
653 <> };
654 <> $.fn = Dom7.prototype;
655 <> $.unique = function (arr) {
656 <> var unique = [];
657 <> for (var i = 0; i < arr.length; i++) {
658 <> if (unique.indexOf(arr[i]) === -1) unique.push(arr[i]);
659 <> }
660 <> return unique;
661 <> };
662  
663 <> return $;
664 <>})();