2015-10-13 16:42:40 +02:00
|
|
|
'use strict';
|
|
|
|
|
2016-02-05 10:38:59 +01:00
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import { getLinkRegex, isEmail } from './regex_utils';
|
|
|
|
|
2015-10-13 16:42:40 +02:00
|
|
|
/**
|
2015-10-20 11:35:32 +02:00
|
|
|
* Set the title in the browser window.
|
2015-10-13 16:42:40 +02:00
|
|
|
*/
|
|
|
|
export function setDocumentTitle(title) {
|
2015-10-20 11:35:32 +02:00
|
|
|
document.title = title;
|
2015-10-13 16:42:40 +02:00
|
|
|
}
|
2015-10-28 17:45:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} elementType: string, is the type of the element, such as link, meta, etc.
|
|
|
|
* @param {string} elementId id of the element
|
|
|
|
* @param {object} elementAttributes: hash table containing the attributes of the relevant element
|
|
|
|
*/
|
|
|
|
function constructHeadElement(elementType, elementId, elementAttributes) {
|
2016-02-05 10:38:59 +01:00
|
|
|
const head = (document.head || document.getElementsByTagName('head')[0]);
|
|
|
|
const element = document.createElement(elementType);
|
|
|
|
const oldElement = document.getElementById(elementId);
|
|
|
|
|
2015-10-28 17:45:28 +01:00
|
|
|
element.setAttribute('id', elementId);
|
2016-02-05 10:38:59 +01:00
|
|
|
|
|
|
|
for (let k in elementAttributes) {
|
2015-10-28 17:45:28 +01:00
|
|
|
try {
|
|
|
|
element.setAttribute(k, elementAttributes[k]);
|
2016-02-05 10:38:59 +01:00
|
|
|
} catch(e) {
|
2015-10-28 17:45:28 +01:00
|
|
|
console.warn(e.message);
|
|
|
|
}
|
|
|
|
}
|
2016-02-05 10:38:59 +01:00
|
|
|
|
2015-10-28 17:45:28 +01:00
|
|
|
if (oldElement) {
|
|
|
|
head.removeChild(oldElement);
|
|
|
|
}
|
2016-02-05 10:38:59 +01:00
|
|
|
|
2015-10-28 17:45:28 +01:00
|
|
|
head.appendChild(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accepts a dictionary of dictionaries which comprises a part or all of html head part
|
|
|
|
* @param {object} headObject {link : {id1: {rel: ... }}}
|
|
|
|
*/
|
|
|
|
export function constructHead(headObject){
|
|
|
|
for (let k in headObject){
|
2016-02-05 10:38:59 +01:00
|
|
|
const favicons = headObject[k];
|
2015-10-28 17:45:28 +01:00
|
|
|
for (let f in favicons){
|
|
|
|
constructHeadElement(k, f, favicons[f]);
|
|
|
|
}
|
|
|
|
}
|
2016-02-05 10:38:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces the links and emails in a given string with anchor elements.
|
|
|
|
*
|
|
|
|
* @param {string} string String to anchorize
|
|
|
|
* @param {(object)} options Options object for anchorizing
|
|
|
|
* @param {(boolean)} emails Whether or not to replace emails (default: true)
|
|
|
|
* @param {(boolean)} links Whether or not to replace links (default: true)
|
|
|
|
* @param {(string)} target Anchor target attribute (default: '_blank')
|
|
|
|
* @return {string|React.element[]} Anchorized string as usable react element, either as an array of
|
|
|
|
* elements or just a string
|
|
|
|
*/
|
|
|
|
export function anchorize(string, { emails: replaceEmail = true, links: replaceLink = true, target = '_blank' } = {}) {
|
|
|
|
if (!replaceEmail && !replaceLink) {
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const linkRegex = getLinkRegex();
|
|
|
|
const strWithAnchorElems = [];
|
|
|
|
let lastMatchIndex = 0;
|
|
|
|
let regexMatch;
|
|
|
|
|
|
|
|
while (regexMatch = linkRegex.exec(string)) {
|
|
|
|
const [ matchedStr, schemeName ] = regexMatch;
|
|
|
|
const matchedStrIsEmail = isEmail(matchedStr);
|
|
|
|
|
|
|
|
let anchorizedMatch;
|
|
|
|
if (matchedStrIsEmail && replaceEmail) {
|
|
|
|
anchorizedMatch = (<a href={`mailto:${matchedStr}`}>{matchedStr}</a>);
|
|
|
|
} else if (!matchedStrIsEmail && replaceLink) {
|
|
|
|
anchorizedMatch = (<a href={`${schemeName ? matchedStr : ('http://' + matchedStr)}`} target={target}>{matchedStr}</a>);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only need to add an element to the array and update the lastMatchIndex if we actually create an anchor
|
|
|
|
if (anchorizedMatch) {
|
|
|
|
// First add the string between the end of the last anchor text and the start of the current match
|
|
|
|
const currentMatchStartIndex = linkRegex.lastIndex - matchedStr.length;
|
|
|
|
|
|
|
|
if (lastMatchIndex !== currentMatchStartIndex) {
|
|
|
|
strWithAnchorElems.push(string.substring(lastMatchIndex, currentMatchStartIndex));
|
|
|
|
}
|
|
|
|
|
|
|
|
strWithAnchorElems.push(anchorizedMatch);
|
|
|
|
|
|
|
|
lastMatchIndex = linkRegex.lastIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strWithAnchorElems.length) {
|
|
|
|
// Add the string between the end of the last anchor and the end of the string
|
|
|
|
if (lastMatchIndex !== string.length) {
|
|
|
|
strWithAnchorElems.push(string.substring(lastMatchIndex));
|
|
|
|
}
|
|
|
|
|
|
|
|
return strWithAnchorElems;
|
|
|
|
} else {
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
}
|