@oat-sa/tao-core-ui 1.54.3 → 1.56.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,670 @@
1
+ define(['jquery', 'nouislider', 'ui/resourcemgr', 'ui/tooltip', 'lodash', 'i18n', 'core/mimetype', 'ui/mediaEditor/plugins/mediaAlignment/helper', 'ui/mediaEditor/mediaEditorComponent', 'handlebars', 'lib/dompurify/purify'], function ($$1, nouislider, resourcemgr, tooltip, _, __, mimeType, alignmentHelper, mediaEditorComponent, Handlebars, DOMPurify) { 'use strict';
2
+
3
+ $$1 = $$1 && Object.prototype.hasOwnProperty.call($$1, 'default') ? $$1['default'] : $$1;
4
+ _ = _ && Object.prototype.hasOwnProperty.call(_, 'default') ? _['default'] : _;
5
+ __ = __ && Object.prototype.hasOwnProperty.call(__, 'default') ? __['default'] : __;
6
+ mimeType = mimeType && Object.prototype.hasOwnProperty.call(mimeType, 'default') ? mimeType['default'] : mimeType;
7
+ alignmentHelper = alignmentHelper && Object.prototype.hasOwnProperty.call(alignmentHelper, 'default') ? alignmentHelper['default'] : alignmentHelper;
8
+ mediaEditorComponent = mediaEditorComponent && Object.prototype.hasOwnProperty.call(mediaEditorComponent, 'default') ? mediaEditorComponent['default'] : mediaEditorComponent;
9
+ Handlebars = Handlebars && Object.prototype.hasOwnProperty.call(Handlebars, 'default') ? Handlebars['default'] : Handlebars;
10
+ DOMPurify = DOMPurify && Object.prototype.hasOwnProperty.call(DOMPurify, 'default') ? DOMPurify['default'] : DOMPurify;
11
+
12
+ /*
13
+ * This program is free software; you can redistribute it and/or
14
+ * modify it under the terms of the GNU General Public License
15
+ * as published by the Free Software Foundation; under version 2
16
+ * of the License (non-upgradable).
17
+ *
18
+ * This program is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License
24
+ * along with this program; if not, write to the Free Software
25
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26
+ *
27
+ * Copyright (c) 2022 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
28
+ */
29
+ function getImage(widget) {
30
+ var $img = widget.$original;
31
+ var img = widget.element;
32
+
33
+ if (widget.element.is('figure')) {
34
+ $img = widget.$original.find('img');
35
+ img = _.find(widget.element.getBody().elements, function (elem) {
36
+ return elem.is('img');
37
+ });
38
+ }
39
+
40
+ return {
41
+ $img: $img,
42
+ img: img
43
+ };
44
+ }
45
+
46
+ /*
47
+ * This program is free software; you can redistribute it and/or
48
+ * modify it under the terms of the GNU General Public License
49
+ * as published by the Free Software Foundation; under version 2
50
+ * of the License (non-upgradable).
51
+ *
52
+ * This program is distributed in the hope that it will be useful,
53
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
54
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
55
+ * GNU General Public License for more details.
56
+ *
57
+ * You should have received a copy of the GNU General Public License
58
+ * along with this program; if not, write to the Free Software
59
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
60
+ *
61
+ * Copyright (c) 2021 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
62
+ */
63
+ var mediaSizer = function mediaSizer(media, widget) {
64
+ var _getImage = getImage(widget),
65
+ img = _getImage.img,
66
+ $img = _getImage.$img;
67
+
68
+ var $mediaSpan = widget.$container;
69
+
70
+ if (img.data('responsive') !== media.responsive) {
71
+ // switch between responsive and absolute values
72
+ img.data('responsive', media.responsive); // clean styles from prev mode
73
+
74
+ $img.removeAttr('width');
75
+ $img.removeAttr('height');
76
+ $mediaSpan[0].removeAttribute('style');
77
+ }
78
+
79
+ _(['width', 'height']).each(function (sizeAttr) {
80
+ var val;
81
+
82
+ if (media[sizeAttr] === '' || typeof media[sizeAttr] === 'undefined' || media[sizeAttr] === null) {
83
+ img.removeAttr(sizeAttr);
84
+ $img.removeAttr(sizeAttr);
85
+ $mediaSpan.css(sizeAttr, '');
86
+ } else {
87
+ val = Math.round(media[sizeAttr]);
88
+
89
+ if (media.responsive) {
90
+ val += '%';
91
+ img.attr(sizeAttr, val);
92
+ $img.attr(sizeAttr, '100%');
93
+ } else {
94
+ img.attr(sizeAttr, val);
95
+ $img.attr(sizeAttr, val);
96
+ }
97
+
98
+ if (media.responsive || sizeAttr === 'width') {
99
+ $mediaSpan.css(sizeAttr, val);
100
+ }
101
+ } //trigger choice container size adaptation
102
+
103
+
104
+ widget.$container.trigger('contentChange.qti-widget');
105
+ });
106
+ };
107
+
108
+ /*
109
+ * This program is free software; you can redistribute it and/or
110
+ * modify it under the terms of the GNU General Public License
111
+ * as published by the Free Software Foundation; under version 2
112
+ * of the License (non-upgradable).
113
+ *
114
+ * This program is distributed in the hope that it will be useful,
115
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
116
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
117
+ * GNU General Public License for more details.
118
+ *
119
+ * You should have received a copy of the GNU General Public License
120
+ * along with this program; if not, write to the Free Software
121
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
122
+ *
123
+ * Copyright (c) 2021 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
124
+ */
125
+
126
+ var getMedia = function getMedia(imgQtiElement, $imgNode, cb) {
127
+ //init data-responsive:
128
+ if (typeof imgQtiElement.data('responsive') === 'undefined') {
129
+ if (imgQtiElement.attr('width') && !/[0-9]+%/.test(imgQtiElement.attr('width'))) {
130
+ imgQtiElement.data('responsive', false);
131
+ } else {
132
+ imgQtiElement.data('responsive', true);
133
+ }
134
+ }
135
+
136
+ if (typeof imgQtiElement.attr('original-width') !== 'undefined' && typeof imgQtiElement.attr('original-height') !== 'undefined' && typeof imgQtiElement.attr('type') !== 'undefined' && typeof imgQtiElement.attr('src') !== 'undefined' && typeof imgQtiElement.attr('width') !== 'undefined' && typeof imgQtiElement.attr('height') !== 'undefined') {
137
+ cb({
138
+ $node: $imgNode,
139
+ type: imgQtiElement.attr('type'),
140
+ src: imgQtiElement.attr('src'),
141
+ width: imgQtiElement.attr('width'),
142
+ height: imgQtiElement.attr('height'),
143
+ responsive: imgQtiElement.data('responsive')
144
+ });
145
+ } else {
146
+ mimeType.getResourceType($imgNode.attr('src'), function (err, type) {
147
+ imgQtiElement.attr('type', type);
148
+ cb({
149
+ $node: $imgNode,
150
+ type: imgQtiElement.attr('type'),
151
+ src: imgQtiElement.attr('src'),
152
+ width: imgQtiElement.attr('width'),
153
+ height: imgQtiElement.attr('height'),
154
+ responsive: imgQtiElement.data('responsive')
155
+ });
156
+ });
157
+ }
158
+ };
159
+
160
+ var getMediaCb = function getMediaCb(media, widget, mediaEditor, options) {
161
+ var $mediaResizer = widget.$form.find('.img-resizer');
162
+ media.$container = widget.$container.parents('.widget-box');
163
+
164
+ if (media.$container.length) {
165
+ // eslint-disable-next-line no-unused-vars
166
+ mediaEditor = mediaEditorComponent($mediaResizer, media, options).on('change', function (nMedia) {
167
+ media = nMedia;
168
+ widget.$original.prop('style', null); // not allowed by qti
169
+
170
+ widget.$original.removeAttr('style');
171
+ alignmentHelper.positionFloat(widget, media.align);
172
+ mediaSizer(media, widget);
173
+ widget.$original.removeClass('hidden');
174
+ });
175
+ }
176
+ };
177
+
178
+ function initMediaEditor(widget, mediaEditor, options) {
179
+ if (mediaEditor) {
180
+ mediaEditor.destroy();
181
+ }
182
+
183
+ if (!widget.$form.find('input[name=src]').val()) {
184
+ return;
185
+ }
186
+
187
+ var _getImage = getImage(widget),
188
+ img = _getImage.img,
189
+ $img = _getImage.$img;
190
+
191
+ getMedia(img, $img, function (m) {
192
+ return getMediaCb(m, widget, mediaEditor, options);
193
+ });
194
+ }
195
+
196
+ /*
197
+ * This program is free software; you can redistribute it and/or
198
+ * modify it under the terms of the GNU General Public License
199
+ * as published by the Free Software Foundation; under version 2
200
+ * of the License (non-upgradable).
201
+ *
202
+ * This program is distributed in the hope that it will be useful,
203
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
204
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
205
+ * GNU General Public License for more details.
206
+ *
207
+ * You should have received a copy of the GNU General Public License
208
+ * along with this program; if not, write to the Free Software
209
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
210
+ *
211
+ * Copyright (c) 2021 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
212
+ */
213
+
214
+ /**
215
+ * Extract a default label from a file/path name
216
+ * @param {String} fileName - the file/path
217
+ * @returns {String} a label
218
+ */
219
+ function extractLabel(fileName) {
220
+ return fileName.replace(/\.[^.]+$/, '').replace(/^(.*)\//, '').replace(/\W/, ' ').substr(0, 255);
221
+ }
222
+
223
+ /*
224
+ * This program is free software; you can redistribute it and/or
225
+ * modify it under the terms of the GNU General Public License
226
+ * as published by the Free Software Foundation; under version 2
227
+ * of the License (non-upgradable).
228
+ *
229
+ * This program is distributed in the hope that it will be useful,
230
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
231
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
232
+ * GNU General Public License for more details.
233
+ *
234
+ * You should have received a copy of the GNU General Public License
235
+ * along with this program; if not, write to the Free Software
236
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
237
+ *
238
+ * Copyright (c) 2021 (original work) Open Assessment Technologies SA ;
239
+ *
240
+ */
241
+ var initAdvanced = function initAdvanced(widget) {
242
+ var $form = widget.$form;
243
+
244
+ var _getImage = getImage(widget),
245
+ img = _getImage.img;
246
+
247
+ var src = img ? img.attr('src') : '';
248
+
249
+ if (src) {
250
+ $form.find('[data-role=advanced]').show();
251
+ } else {
252
+ $form.find('[data-role=advanced]').hide();
253
+ }
254
+ };
255
+ var initUpload = function initUpload(widget) {
256
+ var $form = widget.$form;
257
+ var options = widget.options;
258
+
259
+ var _getImage2 = getImage(widget),
260
+ img = _getImage2.img;
261
+
262
+ var $uploadTrigger = $form.find('[data-role="upload-trigger"]');
263
+ var $src = $form.find('input[name=src]');
264
+ var $alt = $form.find('input[name=alt]');
265
+
266
+ var _openResourceMgr = function _openResourceMgr() {
267
+ $uploadTrigger.resourcemgr({
268
+ title: __('Please select an image file from the resource manager. You can add files from your computer with the button "Add file(s)".'),
269
+ appendContainer: options.mediaManager.appendContainer,
270
+ mediaSourcesUrl: options.mediaManager.mediaSourcesUrl,
271
+ browseUrl: options.mediaManager.browseUrl,
272
+ uploadUrl: options.mediaManager.uploadUrl,
273
+ deleteUrl: options.mediaManager.deleteUrl,
274
+ downloadUrl: options.mediaManager.downloadUrl,
275
+ fileExistsUrl: options.mediaManager.fileExistsUrl,
276
+ params: {
277
+ uri: options.uri,
278
+ lang: options.lang,
279
+ filters: [{
280
+ mime: 'image/jpeg'
281
+ }, {
282
+ mime: 'image/png'
283
+ }, {
284
+ mime: 'image/gif'
285
+ }, {
286
+ mime: 'image/svg+xml'
287
+ }, {
288
+ mime: 'application/x-gzip',
289
+ extension: 'svgz'
290
+ }]
291
+ },
292
+ pathParam: 'path',
293
+ path: options.mediaManager.path,
294
+ root: options.mediaManager.root,
295
+ select: function select(e, files) {
296
+ var file, alt;
297
+ var confirmBox, cancel, save;
298
+
299
+ if (files && files.length) {
300
+ file = files[0].file;
301
+ alt = files[0].alt;
302
+ $src.val(file);
303
+
304
+ if ($$1.trim($alt.val()) === '') {
305
+ if (alt === '') {
306
+ alt = extractLabel(file);
307
+ }
308
+
309
+ img.attr('alt', alt);
310
+ $alt.val(alt).trigger('change');
311
+ } else {
312
+ confirmBox = $$1('.change-alt-modal-feedback', $form);
313
+ cancel = confirmBox.find('.cancel');
314
+ save = confirmBox.find('.save');
315
+ $$1('.alt-text', confirmBox).html("\"".concat($alt.val(), "\"<br>").concat(__('with'), "<br>\"").concat(alt, "\" ?"));
316
+ confirmBox.modal({
317
+ width: 500
318
+ });
319
+ save.off('click').on('click', function () {
320
+ img.attr('alt', alt);
321
+ $alt.val(alt).trigger('change');
322
+ confirmBox.modal('close');
323
+ });
324
+ cancel.off('click').on('click', function () {
325
+ confirmBox.modal('close');
326
+ });
327
+ }
328
+
329
+ _.defer(function () {
330
+ img.attr('off-media-editor', 1);
331
+ $src.trigger('change');
332
+ });
333
+ }
334
+ },
335
+ open: function open() {
336
+ // hide tooltip if displayed
337
+ if ($src.data('$tooltip')) {
338
+ $src.blur().data('$tooltip').hide();
339
+ }
340
+ },
341
+ close: function close() {
342
+ // triggers validation:
343
+ $src.blur();
344
+ }
345
+ });
346
+ };
347
+
348
+ $uploadTrigger.on('click', _openResourceMgr); //if empty, open file manager immediately
349
+
350
+ if (!$src.val()) {
351
+ _openResourceMgr();
352
+ }
353
+ };
354
+ function initAll(widget, mediaEditor, options) {
355
+ initAdvanced(widget);
356
+ initMediaEditor(widget, mediaEditor, options);
357
+ initUpload(widget);
358
+ }
359
+
360
+ function _typeof(obj) {
361
+ "@babel/helpers - typeof";
362
+
363
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
364
+ return typeof obj;
365
+ } : function (obj) {
366
+ return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
367
+ }, _typeof(obj);
368
+ }
369
+
370
+ function Helpers0 (hb) {
371
+ //register a i18n helper
372
+ hb.registerHelper('__', function (key) {
373
+ return __(key);
374
+ });
375
+ /**
376
+ * Register dompurify helper
377
+ *
378
+ * https://github.com/cure53/DOMPurify
379
+ * with config SAFE_FOR_TEMPLATES: true
380
+ * to make output safe for template systems
381
+ */
382
+
383
+ hb.registerHelper('dompurify', function (context) {
384
+ return DOMPurify.sanitize(context);
385
+ });
386
+ /**
387
+ * Register join helper
388
+ *
389
+ * Example :
390
+ * var values = {a:v1, b:v2, c:v3};
391
+ * Using {{{join attributes '=' ' ' '"'}}} will return : a="v1" b="v2" c="v3"
392
+ * Using {{{join values null ' or ' '*'}}} will return : *v1* or *v2* or *v3*
393
+ */
394
+
395
+ hb.registerHelper('join', function (arr, keyValueGlue, fragmentGlue, wrapper) {
396
+ var fragments = [];
397
+ keyValueGlue = typeof keyValueGlue === 'string' ? keyValueGlue : undefined;
398
+ fragmentGlue = typeof fragmentGlue === 'string' ? fragmentGlue : ' ';
399
+ wrapper = typeof wrapper === 'string' ? wrapper : '"';
400
+
401
+ _.forIn(arr, function (value, key) {
402
+ var fragment = '';
403
+
404
+ if (value !== null || value !== undefined) {
405
+ if (typeof value === 'boolean') {
406
+ value = value ? 'true' : 'false';
407
+ } else if (_typeof(value) === 'object') {
408
+ value = _.values(value).join(' ');
409
+ }
410
+ } else {
411
+ value = '';
412
+ }
413
+
414
+ if (keyValueGlue !== undefined) {
415
+ fragment += key + keyValueGlue;
416
+ }
417
+
418
+ fragment += wrapper + value + wrapper;
419
+ fragments.push(fragment);
420
+ });
421
+
422
+ return fragments.join(fragmentGlue);
423
+ }); //register a classic "for loop" helper
424
+ //it also adds a local variable "i" as the index in each iteration loop
425
+
426
+ hb.registerHelper('for', function (startIndex, stopIndex, increment, options) {
427
+ var ret = '';
428
+ startIndex = parseInt(startIndex);
429
+ stopIndex = parseInt(stopIndex);
430
+ increment = parseInt(increment);
431
+
432
+ for (var i = startIndex; i < stopIndex; i += increment) {
433
+ ret += options.fn(_.extend({}, this, {
434
+ i: i
435
+ }));
436
+ }
437
+
438
+ return ret;
439
+ });
440
+ hb.registerHelper('equal', function (var1, var2, options) {
441
+ if (var1 == var2) {
442
+ return options.fn(this);
443
+ } else {
444
+ return options.inverse(this);
445
+ }
446
+ }); // register a "get property" helper
447
+ // it gets the named property from the provided context
448
+
449
+ hb.registerHelper('property', function (name, context) {
450
+ return context[name] || '';
451
+ }); // register an 'includes' helper
452
+ // it checks if value is in array
453
+
454
+ hb.registerHelper('includes', function (haystack, needle, options) {
455
+ if (_.contains(haystack, needle)) {
456
+ return options.fn(this);
457
+ }
458
+ });
459
+ }
460
+
461
+ if (!Helpers0.__initialized) {
462
+ Helpers0(Handlebars);
463
+ Helpers0.__initialized = true;
464
+ }
465
+ var Template = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
466
+ this.compilerInfo = [4,'>= 1.0.0'];
467
+ helpers = this.merge(helpers, Handlebars.helpers); data = data || {};
468
+ var stack1, helper, options, helperMissing=helpers.helperMissing;
469
+
470
+
471
+ stack1 = (helper = helpers.dompurify || (depth0 && depth0.dompurify),options={hash:{},data:data},helper ? helper.call(depth0, (depth0 && depth0.text), options) : helperMissing.call(depth0, "dompurify", (depth0 && depth0.text), options));
472
+ if(stack1 || stack1 === 0) { return stack1; }
473
+ else { return ''; }
474
+ });
475
+ function tplCaptionText(data, options, asString) {
476
+ var html = Template(data, options);
477
+ return (asString || true) ? html : $(html);
478
+ }
479
+
480
+ /*
481
+ * This program is free software; you can redistribute it and/or
482
+ * modify it under the terms of the GNU General Public License
483
+ * as published by the Free Software Foundation; under version 2
484
+ * of the License (non-upgradable).
485
+ *
486
+ * This program is distributed in the hope that it will be useful,
487
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
488
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
489
+ * GNU General Public License for more details.
490
+ *
491
+ * You should have received a copy of the GNU General Public License
492
+ * along with this program; if not, write to the Free Software
493
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
494
+ *
495
+ * Copyright (c) 2022 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
496
+ */
497
+ var options = {
498
+ mediaDimension: {
499
+ active: true
500
+ },
501
+ mediaAlignment: {
502
+ active: true
503
+ }
504
+ };
505
+
506
+ var getImage$1 = function getImage(widget) {
507
+ return widget.$original.find('img');
508
+ };
509
+
510
+ var getCaption = function getCaption(widget) {
511
+ return widget.$original.find('figcaption');
512
+ };
513
+
514
+ var getImageElement = function getImageElement(widget) {
515
+ return _.find(widget.element.getBody().elements, function (elem) {
516
+ return elem.is('img');
517
+ });
518
+ };
519
+
520
+ var getCaptionElement = function getCaptionElement(widget) {
521
+ return _.find(widget.element.getBody().elements, function (elem) {
522
+ return elem.is('figcaption');
523
+ });
524
+ };
525
+
526
+ var formCallbacks = function formCallbacks(_ref) {
527
+ var widget = _ref.widget,
528
+ formElement = _ref.formElement,
529
+ mediaEditor = _ref.mediaEditor,
530
+ togglePlaceholder = _ref.togglePlaceholder;
531
+ var $img = getImage$1(widget);
532
+ var $figcaption = getCaption(widget);
533
+ var imageElem = getImageElement(widget);
534
+ var figcaptionElem = getCaptionElement(widget);
535
+ return {
536
+ src: _.throttle(function (elem, value) {
537
+ imageElem.attr('src', value);
538
+ $img.attr('src', widget.getAssetManager().resolve(value));
539
+ $img.trigger('contentChange.qti-widget').change();
540
+ togglePlaceholder(widget);
541
+ imageElem.removeAttr('off-media-editor');
542
+
543
+ if (widget.$form.find('[data-role=advanced]').is(':hidden')) {
544
+ var initPanel = function initPanel() {
545
+ initAdvanced(widget);
546
+ initMediaEditor(widget, mediaEditor, options);
547
+ };
548
+
549
+ if ($img[0].complete) {
550
+ initPanel();
551
+ } else {
552
+ $img.on('load.widget-panel', function () {
553
+ initPanel();
554
+ $img.off('.widget-panel');
555
+ });
556
+ }
557
+ }
558
+ }, 1000),
559
+ alt: function alt(elem, value) {
560
+ imageElem.attr('alt', value);
561
+ },
562
+ figcaption: function figcaption(elem, value) {
563
+ // using dompurify to clean <script> tags
564
+ var text = tplCaptionText({
565
+ text: value
566
+ });
567
+
568
+ if (figcaptionElem && value) {
569
+ // update existing capture
570
+ $figcaption.html(text);
571
+ figcaptionElem.body(text);
572
+ } else if (!figcaptionElem && value) {
573
+ // add capture
574
+ figcaptionElem = widget.element.addCaption(text);
575
+ $figcaption = $$1("<figcaption>".concat(text, "</figcaption>"));
576
+ widget.$original.append($figcaption);
577
+ } else if (figcaptionElem && !value) {
578
+ widget.element.removeCaption();
579
+ $figcaption.remove();
580
+ $figcaption = null;
581
+ figcaptionElem = null;
582
+ }
583
+ },
584
+ longdesc: formElement.getAttributeChangeCallback()
585
+ };
586
+ };
587
+
588
+ var initForm = function initForm(_ref2) {
589
+ var widget = _ref2.widget,
590
+ formElement = _ref2.formElement,
591
+ formTpl = _ref2.formTpl,
592
+ mediaEditor = _ref2.mediaEditor,
593
+ togglePlaceholder = _ref2.togglePlaceholder;
594
+ var imageElem = getImageElement(widget);
595
+ var figcaptionElem = getCaptionElement(widget);
596
+ widget.$form.html(formTpl({
597
+ baseUrl: widget.options.baseUrl || '',
598
+ src: imageElem.attr('src'),
599
+ alt: imageElem.attr('alt'),
600
+ figcaption: figcaptionElem ? figcaptionElem.body() : ''
601
+ }));
602
+ widget.$form.find('textarea#figcaption').on('focus.qti-widget', function () {
603
+ return widget.$container.addClass('edit-figcaption');
604
+ }).on('blur.qti-widget', function () {
605
+ return widget.$container.removeClass('edit-figcaption');
606
+ }); // init upload, advanced and media editor
607
+
608
+ initAll(widget, mediaEditor, options); // init standard ui widget
609
+
610
+ formElement.initWidget(widget.$form); // init data change callbacks
611
+
612
+ formElement.setChangeCallbacks(widget.$form, widget.element, formCallbacks({
613
+ widget: widget,
614
+ formElement: formElement,
615
+ mediaEditor: mediaEditor,
616
+ togglePlaceholder: togglePlaceholder
617
+ }));
618
+ };
619
+
620
+ function FigureStateActive (stateFactory, ActiveState, formTpl, formElement, inlineHelper) {
621
+ /**
622
+ * media Editor instance if has been initialized
623
+ * @type {null}
624
+ */
625
+ var mediaEditor = null;
626
+ var textareaObserver = null;
627
+ var texareaHTMLElem = null;
628
+ var ImgStateActive = stateFactory.extend(ActiveState, function () {
629
+ this.initForm();
630
+ }, function () {
631
+ this.widget.$form.find('textarea#figcaption').off('.qti-widget');
632
+
633
+ if (textareaObserver) {
634
+ textareaObserver.unobserve(texareaHTMLElem);
635
+ }
636
+
637
+ this.widget.$form.empty();
638
+ });
639
+
640
+ ImgStateActive.prototype.initForm = function () {
641
+ initForm({
642
+ widget: this.widget,
643
+ formElement: formElement,
644
+ formTpl: formTpl,
645
+ mediaEditor: mediaEditor,
646
+ togglePlaceholder: inlineHelper.togglePlaceholder
647
+ });
648
+ var figurelem = this.widget.element;
649
+ var $texarea = this.widget.$form.find('textarea#figcaption');
650
+ texareaHTMLElem = $texarea[0];
651
+
652
+ function outputsize() {
653
+ figurelem.data('heigthCaptionInput', $texarea.height());
654
+ }
655
+
656
+ if (typeof ResizeObserver !== 'undefined') {
657
+ textareaObserver = new ResizeObserver(outputsize).observe(texareaHTMLElem);
658
+
659
+ if (figurelem.data('heigthCaptionInput')) {
660
+ $texarea.height(figurelem.data('heigthCaptionInput'));
661
+ }
662
+ }
663
+ };
664
+
665
+ return ImgStateActive;
666
+ }
667
+
668
+ return FigureStateActive;
669
+
670
+ });