alchemy-chimera 0.5.3 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/assets/scripts/chimera/chimera.js +13 -288
  3. package/assets/stylesheets/chimera/chimera.scss +273 -0
  4. package/bootstrap.js +6 -302
  5. package/config/routes.js +53 -5
  6. package/controller/00-chimera_controller.js +16 -0
  7. package/controller/chimera_editor_controller.js +161 -615
  8. package/controller/chimera_static_controller.js +102 -9
  9. package/lib/chimera_config.js +107 -163
  10. package/model/model.js +1 -1
  11. package/package.json +8 -12
  12. package/view/chimera/dashboard.hwk +8 -0
  13. package/view/chimera/editor/index.hwk +20 -0
  14. package/view/chimera/sidebar.hwk +4 -0
  15. package/view/chimera/widgets.hwk +8 -0
  16. package/view/layouts/chimera_base.hwk +15 -0
  17. package/view/layouts/chimera_basics.hwk +15 -0
  18. package/view/layouts/chimera_body.hwk +24 -0
  19. package/assets/scripts/chimera/assoc_field.js +0 -311
  20. package/assets/scripts/chimera/boolean_field.js +0 -53
  21. package/assets/scripts/chimera/chimera_field.js +0 -617
  22. package/assets/scripts/chimera/chimera_field_wrapper.js +0 -557
  23. package/assets/scripts/chimera/date_field.js +0 -170
  24. package/assets/scripts/chimera/geopoint_field.js +0 -147
  25. package/assets/scripts/chimera/medium-button.js +0 -6
  26. package/assets/scripts/chimera/object_field.js +0 -51
  27. package/assets/scripts/chimera/page-editor.js +0 -3
  28. package/assets/scripts/chimera/password_field.js +0 -54
  29. package/assets/scripts/chimera/schema_field.js +0 -300
  30. package/assets/scripts/chimera/text_field.js +0 -119
  31. package/assets/scripts/rome/rome.js +0 -4978
  32. package/assets/scripts/vex/vex.dialog.js +0 -151
  33. package/assets/scripts/vex/vex.js +0 -193
  34. package/assets/stylesheets/chimera/chimera.less +0 -1075
  35. package/assets/stylesheets/chimera_old/chimera.less +0 -351
  36. package/assets/stylesheets/chimera_old/core.less +0 -315
  37. package/assets/stylesheets/chimera_old/font.less +0 -38
  38. package/assets/stylesheets/chimera_old/mixins.less +0 -35
  39. package/assets/stylesheets/chimera_old/page-editor.less +0 -23
  40. package/assets/stylesheets/rome/rome.less +0 -130
  41. package/assets/stylesheets/vex/vex-theme-bottom-right-corner.less +0 -613
  42. package/assets/stylesheets/vex/vex.default.less +0 -501
  43. package/assets/stylesheets/vex/vex.flat.less +0 -443
  44. package/assets/stylesheets/vex/vex.less +0 -320
  45. package/controller/chimera_page_controller.js +0 -14
  46. package/controller/chimera_settings_controller.js +0 -129
  47. package/controller/tasks_chimera_controller.js +0 -106
  48. package/helper/chimera_helper.js +0 -202
  49. package/lib/chimera_action_fields.js +0 -292
  50. package/lib/chimera_field/belongsto_chimera_field.js +0 -155
  51. package/lib/chimera_field/boolean_chimera_field.js +0 -19
  52. package/lib/chimera_field/date_chimera_field.js +0 -20
  53. package/lib/chimera_field/datetime_chimera_field.js +0 -20
  54. package/lib/chimera_field/enum_chimera_field.js +0 -131
  55. package/lib/chimera_field/geopoint_chimera_field.js +0 -20
  56. package/lib/chimera_field/habtm_chimera_field.js +0 -55
  57. package/lib/chimera_field/hasoneparent_chimera_field.js +0 -18
  58. package/lib/chimera_field/html_chimera_field.js +0 -20
  59. package/lib/chimera_field/object_chimera_field.js +0 -18
  60. package/lib/chimera_field/password_chimera_field.js +0 -18
  61. package/lib/chimera_field/schema_chimera_field.js +0 -149
  62. package/lib/chimera_field/text_chimera_field.js +0 -20
  63. package/lib/chimera_field/time_chimera_field.js +0 -18
  64. package/lib/chimera_field.js +0 -208
  65. package/model/setting_model.js +0 -51
  66. package/public/chimera/codedor.svg +0 -28
  67. package/view/chimera/dashboard.ejs +0 -5
  68. package/view/chimera/editor/add.ejs +0 -32
  69. package/view/chimera/editor/edit.ejs +0 -33
  70. package/view/chimera/editor/index.ejs +0 -87
  71. package/view/chimera/editor/remove.ejs +0 -23
  72. package/view/chimera/editor/view.ejs +0 -54
  73. package/view/chimera/elements/editor_actions.ejs +0 -4
  74. package/view/chimera/elements/empty +0 -0
  75. package/view/chimera/field_wrappers/_entries.ejs +0 -91
  76. package/view/chimera/field_wrappers/_wrapper.ejs +0 -78
  77. package/view/chimera/field_wrappers/date_edit.ejs +0 -23
  78. package/view/chimera/field_wrappers/date_list.ejs +0 -3
  79. package/view/chimera/field_wrappers/date_view.ejs +0 -16
  80. package/view/chimera/field_wrappers/default_edit.ejs +0 -22
  81. package/view/chimera/field_wrappers/default_list.ejs +0 -3
  82. package/view/chimera/field_wrappers/default_peek.ejs +0 -3
  83. package/view/chimera/field_wrappers/default_view.ejs +0 -14
  84. package/view/chimera/field_wrappers/geopoint_edit.ejs +0 -25
  85. package/view/chimera/field_wrappers/geopoint_list.ejs +0 -5
  86. package/view/chimera/field_wrappers/geopoint_view.ejs +0 -18
  87. package/view/chimera/field_wrappers/schema_edit.ejs +0 -19
  88. package/view/chimera/field_wrappers/text_edit.ejs +0 -22
  89. package/view/chimera/field_wrappers/text_list.ejs +0 -1
  90. package/view/chimera/fields/belongsto_edit.ejs +0 -4
  91. package/view/chimera/fields/belongsto_list.ejs +0 -1
  92. package/view/chimera/fields/belongsto_peek.ejs +0 -1
  93. package/view/chimera/fields/boolean_edit.ejs +0 -17
  94. package/view/chimera/fields/boolean_list.ejs +0 -3
  95. package/view/chimera/fields/boolean_view.ejs +0 -3
  96. package/view/chimera/fields/date_edit.ejs +0 -14
  97. package/view/chimera/fields/date_list.ejs +0 -9
  98. package/view/chimera/fields/date_peek.ejs +0 -9
  99. package/view/chimera/fields/date_view.ejs +0 -9
  100. package/view/chimera/fields/datetime_edit.ejs +0 -24
  101. package/view/chimera/fields/datetime_list.ejs +0 -9
  102. package/view/chimera/fields/datetime_view.ejs +0 -10
  103. package/view/chimera/fields/default_edit.ejs +0 -10
  104. package/view/chimera/fields/default_list.ejs +0 -15
  105. package/view/chimera/fields/default_view.ejs +0 -1
  106. package/view/chimera/fields/enum_edit.ejs +0 -10
  107. package/view/chimera/fields/geopoint_edit.ejs +0 -10
  108. package/view/chimera/fields/geopoint_list.ejs +0 -9
  109. package/view/chimera/fields/geopoint_view.ejs +0 -9
  110. package/view/chimera/fields/habtm_edit.ejs +0 -5
  111. package/view/chimera/fields/hasoneparent_edit.ejs +0 -6
  112. package/view/chimera/fields/hasoneparent_list.ejs +0 -1
  113. package/view/chimera/fields/object_edit.ejs +0 -8
  114. package/view/chimera/fields/password_edit.ejs +0 -11
  115. package/view/chimera/fields/schema_edit.ejs +0 -14
  116. package/view/chimera/fields/text_edit.ejs +0 -5
  117. package/view/chimera/fields/text_list.ejs +0 -9
  118. package/view/chimera/fields/text_view.ejs +0 -3
  119. package/view/chimera/fields/time_edit.ejs +0 -15
  120. package/view/chimera/fields/time_list.ejs +0 -9
  121. package/view/chimera/fields/time_view.ejs +0 -9
  122. package/view/chimera/page/edit.ejs +0 -18
  123. package/view/chimera/page_editor.ejs +0 -5
  124. package/view/chimera/sidebar.ejs +0 -40
  125. package/view/chimera/tasks/index.ejs +0 -114
  126. package/view/layouts/chimera.ejs +0 -12
  127. package/view/layouts/chimera_base.ejs +0 -11
  128. package/view/layouts/chimera_body.ejs +0 -43
@@ -1,714 +1,260 @@
1
1
  /**
2
2
  * The Chimera Editor Controller class
3
3
  *
4
- * @author Jelle De Loecker <jelle@kipdola.be>
4
+ * @author Jelle De Loecker <jelle@elevenways.be>
5
5
  * @since 0.2.0
6
- * @version 0.5.1
6
+ * @version 1.0.0
7
7
  */
8
- var Editor = Function.inherits('Alchemy.Controller.Chimera', function Editor(conduit, options) {
9
-
10
- Editor.super.call(this, conduit, options);
11
-
12
- this.addComponent('paginate');
13
-
14
- this.addAction('model', 'index', {title: 'Index', icon: '<x-svg data-src="chimera/list"></x-svg>'});
15
- this.addAction('model', 'add', {title: 'Add', icon: '<x-svg data-src="chimera/plus"></x-svg>'});
16
-
17
- this.addAction('model-list', 'add', {title: 'Add', icon: '<x-svg data-src="chimera/plus"></x-svg>', route_name: 'ModelAction'});
18
-
19
- this.addAction('draft', 'save', {title: 'Save', icon: '<x-svg data-src="chimera/floppy"></x-svg>', handleManual: true});
20
-
21
- this.addAction('record', 'edit', {title: 'Edit', icon: '<x-svg data-src="chimera/edit"></x-svg>'});
22
- this.addAction('record', 'view', {title: 'View', icon: '<x-svg data-src="chimera/eye"></x-svg>'});
23
- this.addAction('record', 'remove', {title: 'Remove', icon: '<x-svg data-src="chimera/garbage"></x-svg>'});
24
-
25
- this.addAction('record-edit', 'save', {title: 'Save', icon: '<x-svg data-src="chimera/floppy"></x-svg>', handleManual: true, route_name: 'RecordAction'});
26
- this.addAction('record-edit', 'saveClose', {title: 'Save and close', icon: '<x-svg data-src="chimera/floppy"></x-svg>', handleManual: true, route_name: 'RecordAction'});
27
-
28
- this.addAction('record-draft', 'save', {title: 'Save', icon: '<x-svg data-src="chimera/floppy"></x-svg>', handleManual: true, route_name: 'RecordAction'});
29
- this.addAction('record-draft', 'saveClose', {title: 'Save and close', icon: '<x-svg data-src="chimera/floppy"></x-svg>', handleManual: true, route_name: 'RecordAction'});
30
- });
8
+ const Editor = Function.inherits('Alchemy.Controller.Chimera', 'Editor');
31
9
 
32
10
  /**
33
- * The index action
11
+ * Set the title
34
12
  *
35
- * @author Jelle De Loecker <jelle@develry.be>
36
- * @since 0.1.0
37
- * @version 0.1.0
13
+ * @author Jelle De Loecker <jelle@elevenways.be>
14
+ * @since 1.0.1
15
+ * @version 1.0.1
38
16
  *
39
- * @param {Conduit} conduit
17
+ * @param {String} title
40
18
  */
41
- Editor.setAction(function index(conduit) {
42
- return this.doAction('listing', [conduit, 'list', 'index']);
43
- });
44
-
45
- /**
46
- * The generic listing method
47
- *
48
- * @author Jelle De Loecker <jelle@develry.be>
49
- * @since 0.1.0
50
- * @version 0.5.1
51
- *
52
- * @param {Conduit} conduit
53
- * @param {String} type
54
- * @param {String} view
55
- */
56
- Editor.setAction(function listing(conduit, type, view) {
57
-
58
- var that = this,
59
- model_plural,
60
- model_title,
61
- model_name,
62
- chimera,
63
- model;
64
-
65
- // Get the name of the model
66
- model_name = conduit.routeParam('subject');
67
-
68
- if (!model_name) {
69
- return conduit.error(new Error('No model name was given, nothing to list'));
70
- }
71
-
72
- // Get the actual model
73
- model = this.getModel(model_name);
74
-
75
- // And get the chimera instance
76
- chimera = model.constructor.chimera;
77
-
78
- if (chimera == null) {
79
- return conduit.error(new Error('Model "' + model_name + '" has no chimera configuration'));
80
- }
81
-
82
- // Get the model title
83
- model_title = model_name.titleize();
19
+ Editor.setMethod(function setTitle(title) {
84
20
 
85
- // And the plural form
86
- model_plural = model_title.pluralize();
21
+ let window_title,
22
+ page_title;
87
23
 
88
- // Disable translation behaviour of the model
89
- model.disableTranslations();
24
+ if (alchemy.plugins.chimera.title) {
25
+ window_title = alchemy.plugins.chimera.title || '';
90
26
 
91
- // Set the page title
92
- this.setTitle(model_plural);
93
-
94
- if (view == null) {
95
- view = type;
96
- }
97
-
98
- let action_fields = chimera.getActionFields(type),
99
- general = action_fields.getGroup('general'),
100
- sorted = general.getSorted(false),
101
- fields = [],
102
- i;
103
-
104
- for (i = 0; i < sorted.length; i++) {
105
- fields.push(sorted[i].path);
27
+ if (title && window_title) {
28
+ window_title += ' | ';
29
+ }
106
30
  }
107
31
 
108
-
109
- let options = {
110
- fields : fields,
111
- pageSize : 10
112
- };
113
-
114
- // Hacky way of stopping pagination
115
- if (action_fields.options.paginate === false) {
116
- options.pageSize = 999;
32
+ if (title) {
33
+ window_title += title;
117
34
  }
118
35
 
119
- // Find the paginated records
120
- that.components.paginate.find(model, options, function gotRecords(err, items) {
121
-
122
- if (err) {
123
- return conduit.error(err);
124
- }
36
+ page_title = title;
125
37
 
126
- that.conduit.set('available_records', items.available || 0);
127
-
128
- action_fields.processRecords('general', model, items, function groupedRecords(err, results) {
129
-
130
- if (err) {
131
- return conduit.error(err);
132
- }
133
-
134
- that.set('prefixes', Prefix.getPrefixList());
135
- that.set('fields', general);
136
- that.set('records', results.general);
137
- that.set('actions', that.getActions());
138
- that.set('show_index_filters', model.chimera.show_index_filters);
139
-
140
- that.set('model_title', model_title);
141
- that.set('model_name', model_name);
142
- that.internal('model_name', model_name);
143
-
144
- // Deprecated modelName
145
- that.set('modelName', model_name);
146
- that.internal('modelName', model_name);
147
-
148
- that.render('chimera/editor/' + view);
149
- });
150
- });
38
+ this.set('page_title', page_title);
39
+ this.set('window_title', window_title || page_title);
151
40
  });
152
41
 
153
42
  /**
154
- * Create a field value
43
+ * The index action
155
44
  *
156
- * @param {Conduit} conduit
157
- */
158
- Editor.setAction(function create_field_value(conduit, controller, subject, action) {
159
-
160
- var that = this,
161
- model = this.getModel(subject),
162
- data = {name: conduit.body.text, title: conduit.body.text};
163
-
164
- model.save(data, function saved(err, doc) {
165
-
166
- if (err) {
167
- return conduit.error(err);
168
- }
169
-
170
- conduit.end({_id: doc._id});
171
- });
172
- });
173
-
174
- /**
175
- * The add action
45
+ * @author Jelle De Loecker <jelle@elevenways.be>
46
+ * @since 0.1.0
47
+ * @version 1.0.1
176
48
  *
177
- * @param {Conduit} conduit
49
+ * @param {Conduit} conduit
50
+ * @param {String} model_name
178
51
  */
179
- Editor.setAction(function add(conduit) {
180
-
181
- var that = this,
182
- model_plural,
183
- model_title,
184
- model_name,
185
- chimera,
186
- model;
187
-
188
- // Get the name of the model
189
- model_name = conduit.routeParam('subject');
190
-
191
- if (!model_name) {
192
- return conduit.error(new Error('No model name was given, nothing to list'));
193
- }
194
-
195
- // Get the actual model
196
- model = this.getModel(model_name);
197
-
198
- // And get the chimera instance
199
- chimera = model.constructor.chimera;
200
-
201
- if (chimera == null) {
202
- return conduit.error(new Error('Model "' + model_name + '" has no chimera configuration'));
203
- }
52
+ Editor.setAction(function index(conduit, model_name) {
204
53
 
205
- // Get the model title
206
- model_title = model_name.titleize();
54
+ let model = this.getModel(model_name);
207
55
 
208
- // And the plural form
209
- model_plural = model_title.pluralize();
56
+ let widget_config = model.chimera.getWidgetConfig('index', conduit);
210
57
 
211
- // Disable translation behaviour of the model
212
- model.disableTranslations();
58
+ this.setTitle(model_name.titleize());
213
59
 
214
- this.setTitle(model_title + ': Add');
60
+ this.set('model_name', model_name);
215
61
 
216
- let action_fields = chimera.getActionFields('edit'),
217
- groups = action_fields.groups.clone(),
218
- item = model.compose(),
219
- id = item._id;
62
+ this.set('widget_config', widget_config);
220
63
 
221
- action_fields.processRecords(model, [item], function groupedRecords(err, groups) {
222
-
223
- if (err) {
224
- throw err;
225
- }
226
-
227
- that.set('editor_action', 'add');
228
- that.set('prefixes', Prefix.getPrefixList());
229
- that.set('groups', groups);
230
- that.set('actions', that.getActions());
231
-
232
- that.set('model_title', model_title);
233
- that.set('model_name', model_name);
234
- that.internal('model_name', model_name);
235
- that.internal('record_id', id);
236
-
237
- // Deprecated variable names
238
- that.set('modelName', model_name);
239
- that.internal('modelName', model_name);
240
- that.internal('recordId', id);
241
-
242
- that.render('chimera/editor/add');
243
- });
64
+ this.render('chimera/editor/index');
244
65
  });
245
66
 
246
67
  /**
247
- * The edit action
68
+ * The add action
69
+ *
70
+ * @author Jelle De Loecker <jelle@elevenways.be>
71
+ * @since 0.1.0
72
+ * @version 1.0.1
248
73
  *
249
- * @param {Conduit} conduit
74
+ * @param {Conduit} conduit
75
+ * @param {String} model_name
250
76
  */
251
- Editor.setAction(function edit(conduit) {
77
+ Editor.setAction(async function add(conduit, model_name) {
252
78
 
253
- var that = this,
254
- modelName = conduit.routeParam('subject'),
255
- model = this.getModel(modelName),
256
- chimera = model.constructor.chimera,
257
- id = conduit.routeParam('id');
79
+ let model = this.getModel(model_name);
258
80
 
259
- model.disableTranslations();
81
+ model.translateItems = false;
260
82
 
261
- var actionFields = chimera.getActionFields('edit'),
262
- groups = actionFields.groups.clone();
83
+ if (conduit.method == 'post') {
263
84
 
264
- model.find('first', {conditions: {_id: alchemy.castObjectId(id)}}, function(err, item) {
85
+ let record = model.createDocument();
265
86
 
266
- if (err) {
267
- return conduit.error(err);
268
- }
87
+ record.setDataRecord(conduit.body[model_name]);
269
88
 
270
- if (!item) {
271
- return conduit.notFound();
272
- }
273
-
274
- actionFields.processRecords(model, [item], function groupedRecords(err, groups) {
275
-
276
- if (err) {
277
- return conduit.error(err);
278
- }
279
-
280
- that.set('editor_action', 'edit');
281
- that.set('prefixes', Prefix.getPrefixList());
282
- that.set('groups', groups);
283
- that.set('actions', that.getActions());
284
- that.set('modelName', modelName);
285
- that.set('display_field_value', item.getDisplayFieldValue({prefer: 'name'}));
286
- that.set('pagetitle', modelName.humanize() + ': Edit');
287
- that.internal('modelName', modelName);
288
- that.internal('recordId', id);
289
-
290
- that.render('chimera/editor/edit');
291
- });
292
- });
293
- });
294
-
295
- /**
296
- * The "peek" method
297
- *
298
- * @param {Conduit} conduit
299
- */
300
- Editor.setAction(function peek(conduit) {
301
-
302
- var that = this,
303
- action_fields,
304
- model_name,
305
- chimera,
306
- groups,
307
- model,
308
- id;
309
-
310
- model_name = conduit.routeParam('subject');
311
- model = this.getModel(model_name);
312
- chimera = model.constructor.chimera;
313
- id = conduit.routeParam('id');
89
+ try {
90
+ await record.save();
314
91
 
315
- // Disable translations in the CMS
316
- model.disableTranslations();
317
-
318
- // Get the "peek" fields
319
- action_fields = chimera.getActionFields('peek');
320
- groups = action_fields.groups.clone();
92
+ let url = alchemy.routeUrl('Chimera.Editor#edit', {
93
+ model: model_name,
94
+ pk: record.$pk,
95
+ message: 'added'
96
+ });
321
97
 
322
- // Get the wanted record
323
- model.find('first', {conditions: {_id: id}}, function gotRecord(err, item) {
98
+ return conduit.redirect(url);
99
+ } catch (err) {
100
+ // @TODO: set this in the context somehow?
101
+ this.set('record_violations', err);
324
102
 
325
- if (err) {
326
- return conduit.error(err);
103
+ this.set('context_variables', {
104
+ record : record
105
+ });
327
106
  }
107
+ }
328
108
 
329
- if (!item) {
330
- return conduit.notFound();
331
- }
109
+ let widget_config = model.chimera.getWidgetConfig('edit', conduit);
332
110
 
333
- action_fields.processRecords(model, [item], function groupedRecords(err, groups) {
111
+ if (!widget_config.class_names) {
112
+ widget_config.class_names = [];
113
+ }
334
114
 
335
- if (err) {
336
- return conduit.error(err);
337
- }
115
+ widget_config.class_names.push('chimera-editor-widgets');
338
116
 
339
- that.set('editor_action', 'peek');
340
- that.set('prefixes', Prefix.getPrefixList());
341
- that.set('groups', groups);
342
- that.set('actions', that.getActions());
343
- that.set('modelName', model_name);
344
- that.set('display_field_value', item.getDisplayFieldValue({prefer: 'name'}));
345
- that.internal('modelName', model_name);
346
- that.internal('recordId', id);
347
-
348
- that.render('chimera/editor/peek');
349
- });
350
- });
351
- });
117
+ this.set('widget_config', widget_config);
118
+ this.setTitle(model.constructor.title + ' Add');
352
119
 
120
+ this.render('chimera/widgets');
121
+ });
353
122
 
354
123
  /**
355
- * Reorder an array
124
+ * The edit action
356
125
  *
357
- * @author Jelle De Loecker <jelle@develry.be>
358
- * @since 0.5.1
359
- * @version 0.5.1
126
+ * @author Jelle De Loecker <jelle@elevenways.be>
127
+ * @since 0.1.0
128
+ * @version 1.0.1
360
129
  *
361
- * @param {Array} arr The array to reorder
362
- * @param {Object} query The query that should match the entries
363
- * @param {Number} new_index The new index this should be moved to
130
+ * @param {Conduit} conduit
131
+ * @param {String} model_name
132
+ * @param {String} pk_val
364
133
  */
365
- function reorderByQuery(arr, query, new_index, path) {
134
+ Editor.setAction(async function edit(conduit, model_name, pk_val) {
366
135
 
367
- let entry = arr.findByPath(query);
136
+ let model = this.getModel(model_name);
368
137
 
369
- if (!entry) {
370
- return;
371
- }
372
-
373
- arr.move(entry, new_index);
374
-
375
- let i;
376
-
377
- for (i = 0; i < arr.length; i++) {
378
- Object.setPath(arr[i], path, i);
379
- }
380
- };
381
-
382
- /**
383
- * The "reorder" method
384
- *
385
- * @param {Conduit} conduit
386
- */
387
- Editor.setAction(async function reorder(conduit) {
138
+ model.translateItems = false;
388
139
 
389
- var that = this,
390
- model_name,
391
- new_index,
392
- model,
393
- id;
140
+ let record = await model.findByPk(pk_val);
141
+ let message_type = conduit.param('message');
394
142
 
395
- new_index = Number(conduit.param('new_index'));
396
- model_name = conduit.routeParam('subject');
397
- model = this.getModel(model_name);
398
- id = conduit.routeParam('id');
399
-
400
- let record = await model.findById(id);
401
- let records = await model.find('all', {fields: ['_id', 'order']});
143
+ this.set('context_variables', {
144
+ record : record
145
+ });
402
146
 
403
- records = Array.cast(records);
147
+ if (conduit.method == 'post') {
404
148
 
405
- reorderByQuery(records, {_id: String(id)}, new_index, 'order');
149
+ Object.assign(record, conduit.body[model_name]);
406
150
 
407
- for (let i = 0; i < records.length; i++) {
408
- if (!records[i].hasChanged()) {
409
- continue;
151
+ try {
152
+ await record.save();
153
+ message_type = 'saved';
154
+ } catch (err) {
155
+ // @TODO: set this in the context somehow?
156
+ this.set('record_violations', err);
410
157
  }
411
-
412
- await records[i].save();
413
158
  }
414
159
 
415
- conduit.end({saved: true});
416
- });
160
+ let widget_config = model.chimera.getWidgetConfig('edit', conduit);
417
161
 
418
- /**
419
- * Associated model data
420
- *
421
- * @param {Conduit} conduit
422
- */
423
- Editor.setAction(async function model_assoc_data(conduit) {
424
-
425
- var model = Model.get(conduit.routeParam('subject')),
426
- options;
427
-
428
- let records = await model.find('all'),
429
- results = [],
430
- response = {};
431
-
432
- for (let i = 0; i < records.length; i++) {
433
- let record = records[i];
434
-
435
- let entry = {
436
- _id : record._id
437
- };
438
-
439
- if (record[model.displayField]) {
440
- entry[model.displayField] = record[model.displayField];
441
- } else {
442
- entry.title = record.title;
443
- entry.name = record.name;
444
- }
445
-
446
- results.push(entry);
162
+ if (!widget_config.class_names) {
163
+ widget_config.class_names = [];
447
164
  }
448
165
 
449
- conduit.end({
450
- items : results,
451
- displayField : model.displayField
452
- });
453
- });
454
-
455
- /**
456
- * The related_data action for a certain field
457
- *
458
- * @param {Conduit} conduit
459
- */
460
- Editor.setAction(function related_data(conduit) {
461
-
462
- var that = this,
463
- modelName = conduit.routeParam('subject'),
464
- model = this.getModel(modelName),
465
- chimera = model.constructor.chimera,
466
- options = {},
467
- id = conduit.routeParam('id'),
468
- field;
469
-
470
- if (conduit.param('display_field_only')) {
471
- options.display_field_only = true;
166
+ if (message_type) {
167
+ if (message_type == 'added') {
168
+ this.set('message', 'Record has been added');
169
+ } else if (message_type == 'saved') {
170
+ this.set('message', 'Record has been saved');
171
+ }
472
172
  }
473
173
 
474
- model.disableTranslations();
174
+ widget_config.class_names.push('chimera-editor-widgets');
475
175
 
476
- // Some fields (like subschemas) require record info for related data
477
- model.find('first', {conditions: {_id: id}, document: false}, function gotResult(err, items) {
478
-
479
- var nested_in = conduit.param('nested_in'),
480
- fieldpath = conduit.param('fieldpath');
481
-
482
- if (nested_in) {
483
- fieldpath = nested_in + '.' + fieldpath;
484
- }
176
+ this.set('widget_config', widget_config);
177
+ this.setTitle(model.constructor.title + ' Edit');
485
178
 
486
- field = chimera.getField(fieldpath, items[0]);
487
-
488
- if (!field) {
489
- conduit.notFound(new Error('Could not find field "' + fieldpath + '"'));
490
- } else {
491
- field.sendRelatedData(conduit, items[0], options);
492
- }
493
- });
179
+ this.render('chimera/widgets');
494
180
  });
495
181
 
496
182
  /**
497
- * The view action
183
+ * The records API action
498
184
  *
499
- * @todo: code is mostly alike to edit, merge together
185
+ * @author Jelle De Loecker <jelle@elevenways.be>
186
+ * @since 1.0.0
187
+ * @version 1.0.0
500
188
  *
501
- * @param {Conduit} conduit
189
+ * @param {Conduit} conduit
190
+ * @param {String} model_name
502
191
  */
503
- Editor.setAction(async function view(conduit) {
504
-
505
- var that = this,
506
- modelName = conduit.routeParam('subject'),
507
- model = this.getModel(modelName),
508
- chimera = model.constructor.chimera,
509
- id = conduit.routeParam('id');
510
-
511
- model.disableTranslations();
192
+ Editor.setAction(async function records(conduit, model_name) {
512
193
 
513
- var actionFields = chimera.getActionFields('edit'),
514
- groups = actionFields.groups.clone();
515
-
516
- let item = await model.findById(id);
194
+ let body = conduit.body,
195
+ model = this.getModel(model_name),
196
+ crit = model.find();
517
197
 
518
- if (!item) {
519
- return conduit.notFound();
520
- }
198
+ let page_size = body.page_size,
199
+ fields = body.fields,
200
+ page = body.page;
521
201
 
522
- actionFields.processRecords(model, [item], function groupedRecords(err, groups) {
202
+ if (fields) {
523
203
 
524
- if (err) {
525
- return conduit.error(err);
204
+ // @TODO: fix FieldSet being sent with regular json?
205
+ if (fields.fields) {
206
+ fields = fields.fields;
526
207
  }
527
208
 
528
- that.set('editor_action', 'view');
529
- that.set('prefixes', Prefix.getPrefixList());
530
- that.set('groups', groups);
531
- that.set('actions', that.getActions());
532
- that.set('modelName', modelName);
533
- that.set('pageTitle', modelName.humanize());
534
- that.set('display_field_value', item.getDisplayFieldValue({prefer: 'name'}));
535
- that.internal('modelName', modelName);
536
- that.internal('recordId', id);
537
- that.internal('chimeraReadOnly', true);
538
-
539
- that.render('chimera/editor/view');
540
- });
541
- });
542
-
543
- /**
544
- * The save action
545
- *
546
- * @author Jelle De Loecker <jelle@develry.be>
547
- * @since 0.1.0
548
- * @version 0.5.1
549
- *
550
- * @param {Conduit} conduit
551
- */
552
- Editor.setAction(function save(conduit) {
553
- this.save(conduit, false);
554
- });
555
-
556
- /**
557
- * The save & close action
558
- *
559
- * @author Jelle De Loecker <jelle@develry.be>
560
- * @since 0.5.1
561
- * @version 0.5.1
562
- *
563
- * @param {Conduit} conduit
564
- */
565
- Editor.setAction(function saveClose(conduit) {
566
- this.save(conduit, true);
567
- });
568
-
569
- /**
570
- * Save a record
571
- *
572
- * @author Jelle De Loecker <jelle@develry.be>
573
- * @since 0.5.1
574
- * @version 0.5.1
575
- *
576
- * @param {Conduit} conduit
577
- */
578
- Editor.setMethod(function save(conduit, go_to_index) {
579
-
580
- var that = this,
581
- actionFields,
582
- modelName,
583
- chimera,
584
- options,
585
- groups,
586
- record,
587
- model,
588
- data,
589
- id;
590
-
591
- if (conduit.method != 'post') {
592
- return conduit.error('Use POST method to apply changes');
209
+ crit.select(fields);
593
210
  }
594
211
 
595
- modelName = conduit.routeParam('subject');
596
- model = this.getModel(modelName);
597
- model.disableTranslations();
598
-
599
- chimera = model.constructor.chimera;
600
- data = conduit.body.data;
601
-
602
- if (!data) {
603
- return conduit.error('Data was not found in POST body');
212
+ if (page) {
213
+ crit.page(page, page_size);
604
214
  }
605
215
 
606
- id = conduit.routeParam('id');
607
-
608
- actionFields = chimera.getActionFields('edit');
609
- groups = actionFields.groups.clone();
610
-
611
- record = data[modelName.classify()];
612
- record._id = alchemy.castObjectId(id);
613
-
614
- options = {};
615
-
616
- // Force create, even though an _id could be given
617
- if (conduit.body.create === true) {
618
- options.create = true;
216
+ if (body.sort && body.sort.field && body.sort.dir) {
217
+ crit.sort([body.sort.field, body.sort.dir]);
619
218
  }
620
219
 
621
- model.save(record, options, function afterSave(err, items) {
622
-
623
- if (err != null) {
624
- conduit.flash('Could not save record: ' + err, {className: 'chimera-fail'});
625
- return conduit.error(err);
626
- }
627
-
628
- let result = items[0];
629
-
630
- let route_params = Object.assign({}, conduit.params);
631
-
632
- conduit.flash('Record has been saved', {className: 'chimera-success'});
633
-
634
- if (go_to_index) {
635
- route_params.action = 'index';
636
-
637
- return conduit.redirect({
638
- headers : conduit.headers,
639
- url : Router.getUrl('ModelAction', route_params)
640
- });
641
- }
220
+ if (body.filters) {
221
+ let key,
222
+ val;
642
223
 
643
- route_params.action = 'edit';
644
- route_params.id = result._id;
224
+ for (key in body.filters) {
225
+ val = body.filters[key];
645
226
 
646
- // When creating a new record we need to supply a new url,
647
- // because the 'history' will be activated
648
- // The redirect does remain internal, though!
649
- if (options.create) {
650
-
651
- return conduit.redirect({
652
- headers : conduit.headers,
653
- url : Router.getUrl('RecordAction', route_params)
654
- });
655
- }
656
-
657
- that.doAction('edit', [conduit]);
658
- });
659
- });
660
-
661
- /**
662
- * The remove action
663
- *
664
- * @param {Conduit} conduit
665
- */
666
- Editor.setAction(async function remove(conduit) {
667
-
668
- var that = this,
669
- model_name = conduit.routeParam('subject'),
670
- model = this.getModel(model_name),
671
- id = conduit.routeParam('id');
672
-
673
- model.disableTranslations();
674
-
675
- if (conduit.body.sure === 'yes') {
676
- model.remove(id, function onRemoved(err) {
677
-
678
- if (err) {
679
- return conduit.error(err);
227
+ // The value should always be a string,
228
+ // so anything that is falsy can be ignored
229
+ if (!val) {
230
+ continue;
680
231
  }
681
232
 
682
- conduit.setHeader('x-history-url', '/chimera/editor/' + model_name + '/index');
683
- conduit.flash('Record has been removed', {className: 'chimera-success'});
684
-
685
- let route_params = Object.assign({}, conduit.params);
686
- route_params.action = 'index';
687
- delete route_params.id;
688
-
689
- return conduit.redirect({
690
- headers : conduit.headers,
691
- url : Router.getUrl('ModelAction', route_params)
692
- });
693
- });
694
- } else {
695
-
696
- let item = await model.findById(id);
697
-
698
- if (!item) {
699
- return conduit.notFound();
233
+ val = RegExp.interpretWildcard('*' + val + '*');
234
+ crit.where(key).equals(val);
700
235
  }
236
+ }
701
237
 
702
- that.set('editor_action', 'remove');
703
- that.set('actions', that.getActions());
704
- that.set('modelName', model_name);
705
- that.set('pageTitle', model_name.humanize());
706
- that.set('item', item);
707
- that.set('display_field_value', item.getDisplayFieldValue({prefer: 'name'}));
708
- that.internal('modelName', model_name);
709
- that.internal('recordId', id);
710
-
711
- that.render('chimera/editor/remove');
238
+ let records = await model.find('all', crit),
239
+ result = [],
240
+ record,
241
+ main;
242
+
243
+ for (record of records) {
244
+
245
+ record.$hold.actions = [
246
+ {
247
+ name : 'edit',
248
+ icon : 'edit',
249
+ url : alchemy.routeUrl('Chimera.Editor#edit', {
250
+ model : model_name,
251
+ pk : record.$pk,
252
+ })
253
+ }
254
+ ];
712
255
  }
713
256
 
257
+ conduit.end({
258
+ records : records
259
+ });
714
260
  });