-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmostly-filter.html
240 lines (217 loc) · 5.64 KB
/
mostly-filter.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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="behaviors/mostly-filters-behavior.html">
<script>
/**
* Stamps the template if and only if all of its conditions are met.
*
* Evaluation context includes `document` and `user` which need to be set if used.
*
* Example:
*
* <mostly-filter document="[[document]]" type="Picture"
* user="[[user]]" group="Administrator">
* <template>
* ...
* </template>
* </mostly-filter>
*
* @demo demo/mostly-filter/index.html
*/
Polymer({
is: 'mostly-filter',
properties: {
/**
* The context document
*/
document: {
type: Object,
value: {}
},
/**
* The context user
*/
user: {
type: Object,
value: {}
},
/**
* Document has one of these types
*/
type: {
type: String,
value: '',
_filter: {
ctx: ['document'], // the context made available to the filter function
fn: 'hasType', // the filter function name
multiple: true // flag to indicate if the property takes csv values
}
},
/**
* Document has one of these facets
*/
facet: {
type: String,
value: '',
_filter: {
ctx: ['document'],
fn: 'hasFacet',
multiple: true
}
},
/**
* Document state
*/
state: {
type: String,
value: '',
_filter: {
ctx: ['document'],
fn: 'hasState',
multiple: true
}
},
/**
* Document path starts with
*/
path: {
type: String,
value: '',
_filter: {
ctx: ['document'],
fn: 'pathStartsWith'
}
},
/**
* User has one of these permissions in the document
*/
permission: {
type: String,
value: '',
_filter: {
ctx: ['document'],
fn: 'hasPermission',
multiple: true
}
},
/**
* Javascript expression to evaluate
*/
expression: {
type: String,
value: '',
_filter: {
ctx: ['document', 'user'],
fn: '_evaluate'
}
},
/**
* User is member of one of these groups
*/
group: {
type: String,
value: '',
_filter: {
ctx: ['user'],
fn: 'isMember',
multiple: true
}
}
},
behaviors: [
Polymer.Templatizer, Mostly.FiltersBehavior
],
observers: [
'_update(document, user, facet, type, state, path, permission, expression, group)'
],
_evaluate: function(document, user, expression) {
try {
var fn = new Function(['document', 'user'], 'return ' + expression + ';');
return fn.apply(this, [document, user]);
} catch (err) {
return false;
}
},
// Evaluate the filter
check: function() {
for (var k in this.properties) {
var v = this[k],
filter = this.properties[k]._filter;
if (v && filter) {
var args = filter.ctx.map(arg => {
return this[arg];
});
// if filter supports multiple values apply the function to each one
var values = filter.multiple? v.trim().split(/\s*,\s*/) : [v];
var fn = this[filter.fn];
// pass if any check returns true, basically Array.some()
var pass = false;
for (var i = 0; i < values.length; i++) {
if ((pass = fn.apply(this, args.concat(values[i])))) {
break;
}
}
// if any of the filters fail the check fails
if (!pass) {
return false;
}
}
}
return true;
},
_update: function() {
this.__renderDebouncer = Polymer.Debouncer.debounce(
this.__renderDebouncer,
Polymer.Async.microTask,
() => {
this.check()? this._render() : this._clear();
});
// enqueuing is needed for testing, as it allows content to be stamped on flush
Polymer.enqueueDebouncer(this.__renderDebouncer);
},
_render: function() {
if (this._instance) {
return;
}
var parentNode = Polymer.dom(this).parentNode;
if (parentNode) {
var template = Polymer.dom(this).querySelector('template');
var parent = Polymer.dom(parentNode);
if (template) {
delete template.__templatizeOwner;
this.templatize(template);
this._instance = this.stamp();
for (var prop in this.properties) {
this._instance._setPendingProperty(prop, this[prop]);
}
this._instance._flushProperties();
var root = this._instance.root;
parent.insertBefore(root, this);
}
}
},
attached: function() {
this._update();
},
detached: function() {
this._clear();
},
_clear: function() {
if (this._instance) {
var c$ = this._instance.children;
if (c$ && c$.length) {
// use first child parent, for case when dom-if may have been detached
var parent = Polymer.dom(Polymer.dom(c$[0]).parentNode);
for (var i=0, n; (i<c$.length) && (n=c$[i]); i++) {
parent.removeChild(n);
}
}
this._instance = null;
}
},
_forwardHostPropV2: function(prop, value) {
if (this._instance) {
this._instance.forwardHostProp(prop, value);
}
}
});
</script>