forked from ActindoElements/actindo-grid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactindo-grid-filter-form.html
258 lines (237 loc) · 8.48 KB
/
actindo-grid-filter-form.html
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
<dom-module id="actindo-grid-filter-form">
<template>
<style>
.filter-form
{
display: flex;
flex-direction: row;
align-items: flex-end;
}
.btn-add
{
background-color: var(--actindo-primary);
color: var(--actindo-light-grey-light);
height: 35px;
padding: 0px 25px;
margin-bottom: 7px;
margin-left: 10px;
}
actindo-combobox
{
margin-right: 10px;
}
</style>
<div class="filter-form">
<template is="dom-if" if="[[columns.length]]">
<actindo-combobox
label="Column"
items="[[columns]]"
display-field="label"
value-field="field"
selected-item="{{_selectedColumn}}"
value="{{property}}"
disabled="[[editing]]"
allow-deselect
>
</actindo-combobox>
</template>
<template is="dom-if" if="[[_selectedColumn.type]]">
<actindo-combobox
label="Relation"
items="[[_operators]]"
display-field="name"
value-field="value"
value="{{operator}}"
>
</actindo-combobox>
</template>
<div hidden="[[!operator]]">
<div id="inputFilter"></div>
</div>
<paper-button class="btn-add" on-click="_save" raised>
<span>Add Filter</span>
</paper-button>
</div>
</template>
<script>
class ActindoGridFilterForm extends Polymer.Element {
static get is() { return 'actindo-grid-filter-form'; }
static get properties() {
return {
/**
* Whether is editing or not
*/
editing: {
type: Boolean,
value: false,
},
/**
* The status of the filter
*/
active: {
type: Boolean,
value: true
},
/**
* contains all the columns has filterable property equal to true
*/
columns: {
type: Array,
},
/**
* contains the filter criteria, the key "property" contains the column value,
* the field "operator" contains operator value(=,>,like,etc...), the field "value" contains the value need to be filtered
* {property: String, operator: string, value: mixed}
*/
filter: {
type: Object
},
/**
* contains the column value
*/
property: {
type: String,
},
/**
* contains the operator value
*/
operator: {
type: String,
observer: '_onOperatorChanged'
},
/**
* contains the filter value
*/
value: {},
/**
* Internal property
*/
_valueHistory: {
type: Object,
value: function() {
return {}
}
},
/**
* Internal property
*/
_operators: {
type: Array,
value: []
},
/**
* Internal property
*/
_selectedColumn: {
type: Object,
observer: '_onSelectedColumnChanged'
}
};
}
_onSelectedColumnChanged(newColumn, oldColumn)
{
if (!newColumn.type)
{
return;
}
if (oldColumn.type)
{
this.set('operator', null);
}
this.setProperties({
_operators: newColumn.operators
});
}
_onOperatorChanged(opt)
{
if (!opt)
{
return;
}
const inputFilter = this.$.inputFilter;
let {firstChild} = inputFilter;
const column = this._selectedColumn;
const operatorObj = this._operators.find(item => item.value === opt);
this.getInputComponent(column, operatorObj).then(node => {
// Check if in edit mode and first load
if (!firstChild && this.editing)
{
node.value = this.value;
}
// keep if the new input component name is not changed
firstChild = firstChild || {};
if (firstChild.tagName === node.tagName)
{
return;
}
inputFilter.innerHTML = '';
node.addEventListener("value-changed", this._onValueChanged.bind(this));
inputFilter.appendChild(node);
});
}
getInputComponent(column, operator)
{
return new Promise(resolve => {
if (!column.customFilter)
{
const node = column.renderDefaultFilter(column.defaultFilterProperties, operator);
resolve(node);
return;
}
const {importPath, component} = column.customFilter;
Polymer.importHref(importPath, (element) => {
const node = document.createElement(component);
node.element = element.element;
resolve(node);
});
});
}
_onValueChanged(event)
{
// Ignore length array event
if ('value.length' === event.detail.path)
{
return;
}
if (event.detail.path === 'value.splices')
{
this.set('value', event.detail.value.indexSplices[0].object);
return;
}
if (event.detail.path)
{
this.set(event.detail.path, event.detail.value);
return;
}
this.set('value', event.detail.value);
}
_save()
{
let {property, operator, value} = this;
this.dispatchEvent( new CustomEvent("save", {detail: {
property: property,
operator: operator,
value: value,
active: this.active
} }));
if (!this.editing)
{
const inputFilter = this.shadowRoot.querySelector('#inputFilter');
inputFilter.removeChild(inputFilter.firstChild);
}
this.reset();
}
reset()
{
const filter = JSON.parse(JSON.stringify(this.filter || {}));
this.set('operator', '');
this.setProperties({
property: filter.property,
operator: filter.operator,
value: filter.value
});
}
}
customElements.define(ActindoGridFilterForm.is, ActindoGridFilterForm);
</script>
</dom-module>