-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlFunctions.js
122 lines (104 loc) · 3.56 KB
/
htmlFunctions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
HTML Helper functions, using a 'private' html Class.
*/
(function initHTMLFunctions (J) {
'use strict';
var JHTML = J.Class("JHTML");
JHTML.prototype.init = function (html_nodeOrCollection) {
if (this.isHtml(html_nodeOrCollection)){
return new JHTMLNode(html_nodeOrCollection);
} else if (html_nodeOrCollection instanceof window.NodeList || html_nodeOrCollection instanceof window.HTMLCollection) {
return new JHTMLCollection(html_nodeOrCollection);
}
return null;
};
JHTML.prototype.isHtml = function (n) {
var node = n || this.node;
return (node.nodeType === 1 && typeof (node.innerHTML) !== undefined);
};
JHTML.prototype.getDataSet = function (n) {
var a = 0, attribute, dataset = {}, pattern = /^data-[a-zA-Z]/i, node = n || this.node;
if (this.isHtml(node)) {
if (!!node.dataset) {
return node.dataset;
} else {
// Polyfill until supported.
for (a; a < node.attributes.length; a += 1) {
attribute = node.attributes[a];
if (pattern.test(attribute.nodeName)) {
dataset[attribute.nodeName.substr(attribute.nodeName.indexOf('-') + 1)] = attribute.value;
}
}
return dataset;
}
}
return null;
};
JHTML.prototype.getClassList = function (n) {
var node = n || this.node;
if (this.isHtml(node)) {
if (!!node.classList) {
return node.classList;
} else {
J.log('Class Lists not support in this browser','warning');
}
} else {
J.log('Passed object is not an HTML node','warning');
}
return null;
};
JHTML.prototype.getTemplateContent = function (n) {
var node = n || this.node;
if(!!node.content) {
return document.importNode(node.content, true);
} else {
var fragment = document.createDocumentFragment();
var childcount = node.childNodes.length;
for(var i = 0; i < childcount; i++) {
fragment.appendChild( node.childNodes[i].cloneNode(true) );
}
return fragment;
}
}
var JHTMLNode = J.Class('JHTMLNode').extend(JHTML);
JHTMLNode.prototype.init = function (htmlNode) {
this.node = htmlNode;
if (!this.node.dataset) {
Object.defineProperty(this, 'dataset', {
get: (function () { return JHTML.getDataSet(this.node) }).bind(this)
});
}
if (!this.node.classList) {
Object.defineProperty(this, 'classList', {
get: (function () { return JHTML.getClassList(this.node) }).bind(this)
});
}
return this;
};
var JHTMLCollection = J.Class('JHTMLCollection').extend(JHTML);
JHTMLCollection.prototype.init = function (htmlNodeList) {
this.collection = htmlNodeList;
return this;
};
J.html = function (htmlNode) {
return new JHTML(htmlNode);
};
var getScrollX = function (n) {
var supportPageOffset = window.pageXOffset !== undefined;
var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat");
return supportPageOffset ? window.pageXOffset : isCSS1Compat ? document.documentElement.scrollLeft : document.body.scrollLeft;
};
var getScrollY = function (n) {
var supportPageOffset = window.pageXOffset !== undefined;
var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat");
return supportPageOffset ? window.pageYOffset : isCSS1Compat ? document.documentElement.scrollTop : document.body.scrollTop;
};
if (!window.hasOwnProperty('scrollX') || !window.hasOwnProperty('scrollY')) {
Object.defineProperty(window, 'scrollX', {
get: function () { return getScrollX() }
});
Object.defineProperty(window, 'scrollY', {
get: function () { return getScrollY() }
});
}
})(window.J || {});