ResponseHighlighter enhancements - hyperlinks and dateTime/UUID whole-string selection via some javascript

This commit is contained in:
Eugene Lubarsky 2017-05-19 19:29:52 +10:00
parent f968afd7bd
commit 3afab596ab
2 changed files with 64 additions and 3 deletions

View File

@ -24,6 +24,7 @@ import static org.apache.commons.lang3.StringUtils.isNotBlank;
*/
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Map;
import java.util.Set;
@ -33,6 +34,7 @@ import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.hl7.fhir.instance.model.api.IBaseResource;
@ -325,8 +327,13 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
b.append(" <head>\n");
b.append(" <meta charset=\"utf-8\" />\n");
b.append(" <style>\n");
b.append(".hlQuot {\n");
b.append(" color: #88F;\n");
b.append(".hlQuot { color: #88F; }\n");
b.append(".hlQuot a { text-decoration: none; color: #88F; }\n");
b.append(".hlQuot .uuid, .hlQuot .dateTime {\n");
b.append(" user-select: all;\n");
b.append(" -moz-user-select: all;\n");
b.append(" -webkit-user-select: all;\n");
b.append(" -ms-user-select: element;\n");
b.append("}\n");
b.append(".hlAttr {\n");
b.append(" color: #888;\n");
@ -409,7 +416,16 @@ public class ResponseHighlighterInterceptor extends InterceptorAdapter {
b.append("<pre>");
b.append(format(encoded, encoding));
b.append("</pre>");
b.append(" </body>");
b.append("\n");
InputStream jsStream = ResponseHighlighterInterceptor.class.getResourceAsStream("ResponseHighlighter.js");
String jsStr = jsStream != null ? IOUtils.toString(jsStream, "UTF-8") : "console.log('ResponseHighlighterInterceptor: javascript resource not found')";
jsStr = jsStr.replace("FHIR_BASE", theRequestDetails.getServerBaseForRequest());
b.append("<script type=\"text/javascript\">");
b.append(jsStr);
b.append("</script>\n");
b.append("</body>");
b.append("</html>");
//@formatter:off
String out = b.toString();

View File

@ -0,0 +1,45 @@
(function() {
'use strict';
/* bail out if user is testing a version of this script via Greasemonkey or Tampermonkey */
if (window.HAPI_ResponseHighlighter_userscript) {
console.log("HAPI ResponseHighlighter: userscript detected - not executing embedded script");
return;
}
/* adds hyperlinks and CSS styles to dates and UUIDs (e.g. to enable user-select: all) */
const logicalReferenceRegex = /^[A-Z][A-Za-z]+\/[0-9]+$/;
const dateTimeRegex = /^-?[0-9]{4}(-(0[1-9]|1[0-2])(-(0[0-9]|[1-2][0-9]|3[0-1])(T([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?(Z|(\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00)))?)?)?$/; // from the spec - https://www.hl7.org/fhir/datatypes.html#datetime
const uuidRegex = /^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$/;
const allQuotes = document.querySelectorAll(".hlQuot");
for (var i = 0; i < allQuotes.length; i++) {
const quote = allQuotes[i];
const text = quote.textContent.substr(1, quote.textContent.length - 2 /* remove quotes */);
const absHyperlink = text.startsWith("http://") || text.startsWith("https://");
const relHyperlink = text.match(logicalReferenceRegex);
const uuid = text.match(uuidRegex);
const dateTime = text.match(dateTimeRegex);
if (absHyperlink || relHyperlink) {
const link = document.createElement("a");
const href = absHyperlink ? text : "FHIR_BASE/" + text;
link.setAttribute("href", href);
link.textContent = '"' + text + '"';
quote.textContent = "";
quote.appendChild(link);
}
if (uuid || dateTime) {
const span = document.createElement("span");
span.setAttribute("class", uuid ? "uuid" : "dateTime");
span.textContent = text;
quote.textContent = "";
quote.appendChild(document.createTextNode('"'));
quote.appendChild(span);
quote.appendChild(document.createTextNode('"'));
}
}
})();