-
Notifications
You must be signed in to change notification settings - Fork 0
/
phacil.js
213 lines (148 loc) · 4.03 KB
/
phacil.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
class Phacil {
constructor(){
this.error = [];
this.rota = "";
this.registry = new Registry();
console.log(this.error);
this.linkBehavior();
}
get(key) {
return this.registry.get(key);
}
set(key, value) {
this.registry.set(key, value);
}
linkBehavior() {
$('a[intern]').click(function (e) {
e.preventDefault();
});
}
route(rota = 'common/home') {
let parts = rota.split("/");
let nameClass;
this.rota = rota;
let file = 'controller/'+parts[0]+'/'+parts[1]+'.js';
$('head').append('<script type="text/javascript" class=".ccc" src="'+ file +'"></script>');
let folder = parts[0].toLowerCase().replace(/\b[a-z]/g, function(letter) {
return letter.toUpperCase();
});
let archive = parts[1].toLowerCase().replace(/\b[a-z]/g, function(letter) {
return letter.toUpperCase();
});
if(parts.length == 2) {
nameClass = "Controller"+folder+archive;
var myObject = eval("new " + nameClass + "()");
myObject.index();
} else if(parts.length == 3) {
nameClass = "Controller"+folder+archive;
var myObject = eval("new " + nameClass + "()");
var myClass = eval("myObject." + parts[2] + "()");
//myClass;
}
}
}
class Controller {
constructor(registry = null){
this.registry = (registry != null) ? registry : phacil.registry;
this.data = {};
Object.assign(this, this.registry.retorno());
this.replace();
}
get(key) {
return this.registry.get(key);
}
set(key, value) {
this.registry.set(key, value);
}
redirect(url) {
window.location.replace(url);
}
replace(){
$('replace').each(function (index, value) {
console.log(value);
let elemento = $(value);
let route = elemento.attr('route');
elemento.replaceWith('<h4>hdhd</h4>');
})
}
render(){
}
loadMustache(templatePath) {
let template;
let dados = this.data;
let templateURL = 'view/default/common/home.mustache';
let rendered;
$.ajax({
url: templateURL,
method: "get",
async: false,
success: function (data) {
template = data;
Mustache.parse(template); // optional, speeds up future uses
rendered = Mustache.render(template, dados);
},
error: function () {
console.log('Error: impossible to load '+templatePath);
}
});
return (rendered);
}
out(){
this.loadMustache();
this.replace();
}
}
class Registry {
constructor(){
this.data = [];
}
get(key) {
//console.log(this.data[key]);
return this.data[key] ;
}
set(key, value) {
this.data[key] = value;
}
has(key) {
return (this.data[key]);
}
retorno (){
return this.data;
}
}
class DB {
teste(){
console.log("cargaDB");
return true;
}
}
class config {
constructor(json = null){
let jsonData = (json != null) ? json : null;
if(json == null){
$.ajax({
url: 'config.json',
method: "get",
async: false,
dataType: 'json',
success: function (json) {
jsonData = json;
},
error: function () {
console.log('Error: impossible to load config file');
}
});
}
console.log(jsonData);
this.data = jsonData;
}
get(key){
return this.data[key];
}
set(key, value) {
this.data[key] = value;
}
has(key) {
return (this.data[key].length > 0) ? true : false;
}
}