From ee4c8f624eb61356f25f84c8d62e770059f3a403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Wed, 28 Oct 2015 17:45:28 +0100 Subject: [PATCH 1/3] Move code from head_setter.js to dom_utils.js and adjust function-comments --- js/components/header.js | 2 +- js/utils/dom_utils.js | 39 ++++++++++++++++++++++++++++++++++++++- js/utils/head_setter.js | 37 ------------------------------------- 3 files changed, 39 insertions(+), 39 deletions(-) delete mode 100644 js/utils/head_setter.js diff --git a/js/components/header.js b/js/components/header.js index fa32440d..83cad670 100644 --- a/js/components/header.js +++ b/js/components/header.js @@ -31,7 +31,7 @@ import NavRoutesLinks from './nav_routes_links'; import { mergeOptions } from '../utils/general_utils'; import { getLangText } from '../utils/lang_utils'; -import {constructHead} from '../utils/head_setter'; +import {constructHead} from '../utils/dom_utils'; let Header = React.createClass({ diff --git a/js/utils/dom_utils.js b/js/utils/dom_utils.js index c20e1009..6346168b 100644 --- a/js/utils/dom_utils.js +++ b/js/utils/dom_utils.js @@ -1,9 +1,46 @@ 'use strict'; - /** * Set the title in the browser window. */ export function setDocumentTitle(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); + continue; + } + } + 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]); + } + } +} \ No newline at end of file diff --git a/js/utils/head_setter.js b/js/utils/head_setter.js deleted file mode 100644 index 6ca2c9b0..00000000 --- a/js/utils/head_setter.js +++ /dev/null @@ -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]); - } - } -} From 66b86209bf41163d9d4146256d9b568f8cec333e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Wed, 28 Oct 2015 17:55:44 +0100 Subject: [PATCH 2/3] Add appropriate spaces to import statement in header.js --- js/components/header.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/components/header.js b/js/components/header.js index 83cad670..51f91318 100644 --- a/js/components/header.js +++ b/js/components/header.js @@ -31,7 +31,7 @@ import NavRoutesLinks from './nav_routes_links'; import { mergeOptions } from '../utils/general_utils'; import { getLangText } from '../utils/lang_utils'; -import {constructHead} from '../utils/dom_utils'; +import { constructHead } from '../utils/dom_utils'; let Header = React.createClass({ From eba9023efcdee21d4f51ed73d6010b918c60eef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Thu, 29 Oct 2015 13:22:10 +0100 Subject: [PATCH 3/3] From dom_utils.js, remove useless 'continue' statement in loop/try-catch --- js/utils/dom_utils.js | 1 - 1 file changed, 1 deletion(-) diff --git a/js/utils/dom_utils.js b/js/utils/dom_utils.js index 6346168b..d009f90f 100644 --- a/js/utils/dom_utils.js +++ b/js/utils/dom_utils.js @@ -23,7 +23,6 @@ function constructHeadElement(elementType, elementId, elementAttributes) { } catch(e){ console.warn(e.message); - continue; } } if (oldElement) {