-
Notifications
You must be signed in to change notification settings - Fork 0
/
Route.js
executable file
·237 lines (160 loc) · 4.28 KB
/
Route.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
/*!
*
* jQuery Route
* Version: 1.0.1
* Original author: TN Devs
* Support && crowsource: @TodoNoticias
* Extend: jQuery Pattern
* Licensed under the MIT license
*
*/
/**
@module Route
**/
"use stricts";
if(!Function.prototype.appbind){
Function.prototype.appbind = function(){
var fn = this, args = Array.prototype.slice.call(arguments),
object = args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
};
};
}
/**
@class Route
@constructor
**/
function Route(datapage){
this.datapage = datapage;
}
/**
@method get
@return {String} data position
@param dataAttr {String} data-item
@param callback {Function} object
**/
Route.prototype.get = function(callback)
{
var elementSys = document.getElementsByTagName('body')[0];
var self = this;
return elementSys.getAttribute(this.datapage);
}
/**
@method actions
@return {Array}
@param data {String} data-item
@param callback {Function} object
**/
Route.prototype.action = function(data,callback)
{
var self = this;
return new Route().dataSort(data,callback);
}
/**
@method dataSort
@return {Object}
@param data {String} data-item
@param switched {Function} object
**/
Route.prototype.dataSort = function(data,switched)
{
return switched.call(this, data);
};
/**
@method when
@return {Object}
@param page {String} position page
@param parts {Json} options page
**/
Route.prototype.when = function(page,parts)
{
var self = this,
temporizr = false;
(new Route('data-page').get() == page && (typeof parts.onRoute == 'function')) ? parts.onRoute.apply(self,arguments) : false;
setTimeout(function(){
(new Route('data-page').get() == page && typeof parts.onComplete == 'function') ? parts.onComplete.apply(self,arguments) : false;
},300);
}
/**
@method ever
@return {Object}
@param parts {Json} options page
@param prefix {String}
@param not {String} no excutable
**/
Route.prototype.ever = function(parts,prefix,not)
{
if(not.indexOf(prefix) !== -1){
console.warn('Route.prototype.ever');
}else{
(typeof parts.onEver == 'function') ? parts.onEver.apply(self,arguments) : false;
}
}
/**
@method conditions
@return {Object}
@param conditional {String} position page
@param callback {Object}
**/
Route.prototype.condition = function(conditional,callback)
{
return (conditional == 'ever') ? callback.apply(self,arguments) : new Route().rule(this,conditional,callback);
}
/**
@method rules
@return {Object}
@param conditional {String} positions page
@param callback {Object}
**/
Route.prototype.rule = function(self,conditional,callback)
{
var dom = document.getElementsByTagName('body')[0].getAttribute(self.datapage);
if(dom.indexOf(conditional) !== -1 && (typeof conditional != 'object')){
//console.log('%cBasandose en la condicion de:', 'font-weight:bold', conditional);
callback.apply(self,arguments)
}else if(typeof conditional == 'object'){
//console.log('%cBasandose en la condicion de:', 'font-weight:bold', conditional);
for(var i in conditional){
(dom == conditional[i]) ? callback.apply(self,arguments) : false;
}
}
}
Route.prototype.getMethods = function(arr)
{
var res = [];
for(var m in arr){
/*if(typeof obj[m] == "function"){
res.push(m);
}*/
res.push(arr[m]);
}
return res;
}
/*Route.prototype.httpHost = function(){
var arr = [this['test'], this['local'], this['extralocal']];
if (!window.location.origin) window.location.origin = window.location.protocol+"//" + window.location.host;
for(var i in arr){
if(new RegExp(arr[i].split(" >> ")[0]).test(window.location.host) && new RegExp(arr[i].split(" >> ")[1]).test(window.location.pathname)){
var hostgen = window.location.origin + "/" + arr[i].split(" >> ")[1] + "/";
}
}
return hostgen;
}*/
Route.prototype.getPath = function(){
var str = location.pathname.split('/')[1];
return str;
}
Route.prototype._switch = function(options) {
var xsplitin = options.test.split("|");
for(var i in xsplitin){
if(options.path == xsplitin[i]){
return options.path;
break;
}
}
if(i == xsplitin.length - 1){
return options.nope;
}
};