@dile/crud 0.0.69 → 0.1.0

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.
@@ -0,0 +1,2 @@
1
+ export { DileRelationChecker } from './src/DileRelationChecker.js';
2
+ export { DileRelationCheckerItem } from './src/DileRelationCheckerItem.js';
@@ -0,0 +1,3 @@
1
+ import { DileRelationCheckerItem } from './src/DileRelationCheckerItem.js';
2
+
3
+ customElements.define('dile-relation-checker-item', DileRelationCheckerItem);
@@ -0,0 +1,3 @@
1
+ import { DileRelationChecker } from './src/DileRelationChecker.js';
2
+
3
+ customElements.define('dile-relation-checker', DileRelationChecker);
@@ -0,0 +1,193 @@
1
+ import { LitElement, html, css } from 'lit';
2
+ import {ifDefined} from 'lit/directives/if-defined.js';
3
+ import '@dile/ui/components/input/input-search.js';
4
+ import '../relation-checker-item.js';
5
+ import '../../ajax/ajax.js';
6
+
7
+ export class DileRelationChecker extends LitElement {
8
+ static styles = [
9
+ css`
10
+ :host {
11
+ display: block;
12
+ }
13
+ dile-input-search {
14
+ margin-bottom: 1rem;
15
+ }
16
+ h2 {
17
+ font-size: var(--relation-checker-title-font-size, 1.25rem);
18
+ font-weight: var(--relation-checker-title-font-weight, bold);
19
+ color: #303030(--relation-checker-title-color, #303030);
20
+ }
21
+ `
22
+ ];
23
+
24
+ static get properties() {
25
+ return {
26
+ /**
27
+ * Endpoint to receive the list of items that can be checked, with their status
28
+ * Receives all items, indicating which ones are checked
29
+ * { label, id, checked }
30
+ */
31
+ endpointGet: { type: String },
32
+
33
+ /**
34
+ * Route to toggle the checked/unchecked status of an item
35
+ * It will send an objetc { id, checked }
36
+ */
37
+ endpointCheck: { type: String},
38
+
39
+ /**
40
+ * Array of items received for potential relationships
41
+ */
42
+ items: { type: Array },
43
+
44
+ /**
45
+ * Title label for the box
46
+ */
47
+ label: { type: String },
48
+
49
+ /**
50
+ * Keyword for the filter
51
+ */
52
+ keyword: { type: String },
53
+
54
+ /**
55
+ * Function to get the items for the relations
56
+ */
57
+ getRelationItems: { type: Object },
58
+
59
+ /**
60
+ * Tooltip compute function
61
+ */
62
+ computeTooltip: { type: Object },
63
+ };
64
+ }
65
+
66
+ constructor() {
67
+ super();
68
+ this.items = [];
69
+ this.getRelationItems = (response) => response.data;
70
+ this.computeTooltip = (item) => null;
71
+ }
72
+
73
+ render() {
74
+ return html`
75
+ ${this.ajaxTemplate}
76
+ ${this.label
77
+ ? html`<h2>${this.label}</h2>`
78
+ : ''
79
+ }
80
+ <dile-input-search
81
+ @dile-input-search=${this.searchHandler}
82
+ @dile-input-search-cleared=${this.clearKeywordHandler}
83
+ ></dile-input-search>
84
+ ${this.computedItems(this.items, this.keyword).map( item => html`
85
+ <dile-relation-checker-item
86
+ .item=${item}
87
+ @relation-item-changed="${this.relationItemChanged}"
88
+ tooltip=${ifDefined(this.computeTooltip(item))}
89
+ ></dile-relation-checker-item>
90
+ `)}
91
+ `;
92
+ }
93
+
94
+ firstUpdated() {
95
+ this.ajaxsave = this.shadowRoot.getElementById('ajaxsave');
96
+ this.ajaxget = this.shadowRoot.getElementById('ajaxget');
97
+ this.refresh();
98
+ }
99
+
100
+ get ajaxTemplate() {
101
+ return html`
102
+ <dile-ajax
103
+ id="ajaxget"
104
+ method="get"
105
+ url="${this.endpointGet}"
106
+ @ajax-success="${this.doSuccessGet}"
107
+ @ajax-error="${this.doErrorGet}"
108
+ ></dile-ajax>
109
+ <dile-ajax
110
+ id="ajaxsave"
111
+ method="post"
112
+ url="${this.endpointCheck}"
113
+ @ajax-success="${this.doSuccessSave}"
114
+ @ajax-error="${this.doErrorSave}"
115
+ ></dile-ajax>
116
+ `
117
+ }
118
+
119
+ relationItemChanged(e) {
120
+ this.save(e.detail.item.id, e.detail.checked);
121
+ }
122
+
123
+ save(name, checked) {
124
+ this.ajaxsave.data = {
125
+ id: name,
126
+ checked: checked,
127
+ };
128
+ this.ajaxsave.generateRequest();
129
+ }
130
+
131
+ doSuccessGet(e) {
132
+ this.items = this.getRelationItems(e.detail);
133
+ this.dispatchEvent(new CustomEvent('relation-data-recived', {
134
+ bubbles: true,
135
+ composed: true,
136
+ detail: {
137
+ items: this.items
138
+ }
139
+ }));
140
+ }
141
+
142
+ doErrorGet() {
143
+ this.customDispatch('relation-error-get');
144
+ }
145
+
146
+ doSuccessSave() {
147
+ this.customDispatch('relation-success-save');
148
+ this.refresh();
149
+ }
150
+
151
+ refresh() {
152
+ this.updateComplete.then(() => this.ajaxget.generateRequest());
153
+ }
154
+
155
+ doErrorSave() {
156
+ this.customDispatch('relation-error-save');
157
+ }
158
+
159
+ customDispatch(eventName) {
160
+ this.dispatchEvent(new CustomEvent(eventName, {
161
+ bubbles: true,
162
+ composed: true,
163
+ }));
164
+ }
165
+
166
+ doCheckItem(ele) {
167
+ this.items = this.items.map(item => {
168
+ if(item.id == ele.id) {
169
+ return {
170
+ ...item,
171
+ checked: true
172
+ }
173
+ }
174
+ return item;
175
+ })
176
+ this.save(ele.id, true);
177
+ }
178
+
179
+ searchHandler(e) {
180
+ this.keyword = e.detail.keyword;
181
+ }
182
+
183
+ clearKeywordHandler() {
184
+ this.keyword = undefined;
185
+ }
186
+
187
+ computedItems(items, keyword) {
188
+ if(!keyword) {
189
+ return items;
190
+ }
191
+ return items.filter( item => item.label.toLowerCase().indexOf(keyword.toLowerCase()) != -1);
192
+ }
193
+ }
@@ -0,0 +1,69 @@
1
+ import { LitElement, html, css } from 'lit';
2
+ import '@dile/ui/components/checkbox/checkbox.js';
3
+ import '@dile/ui/components/tooltip/chip-tooltip';
4
+
5
+ export class DileRelationCheckerItem extends LitElement {
6
+ static styles = [
7
+ css`
8
+ :host {
9
+ display: block;
10
+ --dile-checkbox-size: var(--relation-checker-checkbox-size, 24px);
11
+ }
12
+ div.item {
13
+ display: flex;
14
+ align-items: center;
15
+ margin: 0 0 0.6rem;
16
+ padding-bottom: 0.1rem;
17
+ border-bottom: 1px solid #ddd;
18
+ }
19
+ div.grow {
20
+ flex-grow: 1;
21
+ }
22
+ dile-chip-tooltip {
23
+ margin-right: 0.5rem;
24
+ }
25
+ .onlyicon {
26
+ --dile-chip-tooltip-padding: 0.15rem;
27
+ --dile-chip-tooltip-icon-size: var(--relation-checker-tooltip-icon-size, 18px);
28
+ }
29
+ `
30
+ ];
31
+
32
+ static get properties() {
33
+ return {
34
+ item: { type: Object },
35
+ tooltip: { type: String },
36
+ };
37
+ }
38
+
39
+ render() {
40
+ return html`
41
+ <div class="item">
42
+ <div class="grow">
43
+ <dile-checkbox
44
+ name="${this.item.id}"
45
+ @dile-checkbox-changed=${this.checkboxChanged}
46
+ ?checked=${this.item.checked}
47
+ >${this.item.label}</dile-checkbox>
48
+ </div>
49
+ ${this.tooltip
50
+ ? html`<dile-chip-tooltip class="onlyicon" message="${this.tooltip}" position="left"></dile-chip-tooltip>`
51
+ : ''
52
+ }
53
+ </div>
54
+ `;
55
+ }
56
+
57
+ checkboxChanged(e) {
58
+ e.stopPropagation();
59
+ this.dispatchEvent(new CustomEvent('relation-item-changed', {
60
+ bubbles: true,
61
+ composed: true,
62
+ detail: {
63
+ item: this.item,
64
+ checked: e.detail.checked
65
+ }
66
+ }));
67
+ }
68
+
69
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dile/crud",
3
- "version": "0.0.69",
3
+ "version": "0.1.0",
4
4
  "description": "Components to create a generic crud system based on Web Components and Lit",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -31,5 +31,5 @@
31
31
  "publishConfig": {
32
32
  "access": "public"
33
33
  },
34
- "gitHead": "2800729c7bdb8f152e5a693851b323916feb9e79"
34
+ "gitHead": "38268390b55fa3727c2b606cedbded8ca690ad8f"
35
35
  }