Skip to content

Commit

Permalink
FORMS-16342 commenting test
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivam Agarwal committed Sep 17, 2024
1 parent a8f06f0 commit 59381dc
Show file tree
Hide file tree
Showing 17 changed files with 268 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,7 @@

import java.io.IOException;
import java.math.BigDecimal;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand All @@ -36,6 +26,9 @@
import javax.annotation.Nonnull;
import javax.annotation.PostConstruct;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
Expand Down Expand Up @@ -289,6 +282,10 @@ protected boolean getEditMode() {
if (rulesProperties.size() > 0) {
properties.put(CUSTOM_RULE_PROPERTY_WRAPPER, rulesProperties);
}
List<String> disabledScripts = getDisabledXFAScripts();
if (disabledScripts.size() > 0) {
properties.put("fd:disabledXfaScripts", disabledScripts);
}
return properties;
}

Expand Down Expand Up @@ -541,4 +538,24 @@ public Map<String, Object> getDorProperties() {
return customDorProperties;
}

private List<String> getDisabledXFAScripts() {
Set<String> disabledScripts = new HashSet<>();
String xfaScripts = resource.getValueMap().get("fd:xfaScripts", "");
if (StringUtils.isNotEmpty(xfaScripts)) {
// read string xfaScripts to jsonNode
ObjectMapper mapper = new ObjectMapper();
try {
ArrayNode node = (ArrayNode) mapper.readTree(xfaScripts);
// iterate through the array node and add the elements which have disabled property set to true
for (JsonNode jsonNode : node) {
if (jsonNode.has("disabled") && jsonNode.get("disabled").asBoolean()) {
disabledScripts.add(jsonNode.get("activity").asText());
}
}
} catch (IOException e) {
logger.error("Error while parsing xfaScripts {} {}", e, resource.getPath());
}
}
return new ArrayList<>(disabledScripts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ public String[] getEnumNames() {
String[] enumName = map.values().toArray(new String[0]);
return Arrays.stream(enumName)
.map(p -> {
return this.translate(ReservedProperties.PN_ENUM_NAMES, p);
String value = this.translate(ReservedProperties.PN_ENUM_NAMES, p);
if (value == null) {
value = "";
}
return value;
})
.toArray(String[]::new);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
handler="CQ.FormsCoreComponents.editorhooks.viewQualifiedName"
icon="viewSOMExpression"
text="View Qualified Name"/>
<viewXFAScripts
jcr:primaryType="nt:unstructured"
condition="CQ.FormsCoreComponents.editorhooks.hasXfaScripts"
handler="CQ.FormsCoreComponents.editorhooks.viewXfaScripts"
icon="code"
text="View XFA Scripts"/>
</cq:actionConfigs>
<cq:inplaceEditing
jcr:primaryType="cq:InplaceEditingConfig"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
handler="CQ.FormsCoreComponents.editorhooks.viewQualifiedName"
icon="viewSOMExpression"
text="View Qualified Name"/>
<viewXFAScripts
jcr:primaryType="nt:unstructured"
condition="CQ.FormsCoreComponents.editorhooks.hasXfaScripts"
handler="CQ.FormsCoreComponents.editorhooks.viewXfaScripts"
icon="code"
text="View XFA Scripts"/>
</cq:actionConfigs>
<cq:inplaceEditing
jcr:primaryType="cq:InplaceEditingConfig"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,141 @@
});
};

})(window, Granite.author, Coral);
window.CQ.FormsCoreComponents.editorhooks.hasXfaScripts = function (editable) {
const result = $.ajax({
type: 'GET',
async: false,
url: Granite.HTTP.externalize(editable.path + ".json"),
cache: false
});
const json = result.responseJSON;

if (json && json.dataRef?.startsWith('xfa[0]')) {
if (json['fd:xfaScripts']) {
try {
const scripts = JSON.parse(json['fd:xfaScripts']);
return scripts.length > 0;
} catch(e) {
console.error('Error parsing xfaScripts', e, json['fd:xfaScripts']);
}
}
}
return false
}

window.CQ.FormsCoreComponents.editorhooks.viewXfaScripts = function (editable) {
fetch(Granite.HTTP.externalize(editable.path + ".json")).then(async function (resp) {
const json = await resp.json();
// Assuming `resp` contains the JSON string with `fd:xfaScripts`
var xfaScripts = JSON.parse(json['fd:xfaScripts']);
var dialogContent = document.createElement('div');

// Create a Coral Table
var table = document.createElement('coral-table');

// Create the header
var thead = document.createElement('coral-table-head');
var headerRow = document.createElement('coral-table-row');

var eventNameHeader = document.createElement('coral-table-headercell');
eventNameHeader.textContent = 'Event Name';
var eventContentHeader = document.createElement('coral-table-headercell');
eventContentHeader.textContent = 'Event Content';
var disableHeader = document.createElement('coral-table-headercell');
disableHeader.textContent = 'Disable';

headerRow.appendChild(eventNameHeader);
headerRow.appendChild(eventContentHeader);
headerRow.appendChild(disableHeader);
thead.appendChild(headerRow);
table.appendChild(thead);

// Populate the table with data from xfaScripts
var tbody = document.createElement('coral-table-body');
xfaScripts.forEach(function(script) {
var row = document.createElement('coral-table-row');

var nameCell = document.createElement('coral-table-cell');
nameCell.textContent = script.runAt === "server" ? `${script.activity}(server)` : script.activity;
var contentCell = document.createElement('coral-table-cell');
contentCell.innerHTML = script.value.replaceAll("\n", "<br />");

var checkboxCell = document.createElement('coral-table-cell');
var checkbox = new Coral.Checkbox();
checkbox.name = 'disableCheckbox';
checkbox.on('change', function() {
script.disabled = this.checked;
});
checkboxCell.appendChild(checkbox);
checkbox.checked = !!script.disabled;
if (script.runAt === "server") {
checkbox.disabled = true;
}
row.appendChild(nameCell);
row.appendChild(contentCell);
row.appendChild(checkboxCell);

tbody.appendChild(row);
});
table.appendChild(tbody);

dialogContent.appendChild(table);

// Create the dialog
var dialog = new Coral.Dialog().set({
id: 'xfaScriptsDialog',
header: {
innerHTML: 'XFA Scripts'
},
content: {
innerHTML: ''
},
footer: {},
closable: "on"
});

// Add the table to the dialog content
//dialog.content.appendChild(dialogContent);

var okButton = new Coral.Button();
okButton.label.textContent = 'OK';
okButton.variant = Coral.Button.variant.PRIMARY;
okButton.addEventListener('click', function() {
// Prepare the modified xfaScripts for POST request
var modifiedXfaScripts = JSON.stringify({ 'fd:xfaScripts': JSON.stringify(xfaScripts) });
$.ajax({
url: editable.path,
type: 'POST',
data: {
"_charset_" : "UTF-8",
':operation': 'import',
':contentType': 'json',
':content': modifiedXfaScripts,
':replaceProperties': true
},
success: function(response) {
console.log('Successfully posted the data');
dialog.remove();
},
error: function(xhr, status, error) {
console.error('Error posting the data', error);
dialog.remove();
}
});
});
dialog.footer.appendChild(okButton);

// Append and show the dialog
document.body.appendChild(dialog);

// add a listener on dialog show event
dialog.on('coral-overlay:open', function() {
dialog.content.appendChild(dialogContent);
});
dialog.show();

})
return true;
};

})(window, Granite.author, Coral);
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@
this.widgetObject.setDisplayValue(this._model.value);
this.widgetObject.setCalendarWidgetValue(this._model.value);
this.setInactive();
this.triggerExit();
}, this.getWidget());
this.widgetObject.addEventListener('focus', (e) => {
this.widgetObject.setValue(e.target.value);
this.setActive();
this.triggerEnter();
}, this.getWidget());
this.widgetObject.addEventListener('input', (e) => {
if( e.target.value === '') {
Expand All @@ -117,9 +119,11 @@
this.widget.addEventListener('blur', (e) => {
this.setModelValue(e.target.value);
this.setInactive();
this.triggerExit();
});
this.widget.addEventListener('focus', (e) => {
this.setActive();
this.triggerEnter();
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,11 @@
});
this.widget.addEventListener('focus', (e) => {
this.setActive();
this.triggerEnter();
});
this.widget.addEventListener('blur', (e) => {
this.setInactive();
this.triggerExit();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@
this.setModelValue(e.target.value);
this.setWidgetValueToDisplayValue();
this.setInactive();
this.triggerExit();
});
this.widget.addEventListener('focus', (e) => {
this.setActive();
this.setWidgetValueToModelValue();
this.triggerEnter();
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@
this.setModelValue(e.target.value);
if(this.element) {
this.setInactive();
this.triggerExit();
}
});
}
this.getWidget().addEventListener('focus', (e) => {
if (this.element) {
this.setActive();
this.triggerEnter();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,11 @@
handler="CQ.FormsCoreComponents.editorhooks.viewQualifiedName"
icon="viewSOMExpression"
text="View Qualified Name"/>
<viewXFAScripts
jcr:primaryType="nt:unstructured"
condition="CQ.FormsCoreComponents.editorhooks.hasXfaScripts"
handler="CQ.FormsCoreComponents.editorhooks.viewXfaScripts"
icon="code"
text="View XFA Scripts"/>
</cq:actionConfigs>
</jcr:root>
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@
this.setModelValue(e.target.value);
this.setWidgetValueToDisplayValue();
this.setInactive();
this.triggerExit();
});
this.widget.addEventListener('focus', (e) => {
this.setActive();
this.setWidgetValueToModelValue();
this.triggerEnter();
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@
this.setModelValue(e.target.value);
this.setWidgetValueToDisplayValue();
this.setInactive();
this.triggerExit();
});
this.widget.addEventListener('focus', (e) => {
this.setActive();
this.setWidgetValueToModelValue();
this.triggerEnter();
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
</sly>
<sly data-sly-use.clientlib="core/wcm/components/commons/v1/templates/clientlib.html">
<sly data-sly-test="${!wcmmode.edit}" data-sly-call="${clientlib.js @ categories='core.forms.components.runtime.all', async=true}"/>
<sly data-sly-test="${!wcmmode.edit}" data-sly-call="${clientlib.js @ categories='xfaforms.3rdparty,xfaforms.I18N.en,xfaforms.formbridge,xfaforms.xfalibutil,xfaforms.xfalibwidgets,xfaforms.formcalc,xfaforms.xfalibModel'}"/>
</sly>
<sly data-sly-use.page="com.adobe.cq.wcm.core.components.models.Page">
<sly data-sly-test="${page.data && page.dataLayerClientlibIncluded}" data-sly-call="${clientlib.js @ categories='core.forms.components.commons.v1.datalayer', async=true}"></sly>
Expand Down
31 changes: 31 additions & 0 deletions ui.frontend/src/handleXfa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*******************************************************************************
* 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.
******************************************************************************/

export function loadXfa(formdom, renderContext) {
if (window.xfalib) {
formBridge.registerConfig("disabledServerScripts", ["initialize", "$formready", "$layoutready"])
const xfaJson = JSON.parse(JSON.parse(JSON.stringify(formdom)));
xfalib.runtime.renderContext = JSON.parse(JSON.parse(JSON.stringify(renderContext)));
xfalib.script.XfaModelRegistry.prototype.createModel(xfaJson);
//initialize Acrobat specific scripts
new xfalib.acrobat.Acrobat();
return function (model) {
model._syncXfaProps();
xfalib.runtime.xfa.form._initialize(true);
$(window).trigger("XfaInitialized");
}
}
}
Loading

0 comments on commit 59381dc

Please sign in to comment.