-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathTextSearchView.js
More file actions
173 lines (160 loc) · 4.25 KB
/
TextSearchView.js
File metadata and controls
173 lines (160 loc) · 4.25 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
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
/**
* @license
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
foam.CLASS({
package: 'foam.u2.search',
name: 'TextSearchView',
extends: 'foam.u2.View',
requires: [
'foam.comics.SearchMode',
'foam.parse.SimpleQueryParser',
'foam.u2.tag.Input'
],
implements: [
'foam.mlang.Expressions'
],
imports: [
'config?'
],
messages: [
{ name: 'LABEL_SEARCH', message: 'Search' }
],
properties: [
{
class: 'Class',
name: 'of'
},
{
class: 'Boolean',
name: 'richSearch',
value: true
},
{
class: 'Enum',
of: 'foam.comics.SearchMode',
name: 'searchMode',
factory: function() {
return this.config?.searchMode ?? foam.comics.SearchMode.FULL;
}
},
{
class: 'Boolean',
name: 'checkStrictEquality',
documentation: `
Set this flag if you want to match by strict equality instead of
checking if the text contains the string. Doing so should improve
performance.
`
},
{
name: 'queryParser',
factory: function() {
return this.SimpleQueryParser.create({ of: this.of });
}
},
{
class: 'Int',
name: 'width',
value: 80
},
'property',
{
name: 'predicate',
factory: function() { return this.TRUE; }
},
{
name: 'view'
},
{
name: 'label',
expression: function(property) {
return property && property.label ? property.label : this.LABEL_SEARCH;
}
},
{
// All search views (in the SearchManager) need a name.
// This defaults to 'textSearchView'.
name: 'name',
value: 'textSearchView'
},
{
class: 'Boolean',
name: 'onKey'
},
{
class: 'String',
name: 'searchData'
}
],
methods: [
function render() {
this.__subContext__.register(foam.u2.SearchField, 'foam.u2.TextField');
let viewSpec = {
class: 'foam.parse.auto.SmartView',
parser: this.queryParser
};
// value: { class: 'foam.u2.SearchField' }
this
.addClass(this.myClass())
.start(viewSpec, {
alwaysFloatLabel: true,
label$: this.label$,
ariaLabel$: this.label$,
onKey: this.onKey,
mode$: this.mode$,
placeholder$: this.searchMode$.map(s => s == 'MQL' ? 'MQL Search...' : 'Search...')
}, this.view$)
.attrs({ name: this.name$ })
.end();
this.view.data$.sub(this.updateValue);
this.view.data$.follow(this.searchData$);
this.updateValue();
},
function clear() {
this.view.data = '';
this.predicate = this.TRUE;
}
],
listeners: [
{
name: 'updateValue',
isIdled: true,
delay: 500,
code: function() {
var value = this.searchData = this.view.data;
if ( ! value ) {
this.predicate = this.True.create();
return;
}
// TODO: dont think we ever use anything other than richSearch, maybe remove the boolean and only perform richSearch
if ( this.richSearch ) {
var mql = value.indexOf(':') != -1 && this.queryParser.parseString(value);
if ( this.searchMode === this.SearchMode.MQL || mql ) {
this.predicate = mql || this.FALSE;
} else {
this.predicate = this.KEYWORD(value);
}
} else if ( this.checkStrictEquality ) {
this.predicate = this.EQ(this.property, value);
} else if ( this.searchMode === this.SearchMode.SIMPLE ) {
this.predicate = this.CONTAINS_IC(this.property, value);
} else {
this.predicate = this.False.create();
}
}
}
]
});