forked from colmena/colmena
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeta.js
More file actions
109 lines (95 loc) · 2.97 KB
/
meta.js
File metadata and controls
109 lines (95 loc) · 2.97 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
'use strict'
import angular from 'angular'
function MetaService ($injector, CoreService, Meta, gettextCatalog) {
this.find = () => Meta.getModels().$promise
this.findById = (name) => Meta.getModelById({ name }).$promise
this.getModelInstance = (name) => $injector.get(name)
this.getModelItems = (name) => {
const Model = this.getModelInstance(name)
return (typeof Model.find !== 'function') ? false : Model.find().$promise
}
this.getModelItem = (modelName, modelId) => {
const Model = this.getModelInstance(modelName)
if (typeof Model.find !== 'function') {
return false
} else {
return Model.findOne({
filter: {
where: {
id: modelId,
},
},
}).$promise
}
}
this.getModelFields = (model) => {
const result = []
angular.forEach(model.properties, (property, propertyName) => {
if (propertyName !== 'id') {
result.push(getModelField(propertyName, property))
}
})
return result
}
function getModelField (propertyName, property) {
return {
key: propertyName,
type: getModelFieldType(property),
templateOptions: {
label: propertyName,
required: property.required !== undefined ? property.required : false,
description: property.description !== undefined ? property.description : false,
},
}
}
function getModelFieldType (property) {
let result = 'input'
if (property.meta !== undefined && property.meta.formType !== undefined) {
result = property.meta.formType
}
return result
}
this.upsert = (modelName, item) => {
const Model = this.getModelInstance(modelName)
return Model.upsert(item).$promise
.then(() => {
CoreService.toastSuccess(
gettextCatalog.getString('Item saved'),
gettextCatalog.getString('Your item is safe with us!')
)
})
.catch((err) => {
CoreService.toastError(
gettextCatalog.getString('Error saving item '),
gettextCatalog.getString(`This item could no be saved: ${err}`)
)
}
)
}
this.delete = (modelName, modelId, successCb, cancelCb) => {
const Model = this.getModelInstance(modelName)
CoreService.confirm(
gettextCatalog.getString('Are you sure?'),
gettextCatalog.getString('Deleting this cannot be undone'),
() => {
Model.deleteById({ id: modelId }).$promise.then(() => {
CoreService.toastSuccess(
gettextCatalog.getString('Item deleted'),
gettextCatalog.getString('Your item is deleted!'))
successCb()
}).catch((err) => {
CoreService.toastError(
gettextCatalog.getString('Error deleting item'),
gettextCatalog.getString(`Your item is not deleted! ${err}`))
cancelCb()
})
},
() => {
cancelCb()
}
)
}
}
angular
.module('com.module.core.services.meta', [])
.service('MetaService', MetaService)