Skip to content
This repository was archived by the owner on Jan 4, 2021. It is now read-only.

Commit 99d1f4d

Browse files
committed
[DEV] Externalize candidates list method
1 parent cdc65d2 commit 99d1f4d

File tree

1 file changed

+95
-84
lines changed

1 file changed

+95
-84
lines changed

myscript-text-web.html

Lines changed: 95 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<template>
6565
<div class="resultField" hidden="[[ displayResultZone(hideresult, unloaded) ]]">
6666
<template is="dom-repeat" id="candidateList" items="[[ candidates ]]" as="candidate">
67-
<span selected$="[[ isSelected(candidate, result) ]]" inner-h-t-m-l="[[ getLabel(candidate) ]]" on-tap="select"></span>
67+
<span id="candidate" index="[[ index ]]" selected$="[[ candidate.isSelected ]]" inner-h-t-m-l="[[ candidate.customlabel ]]"></span>
6868
</template>
6969
</div>
7070
<myscript-common-element
@@ -145,10 +145,11 @@
145145
*/
146146
function textManageResultEvent(myscriptTextWebElement, eventDetail) {
147147
myscriptTextWebElement.result = {};
148-
// Transforming an Array into an hashmap to ease bindings.
149148
if (eventDetail.rawResult && eventDetail.rawResult.result) {
149+
textUpdateCandidatesCustomLabel(eventDetail.rawResult.result);
150+
textUpdateCandidatesIsSelected(eventDetail.rawResult.result);
150151
myscriptTextWebElement.result = eventDetail.rawResult.result;
151-
myscriptTextWebElement.resultlabel = eventDetail.rawResult.result.textSegmentResult.candidates[eventDetail.rawResult.result.textSegmentResult.selectedCandidateIdx].label
152+
myscriptTextWebElement.resultlabel = eventDetail.rawResult.result.textSegmentResult.candidates[eventDetail.rawResult.result.textSegmentResult.selectedCandidateIdx].label;
152153
myscriptTextWebElement.candidates = eventDetail.rawResult.result.textSegmentResult.candidates;
153154
}
154155
}
@@ -189,6 +190,95 @@
189190
}
190191
}
191192

193+
/**
194+
* Update custom label of candidates by theirs owns properties
195+
*/
196+
function textUpdateCandidatesCustomLabel(result) {
197+
result.textSegmentResult.candidates.forEach(function (candidate) {
198+
var textLabel = '<span class="text myscript-text-web">';
199+
if (candidate.flags && candidate.flags.indexOf('PREDICTED') > -1) {
200+
textLabel = '<span class="text predicted myscript-text-web">';
201+
}
202+
else if (candidate.flags && candidate.flags.indexOf('COMPLETED') > -1) {
203+
textLabel = '<span class="text completed myscript-text-web">';
204+
}
205+
if (!result.wordSegments || result.wordSegments.length <= 0) {
206+
textLabel += candidate.label;
207+
} else {
208+
for (var i in candidate.children) {
209+
var wordSegmentItem = candidate.children[i];
210+
var wordCandidateIndex = wordSegmentItem.selectedCandidateIdx;
211+
var wordInkRanges = wordSegmentItem.inkRanges;
212+
var wordSegment = undefined;
213+
for (var i = 0; i < result.wordSegments.length; i++) {
214+
if (JSON.stringify(result.wordSegments[i].inkRanges) === JSON.stringify(wordInkRanges)) {
215+
wordSegment = result.wordSegments[i];
216+
break;
217+
}
218+
}
219+
if (wordSegment) {
220+
var wordCandidate = wordSegment.candidates[wordCandidateIndex];
221+
if (wordCandidate) {
222+
var wordLabel = '<span class="word myscript-text-web">';
223+
if (wordCandidate.flags && wordCandidate.flags.indexOf('PREDICTED') > -1) {
224+
wordLabel = '<span class="word predicted myscript-text-web">';
225+
} else if (wordCandidate.flags && wordCandidate.flags.indexOf('COMPLETED') > -1) {
226+
wordLabel = '<span class="word completed myscript-text-web">';
227+
}
228+
if (!result.charSegments || result.charSegments.length <= 0) {
229+
wordLabel += wordCandidate.label;
230+
} else {
231+
for (var j in wordCandidate.children) {
232+
var charSegmentItem = wordCandidate.children[j];
233+
var charLabel = '<span class="char myscript-text-web">';
234+
if (!(charSegmentItem.inkRanges && charSegmentItem.inkRanges.length > 0)) {
235+
if (wordCandidate.flags.indexOf('PREDICTED') > -1) {
236+
charLabel = '<span class="char predicted myscript-text-web">';
237+
} else if (wordCandidate.flags.indexOf('COMPLETED') > -1) {
238+
charLabel = '<span class="char completed myscript-text-web">';
239+
}
240+
}
241+
charLabel += wordCandidate.label.charAt(j);
242+
charLabel += '</span>';
243+
wordLabel += charLabel;
244+
}
245+
}
246+
wordLabel += '</span>';
247+
textLabel += wordLabel;
248+
}
249+
}
250+
}
251+
}
252+
candidate.customlabel = textLabel + '</span>';
253+
});
254+
}
255+
/**
256+
* Update custom isSelected property of candidates
257+
*/
258+
function textUpdateCandidatesIsSelected (result) {
259+
result.textSegmentResult.candidates.forEach(function (candidate) {
260+
candidate.isSelected = (candidate === result.textSegmentResult.candidates[result.textSegmentResult.selectedCandidateIdx]);
261+
});
262+
}
263+
264+
function textUpdateOnTapCandidatesEventListener (myscriptTextWebElement){
265+
myscriptTextWebElement.querySelectorAll('#candidate').forEach(function(candidate){
266+
candidate.addEventListener('tap', function (event) {
267+
LOG("text select candidate intercepted", event);
268+
//var result = myscriptTextWebElement.result;
269+
//result.textSegmentResult.selectedCandidateIdx = candidate.index;
270+
//myscriptTextWebElement.result = {};
271+
//myscriptTextWebElement.result = result;
272+
273+
//textManageResultEvent(myscriptTextWebElement, e.detail);
274+
//myscriptTextWebElement.querySelector('[selected]').removeAttribute('selected');
275+
//candidate.setAttribute('selected', true);
276+
myscriptTextWebElement.result.textSegmentResult.selectedCandidateIdx = candidate.index;
277+
myscriptTextWebElement.myscriptcommonelement.dispatchEvent(new CustomEvent('myscript-common-element-result', { detail: { rawResult: { result: myscriptTextWebElement.result }}}));
278+
});
279+
});
280+
}
281+
192282
Polymer({
193283
is: 'myscript-text-web',
194284
/**
@@ -504,13 +594,15 @@
504594
type: Number
505595
},
506596
/**
597+
* The size of the candidate prediction lists that will be provided at the word level in the recognition result.
507598
* This value must be between `0` and `20`.
508599
*/
509600
wordpredictionlistsize: {
510601
type: Number,
511602
value: 0
512603
},
513604
/**
605+
* The size of the candidate completion lists that will be provided at the word level in the recognition result.
514606
* This value must be between `0` and `20`.
515607
*/
516608
wordcompletionlistsize: {
@@ -684,87 +776,6 @@
684776
LOG('shouldLoadCommonElement', hideResultValue, unloadedValue);
685777
// Common element should not be attached if the unload attribute of myscript-math-web element is set to true and if he is not already connected to the dom.
686778
return hideResultValue && ( unloadedValue || unloadedValue !== true);
687-
},
688-
/**
689-
* Select a new candidate
690-
* @param e
691-
*/
692-
select: function (e) {
693-
var result = this.result;
694-
result.textSegmentResult.selectedCandidateIdx = e.model.index;
695-
this.result = {};
696-
this.result = result;
697-
},
698-
isSelected: function (candidate, result) {
699-
if (result && result.textSegmentResult) {
700-
return (candidate === result.textSegmentResult.candidates[result.textSegmentResult.selectedCandidateIdx]);
701-
}
702-
return false;
703-
},
704-
/**
705-
* Méthode d'affichage des label d'un candidat.
706-
* @param candidate
707-
* @returns {*}
708-
*/
709-
getLabel: function (candidate) {
710-
if (!candidate) {
711-
return undefined;
712-
}
713-
var textLabel = '<span class="text myscript-text-web">';
714-
if (candidate.flags && candidate.flags.indexOf('PREDICTED') > -1) {
715-
textLabel = '<span class="text predicted myscript-text-web">';
716-
}
717-
else if (candidate.flags && candidate.flags.indexOf('COMPLETED') > -1) {
718-
textLabel = '<span class="text completed myscript-text-web">';
719-
}
720-
if (!this.result.wordSegments || this.result.wordSegments.length <= 0) {
721-
textLabel += candidate.label;
722-
} else {
723-
for (var i in candidate.children) {
724-
var wordSegmentItem = candidate.children[i];
725-
var wordCandidateIndex = wordSegmentItem.selectedCandidateIdx;
726-
var wordInkRanges = wordSegmentItem.inkRanges;
727-
var wordSegment = undefined;
728-
for (var i = 0; i < this.result.wordSegments.length; i++) {
729-
if (JSON.stringify(this.result.wordSegments[i].inkRanges) === JSON.stringify(wordInkRanges)) {
730-
wordSegment = this.result.wordSegments[i];
731-
break;
732-
}
733-
}
734-
if (wordSegment) {
735-
var wordCandidate = wordSegment.candidates[wordCandidateIndex];
736-
if (wordCandidate) {
737-
var wordLabel = '<span class="word myscript-text-web">';
738-
if (wordCandidate.flags && wordCandidate.flags.indexOf('PREDICTED') > -1) {
739-
wordLabel = '<span class="word predicted myscript-text-web">';
740-
} else if (wordCandidate.flags && wordCandidate.flags.indexOf('COMPLETED') > -1) {
741-
wordLabel = '<span class="word completed myscript-text-web">';
742-
}
743-
if (!this.result.charSegments || this.result.charSegments.length <= 0) {
744-
wordLabel += wordCandidate.label;
745-
} else {
746-
for (var j in wordCandidate.children) {
747-
var charSegmentItem = wordCandidate.children[j];
748-
var charLabel = '<span class="char myscript-text-web">';
749-
if (!(charSegmentItem.inkRanges && charSegmentItem.inkRanges.length > 0)) {
750-
if (wordCandidate.flags.indexOf('PREDICTED') > -1) {
751-
charLabel = '<span class="char predicted myscript-text-web">';
752-
} else if (wordCandidate.flags.indexOf('COMPLETED') > -1) {
753-
charLabel = '<span class="char completed myscript-text-web">';
754-
}
755-
}
756-
charLabel += wordCandidate.label.charAt(j);
757-
charLabel += '</span>';
758-
wordLabel += charLabel;
759-
}
760-
}
761-
wordLabel += '</span>';
762-
textLabel += wordLabel;
763-
}
764-
}
765-
}
766-
}
767-
return textLabel + '</span>';
768779
}
769780
})
770781
;

0 commit comments

Comments
 (0)