forked from IshaiJaffe/formage-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminForm.js
More file actions
207 lines (171 loc) · 6.67 KB
/
AdminForm.js
File metadata and controls
207 lines (171 loc) · 6.67 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
'use strict';
if (!module.parent) console.error('Please don\'t call me directly.I am just the main app\'s minion.') || process.process.exit(1);
var forms = require('./forms')
, mongoose = require.main.require('mongoose')
, fields = forms.fields
, widgets = forms.widgets
, MongooseForm = forms.forms.MongooseForm
,_ = require('lodash')
, jest = require('jest');
var api_loaded = false;
var api_path;
//noinspection JSHint
var _escaper = /[-[\]{}()*+?.,\\^$|#\s]/g;
var AdminForm = exports.AdminForm = MongooseForm.extend({
init: function (request, options, model) {
this._super(request, options, model);
// no need for these, as they are already in formage-admin layout.jade
// this.static.js.push('/js/forms.js');
// this.static.js.push('/js/document.js');
// this.static.css.push('/css/forms.css');
},
scanFields: function (form_fields) {
var self = this;
Object.keys(form_fields).forEach(function (key) {
var value = form_fields[key];
if (value instanceof fields.RefField) {
if ((value.options.url || api_loaded) && value.options.query) {
value.options.widget_options.url = value.options.url || api_path;
value.options.widget_options.data = value.options.widget_options.data || {};
value.options.widget_options.data.data = encodeURIComponent(JSON.stringify({
model: value.options.ref,
query: value.options.query || '/__value__/i.test(this.name || this.title || this._id.toString())',
constraints:value.options.constraints
}));
value.widget = new widgets.AutocompleteWidget(value.options.widget_options);
}
}
else if (value instanceof fields.EnumField) {
value.widget = new widgets.ComboBoxWidget(value.options.widget_options);
}
else if (value instanceof fields.ListField) {
if(value.fields['__self__']){
var innerField = value.fields['__self__'];
if(innerField instanceof fields.RefField){
var options = _.extend({},
innerField.options,
value.options,{
required:value.required,
widget:null
});
form_fields[key] = new fields.MultiRefField(options,innerField.ref);
}
}
else
self.scanFields(value.fields);
}
});
},
get_fields: function () {
this._super();
this.scanFields(this.fields);
}
});
var _JestAdminResource = jest.Resource.extend({
init: function () {
this._super();
this.fields = {
id: null,
text: null
};
this.allowed_methods = ['get'];
this.filtering = {
data: null,
query: null,
id:null
};
},
get_objects: function (req, filters, sorts, limit, offset, callback) {
var data = JSON.parse(filters.data);
var model = mongoose.model(data.model);
if(filters.id){
model.findById(filters.id,function(err,doc){
var result = doc && {id:doc.id,text:doc.toString()};
callback(err,result);
});
}
else {
var qry;
var escaped_filters = filters.query.replace(_escaper, "\\$&") || '.';
var obj = data.constraints || {};
if(data.query.indexOf('__value__') > -1){
var query = data.query.replace(/__value__/g, escaped_filters);
obj['$where'] = query;
qry = model.find(obj);
}
else{
qry = model.find(obj).where(data.query,new RegExp('^' + escaped_filters));
}
qry.limit(20).exec(function (err, results) {
if (results) {
if (results.objects) {
results = results.objects;
}
results = results.map(function (object) { return { id: object.id, text: object.toString() }; });
}
callback(err, results);
});
}
}
});
exports.loadApi = function (app, path) {
var api = new jest.Api(path || 'admin_api', app);
api.register('ref', new _JestAdminResource());
api_path = '/' + api.path + 'ref';
api_loaded = true;
};
exports.AdminForm.getApiPath = function(){
return api_path;
}
var crypt = require('./models/mongoose_admin_user').crypt;
exports.AdminUserForm = AdminForm.extend({
init:function(request,options)
{
this._super(request,options,mongoose.model('_MongooseAdminUser'));
}
,get_fields:function(){
this._super();
var fields = this.fields;
delete fields['passwordHash'];
this.fields['current_password'] = new forms.fields.StringField({widget:forms.widgets.PasswordWidget,label:'Current Password'});
this.fields['password'] = new forms.fields.StringField({widget:forms.widgets.PasswordWidget,label:'New Password'});
this.fields['password_again'] = new forms.fields.StringField({widget:forms.widgets.PasswordWidget,label:'Again'});
this.fieldsets[0].fields = ['username','is_superuser','permissions','current_password','password','password_again'];
return fields;
},
is_valid:function(callback)
{
var self = this;
this._super(function(err,result)
{
if(err || !result)
callback(err,result);
else
{
if(self.data.password) {
if(!crypt.compareSync(self.data.current_password,self.instance.passwordHash))
self.errors['current_password'] = self.fields['current_password'].errors = ['Password incorrect'];
else
{
if(self.data.password != self.data.password_again)
{
self.errors['password_again'] = self.fields['password_again'].errors = ['typed incorrectly'];
}
}
}
else{
delete self.data.password;
delete self.data.current_password;
delete self.data.password;
}
callback(null,Object.keys(self.errors).length == 0);
}
});
},
actual_save:function(callback)
{
if(this.data.password)
this.instance.passwordHash = crypt.encryptSync(this.data.password);
this._super(callback);
}
});