forked from marmelab/ng-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.js
More file actions
371 lines (335 loc) · 16.7 KB
/
config.js
File metadata and controls
371 lines (335 loc) · 16.7 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*global angular*/
(function () {
"use strict";
var app = angular.module('myApp', ['ng-admin']);
app.config(['NgAdminConfigurationProvider', 'RestangularProvider', function (NgAdminConfigurationProvider, RestangularProvider) {
var nga = NgAdminConfigurationProvider;
function truncate(value) {
if (!value) {
return '';
}
return value.length > 50 ? value.substr(0, 50) + '...' : value;
}
// use the custom query parameters function to format the API request correctly
RestangularProvider.addFullRequestInterceptor(function(element, operation, what, url, headers, params) {
if (operation == "getList") {
// custom pagination params
if (params._page) {
params._start = (params._page - 1) * params._perPage;
params._end = params._page * params._perPage;
}
delete params._page;
delete params._perPage;
// custom sort params
if (params._sortField) {
params._sort = params._sortField;
delete params._sortField;
}
// custom filters
if (params._filters) {
for (var filter in params._filters) {
params[filter] = params._filters[filter];
}
delete params._filters;
}
}
return { params: params };
});
var admin = nga.application('ng-admin backend demo') // application main title
.debug(false) // debug disabled
.baseApiUrl('http://localhost:3000/'); // main API endpoint
// define all entities at the top to allow references between them
var post = nga.entity('posts'); // the API endpoint for posts will be http://localhost:3000/posts/:id
var comment = nga.entity('comments')
.baseApiUrl('http://localhost:3000/') // The base API endpoint can be customized by entity
.identifier(nga.field('id')); // you can optionally customize the identifier used in the api ('id' by default)
var tag = nga.entity('tags')
.readOnly(); // a readOnly entity has disabled creation, edition, and deletion views
// set the application entities
admin
.addEntity(post)
.addEntity(tag)
.addEntity(comment);
// customize entities and views
post.dashboardView() // customize the dashboard panel for this entity
.name('posts')
.title('Recent posts')
.order(1) // display the post panel first in the dashboard
.perPage(5) // limit the panel to the 5 latest posts
.fields([nga.field('title').isDetailLink(true).map(truncate)]); // fields() called with arguments add fields to the view
post.listView()
.title('All posts') // default title is "[Entity_name] list"
.description('List of posts with infinite pagination') // description appears under the title
.infinitePagination(true) // load pages as the user scrolls
.fields([
nga.field('id').label('id'), // The default displayed name is the camelCase field name. label() overrides id
nga.field('title'), // the default list field type is "string", and displays as a string
nga.field('published_at', 'date'), // Date field type allows date formatting
nga.field('average_note', 'float'), // Float type also displays decimal digits
nga.field('views', 'number'),
nga.field('tags', 'reference_many') // a Reference is a particular type of field that references another entity
.targetEntity(tag) // the tag entity is defined later in this file
.targetField(nga.field('name')) // the field to be displayed in this list
])
.listActions(['show', 'edit', 'delete']);
post.creationView()
.fields([
nga.field('title') // the default edit field type is "string", and displays as a text input
.attributes({ placeholder: 'the post title' }) // you can add custom attributes, too
.validation({ required: true, minlength: 3, maxlength: 100 }), // add validation rules for fields
nga.field('teaser', 'text'), // text field type translates to a textarea
nga.field('body', 'wysiwyg'), // overriding the type allows rich text editing for the body
nga.field('published_at', 'date') // Date field type translates to a datepicker
]);
var subCategories = [
{ category: 'tech', label: 'Computers', value: 'computers' },
{ category: 'tech', label: 'Gadgets', value: 'gadgets' },
{ category: 'lifestyle', label: 'Travel', value: 'travel' },
{ category: 'lifestyle', label: 'Fitness', value: 'fitness' }
];
post.editionView()
.title('Edit post "{{ entry.values.title }}"') // title() accepts a template string, which has access to the entry
.actions(['list', 'show', 'delete']) // choose which buttons appear in the top action bar. Show is disabled by default
.fields([
post.creationView().fields(), // fields() without arguments returns the list of fields. That way you can reuse fields from another view to avoid repetition
nga.field('category', 'choice') // a choice field is rendered as a dropdown in the edition view
.choices([ // List the choice as object literals
{ label: 'Tech', value: 'tech' },
{ label: 'Lifestyle', value: 'lifestyle' }
]),
nga.field('subcategory', 'choice')
.choices(function(entry) { // choices also accepts a function to return a list of choices based on the current entry
return subCategories.filter(function (c) {
return c.category === entry.values.category
});
}),
nga.field('tags', 'reference_many') // ReferenceMany translates to a select multiple
.targetEntity(tag)
.targetField(nga.field('name'))
.cssClasses('col-sm-4'), // customize look and feel through CSS classes
nga.field('pictures', 'json'),
nga.field('views', 'number')
.cssClasses('col-sm-4'),
nga.field('average_note', 'float')
.cssClasses('col-sm-4'),
nga.field('comments', 'referenced_list') // display list of related comments
.targetEntity(comment)
.targetReferenceField('post_id')
.targetFields([
nga.field('created_at').label('Posted'),
nga.field('body').label('Comment')
])
.sortField('created_at')
.sortDir('DESC'),
nga.field('', 'template').label('')
.template('<span class="pull-right"><ma-filtered-list-button entity-name="comments" filter="{ post_id: entry.values.id }" size="sm"></ma-filtered-list-button></span>')
]);
post.showView() // a showView displays one entry in full page - allows to display more data than in a a list
.fields([
nga.field('id'),
post.editionView().fields(), // reuse fields from another view in another order
nga.field('custom_action', 'template')
.label('')
.template('<send-email post="entry"></send-email>')
]);
comment.dashboardView()
.title('Last comments')
.order(2) // display the comment panel second in the dashboard
.perPage(5)
.fields([
nga.field('id'),
nga.field('body', 'wysiwyg')
.label('Comment')
.stripTags(true)
.map(truncate),
nga.field(null, 'template') // template fields don't need a name in dashboard view
.label('')
.template('<post-link entry="entry"></post-link>') // you can use custom directives, too
]);
comment.listView()
.title('Comments')
.perPage(10) // limit the number of elements displayed per page. Default is 30.
.fields([
nga.field('created_at', 'date')
.label('Posted'),
nga.field('author'),
nga.field('body', 'wysiwyg')
.stripTags(true)
.map(truncate),
nga.field('post_id', 'reference')
.label('Post')
.map(truncate)
.targetEntity(post)
.targetField(nga.field('title').map(truncate))
])
.filters([
nga.field('q', 'string').label('').attributes({'placeholder': 'Global Search'}),
nga.field('created_at', 'date')
.label('Posted')
.attributes({'placeholder': 'Filter by date'}),
nga.field('today', 'boolean').map(function() {
var now = new Date(),
year = now.getFullYear(),
month = now.getMonth() + 1,
day = now.getDate();
month = month < 10 ? '0' + month : month;
day = day < 10 ? '0' + day : day;
return {
created_at: [year, month, day].join('-') // ?created_at=... will be appended to the API call
};
}),
nga.field('post_id', 'reference')
.label('Post')
.targetEntity(post)
.targetField(nga.field('title'))
])
.listActions(['edit', 'delete']);
comment.creationView()
.fields([
nga.field('created_at', 'date')
.label('Posted')
.defaultValue(new Date()), // preset fields in creation view with defaultValue
nga.field('author'),
nga.field('body', 'wysiwyg'),
nga.field('post_id', 'reference')
.label('Post')
.map(truncate)
.targetEntity(post)
.targetField(nga.field('title'))
.validation({ required: true }),
]);
comment.editionView()
.fields(comment.creationView().fields())
.fields([nga.field(null, 'template')
.label('')
.template('<post-link entry="entry"></post-link>') // template() can take a function or a string
]);
comment.deletionView()
.title('Deletion confirmation'); // customize the deletion confirmation message
tag.dashboardView()
.title('Recent tags')
.order(3)
.perPage(10)
.fields([
nga.field('id'),
nga.field('name'),
nga.field('published', 'boolean').label('Is published ?')
]);
tag.listView()
.infinitePagination(false) // by default, the list view uses infinite pagination. Set to false to use regulat pagination
.fields([
nga.field('id').label('ID'),
nga.field('name'),
nga.field('published', 'boolean').cssClasses(function(entry) { // add custom CSS classes to inputs and columns
if (entry.values.published) {
return 'bg-success text-center';
}
return 'bg-warning text-center';
}),
nga.field('custom', 'template')
.label('Upper name')
.template('{{ entry.values.name.toUpperCase() }}')
])
.batchActions([]) // disable checkbox column and batch delete
.listActions(['show']);
tag.showView()
.fields([
nga.field('name'),
nga.field('published', 'boolean')
]);
// customize header
var customHeaderTemplate =
'<div class="navbar-header">' +
'<a class="navbar-brand" href="#" ng-click="appController.displayHome()">ng-admin backend demo</a>' +
'</div>' +
'<p class="navbar-text navbar-right">' +
'<a href="https://github.com/marmelab/ng-admin/blob/master/examples/blog/config.js"><span class="glyphicon glyphicon-sunglasses"></span> View Source</a>' +
'</p>';
admin.header(customHeaderTemplate);
// customize menu
admin.menu(nga.menu()
.addChild(nga.menu(post).icon('<span class="glyphicon glyphicon-file"></span>')) // customize the entity menu icon
.addChild(nga.menu(comment).icon('<strong style="font-size:1.3em;line-height:1em">✉</strong>')) // you can even use utf-8 symbols!
.addChild(nga.menu(tag).icon('<span class="glyphicon glyphicon-tags"></span>'))
.addChild(nga.menu().title('Other')
.addChild(nga.menu().title('Stats').icon('').link('/stats'))
)
);
nga.configure(admin);
}]);
app.directive('postLink', ['$location', function ($location) {
return {
restrict: 'E',
scope: { entry: '&' },
template: '<p class="form-control-static"><a ng-click="displayPost()">View post</a></p>',
link: function (scope) {
scope.displayPost = function () {
$location.path('/posts/show/' + scope.entry().values.post_id);
};
}
};
}]);
app.directive('sendEmail', ['$location', function ($location) {
return {
restrict: 'E',
scope: { post: '&' },
template: '<a class="btn btn-default" ng-click="send()">Send post by email</a>',
link: function (scope) {
scope.send = function () {
$location.path('/sendPost/' + scope.post().values.id);
};
}
};
}]);
// custom 'send post by email' page
function sendPostController($stateParams, notification) {
this.postId = $stateParams.id;
// notification is the service used to display notifications on the top of the screen
this.notification = notification;
}
sendPostController.prototype.sendEmail = function() {
if (this.email) {
this.notification.log('Email successfully sent to ' + this.email, {addnCls: 'humane-flatty-success'});
} else {
this.notification.log('Email is undefined', {addnCls: 'humane-flatty-error'});
}
};
sendPostController.$inject = ['$stateParams', 'notification'];
var sendPostControllerTemplate =
'<div class="row"><div class="col-lg-12">' +
'<ma-view-actions><ma-back-button></ma-back-button></ma-view-actions>' +
'<div class="page-header">' +
'<h1>Send post #{{ controller.postId }} by email</h1>' +
'<p class="lead">You can add custom pages, too</p>' +
'</div>' +
'</div></div>' +
'<div class="row">' +
'<div class="col-lg-5"><input type="text" size="10" ng-model="controller.email" class="form-control" placeholder="name@example.com"/></div>' +
'<div class="col-lg-5"><a class="btn btn-default" ng-click="controller.sendEmail()">Send</a></div>' +
'</div>';
app.config(['$stateProvider', function ($stateProvider) {
$stateProvider.state('send-post', {
parent: 'main',
url: '/sendPost/:id',
params: { id: null },
controller: sendPostController,
controllerAs: 'controller',
template: sendPostControllerTemplate
});
}]);
// custom page with menu item
var customPageTemplate = '<div class="row"><div class="col-lg-12">' +
'<ma-view-actions><ma-back-button></ma-back-button></ma-view-actions>' +
'<div class="page-header">' +
'<h1>Stats</h1>' +
'<p class="lead">You can add custom pages, too</p>' +
'</div>' +
'</div></div>';
app.config(['$stateProvider', function ($stateProvider) {
$stateProvider.state('stats', {
parent: 'main',
url: '/stats',
template: customPageTemplate
});
}]);
}());