Skip to content

Commit

Permalink
Styling improvements. Adding Separate label, input and copy button fo…
Browse files Browse the repository at this point in the history
…r each url.
  • Loading branch information
Krassimir Boyanov committed Nov 27, 2024
1 parent f7f5233 commit 62debdc
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* ACS AEM Commons Bundle
* %%
* Copyright (C) 2013 - 2024 AEM developer community
* Copyright (C) 2013 - 2024 Adobe
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* ACS AEM Commons Bundle
* %%
* Copyright (C) 2013 - 2024 AEM developer community
* Copyright (C) 2013 - 2024 Adobe
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,19 +21,15 @@
package com.adobe.acs.commons.wcm.impl;

import com.day.cq.commons.Externalizer;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import javax.servlet.Servlet;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.apache.tika.mime.MediaType;
import org.jetbrains.annotations.NotNull;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
Expand All @@ -42,19 +38,26 @@
import org.osgi.service.metatype.annotations.Designate;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;

import javax.servlet.Servlet;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

@Component(service = Servlet.class)
@SlingServletResourceTypes(
resourceTypes = PublishUrlServlet.RESOURCE_TYPE,
methods = HttpConstants.METHOD_GET,
extensions = PublishUrlServlet.TXT_EXTENSION
resourceTypes = PublishUrlServlet.RESOURCE_TYPE,
methods = HttpConstants.METHOD_GET,
extensions = PublishUrlServlet.TXT_EXTENSION
)
@Designate(ocd = PublishUrlServlet.PublishUrlServletConfig.class)
public class PublishUrlServlet extends SlingSafeMethodsServlet implements Serializable {

private static final long serialVersionUID = 1L;
protected static final String RESOURCE_TYPE = "acs-commons/components/utilities/publish-url";
public static final String TXT_EXTENSION = "txt";
public static final String PATH = "path";
protected static final String TXT_EXTENSION = "txt";
private static final String PATH = "path";
private static final String JSON_TYPE = "application/json";
private String[] externalizerKeys;

@Activate
Expand All @@ -63,36 +66,32 @@ protected void activate(final PublishUrlServletConfig config) {
}

@Override
protected void doGet(SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response)
throws IOException {
protected void doGet(SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws IOException {
String path = request.getParameter(PATH);
StringBuilder builder = new StringBuilder();
ResourceResolver resolver = request.getResourceResolver();
Externalizer externalizer = resolver.adaptTo(Externalizer.class);
ObjectMapper mapper = new ObjectMapper();
ObjectNode jsonResponse = mapper.createObjectNode();

if (externalizer != null) {
Arrays.asList(externalizerKeys)
.forEach(key -> builder.append(StringUtils.capitalize(key))
.append(" : ")
.append(externalizer.externalLink(resolver, key, request.getScheme(), path))
.append("\n"));
Arrays.asList(externalizerKeys).forEach(key -> {
String capitalizedKey = StringUtils.capitalize(key);
String externalLink = externalizer.externalLink(resolver, key, request.getScheme(), path);
jsonResponse.put(capitalizedKey, externalLink);
});
}

response.setContentType(MediaType.TEXT_PLAIN.getType());
response.setContentType(JSON_TYPE);
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
response.getWriter().write(builder.toString());
response.getWriter().write(jsonResponse.toString());
}

@ObjectClassDefinition(
name = "ACS AEM Commons - Publish URL Servlet Configuration",
description = "Configuration for the Publish URL Servlet"
)
@ObjectClassDefinition(name = "ACS AEM Commons - Publish URL Servlet Configuration",
description = "Configuration for the Publish URL Servlet")
public @interface PublishUrlServletConfig {
@AttributeDefinition(
name = "Externalizer Environment Keys",
description = "Externalizer Environment Keys. They need to match the environment keys configured in the Externalizer configuration.",
type = AttributeType.STRING
)

@AttributeDefinition(name = "Externalizer Environment Keys", description = "Externalizer Environment Keys. They " +
"need to match the environment keys configured in the Externalizer configuration.", type = AttributeType.STRING)
String[] externalizerKeys() default {};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,26 @@
text-align: center;
color: #EA7B6F;
min-width: 30rem;
}

.copy-publish-url-group {
display: flex;
align-items: center;
margin-bottom: 5px;
}

.copy-publish-url-group .coral-Form-field,
.copy-publish-url-group .sites-publishurl-copy-cmd {
margin-right: 10px;
}

.copy-publish-url-group {
display: flex;
align-items: center;
margin-bottom: 5px;
}

.copy-publish-url-group .coral-Form-field,
.copy-publish-url-group .sites-publishurl-copy-cmd {
margin-right: 10px;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,43 @@
url : publishUrl
});
result.done(function(text) {
var content = '<textarea class="coral-Form-field acs-aem-commons__sites-copy-published-url__text " readonly rows="5">' +
text +
'</textarea>';
var jsonResponse = JSON.parse(text);
var content = '';
var labelWidth = 0;
var inputWidth = 0;

Object.keys(jsonResponse).forEach(function(key) {
if (key.length > labelWidth) {
labelWidth = key.length;
}
if (jsonResponse[key].length > inputWidth) {
inputWidth = jsonResponse[key].length;
}
});
inputWidth = inputWidth > 0 ? inputWidth - 10 : 0;

Object.keys(jsonResponse).forEach(function(key) {
content += '<div class="coral-Form-fieldwrapper copy-publish-url-group">' +
'<label class="coral-Form-fieldlabel" style="width: ' + labelWidth + 'ch; display: inline-block">' + key + ' : </label>' +
'<input type="text" class="coral-Form-field" value="' + jsonResponse[key] + '" readonly style="width: ' + inputWidth + 'ch;" />' +
'<button type="button" class="sites-publishurl-copy-cmd coral3-Button coral3-Button--primary" data-copy-target="' + key + '"><coral-icon class="coral3-Icon coral3-Icon--attach coral3-Icon--sizeXS" icon="attach" size="XS" autoarialabel="off" alt=""></coral-icon><coral-button-label>Copy</coral-button-label></button>' +
'</div>';
});
modalBody.html(content);
document.querySelectorAll('.sites-publishurl-copy-cmd').forEach(function(button) {
button.addEventListener('click', function() {
var key = this.getAttribute('data-copy-target');
var inputField = this.previousElementSibling;
var textToCopy = inputField.value;
inputField.select();
try {
navigator.clipboard.writeText(textToCopy);
console.log("Text copied to clipboard");
} catch (err) {
console.error("Failed to copy: ", err);
}
});
});
});
result.fail(function() {
modalBody.html('<p class="acs-aem-commons__sites-copy-published-url__text--failure">' + failureMessage + '</p>');
Expand All @@ -54,14 +87,5 @@
if (!document.queryCommandSupported('copy')) {
$('#sites-publishurl-copy-cmd').hide();
}
$('#sitest-publishurl-copy-cmd').on('click', function(e) {
var ecData = document.querySelector('.acs-aem-commons__sites-copy-published-url__text');
ecData.select();
try {
document.execCommand('copy');
} catch (ign) {

}
});
});
})(document, Granite.$);
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
granite:title="Copy Publish URL"
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/button"
sling:orderBefore="viewinadmin"
text="Copy Publish URL">
<granite:data
jcr:primaryType="nt:unstructured"
Expand Down Expand Up @@ -63,7 +64,7 @@
<dialogpublishurl
granite:id="aem-sites-show-publish-url"
jcr:primaryType="nt:unstructured"
jcr:title="Publish URL"
jcr:title="Publish URLs"
sling:resourceType="granite/ui/components/coral/foundation/dialog"
closable="{Boolean}true">
<granite:data
Expand All @@ -75,31 +76,6 @@
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textarea"/>
</items>
<footer jcr:primaryType="nt:unstructured">
<dismiss
granite:rel="foundation-toggleable"
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/button"
text="Dismiss"
variant="minimal">
<parentConfig
jcr:primaryType="nt:unstructured"
close="{Boolean}true"/>
</dismiss>
<copy
granite:id="sites-publishurl-copy-cmd"
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/button"
icon="attach"
iconSize="XS"
text="Copy"
variant="primary">
<granite:rendercondition
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/renderconditions/simple"
expression="${!granite:containsIgnoreCase(header[&quot;User-Agent&quot;], &quot;iPad&quot;) &amp;&amp; !granite:containsIgnoreCase(header[&quot;User-Agent&quot;], &quot;iPhone&quot;)}"/>
</copy>
</footer>
</dialogpublishurl>
</items>
</header>
Expand Down

0 comments on commit 62debdc

Please sign in to comment.