forked from tyt2y3/F.LF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
154 lines (141 loc) · 2.95 KB
/
util.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
/*\
* util.js
* utilities for F.LF
\*/
define(function(){
if (typeof console==='undefined')
{ //polyfill for IE, this is just for precaution
// we should not use console.log in production anyway unless for fatal error
console={};
console.log = function(){}
}
var util={};
util.select_from=function(from,where,option)
{
var res=[];
for( var i in from)
{
var O=from[i];
var match=true;
if( typeof where==='function')
{
if( !where(O))
match=false;
}
else
for( var j in where)
{
if( O[j]!==where[j])
match=false;
}
if( match)
res.push(O);
}
if( res.length===0)
return ;
else if( res.length===1)
return res[0];
else
return res;
}
util.lookup=function(A,x)
{
for( var i in A)
{
if( x<=i)
return A[i];
}
}
util.lookup_abs=function(A,x)
{
if( x<0) x=-x;
for( var i in A)
{
if( x<=i)
return A[i];
}
}
util.shallow_copy=function(A)
{
var B={};
for( var i in A)
B[i] = A[i];
return B;
}
util.div=function(classname)
{
if( !util.container)
{
util.container = document.getElementsByClassName('LFcontainer')[0];
if( !util.container) return null;
}
if( !classname) return util.container;
return util.container.getElementsByClassName(classname)[0];
}
util.filename=function(file)
{
if( file.lastIndexOf('/')!==-1)
file = file.slice(file.lastIndexOf('/')+1);
if( file.lastIndexOf('.js')!==-1)
file = file.slice(0,file.lastIndexOf('.js'));
return file;
}
/**
The resourcemap specified by F.core allows putting a js function as a condition checker.
This is considered insecure in F.LF. thus F.LF only allows simple predefined condition checking.
*/
util.setup_resourcemap=function(package,Fsprite)
{
var has_resmap=false;
if( package.resourcemap)
if( typeof package.resourcemap.condition==='string')
{
var cond = package.resourcemap.condition.split(' ');
if( cond[0]==='location' && cond[1]==='contain' &&
cond[2] && cond[3]==='at' && cond[4])
{
cond[4]=parseInt(cond[4]);
package.resourcemap.condition = function()
{
return window.location.href.indexOf(cond[2])===cond[4];
}
}
else if( cond[0]==='location' && cond[1]==='contain' && cond[2])
{
package.resourcemap.condition = function()
{
return window.location.href.indexOf(cond[2])!==-1;
}
}
if( typeof package.resourcemap.condition==='function')
{
var resmap = [
package.resourcemap, //package-defined resourcemap
{ //default resourcemap
get: function(res)
{
return package.location+res;
}
}
];
Fsprite.masterconfig_set('resourcemap',resmap);
has_resmap=true;
}
}
if( !has_resmap)
Fsprite.masterconfig_set('baseUrl',package.location);
}
//return the parameters passed by location
util.location_parameters=function()
{
var param = window.location.href.split('/').pop();
if( param.indexOf('?')!==-1)
{
var param = param.split('?').pop().split('&');
for( var i=0; i<param.length; i++)
param[i] = param[i].split('=');
return param;
}
}
return util;
});