|
| 1 | +<!-- |
| 2 | +Copyright © MyScript. |
| 3 | +LICENSE: [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0) |
| 4 | +--> |
| 5 | +<link rel="import" href="../polymer/polymer.html"> |
| 6 | + |
| 7 | +<dom-module id="myscript-text-candidates-element"> |
| 8 | + <style> |
| 9 | + :host { |
| 10 | + display: block; |
| 11 | + overflow: auto; |
| 12 | + text-align: center; |
| 13 | + font-size: xx-large; |
| 14 | + min-height: 95px; |
| 15 | + max-height: 95px; |
| 16 | + } |
| 17 | + |
| 18 | + .text { |
| 19 | + color: #1580CD; |
| 20 | + background-color: #EDF0F2; |
| 21 | + font-size: 1.75rem; |
| 22 | + padding: .5rem .75rem; |
| 23 | + border-radius: 3px; |
| 24 | + cursor: pointer; |
| 25 | + } |
| 26 | + |
| 27 | + .text[selected] { |
| 28 | + color: #FFFFFF; |
| 29 | + background-color: #1580CD; |
| 30 | + } |
| 31 | + |
| 32 | + .candidate[selected] * { |
| 33 | + color: inherit; |
| 34 | + background-color: inherit; |
| 35 | + } |
| 36 | + |
| 37 | + .char.predicted { |
| 38 | + color: #73818C; |
| 39 | + } |
| 40 | + |
| 41 | + .char.completed { |
| 42 | + color: #1A9FFF; |
| 43 | + } |
| 44 | + </style> |
| 45 | + <template> |
| 46 | + <template is="dom-repeat" id="textCandidateList" items="[[ result.textSegmentResult.candidates ]]" |
| 47 | + as="textCandidate" |
| 48 | + index-as="textIndex"> |
| 49 | + <span class$="text {{ _getCandidateFlags(candidate) }}" |
| 50 | + selected$="[[ _isSelected(textIndex, result.textSegmentResult.selectedCandidateIdx) ]]" |
| 51 | + on-tap="_select"> |
| 52 | + <template is="dom-if" if="[[ !textCandidate.children ]]"> |
| 53 | + {{ textCandidate.label }} |
| 54 | + </template> |
| 55 | + <template is="dom-if" if="[[ textCandidate.children ]]"><!--WARN: templates needs to be inline to avoid carriage return after span--> |
| 56 | + <template is="dom-repeat" id="wordCandidateList" |
| 57 | + items="[[ _getChildCandidates(textCandidate, result, 'text') ]]" as="wordCandidate"> |
| 58 | + <span class$="word {{ _getCandidateFlags(wordCandidate) }}"> |
| 59 | + <template is="dom-if" if="[[ !wordCandidate.children ]]"> |
| 60 | + {{ wordCandidate.label }} |
| 61 | + </template> |
| 62 | + <template is="dom-if" if="[[ wordCandidate.children ]]"> |
| 63 | + <template is="dom-repeat" id="charCandidateList" |
| 64 | + items="[[ _getChildCandidates(wordCandidate, result, 'word') ]]" |
| 65 | + as="charCandidate"><span |
| 66 | + class$="char {{ _getCandidateFlags(charCandidate) }}">{{ charCandidate.label }}</span></template> |
| 67 | + </template> |
| 68 | + </span> |
| 69 | + </template> |
| 70 | + </template> |
| 71 | + </span> |
| 72 | + </template> |
| 73 | + </template> |
| 74 | +</dom-module> |
| 75 | + |
| 76 | +<script> |
| 77 | + Polymer({ |
| 78 | + is: 'myscript-text-candidates-element', |
| 79 | + properties: { |
| 80 | + /** |
| 81 | + * Recognition result. |
| 82 | + * @attribute result |
| 83 | + * @type {Object<String, Object>} Attributes depends on recognition type already configured. |
| 84 | + */ |
| 85 | + result: { |
| 86 | + type: Object, |
| 87 | + notify: true |
| 88 | + } |
| 89 | + }, |
| 90 | + /** |
| 91 | + * Select a new candidate |
| 92 | + * @param e |
| 93 | + */ |
| 94 | + _select: function (e) { |
| 95 | + var result = this.result; |
| 96 | + result.textSegmentResult.selectedCandidateIdx = this.$.textCandidateList.indexForElement(e.target); |
| 97 | + this.result = {}; |
| 98 | + this.result = result; |
| 99 | + this.dispatchEvent(new CustomEvent('myscript-text-candidates-element-change'), { detail: this.result }); |
| 100 | + }, |
| 101 | + _isSelected: function (index, selectedIndex) { |
| 102 | + return index === selectedIndex; |
| 103 | + }, |
| 104 | + _getChildSegments: function (candidate, result, type) { |
| 105 | + var segments = []; |
| 106 | + |
| 107 | + function addSegment(child, segment) { |
| 108 | + if (segment.inkRanges === child.inkRanges) { |
| 109 | + segment.selectedCandidateIdx = child.selectedCandidateIdx; |
| 110 | + segments.push(segment); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (candidate.children) { |
| 115 | + candidate.children.forEach(function (child, index) { |
| 116 | + if (type === 'text' && result.wordSegments && result.wordSegments.length > -1) { |
| 117 | + result.wordSegments.forEach(function (segment) { |
| 118 | + addSegment(child, segment); |
| 119 | + }); |
| 120 | + } else if (type === 'word' && result.charSegments && result.charSegments.length > -1) { |
| 121 | + if (!child.inkRanges) { |
| 122 | + var seg = { |
| 123 | + candidates: [{ |
| 124 | + label: candidate.label.charAt(index), |
| 125 | + flags: candidate.flags |
| 126 | + }], |
| 127 | + selectedCandidateIdx: 0 |
| 128 | + }; |
| 129 | + segments.push(seg); |
| 130 | + } else { |
| 131 | + result.charSegments.forEach(function (segment) { |
| 132 | + addSegment(child, segment); |
| 133 | + }); |
| 134 | + } |
| 135 | + } |
| 136 | + }); |
| 137 | + } |
| 138 | + return segments; |
| 139 | + }, |
| 140 | + _getChildCandidates: function (candidate, result, type) { |
| 141 | + var candidates = []; |
| 142 | + this._getChildSegments(candidate, result, type) |
| 143 | + .forEach(function (segment) { |
| 144 | + candidates.push(segment.candidates[segment.selectedCandidateIdx]); |
| 145 | + }); |
| 146 | + return candidates; |
| 147 | + }, |
| 148 | + _getCandidateFlags: function (candidate) { |
| 149 | + if (candidate.flags) { |
| 150 | + return candidate.flags.join(' ').toLowerCase(); |
| 151 | + } |
| 152 | + return ''; |
| 153 | + } |
| 154 | + }); |
| 155 | +</script> |
0 commit comments