/* global ns */ /** * Callback for setting new parameters. * * @callback H5PEditor.newParams * @param {Object} field Current field details. * @param {Object} params New parameters. */ /** * Create a field where one can select and include another library to the form. * * @class H5PEditor.Library * @extends H5P.EventDispatcher * @param {Object} parent Parent field in editor. * @param {Object} field Details for current field. * @param {Object} params Default parameters. * @param {newParams} setValue Callback for setting new parameters. */ ns.Library = function (parent, field, params, setValue) { var self = this; H5P.EventDispatcher.call(this); if (params === undefined) { this.params = { params: {} }; // If you do a console log here it might show that this.params is // something else than what we set it to. One of life's big mysteries... setValue(field, this.params); } else { this.params = params; } this.field = field; this.parent = parent; this.changes = []; this.optionsLoaded = false; this.library = parent.library + '/' + field.name; this.passReadies = true; parent.ready(function () { self.passReadies = false; }); // Confirmation dialog for changing library this.confirmChangeLibrary = new H5P.ConfirmationDialog({ headerText: H5PEditor.t('core', 'changeLibrary'), dialogText: H5PEditor.t('core', 'confirmChangeLibrary') }).appendTo(document.body); // Load library on confirmation this.confirmChangeLibrary.on('confirmed', function () { self.loadLibrary(self.$select.val()); }); // Revert to current library on cancel this.confirmChangeLibrary.on('canceled', function () { self.$select.val(self.currentLibrary); }); H5P.externalDispatcher.on('datainclipboard', this.updateCopyPasteButtons.bind(this)); }; ns.Library.prototype = Object.create(H5P.EventDispatcher.prototype); ns.Library.prototype.constructor = ns.Library; /** * Update state of copy and paste buttons dependent on what is currently in * the clipboard */ ns.Library.prototype.updateCopyPasteButtons = function () { if (!window.localStorage || !this.libraries) { return; } // Check if content type is supported here const pasteCheck = ns.canPastePlus(H5P.getClipboard(), this.libraries); const canPaste = pasteCheck.canPaste; const canCopy = (this.currentLibrary !== undefined && this.currentLibrary !== '-'); this.$copyButton .prop('disabled', !canCopy) .toggleClass('disabled', !canCopy); this.$pasteButton .text(ns.t('core', canCopy ? 'pasteAndReplaceButton' : 'pasteButton')) .attr('title', canPaste ? H5PEditor.t('core', 'pasteFromClipboard') : pasteCheck.description) .toggleClass('disabled', !canPaste) .prop('disabled', !canPaste); }; /** * Append the library selector to the form. * * @alias H5PEditor.Library#appendTo * @param {H5P.jQuery} $wrapper */ ns.Library.prototype.appendTo = function ($wrapper) { var that = this; var html = '
'; const id = ns.getNextFieldId(this.field); if (this.field.label !== 0 && this.field.label !== undefined) { html += '
' + '' + '
'; } html += ns.createDescription(this.field.description, id); html += '