Files
karma/assets/static/templates.js
Łukasz Mierzwa c7442c3adf Linkify URLs in the annotation
use linkifyjs to make all URLs in the annotation clickable, but since it requires us to stop escaping html when rendering annotation object let's first manually escape it to prevent rogue alerts with malicious annotations from executing <scripts> and other ugly things in user browsers
2017-08-18 12:09:35 -07:00

97 lines
2.9 KiB
JavaScript

"use strict";
const $ = require("jquery");
const _ = require("underscore");
const moment = require("moment");
require("javascript-linkify");
const alerts = require("./alerts");
var templates = {},
config = {
// popover with the list of most common labels
breakdown: "#breakdown",
breakdownContent: "#breakdown-content",
// reload message if backend version bump is detected
reloadNeeded: "#reload-needed",
// errors
fatalError: "#fatal-error",
internalError: "#internal-error",
updateError: "#update-error",
instanceError: "#instance-error",
configError: "#configuration-error",
// modal popup with label filters
modalTitle: "#modal-title",
modalBody: "#modal-body",
// modal popup with silence form
silenceForm: "#silence-form",
silenceFormValidationError: "#silence-form-validation-error",
silenceFormResults: "#silence-form-results",
silenceFormSuccess: "#silence-form-success",
silenceFormError: "#silence-form-error",
silenceFormFatal: "#silence-form-fatal",
silenceFormLoading: "#silence-form-loading",
// label button
buttonLabel: "#label-button-filter",
// alert group
alertGroup: "#alert-group",
alertGroupTitle: "#alert-group-title",
alertGroupAnnotations: "#alert-group-annotations",
alertGroupLabels: "#alert-group-labels",
alertGroupElements: "#alert-group-elements",
alertGroupSilence: "#alert-group-silence",
alertGroupLabelMap: "#alert-group-label-map",
// history dropdown
historyMenu: "#history-menu",
historyMenuItem: "#history-menu-item"
};
function getConfig() {
return config;
}
function loadTemplate(name, selector) {
try {
templates[name] = _.template($(selector).html());
} catch (err) {
console.error("Failed to parse template " + name + " " + selector);
console.error(err);
}
}
function init() {
$.each(config, function(name, selector) {
loadTemplate(name, selector);
});
}
function renderTemplate(name, context) {
context["moment"] = moment;
context["linkify"] = window.linkify;
context["renderTemplate"] = renderTemplate;
context["sortMapByKey"] = alerts.sortMapByKey;
context["getLabelAttrs"] = alerts.getLabelAttrs;
var t = templates[name];
if (t === undefined) {
console.error("Unknown template " + name);
return "<div class='jumbotron'><h1>Internal error: unknown template " + name + "</h1></div>";
}
try {
return t(context);
} catch (err) {
return "<div class='jumbotron'>Failed to render template " + name + "<h1><p>" + err + "</p></h1></div>";
}
}
exports.init = init;
exports.getConfig = getConfig;
exports.loadTemplate = loadTemplate;
exports.renderTemplate = renderTemplate;