-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeed.js
100 lines (85 loc) · 2.44 KB
/
Seed.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
'use strict';
/*
/ Creates a new Seed object
/ Parameters:
/ - elementName: The name of the HTML element you want to create (input, div, etc)
/ - attrs: The attributes of your element (style, value, etc)
*/
function Seed (elementName, attrs)
{
let self = this;
self.attrs = attrs || {};
self.ownProps = ["componentWillRender", "componentDidRender", "render", "attrs",
"buildComponent", "elementTagName", "addChild", "component", "ownProps"];
self.component = document.createElement(elementName);
}
/*
Add the attributes to the element
*/
Seed.prototype.filterFields = function() {
let self = this;
Object.keys(self.attrs)
.filter(function(key){return typeof(self.attrs[key]) != "object" &&
typeof(self.attrs[key]) != "array" &&
self.ownProps.indexOf(key) < 0;})
.forEach(function(item) { self[item] = self.attrs[item]; });
}
/*
Appends and render a child component
*/
Seed.prototype.appendChild = function(seedElement) { seedElement.renderOn(this.component); }
Seed.prototype.buildComponent = function(){
let self = this;
self.filterFields();
let keys = Object
.keys(self)
.map(function(item){
if(item.indexOf("element") > -1)
{
let newKey = item.replace("element","")
.split("_")
.join("-")
.toLowerCase();
self[newKey] = self[item];
delete self[item];
return newKey;
}
}).filter(function(key){return key != null});
let i = keys.length;
while(i--)
{
let key = keys[i];
let propType = typeof(self[key]);
let propName = key;
if(propType == "function")
{
let f = self[key];
self.component.addEventListener(propName, f);
}
else
{
self.component.setAttribute(propName, self[key]);
}
}
}
/*
/ Renders the component
/ Parameters:
/ - fatherElement: element where your component must be rendered
*/
Seed.prototype.renderOn = function(fatherElement)
{
let self = this;
let f = function(){};
self.buildComponent();
let willRender = typeof(self.componentWillRender) == "undefined"? f : self.componentWillRender;
let didRender = typeof(self.componentDidRender) == "undefined"? f : self.componentDidRender;
willRender();
if(fatherElement != null)
{
fatherElement.appendChild(self.component);
}
didRender();
return self.component;
};
Seed.prototype.constructor = Seed;