-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.js
More file actions
181 lines (173 loc) · 4.53 KB
/
init.js
File metadata and controls
181 lines (173 loc) · 4.53 KB
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
/**
* @author Gilles Coomans <gilles.coomans@gmail.com>
*/
if (typeof define !== 'function') {
var define = require('amdefine')(module);
}
define(["require", "deepjs/deep"], function(require, deep) {
//var deep = require("deepjs/deep");
var utils = deep.utils;
//_____________________________________________________
var filtersObject = {
randomItem: function(input) {
if (input.push)
return input[Math.round(Math.random() * (input.length - 1))];
return input;
},
mongoIDtoStringID: function(input) {
//return "test";
return input;
},
inArray: function(input, array) {
return utils.inArray(input, array);
},
outArray: function(input, array) {
return !utils.inArray(input, array);
},
select: function(input, what) {
console.log("swig.filter.select : ", input, what);
return utils.fromPath(input, what);
},
notnull: function(input) {
if (input == null || input == "null")
return "";
return input;
},
comaArray: function(input) {
var res = "";
var first = true;
for (var i = 0; i < input.length; ++i) {
if (!first)
res += ", ";
else
first = false;
res += String(input[i]);
}
return res;
},
writeTaxo: function(input, map) {
//console.log("Swig.writeTaxo : "+JSON.stringify(input))
//console.log( " - with : "+JSON.stringify(map))
var res = "";
var first = true;
for (var i = 0; i < input.length; ++i) {
if (!map["m" + input[i]])
continue;
if (!first)
res += ", ";
else
first = false;
res += String(map["m" + input[i]].label.fr);
}
return res;
},
idToLabel: function(id, collection, language) {
//console.log("Swig.idToLabel : "+JSON.stringify(id))
//console.log( " - with : "+JSON.stringify(language))
for (var i = 0; i < collection.length; i++) {
var item = collection[i];
if (item.id == id) {
//console.log( "MATCHING : "+JSON.stringify(item.label[language]))
return item.label[language];
}
}
},
selectIfinArray: function(what, inArr) {
if (utils.inArray(what, inArr))
return "selected";
return "";
},
checkIfinArray: function(what, inArr) {
if (utils.inArray(what, inArr))
return "checked";
return "";
},
smartdate: function(input) {
//console.log("to date : "+input)
var inp = parseInt(input);
var d = new Date(inp * 1000);
return d.toLocaleDateString() + " - " + d.toLocaleTimeString();
},
query: function(input, query) {
if (input.path)
return JsonQuery.query(input, ((input.path != "/") ? (input.path + "/") : input.path) + query, null, null, {
keepCache: true
});
return JsonQuery.query(input, query, null, null, {
keepCache: true
})
},
removeFirstChar: function(input) {
return input.substring(1);
},
json: function(input) {
//console.log("swig json : ", input);
return JSON.stringify(input, null, ' ')
},
floor: function(input) {
return Math.floor(input);
},
ceil: function(input) {
return Math.ceil(input);
},
gridCollumnIndex: function(input, numCols) {
//console.log("gridCollumnIndex : ", input, " - ", numCols, " res : ", input%numCols)
return input % numCols;
},
isTypeOfObject: function(input) {
console.log("typeofobject : ", input)
if (typeof input === "object")
return true
else
return false
}
}
filtersObject.join_coma = filtersObject.comaArray;
/**
* swig related : produce swig-macro-import string
* @deprecated
* @category swig
* @method getMacroImport
* @static
* @param {ViewController} controller
* @param {Array} macrosSet
* @return {String} the macro import string
*/
deep.utils.getMacroImport = function(controller, macrosSet) {
var renderedTemplate = "";
if (controller.layer && controller.layer.templates) {
var macros = controller.layer.templates.macros;
for (var i in macros) {
if (!macros.hasOwnProperty(i) || (macrosSet && !i in macrosSet))
continue;
var m = macros[i];
var prefix = "";
var index = m.indexOf(":");
if (index > -1) {
prefix = m.substring(0, index);
m = m.substring(index + 2);
}
renderedTemplate += "{% import '" + m + "' as " + i + " %}\n";
}
}
return renderedTemplate;
}
function init(layer) {
var defaultObj = {
filters: filtersObject,
allowErrors: true,
autoescape: true,
encoding: 'utf8',
root: '/',
tags: {},
extensions: {},
tzOffset: 0
}
if (layer)
deep.aup(layer, defaultObj);
//swig.init(defaultObj);
for (var i in defaultObj.filters)
swig.setFilter(i, defaultObj.filters[i]);
}
return init;
});