Skip to content

Commit

Permalink
ref: updateChoiceFieldTypes()+ updateCollectionLabel()
Browse files Browse the repository at this point in the history
  • Loading branch information
theus77 committed Feb 15, 2024
1 parent eca4e70 commit 7ff651b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 121 deletions.
114 changes: 0 additions & 114 deletions EMS/admin-ui-bundle/_to_be_ref/edit-revision.js

This file was deleted.

96 changes: 89 additions & 7 deletions EMS/admin-ui-bundle/assets/js/edit-revision.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,86 @@ let synch = true
const primaryBox = $('#revision-primary-box')
const updateMode = primaryBox.data('update-mode')

function updateCollectionLabel () {
$('.collection-panel').each(function () {
const collectionPanel = $(this)
const fieldLabel = collectionPanel.data('label-field')
if (fieldLabel) {
$(this).children(':first').children(':first').children().each(function () {
const val = $(this).find('input[name*=' + fieldLabel + ']').val()
if (typeof val !== 'undefined') {
$(this).find('.collection-label-field').html(' | ' + val)
}
})
}
})
}

function updateChoiceFieldTypes () {
$('.ems-choice-field-type').each(function () {
const choice = $(this)
const collectionName = choice.data('linked-collection')
if (collectionName) {
$('.collection-panel').each(function () {
const collectionPanel = $(this)
if (collectionPanel.data('name') === collectionName) {
const collectionLabelField = choice.data('collection-label-field')

collectionPanel.children('.panel-body').children('.collection-panel-container').children('.collection-item-panel').each(function () {
const collectionItem = $(this)
const index = collectionItem.data('index')
const id = collectionItem.data('id')
let label = ' #' + index

if (collectionLabelField) {
label += ': ' + $('#' + id + '_' + collectionLabelField).val()
}

const multiple = choice.data('multiple')
const expanded = choice.data('expanded')

if (expanded) {
const option = choice.find('input[value="' + index + '"]')
if (option.length) {
const parent = option.closest('.checkbox,.radio')
if ($('#' + id + '__ems_internal_deleted').val() === 'deleted') {
parent.hide()
option.addClass('input-to-hide')
if (multiple) {
option.attr('checked', false)
} else {
option.removeAttr('checked')
}
} else {
option.removeClass('input-to-hide')
parent.find('.checkbox-radio-label-text').text(label)
parent.show()
}
}
} else {
const option = choice.find('option[value="' + index + '"]')
if (option.length) {
if ($('#' + id + '__ems_internal_deleted').val() === 'deleted') {
option.addClass('input-to-hide')
} else {
option.removeClass('input-to-hide')
option.show()
option.text(label)
}
}
}
})
}
})
}

$(this).find('option.input-to-hide').hide()
$(this).find('.input-to-hide').each(function () {
$(this).closest('.checkbox,.radio').hide()
})
})
}

function onChange (allowAutoPublish = false) {
if (updateMode === 'disabled') {
// console.log('No way to save a finalized revision!');
Expand All @@ -22,8 +102,8 @@ function onChange (allowAutoPublish = false) {

synch = false

// updateChoiceFieldTypes();
// updateCollectionLabel();
updateChoiceFieldTypes()
updateCollectionLabel()

if (waitingResponse) {
return
Expand Down Expand Up @@ -84,14 +164,16 @@ function onChange (allowAutoPublish = false) {
})
}

$("form[name=revision]").submit(function( ) {
//disable all pending auto-save
waitingResponse = true;
synch = true;
$('#data-out-of-sync').remove();
$('form[name=revision]').submit(function () {
// disable all pending auto-save
waitingResponse = true
synch = true
$('#data-out-of-sync').remove()
})

window.onload = function () {
updateChoiceFieldTypes()
updateCollectionLabel()
const form = document.querySelector('form[name=revision]')
form.addEventListener(EMS_CHANGE_EVENT, () => onChange())
document.addEventListener(EMS_CTRL_SAVE_EVENT, (event) => { event.detail.parentEvent.preventDefault(); onChange(true) })
Expand Down

0 comments on commit 7ff651b

Please sign in to comment.