import Preload from "client-app/lib/preload"; const entityMap = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'", "/": "/" }; export function escapeHtml(string) { return String(string).replace(/[&<>"'/]/g, s => entityMap[s]); } export function ajax(url, settings) { settings = settings || {}; settings.headers = settings.headers || {}; settings.headers["X-SILENCE-LOGGER"] = true; return Em.$.ajax(Preload.get("rootPath") + url, settings); } export function preloadOrAjax(url, settings) { const preloaded = Preload.get(`preload.${url.replace(".json", "")}`); if (preloaded) { return Em.RSVP.resolve(preloaded); } else { return ajax(url, settings); } } let HIDDEN_PROPERTY; let TITLE; let TITLE_COUNT; export function updateHiddenProperty(property) { HIDDEN_PROPERTY = property; } export function isHidden() { if (HIDDEN_PROPERTY !== undefined) { return document[HIDDEN_PROPERTY]; } else { return !document.hasFocus; } } export function increaseTitleCount(increment) { if (!isHidden()) { return; } TITLE = TITLE || document.title; TITLE_COUNT = TITLE_COUNT || 0; TITLE_COUNT += increment; document.title = `${TITLE} (${TITLE_COUNT})`; } export function resetTitleCount() { TITLE_COUNT = 0; document.title = TITLE || document.title; } export function formatTime(timestamp) { let formatted; const time = moment(timestamp); const now = moment(); if (time.diff(now.startOf("day")) > 0) { formatted = time.format("h:mm a"); } else { if (time.diff(now.startOf("week")) > 0) { formatted = time.format("dd h:mm a"); } else { if (time.diff(now.startOf("year")) > 0) { formatted = time.format("D MMM h:mm a"); } else { formatted = time.format("D MMM YY"); } } } return formatted; } export function buildArrayString(array) { const buffer = []; array.forEach(v => { if (v === null) { buffer.push("null"); } else if (Object.prototype.toString.call(v) === "[object Array]") { buffer.push(buildArrayString(v)); } else { buffer.push(escapeHtml(v.toString())); } }); return "[" + buffer.join(", ") + "]"; } export function buildHashString(hash, recurse) { if (!hash) return ""; const buffer = []; const hashes = []; _.each(hash, (v, k) => { if (v === null) { buffer.push("null"); } else if (Object.prototype.toString.call(v) === "[object Array]") { buffer.push( "
" + escapeHtml(k1) + " | " + buildHashString(v, true) + " | " ); buffer.push("