-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvony.js
258 lines (225 loc) · 6.64 KB
/
vony.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
function ready(callback){
window.addEventListener("DOMContentLoaded", function(){
callback();
});
}
function log(message) {
console.warn('[Vony-JS] ' + message)
}
function reload(u){
var u = u ? u :'';
window.location.href=u;
}
function randomString(l) {
l = (l === undefined) ? l = 10 : l;
var r = '',
c = 'a1b2c3d4e5f6g7h8i9j0kAlBmCnDoEpFqGrHsItJuKvLwMxNyOzPQRSTUVWXYZ';
for (var i = 0; i < l; i++) {
r += c.charAt(Math.floor(Math.random() * c.length))
}
return r
}
function isEmail(e) {
var em = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return (e === undefined) ? !1 : em.test(e)
}
function isUrl(s) {
var x = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
return x.test(s)
}
function isFunction(f) {
var t = {};
return f && t.toString.call(f) === '[object Function]'
}
function whatThis(o) {
return isUndefined(o) ? undefined : typeof o
}
function isUndefined(o) {
return (o === undefined) ? !0 : !1
}
function moneyFormat(v, r) {
r = r === undefined ? 'Rp ' : r;
return r + v.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
}
function validateInput(){
for (var i = 0; i < arguments.length; ++i) {
if (arguments[i].get().trim() === '' || arguments[i].get().trim() == null) {
arguments[i].focus();
return false;
break;
}
}
return true;
}
class _vn_ {
constructor(data) {
if (data) {
this.id = data.id ? data.id : null;
this.url =data.url ? data.url :null;
this.method =data.method ? data.method :null;
this.data =data.data ? data.data :null;
this.obj = null;
this.title = data.title ? data.title : null;
if (this.title!=null){
this.makeTitle(this.title);
}
this.urlRoute = data.urlRoute ? data.urlRoute :null;
this.element = null;
this.documentGetElementById();
if (data.focus && data.focus == true) {
if (this.element === 'INPUT' || this.element === 'TEXTAREA') {
this.obj.focus();
}
}
}
return this;
}
focus() {
if (this.obj) {
if (this.element === 'INPUT' || this.element === 'TEXTAREA') {
this.obj.focus()
}
}
}
documentGetElementById() {
this.obj = document.getElementById(this.id);
if (this.obj) {
this.element = this.obj.tagName;
log('ID of element is ' + this.id + ' , component is ' + this.element)
}
}
hide() {
if (this.obj) {
this.obj.style.visibility = 'hidden'
}
return this;
}
show() {
if (this.obj) {
this.obj.removeAttribute('style');
}
return this;
}
get(attribute) {
if (this.obj) {
if (attribute){
return this.obj.getAttribute(attribute);
}
if (this.element === 'INPUT' || this.element === 'TEXTAREA') {
return this.obj.value;
} else if (this.element === 'IMG') {
return this.obj.src;
} else if (this.element === 'SELECT') {
return this.obj.value;
} else {
return this.obj.innerHTML;
}
}
return this;
}
clear(){
this.set('');
return this;
}
set(text) {
if (this.obj) {
if (this.element === 'INPUT' || this.element === 'TEXTAREA') {
this.obj.value = text;
} else if (this.element === 'IMG') {
this.obj.src = text;
} else if (this.element === 'SELECT') {
this.obj.selectedIndex = text
} else {
this.obj.innerHTML = text;
}
}
return this;
}
on(type, callback) {
if (this.obj) {
var $this = this;
this.obj.addEventListener(type, function () {
callback($this);
})
}
return this
}
ajax(callback){
var _data_=null;
if (this.method==null){
this.method='get';
}
var _METHOD_ = this.method.toLowerCase();
if (this.url==null){
log("URL of ajax is Null, Ajax Stop");
return;
}
var _URL_ = this.url;
if (_METHOD_==='post'){
var i = 0;
for (var key in this.data) {
if (key === 'length' || !this.data.hasOwnProperty(key)) {
continue
}
var vL = this.data[key];
(i == 0) ? _data_ = key + '=' + vL: _data_ += '&' + key + '=' + vL;
i++
}
}
log("Method of ajax is "+_METHOD_);
log("URL of ajax is "+_URL_);
var x = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttp');
x.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
callback(this.responseText);
}
};
x.onerror = function () {
};
x.open(_METHOD_, _URL_, !0);
var header = 'application/x-www-form-urlencoded';
x.setRequestHeader('Content-Type', header);
try {
x.send(_data_)
} catch (e) {
log('Error ajax - '+e);
}
return this
}
change(key,val){
if (this.obj){
this.obj.setAttribute(key,val);
}
}
enabled(v){
if (this.obj){
this.obj.enabled =true;
}
return this
}
disabled(){
if (this.obj){
this.obj.disabled =true;
}
return this
}
makeTitle(value){
document.title = value;
}
route(callback=null,title=null,urlRoute=null){
let finalTitle,finalUrlPath;
finalTitle = (title!=null) ? title : this.title;
finalUrlPath = (urlRoute!=null) ? urlRoute : this.urlRoute;
finalUrlPath = finalUrlPath.toLowerCase();
window.history.pushState({finalUrlPath},finalTitle,finalUrlPath);
log("URL changed -> "+finalUrlPath);
callback==null ? null : callback();
window.onpopstate = (event) => {
log(`location: ${document.location}, state: ${JSON.stringify(event.state)}`)
}
return this
}
}
function Vony(d) {
return new _vn_(d);
}