Skip to content

Cancel button for metadata modal #329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions language/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ H5PEditor.language.core = {
newChangeHasBeenLogged: 'New change has been logged',
loggedChanges: 'Logged changes',
noChangesHaveBeenLogged: 'No changes have been logged',
confirmCloseMetadataModal: 'Are you would like to close this modal? Any unsaved changes will be lost.',
errorHeader: 'An error occured',
errorCalculatingMaxScore: 'Could not calculate the max score for this content. The max score is assumed to be 0. Contact your administrator if this isn’t correct.',
maxScoreSemanticsMissing: 'Could not find the expected semantics in the content.',
Expand Down
3 changes: 2 additions & 1 deletion scripts/h5peditor-metadata-author-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ H5PEditor.metadataAuthorWidget = function (semantics, params, $wrapper, parent)

return {
addAuthor: addAuthor,
addDefaultAuthor: addDefaultAuthor
addDefaultAuthor: addDefaultAuthor,
renderAuthorList: renderAuthorList
};
};
4 changes: 4 additions & 0 deletions scripts/h5peditor-metadata-changelog-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,8 @@ H5PEditor.metadataChangelogWidget = function (semantics, params, $wrapper, paren
async: true
});
}

return {
render: render
};
};
60 changes: 59 additions & 1 deletion scripts/h5peditor-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ H5PEditor.MetadataForm = (function (EventDispatcher, $, metadataSemantics) {
*/
function MetadataForm(parent, params, $container, hasExtraTitleField, populateTitleField) {
var self = this;
let paramsOnModalOpen;

// Initialize event inheritance
EventDispatcher.call(self);
Expand Down Expand Up @@ -39,6 +40,8 @@ H5PEditor.MetadataForm = (function (EventDispatcher, $, metadataSemantics) {
$('.h5peditor').append($overlay);
$wrapper.css('margin-top', (offset > 20 ? offset : 20) + 'px');

paramsOnModalOpen = structuredClone(params);

// Focus title field
titleField.$input.focus();
};
Expand All @@ -51,6 +54,51 @@ H5PEditor.MetadataForm = (function (EventDispatcher, $, metadataSemantics) {
$overlay.detach();
};

/**
* @private
*/
const resetMetadataForm = function () {
titleField.$input.val(paramsOnModalOpen.title).change();
H5PEditor.findField('a11yTitle', self).$input.val(paramsOnModalOpen.a11yTitle ?? '').change();
H5PEditor.findField('license', self).$select.val(paramsOnModalOpen.license).change();
H5PEditor.findField('licenseVersion', self).$select.val(paramsOnModalOpen.licenseVersion ?? '').change();
H5PEditor.findField('yearFrom', self).$input.val(paramsOnModalOpen.yearFrom ?? '').change();
H5PEditor.findField('yearTo', self).$input.val(paramsOnModalOpen.yearTo ?? '').change();
H5PEditor.findField('source', self).$input.val(paramsOnModalOpen.source ?? '').change();
H5PEditor.findField('licenseExtras', self).$input.val(paramsOnModalOpen.licenseExtras ?? '').change();

params.authors = structuredClone(paramsOnModalOpen.authors);
metadataAuthorWidget.renderAuthorList();

params.changes = structuredClone(paramsOnModalOpen.changes);
metadataChangelogWidget.render();

if (paramsOnModalOpen.authorComments) {
params.authorComments = paramsOnModalOpen.authorComments;
$('.field-name-authorComments').find('textarea').val(paramsOnModalOpen.authorComments);
} else {
delete params.authorComments;
$('.field-name-authorComments').find('textarea').val('');
}
}

/**
* @private
*/
const handleClose = function () {
// if changes have been made to the params object, display a confirmation
if (JSON.stringify(params) !== JSON.stringify(paramsOnModalOpen)) {
if (confirm(H5PEditor.t('core', 'confirmCloseMetadataModal'))) {
resetMetadataForm();
closePopup();
}
} else {
resetMetadataForm();
closePopup();
}
};


/**
* @private
*/
Expand Down Expand Up @@ -213,13 +261,23 @@ H5PEditor.MetadataForm = (function (EventDispatcher, $, metadataSemantics) {
'</div>' +
'<div class="metadata-button-wrapper">' +
'<button href="#" class="h5p-metadata-button h5p-save">' + t('saveMetadata') + '</button>' +
'<button href="#" class="h5p-metadata-button h5p-cancel">' + t('cancel') + '</button>' +
'</div>' +
'</div>' +
'</div>');

// Handle click on save button
$wrapper.find('.h5p-save').click(handleSaveButtonClick);

// Handle click on cancel button
$wrapper.find('.h5p-cancel').click(handleClose);

$overlay.click(function (e) {
if (!$wrapper.is(e.target) && !$wrapper.find('*').is(e.target)) {
handleClose();
}
});

const $fieldsWrapper = $('<div/>', {
'class': 'h5p-metadata-fields-wrapper',
appendTo: $wrapper
Expand Down Expand Up @@ -314,7 +372,7 @@ H5PEditor.MetadataForm = (function (EventDispatcher, $, metadataSemantics) {
children = children.concat(self.children);

// Append the metadata changelog widget (Not the same type of widgets as the rest of editor fields)
H5PEditor.metadataChangelogWidget([findField('changes').field], params, $fieldsWrapper, self);
const metadataChangelogWidget = H5PEditor.metadataChangelogWidget([findField('changes').field], params, $fieldsWrapper, self);
children = children.concat(self.children);

// Append the Additional information group
Expand Down
2 changes: 1 addition & 1 deletion styles/css/application.css

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions styles/scss/_metadata-form.scss
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@
background-color: #095345;
}

.h5p-cancel {
border-radius: 0.3rem;
margin-left: 12px;
padding: 0.75rem;
}

.h5peditor-label {
font-size: 0.8em;
color: #333;
Expand Down