/* global ns*/ H5PEditor.widgets.select = H5PEditor.Select = (function (E) { /** * Initialize a new widget. * * @param {object} parent * @param {object} field * @param {object} params * @param {function} setValue * @returns {_L3.C} */ function C(parent, field, params, setValue) { this.field = field; this.value = params; this.setValue = setValue; // Setup event dispatching on change this.changes = []; this.triggerListeners = function (value) { // Run callbacks for (var i = 0; i < this.changes.length; i++) { this.changes[i](value); } }; } /** * Append widget to the DOM. * * @param {jQuery} $wrapper * @returns {undefined} */ C.prototype.appendTo = function ($wrapper) { var that = this; this.$item = E.$(this.createHtml()).appendTo($wrapper); this.$select = this.$item.find('select'); this.$errors = this.$item.children('.h5p-errors'); this.$select.change(function () { var val = that.validate(); if (val !== false) { that.value = val; that.setValue(that.field, val); that.triggerListeners(val); } }); }; /** * Generate HTML for the widget. * * @returns {String} HTML. */ C.prototype.createHtml = function () { const id = ns.getNextFieldId(this.field); let options = ''; if (this.field.optional === true || this.field.default === undefined) { options = E.createOption('-', '-'); } options += C.createOptionsHtml(this.field.options, this.value); let select = '