forked from aykutaktas/autocms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods.js
215 lines (181 loc) · 5.92 KB
/
methods.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
autoCmsObject = function(){
function formatRowData() {
// fetch data from collection
var data = window[collection].find().fetch();
var keys = [];
for (var key in rules.columns) {
keys.push(key);
}
// object will be in rowData
var objAll = {};
// data will be in this array
var rowData = [];
// formatted value
var value;
// For each data in collection
for (i=0; i<data.length; i++) {
// field names
var name = keys.slice(0);
// tempData
var tempData = [];
var tempDataClass = [];
// first step
if (showNo) {
// Start with numbering each data
var no = i+1;
// add to begining
name.unshift('index');
// number is setted, push it!
tempData.push(no);
// add an empty class for first element
tempDataClass[tempData.length - 1] = '';
}
// for each columns setted for collection
keys.forEach(function (item, index) {
var prop = data[i][item];
// If prop isset, check for relation
if (prop) {
// If relation isset, find data from related collection
if (_.isFunction(rules.columns[item])) {
value = rules.columns[item](prop);
// If there isn't any relation with a collection show data
} else {
value = prop;
}
// If prop is undefined set value as ''
} else {
value = '';
}
// Change value by type, for now only images
if (!_.isUndefined(rules.columns[item].type) && rules.columns[item].type == 'image') {
if (value.length > 0) {
// Set default width
if (!_.isUndefined(rules.columns[item].width))
width = rules.columns[item].width;
value = '<img src="'+location.origin+'/cfs/files/images/'+value+'" width="'+width+'"/>';
}
}
// data is formated, push it!
tempData.push(value);
tempDataClass[tempData.length - 1] = setClassForFiels(rules.columns[item].class, value);
});
// end of foreach
// last step
if (showActionButtons) {
// add to end
name.push('buttons');
// create edit and delete buttons
tempData.push(buttonExtra(data[i]._id) +' '+ buttonEdit(data[i]._id) +' '+ buttonDelete(data[i]._id));
// add an empty class for last element
tempDataClass[tempData.length - 1] = '';
}
// set a complete object and create the result, push it!
objAll = {
data: tempData,
class: tempDataClass,
extra: buttonExtra(),
edit: buttonEdit(),
delete: buttonDelete()
}
rowData.push(objAll);
}
//console.log(objOne);
//console.log(rowData);
return rowData;
}
// Set defined class for data
function setClassForFiels(c, v) {
if (!_.isUndefined(c) && !isNaN(parseFloat(v)) && isFinite(v)) {
dataClass = c(v);
if (!_.isUndefined(dataClass))
return dataClass;
} else {
return '';
}
}
// Label of a button
function buttonLabel(button, text) {
if (!button)
return text;
return button;
}
// Edit button
function buttonEdit(data) {
if (_.isUndefined(rules.buttons))
return '<a href="/cms/'+collection+'/item/'+data+'" class="edit" target="_self">Edit</a>';
try{
// Set '' if not defined
if (!rules.buttons.edit.class)
rules.buttons.edit.class = '';
if (!rules.buttons.edit.label)
rules.buttons.edit.label = '';
if (rules.buttons.edit.auth()) {
return '<a href="/cms/'+collection+'/item/'+data+'" class="edit '+rules.buttons.edit.class+'" target="_self">'+ buttonLabel(rules.buttons.edit.label, 'Edit') +'</a>';
} else {
return '';
}
}
catch(e){
//console.log(e); //Log the error
}
return '<a href="/cms/'+collection+'/item/'+data+'" class="edit" target="_self">Edit</a>';
}
// Delete button
function buttonDelete(data) {
if (_.isUndefined(rules.buttons))
return '<a href="javascript:void(0);" class="remove" data-id="'+data+'">Delete</a>';
try{
// Set '' if not defined
if (!rules.buttons.delete.class)
rules.buttons.delete.class = '';
if (!rules.buttons.delete.label)
rules.buttons.delete.label = '';
if (rules.buttons.delete.auth()) {
return '<a href="javascript:void(0);" class="remove '+rules.buttons.delete.class+'" data-id="'+data+'">'+ buttonLabel(rules.buttons.delete.label, 'Delete') +'</a>';
} else {
return '';
}
}
catch(e){
//console.log(e); //Log the error
}
return '<a href="javascript:void(0);" class="remove" data-id="'+data+'">Delete</a>';
}
// Extra button
function buttonExtra(data) {
try {
if (_.isUndefined(rules.buttons.extra))
return '';
}
catch(e) {
return '';
}
try{
// Set '' if not defined
if (!rules.buttons.extra.class)
rules.buttons.extra.class = '';
if (!rules.buttons.extra.label)
rules.buttons.extra.label = '';
if (rules.buttons.extra.auth()) {
if (!_.isUndefined(rules.buttons.extra.href()))
link = rules.buttons.extra.href(data);
else
link = 'javascript:void(0);';
return '<a href="'+link+'" class="extra '+rules.buttons.extra.class+'" data-id="'+data+'">'+ buttonLabel(rules.buttons.extra.label, 'Extra') +'</a>';
} else {
return '';
}
}
catch(e){
//console.log(e); //Log the error
}
return '<a href="javascript:void(0);" class="extra" data-id="'+data+'">Extra</a>';
}
return{
formatRowData: formatRowData,
setClassForFiels: setClassForFiels,
buttonEdit: buttonEdit,
buttonDelete: buttonDelete,
buttonExtra: buttonExtra
}
}();