mirror of
https://github.com/ascribe/onion.git
synced 2024-06-30 21:52:08 +02:00
Merge pull request #10 from ascribe/cleanup-utility-file-declarations
Move code from head_setter.js to dom_utils.js and adjust function-com…
This commit is contained in:
commit
590818cf0b
|
@ -31,7 +31,7 @@ import NavRoutesLinks from './nav_routes_links';
|
||||||
import { mergeOptions } from '../utils/general_utils';
|
import { mergeOptions } from '../utils/general_utils';
|
||||||
import { getLangText } from '../utils/lang_utils';
|
import { getLangText } from '../utils/lang_utils';
|
||||||
|
|
||||||
import {constructHead} from '../utils/head_setter';
|
import { constructHead } from '../utils/dom_utils';
|
||||||
|
|
||||||
|
|
||||||
let Header = React.createClass({
|
let Header = React.createClass({
|
||||||
|
|
|
@ -1,9 +1,45 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the title in the browser window.
|
* Set the title in the browser window.
|
||||||
*/
|
*/
|
||||||
export function setDocumentTitle(title) {
|
export function setDocumentTitle(title) {
|
||||||
document.title = title;
|
document.title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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) {
|
||||||
|
let head = (document.head || document.getElementsByTagName('head')[0]);
|
||||||
|
let element = document.createElement(elementType);
|
||||||
|
let oldElement = document.getElementById(elementId);
|
||||||
|
element.setAttribute('id', elementId);
|
||||||
|
for (let k in elementAttributes){
|
||||||
|
try {
|
||||||
|
element.setAttribute(k, elementAttributes[k]);
|
||||||
|
}
|
||||||
|
catch(e){
|
||||||
|
console.warn(e.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (oldElement) {
|
||||||
|
head.removeChild(oldElement);
|
||||||
|
}
|
||||||
|
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){
|
||||||
|
let favicons = headObject[k];
|
||||||
|
for (let f in favicons){
|
||||||
|
constructHeadElement(k, f, favicons[f]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,37 +0,0 @@
|
||||||
'use strict';
|
|
||||||
|
|
||||||
// elementType: string, is the type of the element, such as link, meta, etc.
|
|
||||||
// elementId id of the element
|
|
||||||
// elementAttributes: hash table containing the attributes of the relevant element
|
|
||||||
|
|
||||||
function constructHeadElement(elementType, elementId, elementAttributes) {
|
|
||||||
let head = (document.head || document.getElementsByTagName('head')[0]);
|
|
||||||
let element = document.createElement(elementType);
|
|
||||||
let oldElement = document.getElementById(elementId);
|
|
||||||
element.setAttribute('id', elementId);
|
|
||||||
for (let k in elementAttributes){
|
|
||||||
try {
|
|
||||||
element.setAttribute(k, elementAttributes[k]);
|
|
||||||
}
|
|
||||||
catch(e){
|
|
||||||
console.warn(e.message);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (oldElement) {
|
|
||||||
head.removeChild(oldElement);
|
|
||||||
}
|
|
||||||
head.appendChild(element);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Accepts a dictionary of dictionaries which comprises a part or all of html head part
|
|
||||||
// {link : {id1: {rel: ... }}}
|
|
||||||
// traverses a tree of depth 3 (no backtracking)
|
|
||||||
export function constructHead(headObject){
|
|
||||||
for (let k in headObject){
|
|
||||||
let favicons = headObject[k];
|
|
||||||
for (let f in favicons){
|
|
||||||
constructHeadElement(k, f, favicons[f]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user