@dile/crud 0.0.9
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.
- package/LICENSE +7 -0
- package/README.md +5 -0
- package/components/action/crud-actions.js +2 -0
- package/components/action/crud-delete-action.js +2 -0
- package/components/action/crud-single-actions.js +2 -0
- package/components/action/src/DileCrudActions.js +181 -0
- package/components/action/src/DileCrudDeleteAction.js +18 -0
- package/components/action/src/DileCrudSingleActions.js +77 -0
- package/components/ajax/ajax.js +2 -0
- package/components/ajax/index.js +1 -0
- package/components/ajax/src/DileAjax.js +108 -0
- package/components/ajax-form/ajax-form.js +2 -0
- package/components/ajax-form/index.js +1 -0
- package/components/ajax-form/src/DileAjaxForm.js +216 -0
- package/components/ajax-select-crud/ajax-select-crud.js +3 -0
- package/components/ajax-select-crud/index.js +1 -0
- package/components/ajax-select-crud/src/DileAjaxSelectCrud.js +75 -0
- package/components/crud/crud-single.js +2 -0
- package/components/crud/crud.js +2 -0
- package/components/crud/delete-action.js +2 -0
- package/components/crud/src/DileCrud.js +352 -0
- package/components/detail/crud-detail.js +2 -0
- package/components/detail/src/DileCrudDetail.js +166 -0
- package/components/insert/crud-insert.js +2 -0
- package/components/insert/index.js +1 -0
- package/components/insert/src/DileCrudInsert.js +78 -0
- package/components/item-delete/crud-item-delete.js +2 -0
- package/components/item-delete/index.js +1 -0
- package/components/item-delete/src/DileCrudItemDelete.js +106 -0
- package/components/list/crud-list-item.js +2 -0
- package/components/list/crud-list-pagination-links.js +2 -0
- package/components/list/crud-list-service.js +2 -0
- package/components/list/crud-list.js +2 -0
- package/components/list/crud-select-all.js +2 -0
- package/components/list/index.js +5 -0
- package/components/list/src/DileCrudList.js +332 -0
- package/components/list/src/DileCrudListItem.js +129 -0
- package/components/list/src/DileCrudListPaginationLinks.js +89 -0
- package/components/list/src/DileCrudListService.js +163 -0
- package/components/list/src/DileCrudSelectAll.js +158 -0
- package/components/single/crud-single.js +2 -0
- package/components/single/index.js +1 -0
- package/components/single/src/DileCrudSingle.js +142 -0
- package/components/ui/crud-filters-form.js +3 -0
- package/components/ui/crud-filters-list-item.js +2 -0
- package/components/ui/crud-filters-list.js +2 -0
- package/components/ui/crud-filters.js +2 -0
- package/components/ui/crud-list-options.js +2 -0
- package/components/ui/crud-page-size-select.js +2 -0
- package/components/ui/crud-page-size.js +2 -0
- package/components/ui/crud-pagination-nav-button.js +2 -0
- package/components/ui/crud-sort-form.js +2 -0
- package/components/ui/index.js +4 -0
- package/components/ui/src/DileCrudFilters.js +62 -0
- package/components/ui/src/DileCrudFiltersForm.js +58 -0
- package/components/ui/src/DileCrudFiltersList.js +70 -0
- package/components/ui/src/DileCrudFiltersListItem.js +28 -0
- package/components/ui/src/DileCrudListOptions.js +64 -0
- package/components/ui/src/DileCrudPageSize.js +27 -0
- package/components/ui/src/DileCrudPageSizeSelect.js +44 -0
- package/components/ui/src/DileCrudPaginationNavButton.js +44 -0
- package/components/ui/src/DileCrudSortForm.js +105 -0
- package/components/update/crud-update.js +2 -0
- package/components/update/index.js +1 -0
- package/components/update/src/DileCrudUpdate.js +81 -0
- package/lib/AxiosInstanceBuilder.js +21 -0
- package/lib/CrudConfigBuilder.js +13 -0
- package/lib/DileAxios.js +14 -0
- package/lib/DileConfig.js +0 -0
- package/lib/DileCrudMixin.js +72 -0
- package/lib/DileLoading.js +31 -0
- package/lib/RequestApiAdapter.js +15 -0
- package/lib/ResponseApiAdapter.js +37 -0
- package/lib/capitalizeString.js +3 -0
- package/lib/deepMerge.js +38 -0
- package/lib/defaultConfig.js +67 -0
- package/package.json +35 -0
- package/styles/crud-styles.js +6 -0
- package/styles/form-styles.js +24 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2019 EscuelaIT
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { LitElement, html, css } from 'lit';
|
|
2
|
+
import { moreVertIcon } from '@dile/icons';
|
|
3
|
+
import '@dile/ui/components/confirm/confirm';
|
|
4
|
+
import '@dile/ui/components/select/select';
|
|
5
|
+
import '../../ajax/ajax.js'
|
|
6
|
+
import '../../ui/crud-list-options.js';
|
|
7
|
+
|
|
8
|
+
export class DileCrudActions extends LitElement {
|
|
9
|
+
static styles = css`
|
|
10
|
+
:host {
|
|
11
|
+
display: block;
|
|
12
|
+
--dile-modal-border-radius: 2px;
|
|
13
|
+
--dile-modal-width: 100vw;
|
|
14
|
+
--dile-modal-height: 100vh;
|
|
15
|
+
--dile-modal-content-padding: 1.5rem;
|
|
16
|
+
--dile-modal-background-color: rgba(0, 0, 0, 0.5);
|
|
17
|
+
--dile-modal-content-shadow-color: rgba(0, 0, 0, 0.5);
|
|
18
|
+
--dile-confirm-accept-button-color: var(--dile-primary-color, #378dca);
|
|
19
|
+
--dile-confirm-accept-text-button-color: var(--dile-on-primary-color, #fff);
|
|
20
|
+
--dile-confirm-cancel-button-color: var(--dile-danger-color, #E31E1B);
|
|
21
|
+
--dile-confirm-cancel-text-button-color: var(--dile-on-danger-color, #fff);
|
|
22
|
+
}
|
|
23
|
+
p {
|
|
24
|
+
margin: 0 0 0.75rem 0;
|
|
25
|
+
}
|
|
26
|
+
.visible {
|
|
27
|
+
display: block;
|
|
28
|
+
}
|
|
29
|
+
.hide {
|
|
30
|
+
display: none;
|
|
31
|
+
}
|
|
32
|
+
.modalcontainer {
|
|
33
|
+
color: #303030;
|
|
34
|
+
}
|
|
35
|
+
@media(min-width: 400px) {
|
|
36
|
+
:host {
|
|
37
|
+
--dile-modal-width: 90%;
|
|
38
|
+
--dile-modal-height: 90%;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
@media(min-width: 500px) {
|
|
42
|
+
:host {
|
|
43
|
+
--dile-modal-width: 80%;
|
|
44
|
+
--dile-modal-height: 80%;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
`;
|
|
48
|
+
|
|
49
|
+
static get properties() {
|
|
50
|
+
return {
|
|
51
|
+
actionIds: { type: Array },
|
|
52
|
+
selection: { type: String },
|
|
53
|
+
endpoint: { type: String },
|
|
54
|
+
actions: { type: Array },
|
|
55
|
+
formActionsTemplate: { type: Object },
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
constructor() {
|
|
60
|
+
super();
|
|
61
|
+
this.actionIds = [];
|
|
62
|
+
this.actions = [];
|
|
63
|
+
this.selection = 'DeleteAction'
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
firstUpdated() {
|
|
67
|
+
this.confirmElement = this.shadowRoot.getElementById('elconfirm');
|
|
68
|
+
this.ajaxAction = this.shadowRoot.getElementById('ajaxaction');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
render() {
|
|
72
|
+
return html`
|
|
73
|
+
${this.ajaxTemplate}
|
|
74
|
+
${this.actionListTemplate}
|
|
75
|
+
${this.confirmActionTemplate}
|
|
76
|
+
`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
get ajaxTemplate() {
|
|
80
|
+
return html`
|
|
81
|
+
<dile-ajax
|
|
82
|
+
id="ajaxaction"
|
|
83
|
+
method="post"
|
|
84
|
+
url="${this.endpoint}/action"
|
|
85
|
+
@ajax-success="${this.doSuccessAction}"
|
|
86
|
+
@ajax-error="${this.doErrorAction}"
|
|
87
|
+
></dile-ajax>
|
|
88
|
+
`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
get actionListTemplate() {
|
|
92
|
+
return html`
|
|
93
|
+
<dile-crud-list-options .icon="${moreVertIcon}" label="Acciones" class="${this.actionIds.length > 0 ? 'visible' : 'hide'}">
|
|
94
|
+
<p>${this.actionIds.length} ${this.actionIds.length == 1 ? 'elemento' : 'elementos'}</p>
|
|
95
|
+
<div @element-changed=${this.selectorChanged}>
|
|
96
|
+
${this.selectActionsTemplate}
|
|
97
|
+
</div>
|
|
98
|
+
<dile-button @click=${this.showAction}>Realizar acción</dile-button>
|
|
99
|
+
</dile-crud-list-options>
|
|
100
|
+
`;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
get selectActionsTemplate() {
|
|
104
|
+
return html`
|
|
105
|
+
<dile-select>
|
|
106
|
+
<select slot="select">
|
|
107
|
+
${this.actions.map(action => html`<option value="${action.name}">${action.label}</option>`)}
|
|
108
|
+
</select>
|
|
109
|
+
</dile-select>
|
|
110
|
+
`
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
get confirmActionTemplate() {
|
|
114
|
+
return html`
|
|
115
|
+
<dile-confirm
|
|
116
|
+
id="elconfirm"
|
|
117
|
+
cancelLabel="Cancelar"
|
|
118
|
+
acceptLabel="Aceptar"
|
|
119
|
+
@dile-confirm-accepted=${this.doAction}
|
|
120
|
+
>
|
|
121
|
+
<div class="modalcontainer">
|
|
122
|
+
${this.formActionsTemplate(this.selection)}
|
|
123
|
+
</div>
|
|
124
|
+
</dile-confirm>
|
|
125
|
+
`;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
setActionIds(items) {
|
|
129
|
+
this.actionIds = items;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
selectorChanged(e) {
|
|
133
|
+
this.selection = e.target.value;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
showAction() {
|
|
137
|
+
this.confirmElement.open();
|
|
138
|
+
let listOptions = this.shadowRoot.querySelector('dile-crud-list-options');
|
|
139
|
+
if (listOptions) {
|
|
140
|
+
listOptions.close()
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
get selectedActionForm() {
|
|
145
|
+
return this.shadowRoot.querySelector(`[action="${this.selection}"]`)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
doAction() {
|
|
149
|
+
let actionData = this.selectedActionForm.getData();
|
|
150
|
+
this.ajaxAction.data = {
|
|
151
|
+
type: this.selection,
|
|
152
|
+
relatedIds: this.actionIds,
|
|
153
|
+
data: actionData,
|
|
154
|
+
};
|
|
155
|
+
this.ajaxAction.generateRequest();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
doSuccessAction(e) {
|
|
159
|
+
this.selectedActionForm.resetData();
|
|
160
|
+
this.dispatchEvent(new CustomEvent('crud-action-success', {
|
|
161
|
+
bubbles: true,
|
|
162
|
+
composed: true,
|
|
163
|
+
detail: {
|
|
164
|
+
msg: e.detail.message,
|
|
165
|
+
action: e.detail.data.action,
|
|
166
|
+
data: e.detail.data.data,
|
|
167
|
+
}
|
|
168
|
+
}));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
doErrorAction(e) {
|
|
172
|
+
this.dispatchEvent(new CustomEvent('crud-action-error', {
|
|
173
|
+
bubbles: true,
|
|
174
|
+
composed: true,
|
|
175
|
+
detail: {
|
|
176
|
+
msg: e.detail,
|
|
177
|
+
}
|
|
178
|
+
}));
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { LitElement, html, css } from 'lit';
|
|
2
|
+
import { DileForm } from '@dile/ui/mixins/form';
|
|
3
|
+
|
|
4
|
+
export class DileCrudDeleteAction extends DileForm(LitElement) {
|
|
5
|
+
static styles = [
|
|
6
|
+
css`
|
|
7
|
+
:host {
|
|
8
|
+
display: block;
|
|
9
|
+
}
|
|
10
|
+
`
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
render() {
|
|
14
|
+
return html`
|
|
15
|
+
<p>Are you sure you want to delete those items?</p>
|
|
16
|
+
`;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { LitElement, html, css } from 'lit';
|
|
2
|
+
import { DileCrudActions } from './DileCrudActions.js';
|
|
3
|
+
import '@dile/ui/components/box-selector/box-selector.js';
|
|
4
|
+
import '@dile/ui/components/box-selector/box-selector-item.js';
|
|
5
|
+
import '@dile/ui/components/card/card.js';
|
|
6
|
+
|
|
7
|
+
export class DileCrudSingleActions extends DileCrudActions {
|
|
8
|
+
static styles = [
|
|
9
|
+
super.styles,
|
|
10
|
+
css`
|
|
11
|
+
:host {
|
|
12
|
+
display: block;
|
|
13
|
+
--dile-box-selector-item-text-align: center;
|
|
14
|
+
--dile-box-selector-item-font-weight: bold;
|
|
15
|
+
}
|
|
16
|
+
dile-card {
|
|
17
|
+
--dile-card-border: none;
|
|
18
|
+
margin-bottom: 1.5rem;
|
|
19
|
+
--dile-card-background-color: var(--dile-secondary-light-color);
|
|
20
|
+
}
|
|
21
|
+
.action-list {
|
|
22
|
+
display: grid;
|
|
23
|
+
grid-template-columns: 1fr 1fr;
|
|
24
|
+
grid-gap: 1rem;
|
|
25
|
+
align-items: stretch;
|
|
26
|
+
}
|
|
27
|
+
dile-box-selector-item:hover {
|
|
28
|
+
--dile-box-selector-item-background-color: var(--dile-primary-light-color);
|
|
29
|
+
--dile-box-selector-item-color: var(--dile-on-primary-light-color);
|
|
30
|
+
--dile-box-selector-item-border: 4px solid var(--dile-primary-color);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
`
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
static get properties() {
|
|
37
|
+
return {
|
|
38
|
+
actions: { type: Array },
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
constructor() {
|
|
43
|
+
super();
|
|
44
|
+
this.actions = [];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
get actionListTemplate() {
|
|
48
|
+
return html`
|
|
49
|
+
${this.actions.length > 0
|
|
50
|
+
? html`
|
|
51
|
+
<dile-card title="Element action">
|
|
52
|
+
<dile-box-selector
|
|
53
|
+
class="action-list"
|
|
54
|
+
attrForSelected="name"
|
|
55
|
+
@dile-selected-changed="${this.onActionSelected}"
|
|
56
|
+
>
|
|
57
|
+
${this.actions.map(action => html`
|
|
58
|
+
<dile-box-selector-item
|
|
59
|
+
label="${action.label}"
|
|
60
|
+
name="${action.name}"
|
|
61
|
+
></dile-box-selector-item>
|
|
62
|
+
`)}
|
|
63
|
+
</dile-box-selector>
|
|
64
|
+
</dile-card>
|
|
65
|
+
`
|
|
66
|
+
: ''
|
|
67
|
+
}
|
|
68
|
+
`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
onActionSelected(e) {
|
|
74
|
+
this.selection = e.detail.selected;
|
|
75
|
+
this.showAction();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { DileAjax } from './src/DileAjax.js';
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { LitElement, html } from 'lit';
|
|
2
|
+
import { DileAxios } from '../../../lib/DileAxios.js';
|
|
3
|
+
|
|
4
|
+
export class DileAjax extends DileAxios(LitElement) {
|
|
5
|
+
static get properties() {
|
|
6
|
+
return {
|
|
7
|
+
data: { type: Object },
|
|
8
|
+
method: { type: String },
|
|
9
|
+
url: { type: String },
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
constructor() {
|
|
14
|
+
super();
|
|
15
|
+
this.data = {};
|
|
16
|
+
this.method = 'post';
|
|
17
|
+
this.url = '';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
generateRequest() {
|
|
21
|
+
let request;
|
|
22
|
+
switch(this.method.toLowerCase().trim()) {
|
|
23
|
+
case 'post':
|
|
24
|
+
request = this.axiosInstance.post(this.url, this.data);
|
|
25
|
+
break;
|
|
26
|
+
case 'get':
|
|
27
|
+
request = this.axiosInstance.get(this.url, {
|
|
28
|
+
params: this.data
|
|
29
|
+
});
|
|
30
|
+
break;
|
|
31
|
+
case 'put':
|
|
32
|
+
request = this.axiosInstance.put(this.url, this.data);
|
|
33
|
+
break;
|
|
34
|
+
case 'delete':
|
|
35
|
+
request = this.axiosInstance.delete(this.url, this.data);
|
|
36
|
+
break;
|
|
37
|
+
case 'patch':
|
|
38
|
+
request = this.axiosInstance.patch(this.url, this.data);
|
|
39
|
+
break
|
|
40
|
+
}
|
|
41
|
+
request.then((response) => {
|
|
42
|
+
if(response.status == 200) {
|
|
43
|
+
let res = response.data;
|
|
44
|
+
if(res.error) {
|
|
45
|
+
this.dispatchError(res.data);
|
|
46
|
+
} else if (res.error === false) {
|
|
47
|
+
this.dispatchEvent(new CustomEvent('ajax-success', {
|
|
48
|
+
detail: res.data
|
|
49
|
+
}));
|
|
50
|
+
} else {
|
|
51
|
+
this.dispatchEvent(new CustomEvent('ajax-success', {
|
|
52
|
+
detail: res
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
} else {
|
|
56
|
+
this.dispatchError('Bad server response');
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
.catch(err => {
|
|
60
|
+
this.describeError(err);
|
|
61
|
+
})
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
describeError(err) {
|
|
65
|
+
if (err.response) {
|
|
66
|
+
const status = err.response.status;
|
|
67
|
+
switch (status) {
|
|
68
|
+
case 422:
|
|
69
|
+
case 400:
|
|
70
|
+
this.dispatchError(err.response.data.message, err.response.data.errors);
|
|
71
|
+
break;
|
|
72
|
+
case 404:
|
|
73
|
+
if(err.response.data.message) {
|
|
74
|
+
this.dispatchError(err.response.data.message);
|
|
75
|
+
} else {
|
|
76
|
+
this.dispatchError("Not found error");
|
|
77
|
+
}
|
|
78
|
+
break;
|
|
79
|
+
case 401:
|
|
80
|
+
this.dispatchError('Unauthorized. Your session may have expired');
|
|
81
|
+
break;
|
|
82
|
+
case 419:
|
|
83
|
+
this.dispatchError('Your session has expired. Please refresh the page');
|
|
84
|
+
break;
|
|
85
|
+
case 502:
|
|
86
|
+
this.dispatchError('Connection error, invalid gateway');
|
|
87
|
+
break;
|
|
88
|
+
case 504:
|
|
89
|
+
this.dispatchError('Connection timeout with the gateway');
|
|
90
|
+
break;
|
|
91
|
+
default:
|
|
92
|
+
this.dispatchError('Action not completed due to a server error');
|
|
93
|
+
}
|
|
94
|
+
} else {
|
|
95
|
+
this.dispatchError('No response received from the server');
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
dispatchError(message, errors = []) {
|
|
100
|
+
this.dispatchEvent(new CustomEvent('ajax-error', {
|
|
101
|
+
detail: {
|
|
102
|
+
message,
|
|
103
|
+
errors
|
|
104
|
+
}
|
|
105
|
+
}));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { DileAjaxForm } from './src/DileAjaxForm.js';
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { LitElement, html, css } from 'lit';
|
|
2
|
+
import '../../ajax/ajax.js';
|
|
3
|
+
import '@dile/ui/components/button/button.js';
|
|
4
|
+
import '@dile/ui/components/inline-feedback/inline-feedback.js';
|
|
5
|
+
import {capitalizeFirstLetter} from '../../../lib/capitalizeString.js';
|
|
6
|
+
import { ResponseApiAdapter } from '../../../lib/ResponseApiAdapter.js';
|
|
7
|
+
|
|
8
|
+
export class DileAjaxForm extends LitElement {
|
|
9
|
+
static styles = [
|
|
10
|
+
css`
|
|
11
|
+
:host {
|
|
12
|
+
display: block;
|
|
13
|
+
}
|
|
14
|
+
:host([small]) {
|
|
15
|
+
--dile-button-padding-y: 0.25rem;
|
|
16
|
+
--dile-button-padding-x: 0.5rem;
|
|
17
|
+
--dile-button-font-size: 0.875rem;
|
|
18
|
+
}
|
|
19
|
+
.actions {
|
|
20
|
+
margin-top: var(--dile-ajax-form-actions-margin-top, 1.2rem);
|
|
21
|
+
padding-left: 3px;
|
|
22
|
+
}
|
|
23
|
+
`
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
static get properties() {
|
|
27
|
+
return {
|
|
28
|
+
operation: { type: String },
|
|
29
|
+
endpoint: { type: String },
|
|
30
|
+
actionLabel: { type: String },
|
|
31
|
+
data: { type: Object },
|
|
32
|
+
relatedId: { type: String },
|
|
33
|
+
loadOnInit: { type: Boolean },
|
|
34
|
+
buttonSmall: { type: Boolean, reflect: true },
|
|
35
|
+
formIdentifier: { type: String, },
|
|
36
|
+
setDataOnInit: { type: Boolean },
|
|
37
|
+
responseAdapter: { type: Object },
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
constructor() {
|
|
42
|
+
super();
|
|
43
|
+
this.responseAdapter = new ResponseApiAdapter();
|
|
44
|
+
this.formIdentifier = 'form';
|
|
45
|
+
this.operation = '';
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
updated(changedProperties) {
|
|
49
|
+
if (changedProperties.has('relatedId') || changedProperties.has('endpoint')) {
|
|
50
|
+
if(this.loadOnInit) {
|
|
51
|
+
this.loadData();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
firstUpdated() {
|
|
57
|
+
this.feedback = this.shadowRoot.getElementById('feedback');
|
|
58
|
+
this.ajaxsave = this.shadowRoot.getElementById('ajaxsave');
|
|
59
|
+
this.ajaxget = this.shadowRoot.getElementById('ajaxget');
|
|
60
|
+
if(this.setDataOnInit) {
|
|
61
|
+
this.initData();
|
|
62
|
+
}
|
|
63
|
+
if(!this.actionLabel) {
|
|
64
|
+
this.actionLabel = capitalizeFirstLetter(this.operation);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get form() {
|
|
69
|
+
return this.querySelector('#' + this.formIdentifier);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
render() {
|
|
73
|
+
return html`
|
|
74
|
+
${this.ajaxComponents}
|
|
75
|
+
<slot></slot>
|
|
76
|
+
<dile-inline-feedback id="feedback"></dile-inline-feedback>
|
|
77
|
+
<div class="actions">
|
|
78
|
+
<dile-button @click=${this.doAction}>${this.actionLabel}</dile-button>
|
|
79
|
+
</div>
|
|
80
|
+
`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
get ajaxComponents() {
|
|
84
|
+
return html`
|
|
85
|
+
<dile-ajax
|
|
86
|
+
id="ajaxget"
|
|
87
|
+
method="get"
|
|
88
|
+
url="${this.endpoint}/${this.relatedId}"
|
|
89
|
+
@ajax-success="${this.doSuccessGet}"
|
|
90
|
+
@ajax-error="${this.doErrorGet}"
|
|
91
|
+
></dile-ajax>
|
|
92
|
+
<dile-ajax
|
|
93
|
+
id="ajaxsave"
|
|
94
|
+
method="${this.saveMethod(this.operation)}"
|
|
95
|
+
url="${this.endpoint}${this.operation == 'insert' ? '' : `/${this.relatedId}`}"
|
|
96
|
+
@ajax-success="${this.doSuccessSave}"
|
|
97
|
+
@ajax-error="${this.doErrorSave}"
|
|
98
|
+
></dile-ajax>
|
|
99
|
+
`
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
loadData() {
|
|
103
|
+
this.ajaxget.generateRequest();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
doAction() {
|
|
107
|
+
this.feedback.clear();
|
|
108
|
+
this.ajaxsave.data = this.form.getData();
|
|
109
|
+
this.ajaxsave.generateRequest();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
doErrorGet(e) {
|
|
113
|
+
this.responseAdapter.setResponse(e.detail);
|
|
114
|
+
let msg = this.customMessage(false);
|
|
115
|
+
this.feedback.negativeFeedback(msg);
|
|
116
|
+
this.dispatchEvent(new CustomEvent('dile-ajax-form-get-error', {
|
|
117
|
+
bubbles: true,
|
|
118
|
+
composed: true,
|
|
119
|
+
detail: {
|
|
120
|
+
msg: msg,
|
|
121
|
+
previousDetail: e.detail
|
|
122
|
+
}
|
|
123
|
+
}));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
doSuccessGet(e) {
|
|
127
|
+
this.responseAdapter.setResponse(e.detail);
|
|
128
|
+
let data = this.responseAdapter.getData()
|
|
129
|
+
this.form.setData(data);
|
|
130
|
+
this.form.clearErrors();
|
|
131
|
+
this.feedback.clear();
|
|
132
|
+
this.dispatchEvent(new CustomEvent('dile-ajax-form-loaded', {
|
|
133
|
+
bubbles: true,
|
|
134
|
+
composed: true,
|
|
135
|
+
detail: {
|
|
136
|
+
data,
|
|
137
|
+
previousDetail: e.detail
|
|
138
|
+
}
|
|
139
|
+
}));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
doErrorSave(e) {
|
|
143
|
+
this.responseAdapter.setResponse(e.detail);
|
|
144
|
+
let msg = this.customMessage(true);
|
|
145
|
+
this.feedback.negativeFeedbackWithDelay(msg, 5000);
|
|
146
|
+
let validationErrors = this.responseAdapter.getValidationErrors();
|
|
147
|
+
this.form.showErrors(validationErrors);
|
|
148
|
+
this.dispatchEvent(new CustomEvent('save-error', {
|
|
149
|
+
bubbles: true,
|
|
150
|
+
composed: true,
|
|
151
|
+
detail: {
|
|
152
|
+
msg,
|
|
153
|
+
validationErrors,
|
|
154
|
+
previousDetail: e.detail
|
|
155
|
+
}
|
|
156
|
+
}));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
doSuccessSave(e) {
|
|
160
|
+
this.responseAdapter.setResponse(e.detail);
|
|
161
|
+
let data = this.responseAdapter.getData();
|
|
162
|
+
let msg = this.customMessage(true);
|
|
163
|
+
this.feedback.positiveFeedbackWithDelay(msg, 5000);
|
|
164
|
+
this.dispatchEvent(new CustomEvent('save-success', {
|
|
165
|
+
bubbles: true,
|
|
166
|
+
composed: true,
|
|
167
|
+
detail: {
|
|
168
|
+
data,
|
|
169
|
+
msg,
|
|
170
|
+
previousDetail: e.detail
|
|
171
|
+
}
|
|
172
|
+
}));
|
|
173
|
+
if(this.operation == 'insert') {
|
|
174
|
+
this.form.clearData();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
saveMethod(operation) {
|
|
179
|
+
switch(operation) {
|
|
180
|
+
case 'insert':
|
|
181
|
+
return 'post';
|
|
182
|
+
case 'update':
|
|
183
|
+
return 'put';
|
|
184
|
+
}
|
|
185
|
+
throw "Operation not supported in fct-ajax-form use 'insert' or 'update'";
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
clearErrors() {
|
|
189
|
+
this.form.clearErrors();
|
|
190
|
+
this.feedback.clear();
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
clearFeedback() {
|
|
194
|
+
this.feedback.clear();
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
initData() {
|
|
198
|
+
if(this.form && this.form.setData) {
|
|
199
|
+
setTimeout(() => this.form.setData(this.data), 300);
|
|
200
|
+
} else {
|
|
201
|
+
setTimeout(this.initData.bind(this), 100);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
customMessage(success) {
|
|
206
|
+
let message = this.responseAdapter.getMessage();
|
|
207
|
+
if(message) {
|
|
208
|
+
return message;
|
|
209
|
+
}
|
|
210
|
+
if(success) {
|
|
211
|
+
return `Success ${this.operation}`;
|
|
212
|
+
}
|
|
213
|
+
return `Error ${this.operation}`;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { DileAjaxSelectCrud } from './src/DileAjaxSelectCrud.js';
|