-
Notifications
You must be signed in to change notification settings - Fork 0
/
j-make.js
64 lines (64 loc) · 2.17 KB
/
j-make.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
(function() {
jMake = {};
jMake.functions = {};
jMake.functions.make = function(p){
$.get(p, function(data, status){
$(data).prependTo('*[data-path="' + p + '"]');
});
};
jMake.functions.nodeTreeToArray = function(e){
jMake.nodeTreeArray = Array();
while(e=e.parentNode){
if(!e.dataset.key){
break;
} else {
jMake.nodeTreeArray.push(e.dataset.key);
}
}
return jMake.nodeTreeArray;
};
jMake.functions.matchNode = function(e){
jMake.nodeIndex= 0;
while((e=e.previousElementSibling)!=null) ++jMake.nodeIndex;
return jMake.nodeIndex;
};
jMake.functions.strings = function(v,e){
jMake.element = document.createElement(v);
e.appendChild(jMake.element);
jMake.nodeTreeToArray = jMake.functions.nodeTreeToArray(jMake.element).reverse();
jMake.matchNode = jMake.functions.matchNode(jMake.element);
jMake.key = v + '_' + jMake.matchNode;
jMake.element.dataset.key = jMake.key;
jMake.element.className = 'j-make';
if(jMake.nodeTreeToArray.length > 0){
jMake.body = 'body/';
} else {
jMake.body = 'body';
}
jMake.element.dataset.path = jMake.body + jMake.nodeTreeToArray.join('/') + '/' + jMake.key;
jMake.functions.make(jMake.element.dataset.path);
};
jMake.functions.arrays = function(v){
if(jQuery.type(v) == 'array'){
jMake.functions.j(v,jMake.element);
} else {
alert('Invalid value: ' + jType + ' type');
}
};
jMake.functions.j = function(d,e){
$(d).each( function(key,val){
if(jQuery.type(val) == 'string'){
jMake.functions.strings(val,e);
} else {
jMake.functions.arrays(val);
}
});
};
document.addEventListener('readystatechange',function(){
if (document.readyState == "complete") {
$.getJSON("body.json", function(data) {
jMake.functions.j(data,document.body);
});
}
});
})();