[ Index ]

PHP Cross Reference of Moodle 310

title

Body

[close]

/h5p/h5plib/v124/joubel/editor/scripts/ -> h5peditor-metadata-author-widget.js (source)

   1  /**
   2   * Creates a widget to add author information to a form
   3   *
   4   * @param {object} semantics
   5   * @param {object} params
   6   * @param {object} group
   7   * @param {mixed} parent used in processSemanticsChunk()
   8   * @returns {ns.Coordinates}
   9   */
  10  H5PEditor.metadataAuthorWidget = function (semantics, params, $wrapper, parent) {
  11    if (!params.authors) {
  12      params.authors = [];
  13    }
  14  
  15    const $ = H5PEditor.$;
  16  
  17    const widget = $('<div class="field h5p-metadata-author-widget"></div>');
  18  
  19    var $authorData = $('<div class="h5p-author-data"></div>');
  20    widget.append($authorData);
  21  
  22    H5PEditor.processSemanticsChunk(semantics, {}, $authorData, parent);
  23  
  24    // Get references to the fields
  25    var nameField = H5PEditor.findField('name', parent);
  26    var roleField = H5PEditor.findField('role', parent);
  27  
  28    var $button = $('<div class="field authorList">' +
  29      '<button type="button" class="h5p-metadata-button inverted h5p-save-author">' +
  30        H5PEditor.t('core', 'addAuthor') +
  31      '</button>' +
  32    '</div>').children('button').click(function (event) {
  33  
  34      // Temporarily set name as mandatory to get the error messages only when
  35      // clicking the Add Author button
  36      nameField.field.optional = false;
  37      var name = nameField.validate();
  38      nameField.field.optional = true;
  39      var role = roleField.validate();
  40  
  41      if (!name) {
  42        return;
  43      }
  44  
  45      // Don't add author if already in list with the same role
  46      const authorDuplicate = params.authors.some(function (author) {
  47        return author.name === name && author.role === role;
  48      });
  49      if (authorDuplicate) {
  50        resetForm();
  51        return;
  52      }
  53  
  54      addAuthor(name, role);
  55    }).end();
  56    $authorData.append($button);
  57  
  58    var authorListWrapper = $('<div class="h5p-author-list-wrapper"><ul class="h5p-author-list"></ul></div>');
  59    widget.append(authorListWrapper);
  60    renderAuthorList();
  61  
  62    widget.appendTo($wrapper);
  63  
  64    /**
  65     * Add an author to the list of authors
  66     * @param {string} [name]
  67     * @param {string} [role]
  68     */
  69    function addAuthor(name, role) {
  70      params.authors.push({
  71        name: name,
  72        role: role
  73      });
  74  
  75      renderAuthorList();
  76      resetForm();
  77    }
  78  
  79    /**
  80     * Add default/current author to list of authors
  81     *
  82     * @param {string} fallbackName Name to fallback to if there is no valid name chosen already
  83     * @param {string} fallbackRole Role to fallback to if there is no valid role chosen already
  84     */
  85    function addDefaultAuthor(fallbackName, fallbackRole) {
  86      var name = nameField.validate();
  87  
  88      if (!name) {
  89        name = fallbackName;
  90      }
  91  
  92      var role = roleField.validate();
  93  
  94      if (!role) {
  95        role = fallbackRole;
  96      }
  97  
  98      addAuthor(name, role);
  99    }
 100  
 101    /**
 102     * Resets the form
 103     */
 104    function resetForm() {
 105      nameField.$input.val('');
 106    }
 107  
 108    /**
 109     * Remove author from list.
 110     *
 111     * @param {object} author - Author to be removed.
 112     * @param {string} author.name - Author name.
 113     * @param {string} author.role - Author role.
 114     */
 115    function removeAuthor(author) {
 116      params.authors = params.authors.filter(function (e) {
 117        return (e !== author);
 118      });
 119  
 120      renderAuthorList();
 121    }
 122  
 123    function renderAuthorList() {
 124      var wrapper = widget.find('.h5p-author-list-wrapper');
 125      wrapper.empty();
 126  
 127      const authorList = $('<ul></ul>');
 128      params.authors.forEach(function (author) {
 129        // Name and role
 130        var listItem = $('<li>', {
 131          html: H5PEditor.htmlspecialchars(author.name),
 132          append: $('<span>', {
 133            'class': 'h5p-metadata-role',
 134            html: author.role
 135          })
 136        });
 137  
 138        // The delete-button
 139        $('<button>', {
 140          type: 'button',
 141          'class': 'h5p-metadata-icon-button',
 142          click: function () {
 143            if (confirm(H5PEditor.t('core', 'confirmRemoveAuthor'))) {
 144              removeAuthor(author);
 145            }
 146          }
 147        }).appendTo(listItem);
 148  
 149        authorList.append(listItem);
 150      });
 151  
 152      wrapper.append(authorList);
 153    }
 154  
 155    return {
 156      addAuthor: addAuthor,
 157      addDefaultAuthor: addDefaultAuthor
 158    };
 159  };


Generated: Wed Jan 22 11:59:49 2025 Cross-referenced by PHPXref 0.7.1