No global mermaid

This commit is contained in:
Tyler Long 2017-09-10 10:24:48 +08:00
parent bdf9b33b10
commit ee912c2b29
2 changed files with 28 additions and 30 deletions

View File

@ -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()
}
}
}

View File

@ -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 = '<div class="mermaid">graph TD;\na;</div>'
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 = '<div class="mermaid">graph TD;\na;</div>'
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 = '<div class="mermaid">graph TD;\na;</div>'
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 = '<div class="mermaid">graph TD;\na;</div>'
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 = '<div class="mermaid">graph TD;\na;</div>'
spyOn(global.mermaid, 'init')
spyOn(mermaid, 'init')
mermaid.contentLoaded()
expect(global.mermaid.init).toHaveBeenCalled()
expect(mermaid.init).toHaveBeenCalled()
})
})