This repository has been archived by the owner on Jan 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmyscript-languages-element.js
310 lines (293 loc) · 9.96 KB
/
myscript-languages-element.js
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
/* eslint-disable no-underscore-dangle,no-undef */
import { html, PolymerElement } from '@polymer/polymer';
import * as MyScript from 'myscript/dist/myscript.esm';
/**
`myscript-languages-element` is a web component to retrieve CDK available languages.
<myscript-languages-element
type="TEXT"
applicationkey="YOUR MYSCRIPT DEVELOPER APPLICATION KEY"
hmackey="YOUR MYSCRIPT DEVELOPER HMAC KEY">
</myscript-languages-element>
@demo src/demo-app/examples/non-version-specific/get_languages.html Get languages [V4]
@demo src/demo-app/examples/non-version-specific/get_languages_programmatic.html Get languages programmatically [V4]
*/
class MyScriptLanguagesElement extends PolymerElement {
static get template() {
return html`
<style>
:host {
contain: content;
box-sizing: border-box;
display: inline-block;
}
[hidden] {
display: none !important;
}
select {
width: auto;
padding: 0 36px 0 12px;
color: #131F26;
background:url(data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTgiIGhlaWdodD0iMTQiIHZpZXdCb3g9IjAgMCAxOCAxNCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48ZyBmaWxsPSJub25lIiBmaWxsLXJ1bGU9ImV2ZW5vZGQiPjxwYXRoIGQ9Ik00LjQyMi4yNmMtLjIzMy0uMzM3LS42MS0uMzM3LS44NDQgMEwuMTc1IDUuMTdDLS4xNDIgNS42MjguMDQ4IDYgLjYxIDZoNi43OGMuNTU4IDAgLjc1NC0uMzcuNDM1LS44M0w0LjQyMi4yNnptLjAyIDEzLjQ3Yy0uMjM2LjM0Mi0uNjE4LjM0Ny0uODUuMDFMLjE2NyA4LjgxOEMtLjE0NiA4LjM2Ny4wNDggOCAuNjA4IDhoNi43ODNjLjU1OCAwIC43NTUuMzcuNDM3LjgzbC0zLjM4NSA0Ljl6IiBmaWxsPSIjMTMxRjI2Ii8+PC9nPjwvc3ZnPg==) right center no-repeat #fff;
-webkit-appearance: none;
appearance: none;
height: 2.625rem;
font: 600 16px "Source Sans Pro",sans-serif;
border: 1px solid #CED5D9;
border-radius: 3px;
-webkit-tap-highlight-color: transparent;
}
select:focus {
padding: 0 35px 0 11px;
outline: 0;
border: 2px solid #1a9fff;
box-shadow: 0 1px 1px 0 rgba(0,0,0,.16);
}
</style>
<select disabled="[[ unloaded ]]" hidden="[[ !select ]]" on-change="_selectListener"></select>
`;
}
static get is() {
return 'myscript-languages-element';
}
static get properties() {
return {
/**
* <b>not mutable</b><br />
* The current recognition type (e.g. TEXT or ANALYZER)
*/
type: {
type: String,
reflectToAttribute: true,
value: 'TEXT'
},
/**
* Scheme to use to connect to MyScript Cloud or Server. (https or http)
*/
scheme: {
type: String,
reflectToAttribute: true,
value: 'https'
},
/**
* The current recognition service host.
*/
host: {
type: String,
reflectToAttribute: true,
value: 'cloud.myscript.com'
},
/**
* Cloud API version to use.
*/
apiversion: {
type: String,
reflectToAttribute: true,
value: 'V4'
},
/**
* Application key to use for recognition on MyScript handwriting recognition server.<br />
* You have to create your own MyScript Developer account at http://dev.myscript.com and then generate your application key at http://cloud.myscript.com. See the Developer Guide to learn how to register.<br /><br />
* <b>Warning</b>: This parameter is <b>mandatory</b> and its value should be a string.
*/
applicationkey: {
type: String,
reflectToAttribute: true
},
/**
* Language list sorts by values parameter. If false, language list sort by canonical name
*/
sortbyvalue: {
type: Boolean,
reflectToAttribute: true,
value: true
},
/**
* Languages List
*/
languages: {
type: Array,
value: [],
notify: true
},
/**
* Properties to set when you wish to set attributes in javascript. unloaded attributes should be removed once all properties are set.
* (see demo/get_languages_programmatic.html for a better understanding)
*/
unloaded: {
type: Boolean,
value: false,
notify: true,
reflectToAttribute: true,
observer: '_unloadedChanged'
},
/**
* Set the additional configuration used to feed MyScript ink Paper (Structure of object as defined in MyScriptjs MyScriptJSOptions.js file)
* Configuration values are taken into account when myscript-common-element is attach to the dom and when unloaded is set to false.
* Configuration values are not reflected to myscript-languages-element attributes plus attributes values are always taken into account before configuration values (see demo/programmatic_init.html for a better understanding).
*/
configuration: {
type: Object,
notify: true
},
/**
* True to display console output, false otherwise.
*/
debug: {
type: Boolean,
value: false,
reflectToAttribute: true
},
/**
* If set to true, display the language select.
*/
select: {
type: Boolean,
reflectToAttribute: true,
value: false,
notify: true
},
/**
* The recognition language used by the recognition process.
*/
language: {
type: String,
notify: true,
reflectToAttribute: true,
value: 'en_US'
}
};
}
static _generateConfiguration(configuration, language, triggerdelay, processdelay, type, protocol, apiversion, scheme, host, applicationkey, hmackey) {
// FIXME: find a way to do a proper deep merge
const conf = Object.assign({}, configuration);
if (!conf.triggers) {
conf.triggers = {};
}
if (!conf.recognitionParams) {
conf.recognitionParams = {};
}
if (!conf.recognitionParams.server) {
conf.recognitionParams.server = {};
}
if (!conf.recognitionParams.v3) {
conf.recognitionParams.v3 = {};
}
if (!conf.recognitionParams.v3.textParameter) {
conf.recognitionParams.v3.textParameter = {};
}
if (!conf.recognitionParams.v3.textParameter.textProperties) {
conf.recognitionParams.v3.textParameter.textProperties = {};
}
if (!conf.recognitionParams.v4) {
conf.recognitionParams.v4 = {};
}
if (!conf.recognitionParams.v4.text) {
conf.recognitionParams.v4.text = {};
}
if (language) {
conf.recognitionParams.v3.textParameter.language = language;
conf.recognitionParams.v4.lang = language;
}
if (triggerdelay) {
conf.triggerDelay = triggerdelay;
}
if (processdelay) {
conf.processDelay = processdelay;
}
if (protocol) {
conf.recognitionParams.protocol = protocol;
conf.triggers.exportContent = protocol === 'REST' ? 'QUIET_PERIOD' : 'POINTER_UP';
}
if (type) {
conf.recognitionParams.type = type;
}
if (apiversion) {
conf.recognitionParams.apiVersion = apiversion;
}
if (scheme) {
conf.recognitionParams.server.scheme = scheme;
}
if (host) {
conf.recognitionParams.server.host = host;
}
if (applicationkey) {
conf.recognitionParams.server.applicationKey = applicationkey;
}
if (hmackey) {
conf.recognitionParams.server.hmacKey = hmackey;
}
return conf;
}
_buildConfiguration() {
return MyScriptLanguagesElement._generateConfiguration(
this.configuration,
this.language,
this.triggerdelay,
this.processdelay,
this.type,
this.protocol,
this.apiversion,
this.scheme,
this.host,
this.applicationkey,
this.hmackey
);
}
_selectListener(event) {
this.languages = this.languages.map(language => Object.assign(language, {
selected: language.value === event.target.value
}));
this.language = this.languages.find(language => language.selected);
this.dispatchEvent(new CustomEvent('change', {
detail: this.languages
}));
}
constructor() {
super();
this.logger = MyScript.LoggerConfig.getLogger('languages-element');
this.logger.setLevel(this.debug ? 'DEBUG' : 'ERROR', false);
Object
.keys(MyScript.Constants.Logger)
.forEach((key) => {
MyScript.LoggerConfig
.getLogger(MyScript.Constants.Logger[key])
.setLevel(this.debug ? 'DEBUG' : 'ERROR', false);
});
this.configuration = this._buildConfiguration(); // Build default configuration
}
connectedCallback() {
super.connectedCallback();
this.selectElement = this.shadowRoot.querySelector('select');
this._unloadedChanged(this.unloaded);
this.logger.info('languages connected');
}
disconnectedCallback() {
super.disconnectedCallback();
this.logger.info('languages disconnected');
}
_unloadedChanged(unloaded) {
if (unloaded === false && (this.select === true ? this.selectElement : true)) {
this.logger.trace('unloaded changed', unloaded);
this._getLanguages();
}
}
_getLanguages() {
const res = MyScript.getAvailableLanguageList();
this.languages = Object
.keys(res.result)
.map(key => ({
value: key,
description: res.result[key],
selected: this.language === key
}));
this.languages.forEach((language) => {
this.selectElement.options[this.selectElement.options.length] = new Option(language.description, language.value, language.selected, language.selected);
});
this.selectElement.disabled = false;
this.dispatchEvent(new CustomEvent('change', {
detail: this.languages
}));
return this.languages;
}
}
customElements.define(MyScriptLanguagesElement.is, MyScriptLanguagesElement);