-
Notifications
You must be signed in to change notification settings - Fork 59
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
Save As Fragment #1433
base: dev
Are you sure you want to change the base?
Save As Fragment #1433
Changes from 18 commits
d942347
da5bc6b
9e3f782
59516ad
0722eb7
5668a5f
e8e4118
2779112
f998f9c
27a05aa
f07043b
2be1d13
87bd533
2437bc4
a7d97f3
a855a8f
6340fbc
7b961c6
2eb8d6a
aa04649
6c0614f
e03180e
38139df
751e5e1
acbfe0c
0f84c3b
2a2de15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
~ Copyright 2024 Adobe | ||
~ | ||
~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
~ you may not use this file except in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, | ||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
~ See the License for the specific language governing permissions and | ||
~ limitations under the License. | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ | ||
|
||
(function($, channel, Coral) { | ||
dmaurya929 marked this conversation as resolved.
Show resolved
Hide resolved
dmaurya929 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"use strict"; | ||
|
||
window.CQ = window.CQ || {}; | ||
dmaurya929 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
window.CQ.FormsCoreComponents = window.CQ.FormsCoreComponents || {}; | ||
window.CQ.FormsCoreComponents.Utils = window.CQ.FormsCoreComponents.Utils || {}; | ||
const DataModel = window.CQ.FormsCoreComponents.Utils.DataModel = window.CQ.FormsCoreComponents.Utils.DataModel || {}; | ||
|
||
var FORM_CONTAINER_SELECTOR = "[data-cmp-is='adaptiveFormContainer']"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None of this code is going to be changed by core component developers, let's move this code to cq-guides (like we have done for replace / rule editor etc), since this logic is very specific There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, these codes should not require any customization. In that case Data Model code will also required to moved to avoid duplicacy right ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I agree that the data model code shouldn't be included here. We should keep the codebase clean by only including the relevant components that are customizable or useful for developers |
||
|
||
var JSON_SCHEMA = 'jsonschema', | ||
NONE = "none", | ||
FORM_DATA_MODEL = "formdatamodel", | ||
SCHEMA_REF = "input[name='./schemaRef']", | ||
SCHEMA_TYPE = "input[name='./schemaType']", | ||
SCHEMA_CONTAINER = ".cmp-adaptiveform-container__schemaselectorcontainer", | ||
FDM_CONTAINER = ".cmp-adaptiveform-container__fdmselectorcontainer", | ||
SCHEMA_DROPDOWN_SELECTOR = ".cmp-adaptiveform-container__schemaselector", | ||
FDM_DROPDOWN_SELECTOR = ".cmp-adaptiveform-container__fdmselector", | ||
FORM_MODEL_SELECTOR = ".cmp-adaptiveform-container__selectformmodel", | ||
FM_AF_ROOT = "/content/forms/af/", | ||
FM_DAM_ROOT ="/content/dam/formsanddocuments/", | ||
DAM_SCHEMA_TYPE, | ||
DAM_SCHEMA_REF; | ||
|
||
var configuredFormModel, | ||
toBeConfiguredFormModel, | ||
isConfirmationDialogAccept = false, | ||
doNotShowDialogFlag = false, | ||
isSchemaChanged = false, | ||
dialogHeader = Granite.I18n.get("Form Model"), | ||
dialogContent = "<p>" + Granite.I18n.get("When you replace the old data model with a new model, some data bindings might break and lead to form submission errors.") + "</p>" + "<coral-checkbox id='doNotShowDialogCheckBox'>" + Granite.I18n.get("Don't show this again") + "</coral-checkbox>", | ||
dialogFooter = '<button id="formModelDialogCancelButton" is="coral-button" variant="default" coral-close>' + Granite.I18n.get("Cancel") + '</button><button id="formModelDialogAcceptButton" is="coral-button" variant="primary" coral-close>' + Granite.I18n.get("Ok") + '</button>'; | ||
|
||
var formModelChangeConfirmationDialog = new Coral.Dialog().set({ | ||
id: 'formModelChange', | ||
header: { | ||
innerHTML: dialogHeader | ||
}, | ||
content: { | ||
innerHTML: dialogContent | ||
}, | ||
footer: { | ||
innerHTML: dialogFooter | ||
}, | ||
variant : 'warning' | ||
}); | ||
|
||
channel.on("dialog-success", function (e) { | ||
configuredFormModel = toBeConfiguredFormModel; | ||
isConfirmationDialogAccept = false; | ||
if(isForm() && isSchemaChanged){ | ||
var customEvent = document.createEvent("CustomEvent"); | ||
customEvent.initCustomEvent("data-model-selected", true, true); | ||
window.dispatchEvent(customEvent); | ||
} | ||
}); | ||
|
||
channel.on('change', '#doNotShowDialogCheckBox', function(e) { | ||
if (this.checked) { | ||
doNotShowDialogFlag = true; | ||
} else { | ||
doNotShowDialogFlag = false; | ||
} | ||
}); | ||
|
||
function selectFormModelOnLoad(dialog) { | ||
var schemaType = dialog.find(FORM_MODEL_SELECTOR); | ||
if(schemaType.length > 0){ | ||
schemaType = schemaType[0].value; | ||
hideContainersExcept(schemaType); | ||
if (isForm()){ | ||
var afAssetPath = getAfAssetMetadataPath(); | ||
DAM_SCHEMA_TYPE = "[name='" + afAssetPath + "/formmodel']"; | ||
DAM_SCHEMA_REF = "[name='" + afAssetPath + "/schemaRef']"; | ||
addFormParameter(afAssetPath + '/formmodel', schemaType); | ||
addFormParameter(afAssetPath + '/schemaRef'); | ||
} | ||
document.body.appendChild(formModelChangeConfirmationDialog); | ||
prefillSchema(schemaType, dialog); | ||
} | ||
} | ||
|
||
function selectFormModelOnChanged(dialog) { | ||
var schemaTypeSelected = dialog.find(FORM_MODEL_SELECTOR); | ||
if(schemaTypeSelected.length > 0){ | ||
schemaTypeSelected = schemaTypeSelected[0].value; | ||
setElementValue(dialog, DAM_SCHEMA_TYPE, schemaTypeSelected) | ||
hideContainersExcept(schemaTypeSelected); | ||
} | ||
} | ||
|
||
function prefillSchema(schemaType, dialog){ | ||
var schemaRef = dialog.find(SCHEMA_REF); | ||
if(schemaRef.length > 0){ | ||
schemaRef = schemaRef[0].value; | ||
configuredFormModel = schemaRef; | ||
setElementValue(dialog, DAM_SCHEMA_REF, schemaRef); | ||
if (schemaType == JSON_SCHEMA) { | ||
$(SCHEMA_DROPDOWN_SELECTOR).val(schemaRef); | ||
} else if (schemaType == FORM_DATA_MODEL) { | ||
$(FDM_DROPDOWN_SELECTOR).val(schemaRef); | ||
} | ||
} | ||
} | ||
|
||
function schemaSelectorOnChanged(dialog) { | ||
var selectedSchema = dialog.find(SCHEMA_DROPDOWN_SELECTOR); | ||
if(selectedSchema.length > 0){ | ||
selectedSchema = selectedSchema[0].value; | ||
setElementValue(dialog, SCHEMA_REF, selectedSchema); | ||
setElementValue(dialog, DAM_SCHEMA_REF, selectedSchema); | ||
isSchemaChanged = true; | ||
if (configuredFormModel) { | ||
confirmFormModelChange(selectedSchema, $(SCHEMA_DROPDOWN_SELECTOR)); | ||
} else { | ||
toBeConfiguredFormModel = selectedSchema; | ||
} | ||
} | ||
}; | ||
|
||
function fdmSelectorOnChanged(dialog) { | ||
var selectedSchema = dialog.find(FDM_DROPDOWN_SELECTOR); | ||
if(selectedSchema.length > 0) { | ||
selectedSchema = selectedSchema[0].value; | ||
setElementValue(dialog, SCHEMA_REF, selectedSchema); | ||
setElementValue(dialog, DAM_SCHEMA_REF, selectedSchema); | ||
isSchemaChanged = true; | ||
if (configuredFormModel) { | ||
confirmFormModelChange(selectedSchema, $(FDM_DROPDOWN_SELECTOR)); | ||
} else { | ||
toBeConfiguredFormModel = selectedSchema; | ||
} | ||
} | ||
}; | ||
|
||
function setElementValue(dialog, elementRef, value){ | ||
var element = dialog.find(elementRef); | ||
if(element.length > 0){ | ||
element[0].value = value; | ||
} | ||
} | ||
|
||
function getAfAssetMetadataPath() { | ||
return getGuideContainerPath().replace(FM_AF_ROOT, FM_DAM_ROOT).replace("/guideContainer", "/metadata"); | ||
} | ||
|
||
function getGuideContainerPath() { | ||
var afWindow = (Granite.author ? Granite.author.ContentFrame.contentWindow : window); | ||
return afWindow.$(FORM_CONTAINER_SELECTOR).data("cmp-path"); | ||
}; | ||
|
||
function isForm(){ | ||
return Granite.author.page.path.startsWith(FM_AF_ROOT); | ||
} | ||
|
||
function addFormParameter(name, value) { | ||
var input = channel[0].createElement('input'); | ||
input.setAttribute("type", "hidden"); | ||
input.setAttribute("name", name); | ||
if(value) { | ||
input.setAttribute("value", value); | ||
} | ||
$("#formmodelparameters").append(input); | ||
} | ||
|
||
function hideContainersExcept(selectedSchemaType) { | ||
if (selectedSchemaType == JSON_SCHEMA) { | ||
$(FDM_CONTAINER).hide(); | ||
$(SCHEMA_CONTAINER).show(); | ||
} else if (selectedSchemaType == FORM_DATA_MODEL) { | ||
$(SCHEMA_CONTAINER).hide(); | ||
$(FDM_CONTAINER).show(); | ||
} else if (selectedSchemaType == 'none') { | ||
$(FDM_CONTAINER).hide(); | ||
$(SCHEMA_CONTAINER).hide(); | ||
} | ||
} | ||
|
||
function confirmFormModelChange(selectedSchema, dataModelSelector) { | ||
if (!isConfirmationDialogAccept && Granite.author.preferences.cookie.get("form-model-change-dialog") !== "disabled") { | ||
if (configuredFormModel !== selectedSchema) { | ||
formModelChangeConfirmationDialog.show(); | ||
$("#formModelDialogCancelButton").on("click", function () { | ||
dataModelSelector.val(configuredFormModel); | ||
}); | ||
$("#formModelDialogAcceptButton").on("click", function () { | ||
if (doNotShowDialogFlag) { | ||
Granite.author.preferences.cookie.set("form-model-change-dialog","disabled"); | ||
} else { | ||
Granite.author.preferences.cookie.set("form-model-change-dialog","enabled"); | ||
} | ||
isConfirmationDialogAccept = true; | ||
}); | ||
} | ||
} | ||
toBeConfiguredFormModel = selectedSchema; | ||
} | ||
|
||
$(window).adaptTo("foundation-registry").register("foundation.validation.validator", { | ||
selector : "[data-validation~='datamodel.config']", | ||
validate : function (el) { | ||
let $el = $(el); | ||
if($el.is(":visible") && $el[0].value === '') { | ||
return Granite.I18n.getMessage("This is a required field"); | ||
} | ||
} | ||
}); | ||
|
||
DataModel.initialiseDataModel = function (dialog) { | ||
var formModelSelector = dialog.find(FORM_MODEL_SELECTOR)[0], | ||
schemaSelector = dialog.find(SCHEMA_DROPDOWN_SELECTOR)[0], | ||
fdmSelector = dialog.find(FDM_DROPDOWN_SELECTOR)[0]; | ||
if (formModelSelector) { | ||
formModelSelector.on("change", function() { | ||
selectFormModelOnChanged(dialog); | ||
}); | ||
} | ||
if (schemaSelector) { | ||
schemaSelector.on("change", function() { | ||
schemaSelectorOnChanged(dialog); | ||
}); | ||
} | ||
if (fdmSelector) { | ||
fdmSelector.on("change", function() { | ||
fdmSelectorOnChanged(dialog); | ||
}); | ||
} | ||
selectFormModelOnLoad(dialog); | ||
} | ||
|
||
})(jQuery, jQuery(document), Coral); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need an empty key here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need a way to distinguish between a panel and a fragment component. So, we changed the component's cq: template to always include
fragmentPath
. Otherwise we will have to create new property like fd:viewType to differentiateThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer fd:viewType rather than fragmentPath, since this is not intuitive.