forked from aykutaktas/autocms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
268 lines (253 loc) · 6.5 KB
/
helpers.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
259
260
261
262
263
264
265
266
267
268
Template.autoCms.onCreated(function(){
// global variables & configs for autoCms
collection = FlowRouter.getParam("collection");
func = FlowRouter.getParam("function");
id = FlowRouter.getParam("id");
if (!_.isUndefined(rules = window[collection].autoCms))
rules = window[collection].autoCms;
else
rules = window[collection].autoOne;
// Set default options for Navigation Buttons
showNavButtons = true;
// navButtonInsert
try {
navButtonInsertClass = rules.buttons.navButtonInsert.class;
} catch(e) {
navButtonInsertClass = '';
}
try {
navButtonInsertLabel = rules.buttons.navButtonInsert.label;
} catch(e) {
navButtonInsertLabel = 'Insert';
}
// navButtonList
try {
navButtonListClass = rules.buttons.navButtonList.class;
} catch(e) {
navButtonListClass = '';
}
try {
navButtonListLabel = rules.buttons.navButtonList.label;
} catch(e) {
navButtonListLabel = 'List';
}
// dataType
dataType = 'table'
if (!_.isUndefined(dataType))
dataType = rules.type;
// Set default options for Action buttons which are in the last td of every tr
showActionButtons = true;
if (!_.isUndefined(rules.buttons)) {
if (!_.isUndefined(showActionButtons)) {
showActionButtons = rules.buttons.showActionButtons;
if (typeof rules.buttons.edit.auth() != undefined && typeof rules.buttons.delete.auth() != undefined) {
if (rules.buttons.edit.auth() == true && rules.buttons.delete.auth() == true)
showActionButtons = true;
else if (rules.buttons.edit.auth() == false && rules.buttons.delete.auth() == false)
showActionButtons = false;
}
}
if (!_.isUndefined(rules.buttons.showNavButtons))
showNavButtons = rules.buttons.showNavButtons;
}
// showNo
showNo = true;
if (!_.isUndefined(rules.showNo))
showNo = rules.showNo;
});
/**
Defines how autoCms & autoForm will work and returns data
into template
*/
Template.autoCms.helpers({
// is autoCmsForm will be in use for insert/update, if not return false to list collection
'autoCmsForm': function() {
switch(func) {
case 'list':
return false;
break;
case 'item':
return true;
break;
case 'update':
return true;
break;
case 'insert':
return true;
break;
default:
return false;
}
},
'autoCmsNavButtons': function() {
return showNavButtons;
},
'autoCmsNavButtonInsert': function () {
return '<a class="'+navButtonInsertClass+'" href="'+location.origin+'/cms/'+collection+'/insert" target="_self">'+navButtonInsertLabel+'</a>';
},
'autoCmsNavButtonList': function () {
return '<a class="'+navButtonListClass+'" href="'+location.origin+'/cms/'+collection+'/list" target="_self">'+navButtonListLabel+'</a>';
},
'autoCmsActionButtons': function() {
return showActionButtons;
},
'autoCmsNo': function() {
return showNo;
},
// main collection which will be handled in autoCms
'collection': function() {
return collection;
},
// fetch data and serve it as wanted
'data': function() {
switch(func) {
case 'list':
return true;
break;
case 'item':
return window[collection].findOne(id);
break;
case 'update':
return window[collection].findOne(id);
break;
case 'insert':
return true;
break;
default:
return false;
}
},
// return formId for autoForm
'formId': function () {
switch(func) {
case 'list':
return true
break;
case 'item':
return 'edit'+FlowRouter.getParam("collection")+'Form';
break;
case 'update':
return 'edit'+FlowRouter.getParam("collection")+'Form';
break;
case 'insert':
return 'insert'+FlowRouter.getParam("collection")+'Form';
break;
default:
return false;
}
},
// return formType for autoForm
'formType': function() {
switch(func) {
case 'list':
return true
break;
case 'item':
return 'update';
break;
case 'update':
return 'update';
break;
case 'insert':
return 'insert';
break;
default:
return false;
}
},
// return the text of button for autoForm
'formButton': function() {
switch(func) {
case 'list':
return true
break;
case 'item':
return 'Update';
break;
case 'update':
return 'Update';
break;
case 'insert':
return 'Submit';
break;
default:
return false;
}
},
// wrapper type defines, how to display data set
'wrapperType': function () {
if (!_.isUndefined(rules.wrapper.type))
contentType = rules.wrapper.type;
if (contentType == 'table')
return true;
else
return false;
},
// wrapper element for data
'wrapperClass': function () {
try{
return rules.wrapper.class;
} catch(e) {
return '';
}
},
// title which will be displayed in the body of the page
'title': function () {
return rules.title;
},
// return only column names from collection.table rule
'tableHead': function () {
// gather column names and push them into keys
var keys = [];
for (var key in rules.columns) {
keys.push(key);
}
var result = [];
// foreach keys format key and push them into result
keys.forEach(function (item, index) {
result.push(s(item).capitalize().value());
});
// push other fields, which is defined in the rule into result
if (showNo) {
result.unshift('No');
}
if (showActionButtons) {
result.push('Actions');
}
// return result
return result;
},
// allClass
'allClass': function (c, index) {
if (!_.isUndefined(c)) {
var res = c.toString().split(",");
return res[index];
} else {
return '';
}
},
'allData': function() {
return autoCmsObject.formatRowData();
},
// condition for remove button in autoform
'removeButton' : function () {
switch(func) {
case 'item':
if (typeof rules.buttons.delete.auth() != undefined)
if (rules.buttons.delete.auth() == true)
return true;
break;
default:
return false;
}
},
// ask again before remove action in autoForm
'beforeRemove' : function () {
return function () {
var doc = window[collection].findOne(id);
if (confirm('Really delete: "'+ doc.title +'"?')) {
this.remove();
}
};
}
});