/
fluent_interface.js
955 lines (786 loc) · 27.3 KB
/
fluent_interface.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
/* global FluentSyntax */
var fluentParser = new FluentSyntax.FluentParser({ withSpans: false });
var fluentSerializer = new FluentSyntax.FluentSerializer();
/* Public functions used across different files */
var Pontoon = (function (my) {
/*
* Is ast element of type that can be presented as a simple string:
* - TextElement
* - Placeable with expression type CallExpression, ExternalArgument
* or MessageReference
*/
function isSimpleElement(element) {
if (element.type === 'TextElement') {
return true;
}
// Placeable
if (
element.expression &&
[
'CallExpression',
'ExternalArgument',
'MessageReference'
].indexOf(element.expression.type) >= 0
) {
return true;
}
return false;
}
/*
* Is ast element representing a pluralized string?
*
* Keys of all variants of such elements are either
* CLDR plurals or numbers.
*/
function isPluralElement(element) {
if (!(element.expression && element.expression.type === 'SelectExpression')) {
return false;
}
var CLDRplurals = ['zero', 'one', 'two', 'few', 'many', 'other'];
return element.expression.variants.every(function (item) {
return (
CLDRplurals.indexOf(item.key.name) !== -1 ||
item.key.type === 'NumberExpression'
);
});
}
/*
* Are all elements supported in rich FTL editor?
*
* Elements are supported if they are:
* - simple elements or
* - select expressions
*/
function areSupportedElements(elements) {
return elements.every(function(element) {
return (
isSimpleElement(element) ||
(element.expression && element.expression.type === 'SelectExpression')
);
});
}
/*
* Is ast of a message, supported in rich FTL editor?
*
* Message is supported if it's valid and all value elements
* and all attribute elements are supported.
*/
function isSupportedMessage(ast) {
// Parse error
if (ast.type === 'Junk') {
return false;
}
var valueSupported = !ast.value || areSupportedElements(ast.value.elements);
var attributesSupported = ast.attributes.every(function (attribute) {
return attribute.value && areSupportedElements(attribute.value.elements);
});
return valueSupported && attributesSupported;
}
/*
* Is ast of a simple message?
*
* A simple message has no attributes and all value
* elements are simple.
*/
function isSimpleMessage(ast) {
if (
ast &&
!ast.attributes.length &&
ast.value &&
ast.value.elements.every(isSimpleElement)
) {
return true;
}
return false;
}
/*
* Render textarea element with the given properties
*/
function renderTextareaElement(id, value, maxlength) {
var element = '<textarea class="value" id="ftl-id-' + id + '"' + ' maxlength="' + maxlength + '"';
element += ' dir="' + Pontoon.locale.direction +
'" data-script="' + Pontoon.locale.script +
'" lang="' + Pontoon.locale.code + '">' + value + '</textarea>';
return element;
}
/*
* Render original string element with given title and ast elements
*/
function renderOriginalElement(title, elements) {
return '<li class="clearfix">' +
'<span class="id">' +
title +
'</span>' +
'<span class="value">' +
stringifyElements(elements, true) +
'</span>' +
'</li>';
}
/*
* Render editor element with given title and ast elements
*/
function renderEditorElement(title, elements, isPlural, isTranslated) {
// Special case: Access keys
var maxlength = '';
var accesskeysDiv = '';
if (title === 'accesskey') {
maxlength = '1';
accesskeysDiv = '<div class="accesskeys"></div>';
}
// Special case: Plurals
var exampleSpan = '';
if (isPlural) {
var example = Pontoon.locale.plural_examples[title];
if (example !== undefined) {
exampleSpan = '<span> (e.g. ' +
'<span class="stress">' + example + '</span>' +
')</span>';
}
}
var value = isTranslated ? stringifyElements(elements) : '';
var textarea = renderTextareaElement(title, value, maxlength);
return '<li class="clearfix">' +
'<label class="id" for="ftl-id-' + title + '">' +
'<span>' + title + '</span>' +
exampleSpan +
'</label>' +
accesskeysDiv +
textarea +
'</li>';
}
/*
* Render original string elements
*
* - Adjoining simple elements are concatenated and presented as a simple string
* - SelectExpression elements are presented as a list of variants
*/
function renderOriginalElements(elements, title) {
var content = '';
var simpleElements = [];
elements.forEach(function (element, i) {
var isLastElement = i === elements.length - 1;
// Collect simple elements
if (isSimpleElement(element)) {
simpleElements.push(element);
}
// Render collected simple elements when non-simple or last element is met
if ((!isSimpleElement(element) || isLastElement) && simpleElements.length) {
content += renderOriginalElement(title, simpleElements);
simpleElements = [];
}
// Render SelectExpression
if (element.expression && element.expression.type === 'SelectExpression') {
element.expression.variants.forEach(function (item) {
content += renderOriginalElement(item.key.value || item.key.name, item.value.elements);
});
}
});
return content;
}
/*
* Render editor elements
*
* - Adjoining simple elements are concatenated and presented as a simple string
* - SelectExpression elements are presented as a list of variants
*/
function renderEditorElements(elements, title, isTranslated) {
var content = '';
var simpleElements = [];
elements.forEach(function (element, i) {
var isLastElement = i === elements.length - 1;
// Collect simple elements
if (isSimpleElement(element)) {
simpleElements.push(element);
}
// Render collected simple elements when non-simple or last element is met
if ((!isSimpleElement(element) || isLastElement) && simpleElements.length) {
content += renderEditorElement(title, simpleElements, false, isTranslated);
simpleElements = [];
}
// Render SelectExpression
if (element.expression && element.expression.type === 'SelectExpression') {
var expression = fluentSerializer.serializeExpression(element.expression.expression);
content += '<li data-expression="' + expression + '"><ul>';
if (isPluralElement(element) && !isTranslated) {
Pontoon.locale.cldr_plurals.forEach(function (pluralName) {
content += renderEditorElement(pluralName, [], true, isTranslated);
});
}
else {
element.expression.variants.forEach(function (item) {
content += renderEditorElement(
item.key.value || item.key.name,
item.value.elements,
isPluralElement(element),
isTranslated
);
});
}
content += '</ul></li>';
}
});
return content;
}
/*
* Serialize values in FTL editor forms into an FTL string
*
* elementNodes A set of jQuery nodes
*/
function serializeFTLEditorElements(elementNodes) {
var value = '';
elementNodes.each(function (i, node) {
var expression = $(node).data('expression');
// Simple element
if (!expression) {
value += $(this).find('textarea').val();
}
// SelectExpression
else {
var nodeValue = expression + ' ->';
var variants = $(node).find('ul li');
var hasTranslatedVariants = false;
var defaultMarker = '';
variants.each(function(index) {
var id = $(this).find('.id span:first').html();
var val = $(this).find('.value').val();
// TODO: UI should allow for explicitly selecting a default variant
if (index === variants.length - 1) {
defaultMarker = '*';
}
if (id && val) {
nodeValue += '\n ' + defaultMarker + '[' + id + '] ' + val;
hasTranslatedVariants = true;
}
});
if (hasTranslatedVariants) {
value += '{ ' + nodeValue + '\n }';
}
}
});
return value;
}
/*
* Convert AST elements of type TextElement or Placeable to string
*
* elements AST elements
* markPlaceables Should placeables be marked up?
*/
function stringifyElements(elements, markPlaceables) {
var string = '';
var startMarker = '';
var endMarker = '';
elements.forEach(function (element) {
if (element.type === 'TextElement') {
if (markPlaceables) {
string += Pontoon.markXMLTags(element.value);
}
else {
string += element.value;
}
}
else if (element.type === 'Placeable') {
if (element.expression.type === 'ExternalArgument') {
if (markPlaceables) {
startMarker = '<mark class="placeable" title="External Argument">';
endMarker = '</mark>';
}
string += startMarker + '{$' + element.expression.id.name + '}' + endMarker;
}
else if (element.expression.type === 'MessageReference') {
if (markPlaceables) {
startMarker = '<mark class="placeable" title="Message Reference">';
endMarker = '</mark>';
}
string += startMarker + '{' + element.expression.id.name + '}' + endMarker;
}
else if (element.expression.type === 'CallExpression') {
if (markPlaceables) {
startMarker = '<mark class="placeable" title="Call Expression">';
endMarker = '</mark>';
}
var expression = fluentSerializer.serializeExpression(element.expression);
string += startMarker + '{' + expression + '}' + endMarker;
}
else if (element.expression.type === 'SelectExpression') {
var variantElements = element.expression.variants.filter(function (variant) {
return variant.default;
})[0].value.elements;
string += stringifyElements(variantElements);
}
}
});
return string;
}
/*
* Perform error checks for provided translationAST and entityAST
*/
function runChecks(translationAST, entityAST) {
// Parse error
if (translationAST.type === 'Junk') {
return translationAST.annotations[0].message;
}
// TODO: Should be removed by bug 1237667
// Detect missing values
else if (entityAST.value && !translationAST.value) {
return 'Please make sure to fill in the value';
}
// Detect missing attributes
else if (
entityAST.attributes &&
translationAST.attributes &&
entityAST.attributes.length !== translationAST.attributes.length
) {
return 'Please make sure to fill in all the attributes';
}
// Detect Message ID mismatch
else if (entityAST.id.name !== translationAST.id.name) {
return 'Please make sure the translation key matches the source string key';
}
}
/*
* Toggle translation length and Copy button in editor toolbar
*/
function toggleEditorToolbar() {
var entity = Pontoon.getEditorEntity();
var show = entity.format !== 'ftl' || !Pontoon.fluent.isComplexFTL();
$('#translation-length, #copy').toggle(show);
if ($('#translation-length').is(':visible')) {
var original = Pontoon.fluent.getSimplePreview(entity, entity.original, entity);
$('#translation-length').find('.original-length').html(original.length);
}
}
return $.extend(true, my, {
fluent: {
/*
* Is FTL editor enabled?
*/
isFTLEditorEnabled: function () {
return $('#ftl-area').is(':visible');
},
/*
* Is Source FTL editor enabled?
*/
isSourceFTLEditorEnabled: function () {
return $('#ftl:visible').is('.active');
},
/*
* Is string in FTL editor complex?
* As opposed to simple which only contains a string value.
*/
isComplexFTL: function () {
return !$('#only-value').is(':visible');
},
/*
* Toggle FTL button visibility
*/
toggleButton: function () {
var entity = Pontoon.getEditorEntity();
$('#ftl').toggle(entity.format === 'ftl');
},
/*
* Toggle between source and FTL translation editor
*/
toggleEditor: function (showFTL) {
var entity = Pontoon.getEditorEntity();
if (typeof showFTL === 'undefined' || showFTL === null) {
showFTL = entity.format === 'ftl';
}
if (showFTL) {
$('#ftl-area').show();
$('#translation').hide();
$('#ftl').removeClass('active');
}
else {
$('#ftl-area').hide();
$('#translation').show().focus();
$('#ftl').addClass('active');
}
toggleEditorToolbar();
Pontoon.moveCursorToBeginning();
},
/*
* Get source string value of a simple FTL message to be used in
* the Copy (original to translation) function
*/
getSourceStringValue: function (entity, fallback) {
if (entity.format !== 'ftl' || this.isComplexFTL()) {
return fallback;
}
var ast = fluentParser.parseEntry(entity.original);
return stringifyElements(ast.value.elements);
},
/*
* Return translation in the editor as FTL source to be used
* in unsaved changes check. If translation contains errors,
* return error message.
*/
getFTLEditorContentsAsSource: function () {
var entity = Pontoon.getEditorEntity();
var fallback = $('#translation').val();
// For non-FTL entities, return unchanged translations
if (entity.format !== 'ftl') {
return fallback;
}
var translation = this.serializeTranslation(entity, fallback);
// If translation broken, incomplete or empty
if (translation.error) {
return translation.error;
}
// Special case: empty translations in rich FTL editor don't serialize properly
if (this.isFTLEditorEnabled()) {
var richTranslation = $.map(
$('#ftl-area textarea:not(".id"):visible'), function(i) {
return $(i).val();
}
).join('');
if (!richTranslation.length) {
translation = entity.key + ' = ';
}
}
return translation;
},
/*
* Render original string of FTL and non-FTL messages.
*/
renderOriginal: function () {
var entity = Pontoon.getEditorEntity();
$('#original').show();
$('#ftl-original').hide();
if (entity.format !== 'ftl') {
return;
}
$('#original').hide();
$('#ftl-original').show();
$('#ftl-original section ul').empty();
var ast = fluentParser.parseEntry(entity.original);
var unsupported = false;
var value = '';
var attributes = '';
// Unsupported string: render as source
if (!isSupportedMessage(ast)) {
ast.comment = null; // Remove comment
value = '<li class="source">' +
fluentSerializer.serializeEntry(ast) +
'</li>';
unsupported = true;
}
// Simple string: only value
else if (isSimpleMessage(ast)) {
value = '<li><p>' +
stringifyElements(ast.value.elements, true) +
'</p></li>';
}
// Value
else if (ast.value) {
value = renderOriginalElements(ast.value.elements, 'Value');
}
// Attributes
if (ast.attributes.length && !unsupported) {
ast.attributes.forEach(function (attr) {
attributes += renderOriginalElements(attr.value.elements, attr.id.name);
});
}
$('#ftl-original .attributes ul').append(attributes);
$('#ftl-original .main-value ul').append(value);
},
/*
* Render form-based FTL editor. Different widgets are displayed depending on the source
* string and the translation.
*/
renderEditor: function (translation) {
var self = this;
var entity = Pontoon.getEditorEntity();
var value = '';
var attributes = '';
var attributesTree = [];
var entityAST = fluentParser.parseEntry(entity.original);
if (entityAST.attributes.length) {
attributesTree = entityAST.attributes;
}
translation = translation || entity.translation[0];
var translationAST = null;
if (translation.pk) {
translationAST = fluentParser.parseEntry(translation.string);
attributesTree = translationAST.attributes;
}
// Reset default editor values
$('#ftl-area > .main-value').show().find('textarea').val('');
$('#ftl-area > .main-value ul li:not(":first")').remove();
$('#ftl-area .attributes ul:first').empty();
$('#only-value').parents('li').hide();
// Unsupported translation or unsuppported original: show source view
if (
(translationAST && !isSupportedMessage(translationAST)) ||
(!translationAST && !isSupportedMessage(entityAST))
) {
value = entity.key + ' = ';
if (translationAST) {
value = translation.string;
}
$('#translation').val(value);
self.toggleEditor(false);
Pontoon.updateCachedTranslation();
return;
}
// Simple string: only value
else if (
(translationAST && isSimpleMessage(translationAST)) ||
(!translationAST && isSimpleMessage(entityAST))
) {
value = '';
if (translationAST) {
value = stringifyElements(translationAST.value.elements);
}
$('#only-value')
.val(value)
.parents('li')
.show();
}
// Value
else if (
(translationAST && translationAST.value) ||
entityAST.value
) {
var ast = translationAST || entityAST;
value = renderEditorElements(ast.value.elements, 'Value', translationAST);
$('#ftl-area .main-value ul').append(value);
}
// No value in source string: no value in translation
else {
$('#ftl-area > .main-value').hide();
}
// Attributes
if (attributesTree.length) {
attributesTree.forEach(function (attr) {
var id = attr.id.name;
attributes += (
'<li data-id="' + id + '">' +
'<ul>' +
renderEditorElements(attr.value.elements, id, translationAST) +
'</ul>' +
'</li>'
);
});
$('#ftl-area .attributes ul:first').append(attributes);
// Update access keys presentation
$('#ftl-area textarea.value').keyup();
}
// Ignore editing for anonymous users
if (!Pontoon.user.id) {
$('#ftl-area textarea').prop('readonly', true);
}
// Focus first field of the FTL editor
$('#ftl-area textarea:not(".id"):visible').first().focus();
toggleEditorToolbar();
Pontoon.moveCursorToBeginning();
Pontoon.updateCurrentTranslationLength();
Pontoon.updateInPlaceTranslation();
return true;
},
/*
* Get AST and any errors for the entity's translation
*/
serializeTranslation: function (entity, translation) {
if (entity.format !== 'ftl') {
return translation;
}
var entityAST = fluentParser.parseEntry(entity.original);
var content = entity.key + ' = ';
var valueElements = $('#ftl-area .main-value ul:first > li:visible');
var attributeElements = $('#ftl-area .attributes ul:first > li:visible');
var value = '';
var attributes = '';
// Unsupported string
if (!this.isFTLEditorEnabled()) {
content = translation;
}
// Simple string
else if (!this.isComplexFTL()) {
value = $('#only-value').val();
// Multiline strings: mark with indent
if (value.indexOf('\n') !== -1) {
value = '\n ' + value.replace(/\n/g, '\n ');
}
content += value;
}
// Value
else if (valueElements.length) {
value = serializeFTLEditorElements(valueElements);
if (value) {
content += value;
}
}
// Attributes
if (attributeElements.length) {
attributeElements.each(function () {
var id = $(this).data('id');
var val = serializeFTLEditorElements($(this).find('ul:first > li'));
if (id && val) {
attributes += '\n .' + id + ' = ' + val;
}
});
if (attributes) {
content = (value ? content : entity.key) + attributes;
}
}
var ast = fluentParser.parseEntry(content);
var error = runChecks(ast, entityAST);
if (error) {
return {
error: error,
};
}
return fluentSerializer.serializeEntry(ast);
},
/*
* Get simplified preview of the FTL message, used when full presentation not possible
* due to lack of real estate (e.g. string list).
*/
getSimplePreview: function (object, fallback, entity) {
var response = fallback;
if (entity.format === 'ftl') {
// Transfrom complex FTL-based strings into single-value strings
var source = object.original || object.string;
if (!source) {
return fallback;
}
var ast = fluentParser.parseEntry(source);
var tree;
// Value: use entire AST
if (ast.value) {
tree = ast;
}
// Attributes (must be present in valid AST if value isn't):
// use AST of the first attribute
else {
tree = ast.attributes[0];
}
response = stringifyElements(tree.value.elements);
// Update source string markup
if (object.hasOwnProperty('original')) {
response = Pontoon.doNotRender(response);
}
}
return response;
},
/*
* Attach event handlers to FTL editor elements
*/
attachFTLEditorHandlers: function () {
var self = this;
// Ignore editing for anonymous users
if (!Pontoon.user.id) {
return;
}
// Toggle FTL and source editors
$('#ftl').click(function (e) {
e.preventDefault();
var entity = Pontoon.getEditorEntity();
var translation = null;
// Update FTL editor
if ($(this).is('.active')) {
translation = $('#translation').val();
// Strip trailing newlines for easier translated status detection
translation = translation.replace(/\n$/, '');
var translated = (translation && translation !== entity.key + ' = ');
// Perform error checks
if (translated) {
var translationAST = fluentParser.parseEntry(translation);
var entityAST = fluentParser.parseEntry(entity.original);
var error = runChecks(translationAST, entityAST);
if (error) {
return Pontoon.endLoader(error, 'error', 5000);
}
}
var isRichEditorSupported = self.renderEditor({
pk: translated, // An indicator that the string is translated
string: translation,
});
// Rich FTL editor does not support the translation
if (!isRichEditorSupported) {
return;
}
}
// Update source editor
else {
translation = self.serializeTranslation(entity, translation);
// If translation broken, incomplete or empty
if (translation.error) {
translation = entity.key + ' = ';
}
$('#translation').val(translation);
Pontoon.updateCachedTranslation();
}
self.toggleEditor($(this).is('.active'));
});
// Generate access key candidates
$('#ftl-area').on('keyup', 'textarea', function () {
var accessKeyExists = $('#ftl-area .attributes [data-id="accesskey"]').length;
var isLabel = $(this).parents('.attributes [data-id="label"]').length;
var isValue = $(this).parents('.main-value').length;
var generateAccessKeys = accessKeyExists && (isLabel || isValue);
// Quit early if the element doesn't have impact on the access key
if (!generateAccessKeys) {
return;
}
// Select textarea elements to generate access key candidates from
var content = '';
var selector = '.main-value';
if (isLabel) {
selector = '.attributes [data-id="label"]';
}
// Build artificial FTL Message from textarea contents to only take
// TextElements into account when generating access key candidates.
// See bug 1447103 for more detals.
$('#ftl-area ' + selector + ' textarea').each(function() {
var value = $(this).val();
var message = 'key = ' + value;
var ast = fluentParser.parseEntry(message);
if (ast.type !== 'Junk') {
value = '';
ast.value.elements.forEach(function (element) {
if (element.type === 'TextElement') {
value += element.value;
}
});
}
content += value
.replace(/\s/g, ''); // Remove whitespace
});
// Extract unique candidates in a list
var candidates = content.split('')
.filter(function (item, i, ar) {
return ar.indexOf(item) === i;
});
// Store currently selected access key
var active = $('#ftl-id-accesskey').val();
// Reset a list of access key candidates
$('.accesskeys').empty();
// Render candidates
candidates.forEach(function (candidate) {
$('.accesskeys').append(
'<div' + ((candidate === active) ? ' class="active"' : '') + '>' + candidate + '</div>'
);
});
});
// Select access key via click
$('#ftl-area .attributes').on('click', '.accesskeys div', function () {
var selected = $(this).is('.active');
$('.accesskeys div').removeClass('active');
if (!selected) {
$(this).addClass('active');
}
$('#ftl-id-accesskey').val($('.accesskeys div.active').html());
});
// Select access key using text input
$('#ftl-area .attributes').on('keyup', '#ftl-id-accesskey', function () {
var accesskey = $(this).val();
if (accesskey) {
$('.accesskeys div').removeClass('active');
$('.accesskeys div:contains("' + accesskey + '")').addClass('active');
}
});
}
},
});
}(Pontoon || {}));