-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index-prod-slim.html
359 lines (307 loc) · 13.1 KB
/
index-prod-slim.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>vsm-box: 'slim build' demo, connected to a PubDictionaries dictionary</title>
<style>
body { font-family: tahoma, verdana, arial, sans-serif; margin: 0; }
#intro { font-size: 12px; color: #aaa; margin: 10px 0 20px 0; }
#app { padding: 1px 0 18px 50px; background-color: #fff; min-height: 180px; }
[v-cloak] { display: none; }
#msg { color: #aaa; font-size: 11px; height: 12px; margin: 8px 0 2px 0; text-align: right; }
#stateText { outline: none; font-size: 11px; white-space: pre; box-sizing: border-box;
display: block; border: 0.5px solid #b8b8b8; }
#info { padding-left: 10px; }
#info, #info a, #info a:visited { font-size: 12px; color: #888; }
#info a:hover { color: #88f; }
</style>
<!-- Include a vsm-dictionary, and a cacher for speed. -->
<script src="https://unpkg.com/vsm-dictionary-local@^2.6.0/dist/vsm-dictionary-local.min.js"></script>
<script src="https://unpkg.com/vsm-dictionary-cacher@^1.2.0/dist/vsm-dictionary-cacher.min.js"></script>
<!-- Add a demo vsmdictionary for accessing a webservice (not required). -->
<script src="../../vsm-dictionary-remote-demo/dist/vsm-dictionary-remote-demo.min.js"></script>
<!-- Include Vue and webcomponent-supporting libraries, which are required for
the 'slim build' of 'vsm-box'.
With Vue not bundled inside a 'standalone' VsmBox here, it can support
additional Vue-activated elements, such as the `<div id="app">` below. -->
<script src="https://unpkg.com/vue@2.5.22/dist/vue.min.js"></script>
<script src="https://unpkg.com/vue-custom-element@3.2.6/dist/vue-custom-element.js"></script>
<script src="https://unpkg.com/document-register-element@1.13.1/build/document-register-element.js"></script>
<!-- Include the 'slim build' version of 'vsm-box'. -->
<script src="../dist/vsm-box.min.js"></script>
<!-- Include a library for showing VSM JS-objects in a readable way, as JSON5. -->
<script src="https://unpkg.com/vsm-json-pretty@^1.0.0/dist/vsm-json-pretty.min.js"></script>
<!-- Include a library for converting JSON5 back to a JS-object. -->
<script src="https://unpkg.com/json5@^2.1.0/dist/index.min.js"></script>
</head>
<body>
<div id="app" v-cloak>
<p id="intro">
Demo of VsmBox based on 'vsm-box.min.js', which is not bundled together
with Vue or<br>webcomponent-related dependencies, so these are added in
the HTML-head section.
</p>
<vsm-box
:vsm-dictionary.prop="vsmDictionary"
:query-options="queryOptions"
:autofocus="false"
:placeholder="placeholder"
:cycle-on-tab="true"
:max-string-lengths.prop="maxStringLengths"
:initial-value.prop="initialValue"
:fresh-list-delay="0"
:advanced-search.prop="advancedSearch"
:allow-class-null="true"
:sizes.prop="sizes"
:custom-term.prop="customTerm"
@change="onChange"
>
</vsm-box>
<div
id="msg"
:style="{ width: stateTextWidth }"
v-html="msg"
></div>
<textarea
id="stateText"
rows="31"
cols="80"
spellcheck="false"
v-model="stateText"
@keydown.tab.prevent="x => x"
></textarea>
<button @click="onButtonClear">Clear</button>
<button @click="onButtonExample1">Example 1</button>
<button @click="onButtonExample2">Example 2</button>
<span id="info">
Info: <a href="https://vsm.github.io/">vsm.github.io</a>,
<a href="https://github.com/vsm/vsm-box">vsm-box</a>,
<a href="http://scicura.org/vsm/vsm.html">VSM</a>,
<a href="http://scicura.org/vsm/examples.html">examples</a>.
</span>
</div>
<!-- Here one can override vsm-box & vsm-autocomplete's default CSS-styles --
<style> .vsm-box .vsm-autocomplete .item-type-literal { color: #55a; }
</style> <!-- -->
<script>
// Register the webcomponent with Vue. (Both were loaded via a <script>-tag).
Vue.customElement('vsm-box', VsmBox.default);
// Choose a VsmDictionary. It can be any VsmDictionary, after it is added
// in the HTML-head section. Here we demo a VsmDictionaryLocal (which does
// not depend on a term/ID-serving webservice), loaded with either no data,
// or some demo data. We also wrap it in a cacher.
var useDemoData = true;
var dict = new (VsmDictionaryCacher(VsmDictionaryLocal)) (
!useDemoData ? undefined :
{
dictData: [ { id: 'http://d.org/A', name: 'Aaa', entries: [
{ id: 'http://d.org/A/A:01', terms: [{ str: 'aaa' }, { str: 'aad' }] },
{ id: 'http://d.org/A/A:02', terms: [{ str: 'aab' }] },
{ id: 'http://d.org/A/A:03', terms: [{ str: 'abc' }], descr: 'example' }
]}],
refTerms: ['it', 'they'],
delay: 200 // `VsmDictionaryCacher` will speed this up again.
}
);
// ---------- Small demo, overriding the above one:
// Use 1 fixed subdictionary of the PubDictionaries webservice.
// (Requires a correctly resolving <script- tag in the HTML-head section).
//
var usePubDictionaries = true;
if (usePubDictionaries && window.VsmDictionaryRemoteDemo) {
class DictionaryPubDictionaries extends VsmDictionaryRemoteDemo {
constructor(options) {
super(options);
this.urlGetMatches = 'http://pubdictionaries.org' +
'/dictionaries/$filterDictID/prefix_completion?term=$str';
}
getMatchesForString(str, options, cb) {
options = Object.assign({}, options,
{ filter: { dictID: ['GO-BP'] } }
);
super.getMatchesForString(str, options, (err, res) => {
if (err) return cb(err);
var arr = res.items.map(e => e.type ? e : ({
id: e.identifier,
dictID: options.filter.dictID[0],
str: e.label,
type: e.label.startsWith(str) ? 'S' : 'T',
}));
cb(err, { items: arr });
});
}
getDictInfos(options, cb) {
cb(null, { items: [ { id: 'GO-BP', name: 'GO - BP' } ] });
}
}
dict = new (VsmDictionaryCacher(DictionaryPubDictionaries)) ();
}
// ----------
var lastAutoFilledText = '';
// Activate Vue on (only) the #app element and its children.
new Vue({
el: '#app',
data: function() { return {
/* + Note: the _web-component_'s html-attr ":vsm-dictionary.prop=..."
differs from a plain Vue-component's attr ":vsm-dictionary=...".
(See https://karol-f.github.io/vue-custom-element/#/demos/binding).
+ Note 2, however: when using the 'vsm-box' package directly
in another Vue project (which makes it use 'vsm-box.min.js'
as well, because our package.json's 'main'-property defines so),
then our Vue-component is _not_ wrapped into a web-component,
and one can simply use ":data-source=..." there. */
vsmDictionary: dict,
queryOptions: {},
placeholder: 'Type a term or doubleclick for menu',
maxStringLengths: { str: 100, strAndDescr: 120 },
initialValue: {
terms: [ ], conns: [ ]
///terms: [
/// { str: 'a', classID: 'http://d.org/A/A:01', instID: null },
/// { str: 'aa', classID: 'http://d.org/A/A:02', instID: null },
/// { str: 'aaaa', classID: 'http://d.org/A/A:04', instID: null } ],
///conns: [ { type: 'T', pos: [ 0, 1, 2 ] } ]
},
sizes: { minWidth: 240 },
msg: '',
stateText: '',
stateTextWidth: 0,
useCustomTerm: false
}; },
watch: {
stateText: function() { // Textarea changes ==> change VsmBox.
this.stateTextToBoxValue();
}
},
mounted: function() {
this.boxValueToStateText(this.initialValue); // Initial VsmBox ==>textarea.
this.setMsg('');
this.stateTextWidth =
getComputedStyle(document.getElementById('stateText')).width;
},
methods: {
/* Note: we can't set `:custom-term.prop` to `false` to deactivate it,
because its '.prop' requires that it is bound to an Object or Function.
So to disable it, you can either remove that binding from the HTML,
or return the default value `data.strs.str`, as shown below. */
customTerm(data) {
return !this.useCustomTerm ? { str: data.strs.str } :
{ str: '<span style="font-size: 8px; color: #777;">' +
data.index + '. </span>' + data.strs.str };
},
/* Here we add a fake an advancedSearch function, just to show that
autocomplete will now show a 'Search' item at the bottom. */
advancedSearch(data, cb) {
setTimeout(() => cb({
str: data.str, style: 'i', id: (data.allowClassNull ? null : 'id123')
}), 150);
},
onChange(event) { // VsmBox changes ==> change textarea.
// Note: we must access `$emit()`'s args via `events.detail[]`. This is
// different for a webpacked webcomponent vs. a pure Vue-component.
this.boxValueToStateText(event.detail[0]);
},
boxValueToStateText(value) { // (See vsm-box's index-dev.js).
lastAutoFilledText = this.stateText =
VsmJsonPretty(value, { json5: true });
this.setMsg(1);
},
stateTextToBoxValue() { // (See vsm-box's index-dev.js).
var abort = lastAutoFilledText === this.stateText;
lastAutoFilledText = false;
if (abort) return;
try {
this.initialValue = JSON5.parse(this.stateText);
this.setMsg(-1);
}
catch (err) {
this.setMsg(err.toString().replace('JSON5: ', ''));
}
},
setMsg(msg) {
var d = new Date();
d = '[' + ('0' + d.getHours()).slice(-2) + ':' +
('0' + d.getMinutes()).slice(-2) + ':' +
('0' + d.getSeconds()).slice(-2) + '.' +
('00' + d.getMilliseconds()).slice(-3, -1) + ']';
this.msg = !msg ? '' :
((msg == -1 ? '<---' : msg == 1 ? '--->' : msg) + ' ' + d);
},
onButtonClear() {
this.fillBoth('Cleared', { terms: [], conns: [] })
},
onButtonExample1() {
var example1 = {
terms: [
{ str: 'John', classID: 'PRS:0010', instID: null },
{ str: 'eats', classID: 'CW:0101', instID: null },
{ str: 'chicken', classID: 'BIO:0042', instID: null,
dictID: 'http://x.org/BIO', descr: 'the animal',
queryOptions: { sort: { dictID: ['http://x.org/BIO'] } } },
{ str: 'with', classID: 'CW:0105', instID: null,
dictID: 'CW', descr: 'using' },
{ str: 'fork', classID: 'CW:0108', instID: null,
queryOptions: { fixedTerms: [ { id: 'CW:0107', str: 'spoon' } ] } }
],
conns: [
{ type: 'T', pos: [0, 1, 2] },
{ type: 'T', pos: [1, 3, 4] }
]
};
this.fillBoth('Example 1', example1)
},
onButtonExample2() {
var example2 = {
terms: [
{ str: 'John', classID: 'PRS:0010', instID: 'db-id20',
dictID: 'PRSNS', descr: 'an example John',
minWidth: 5, maxWidth: 80, editWidth: 50,
queryOptions: {
filter: { dictID: [ 'PRSNS', 'CW', 'VAR' ] },
sort: { dictID: [ 'CW' ] },
fixedTerms: [
{ id: 'PRS:0001', str: 'Alice' }, { id: 'BIO:0010' } ] }
},
{ str: 'activates', classID: 'BIO:0014', instID: null,
isFocal: true },
{ str: 'spicy', classID: 'CW:0215', instID: null },
{ str: 'chicken', classID: 'BIO:0042', instID: null,
dictID: 'http://x.org/BIO', descr: 'the animal',
queryOptions: { sort: { dictID: ['http://x.org/BIO'] } } },
{ str: 'with', classID: 'CW:0105', instID: null,
dictID: 'CW', descr: 'using' },
{ str: 'Ca2+', style: 'u2-4', descr: 'Calcium ion',
classID: 'BIO:0010', instID: null }, ///, minWidth: 47},
{ str: 'with', classID: 'CW:0106', instID: null,
dictID: 'CW', descr: 'accompanied by' },
{ str: 'cls', classID: 'A:01', dictID: 'A', descr: 'descr-1' },
{ str: 'lit' },
{ editWidth: 20, placeholder: 'pl' },
{ type: 'EC', editWidth: 20 },
{ type: 'EL', editWidth: 20 },
{ str: 'and', classID: 'CW:0005', instID: null,
dictID: 'CW', descr: 'a set of items' },
{ str: 'himself', classID: 'PRS:0010', instID: 'db-id40',
parentID: 'db-id20'}
],
conns: [
{ type: 'T', pos: [ 3, -1, 2 ] },
{ type: 'T', pos: [ 0, 1, 3 ] },
{ type: 'T', pos: [ 1, 4, 5 ] },
{ type: 'L', pos: [ 12, 7, 8, 9, 10, 11, 13 ] },
{ type: 'T', pos: [ 1, 6, 12 ] },
{ type: 'R', pos: [ 13, 0 ] }
]
};
this.fillBoth('Example 2', example2)
},
fillBoth(name, data) {
this.initialValue = JSON.parse(JSON.stringify(data)); // Clone=>new Obj.
this.boxValueToStateText(this.initialValue);
this.setMsg(name);
}
}
});
</script>
</body>
</html>