From ee912c2b2924de3cb9c08d3d29955451733ce32b Mon Sep 17 00:00:00 2001 From: Tyler Long Date: Sun, 10 Sep 2017 10:24:48 +0800 Subject: [PATCH] No global mermaid --- src/mermaid.js | 30 +++++++++++++++--------------- src/mermaid.spec.js | 28 +++++++++++++--------------- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/src/mermaid.js b/src/mermaid.js index 5366bc346..4e2392190 100644 --- a/src/mermaid.js +++ b/src/mermaid.js @@ -37,7 +37,7 @@ var init = function () { if (arguments.length >= 2) { /*! sequence config was passed as #1 */ if (typeof arguments[0] !== 'undefined') { - global.mermaid.sequenceConfig = arguments[0] + mermaid.sequenceConfig = arguments[0] } nodes = arguments[1] @@ -68,14 +68,14 @@ var init = function () { if (typeof global.mermaid_config !== 'undefined') { mermaidAPI.initialize(global.mermaid_config) } - log.debug('Start On Load before: ' + global.mermaid.startOnLoad) - if (typeof global.mermaid.startOnLoad !== 'undefined') { - log.debug('Start On Load inner: ' + global.mermaid.startOnLoad) - mermaidAPI.initialize({ startOnLoad: global.mermaid.startOnLoad }) + log.debug('Start On Load before: ' + mermaid.startOnLoad) + if (typeof mermaid.startOnLoad !== 'undefined') { + log.debug('Start On Load inner: ' + mermaid.startOnLoad) + mermaidAPI.initialize({ startOnLoad: mermaid.startOnLoad }) } - if (typeof global.mermaid.ganttConfig !== 'undefined') { - mermaidAPI.initialize({ gantt: global.mermaid.ganttConfig }) + if (typeof mermaid.ganttConfig !== 'undefined') { + mermaidAPI.initialize({ gantt: mermaid.ganttConfig }) } var txt @@ -117,10 +117,10 @@ const initialize = function (config) { log.debug('Initializing mermaid') if (typeof config.mermaid !== 'undefined') { if (typeof config.mermaid.startOnLoad !== 'undefined') { - global.mermaid.startOnLoad = config.mermaid.startOnLoad + mermaid.startOnLoad = config.mermaid.startOnLoad } if (typeof config.mermaid.htmlLabels !== 'undefined') { - global.mermaid.htmlLabels = config.mermaid.htmlLabels + mermaid.htmlLabels = config.mermaid.htmlLabels } } mermaidAPI.initialize(config) @@ -136,30 +136,30 @@ const contentLoaded = function () { // Check state of start config mermaid namespace if (typeof global.mermaid_config !== 'undefined') { if (global.mermaid_config.htmlLabels === false) { - global.mermaid.htmlLabels = false + mermaid.htmlLabels = false } } - if (global.mermaid.startOnLoad) { + if (mermaid.startOnLoad) { // For backwards compatability reasons also check mermaid_config variable if (typeof global.mermaid_config !== 'undefined') { // Check if property startOnLoad is set if (global.mermaid_config.startOnLoad === true) { - global.mermaid.init() + mermaid.init() } } else { // No config found, do check API config config = mermaidAPI.getConfig() if (config.startOnLoad) { - global.mermaid.init() + mermaid.init() } } } else { - if (typeof global.mermaid.startOnLoad === 'undefined') { + if (typeof mermaid.startOnLoad === 'undefined') { log.debug('In start, no config') config = mermaidAPI.getConfig() if (config.startOnLoad) { - global.mermaid.init() + mermaid.init() } } } diff --git a/src/mermaid.spec.js b/src/mermaid.spec.js index 3fc0d8e8e..a4a509bb0 100644 --- a/src/mermaid.spec.js +++ b/src/mermaid.spec.js @@ -7,51 +7,49 @@ */ import mermaid from './mermaid' -global.mermaid = mermaid - describe('when using mermaid and ', function () { describe('when detecting chart type ', function () { it('should not start rendering with mermaid_config.startOnLoad set to false', function () { global.mermaid_config = { startOnLoad: false } document.body.innerHTML = '
graph TD;\na;
' - spyOn(global.mermaid, 'init') + spyOn(mermaid, 'init') mermaid.contentLoaded() - expect(global.mermaid.init).not.toHaveBeenCalled() + expect(mermaid.init).not.toHaveBeenCalled() }) it('should not start rendering with mermaid.startOnLoad set to false', function () { - global.mermaid.startOnLoad = false + mermaid.startOnLoad = false global.mermaid_config = { startOnLoad: true } document.body.innerHTML = '
graph TD;\na;
' - spyOn(global.mermaid, 'init') + spyOn(mermaid, 'init') mermaid.contentLoaded() - expect(global.mermaid.init).not.toHaveBeenCalled() + expect(mermaid.init).not.toHaveBeenCalled() }) it('should start rendering with both startOnLoad set', function () { - global.mermaid.startOnLoad = true + mermaid.startOnLoad = true global.mermaid_config = { startOnLoad: true } document.body.innerHTML = '
graph TD;\na;
' - spyOn(global.mermaid, 'init') + spyOn(mermaid, 'init') mermaid.contentLoaded() - expect(global.mermaid.init).toHaveBeenCalled() + expect(mermaid.init).toHaveBeenCalled() }) it('should start rendering with mermaid.startOnLoad set and no mermaid_config defined', function () { - global.mermaid.startOnLoad = true + mermaid.startOnLoad = true document.body.innerHTML = '
graph TD;\na;
' - spyOn(global.mermaid, 'init') + spyOn(mermaid, 'init') mermaid.contentLoaded() - expect(global.mermaid.init).toHaveBeenCalled() + expect(mermaid.init).toHaveBeenCalled() }) it('should start rendering as a default with no changes performed', function () { document.body.innerHTML = '
graph TD;\na;
' - spyOn(global.mermaid, 'init') + spyOn(mermaid, 'init') mermaid.contentLoaded() - expect(global.mermaid.init).toHaveBeenCalled() + expect(mermaid.init).toHaveBeenCalled() }) })