From 110e4ee586fa1d8b6d393b5c35ae95a0b277cf65 Mon Sep 17 00:00:00 2001 From: Tyler Long Date: Sun, 16 Apr 2017 23:08:37 +0800 Subject: [PATCH] Refactor code --- src/d3.js | 17 ---------------- src/editor.js | 2 +- src/logger.js | 33 +++++++++--------------------- src/mermaid.js | 49 ++++++++++++++++----------------------------- src/mermaid.spec.js | 27 ------------------------- 5 files changed, 27 insertions(+), 101 deletions(-) diff --git a/src/d3.js b/src/d3.js index 46d502c94..7699d087f 100644 --- a/src/d3.js +++ b/src/d3.js @@ -3,7 +3,6 @@ const d3 = require('d3') module.exports = d3; /* - D3 Text Wrap By Vijith Assar http://www.vijithassar.com @@ -11,7 +10,6 @@ module.exports = d3; @vijithassar Detailed instructions at http://www.github.com/vijithassar/d3textwrap - */ (function () { @@ -313,8 +311,6 @@ module.exports = d3; // adjust the length by the offset we've tracked // due to the misreported length discussed above - // var testWidth = newWidth - totalOffset - // if our latest version of the string is too // big for the bounds, use the previous // version of the string (without the newest word @@ -363,9 +359,6 @@ module.exports = d3; textNodeSelected.text('') for (i = 0; i < substrings.length; i++) { substring = substrings[i].string - // if (i > 0) { - // var previousSubstring = substrings[i - 1] - // } // only append if we're sure it won't make the tspans // overflow the bounds. if ((i) * lineHeight < bounds.height - (lineHeight * 1.5)) { @@ -387,15 +380,6 @@ module.exports = d3; if (padding) { xOffset += padding } return xOffset }) - // .attr('dx', function() { - // if(i == 0) { - // var render_offset = 0; - // } else if(i > 0) { - // render_offset = substrings[i - 1].width; - // render_offset = render_offset * -1; - // } - // return render_offset; - // }); } } } @@ -461,4 +445,3 @@ module.exports = d3; } } })() -/* jshint ignore:end */ diff --git a/src/editor.js b/src/editor.js index 59750e9cd..3dc644cbc 100644 --- a/src/editor.js +++ b/src/editor.js @@ -1,6 +1,6 @@ function decodeHTMLEntities (str) { if (str && typeof str === 'string') { - // strip script/html tags + // strip script/html tags var element = document.querySelector('.editor') str = str.replace(/]*>([\S\s]*?)<\/script>/gmi, '') diff --git a/src/logger.js b/src/logger.js index 0e1fe26a2..a7bf9e1ae 100644 --- a/src/logger.js +++ b/src/logger.js @@ -10,29 +10,14 @@ * => [2011-3-3T20:24:4.810 error (5021)] booom */ -// const LEVELS = { -// debug: 1, -// info: 2, -// warn: 3, -// error: 4, -// fatal: 5, -// default: 5 -// } - -// var defaultLevel = LEVELS.error - -// exports.setLogLevel = function (level) { -// defaultLevel = level; -// }; - function formatTime (timestamp) { var hh = timestamp.getUTCHours() var mm = timestamp.getUTCMinutes() var ss = timestamp.getSeconds() var ms = timestamp.getMilliseconds() - // If you were building a timestamp instead of a duration, you would uncomment the following line to get 12-hour (not 24) time - // if (hh > 12) {hh = hh % 12;} - // These lines ensure you have two-digits + // If you were building a timestamp instead of a duration, you would uncomment the following line to get 12-hour (not 24) time + // if (hh > 12) {hh = hh % 12;} + // These lines ensure you have two-digits if (hh < 10) { hh = '0' + hh } @@ -48,7 +33,7 @@ function formatTime (timestamp) { if (ms < 10) { ms = '00' + ms } - // This formats your string to HH:MM:SS + // This formats your string to HH:MM:SS var t = hh + ':' + mm + ':' + ss + ' (' + ms + ')' return t } @@ -58,11 +43,11 @@ function format (level) { return '%c ' + time + ' :%c' + level + ': ' } -var debug = function () {} -var info = function () {} -var warn = function () {} -var error = function () {} -var fatal = function () {} +var debug = function () { } +var info = function () { } +var warn = function () { } +var error = function () { } +var fatal = function () { } /** * logLevel , decides the amount of logging to be used. diff --git a/src/mermaid.js b/src/mermaid.js index f0dd0043e..409599152 100644 --- a/src/mermaid.js +++ b/src/mermaid.js @@ -38,7 +38,7 @@ var init = function () { log.debug('Starting rendering diagrams') var nodes if (arguments.length >= 2) { - /*! sequence config was passed as #1 */ + /*! sequence config was passed as #1 */ if (typeof arguments[0] !== 'undefined') { global.mermaid.sequenceConfig = arguments[0] } @@ -48,7 +48,7 @@ var init = function () { nodes = arguments[0] } - // if last argument is a function this is the callback function + // if last argument is a function this is the callback function var callback if (typeof arguments[arguments.length - 1] === 'function') { callback = arguments[arguments.length - 1] @@ -64,23 +64,21 @@ var init = function () { } } nodes = nodes === undefined ? document.querySelectorAll('.mermaid') - : typeof nodes === 'string' ? document.querySelectorAll(nodes) - : nodes instanceof window.Node ? [nodes] + : typeof nodes === 'string' ? document.querySelectorAll(nodes) + : nodes instanceof window.Node ? [nodes] : nodes // Last case - sequence config was passed pick next - var i - 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}) + mermaidAPI.initialize({ startOnLoad: global.mermaid.startOnLoad }) } if (typeof global.mermaid.ganttConfig !== 'undefined') { - mermaidAPI.initialize({gantt: global.mermaid.ganttConfig}) + mermaidAPI.initialize({ gantt: global.mermaid.ganttConfig }) } var txt @@ -92,10 +90,10 @@ var init = function () { bindFunctions(element) } - for (i = 0; i < nodes.length; i++) { + for (var i = 0; i < nodes.length; i++) { var element = nodes[i] - /*! Check if previously processed */ + /*! Check if previously processed */ if (!element.getAttribute('data-processed')) { element.setAttribute('data-processed', true) } else { @@ -104,16 +102,11 @@ var init = function () { var id = 'mermaidChart' + nextId++ - // Fetch the graph definition including tags + // Fetch the graph definition including tags txt = element.innerHTML - // console.warn('delivererd from the browser: '); - // console.warn(txt); - - // transforms the html to pure text + // transforms the html to pure text txt = he.decode(txt).trim() - // console.warn('he decode: '); - // console.warn(txt); mermaidAPI.render(id, txt, insertSvg, element) } @@ -204,7 +197,7 @@ exports.parseError = global.mermaid.parseError */ exports.contentLoaded = function () { var config - // Check state of start config mermaid namespace + // Check state of start config mermaid namespace if (typeof global.mermaid_config !== 'undefined') { if (equals(false, global.mermaid_config.htmlLabels)) { global.mermaid.htmlLabels = false @@ -212,43 +205,35 @@ exports.contentLoaded = function () { } if (global.mermaid.startOnLoad) { - // For backwards compatability reasons also check mermaid_config variable + // For backwards compatability reasons also check mermaid_config variable if (typeof global.mermaid_config !== 'undefined') { - // Check if property startOnLoad is set + // Check if property startOnLoad is set if (equals(true, global.mermaid_config.startOnLoad)) { global.mermaid.init() } } else { - // No config found, do check API config + // No config found, do check API config config = mermaidAPI.getConfig() if (config.startOnLoad) { global.mermaid.init() } } } else { - // if(typeof global.mermaid === 'undefined' ){ if (typeof global.mermaid.startOnLoad === 'undefined') { log.debug('In start, no config') config = mermaidAPI.getConfig() if (config.startOnLoad) { global.mermaid.init() } - // }else{ - // - // } } } } if (typeof document !== 'undefined') { - /*! - * Wait for document loaded before starting the execution - */ + /*! + * Wait for document loaded before starting the execution + */ window.addEventListener('load', function () { exports.contentLoaded() }, false) } - -// // Your actual module -// return module.exports; -// })); diff --git a/src/mermaid.spec.js b/src/mermaid.spec.js index 9f001e394..3da9db02d 100644 --- a/src/mermaid.spec.js +++ b/src/mermaid.spec.js @@ -5,40 +5,20 @@ /** * Created by knut on 14-11-23. */ -// var rewire = require('rewire'); var mermaid = require('./mermaid') -// var log = require('./logger').create(); describe('when using mermaid and ', function () { describe('when detecting chart type ', function () { - // var mermaid; - // var document; - // var window; - beforeEach(function () { - // var MockBrowser = require('mock-browser').mocks.MockBrowser; - // var mock = new MockBrowser(); - // - // delete global.mermaid_config; - // - /// / and in the run-code inside some object - // global.document = mock.getDocument(); - // global.window = mock.getWindow(); - - }) - it('should not start rendering with mermaid_config.startOnLoad set to false', function () { - // mermaid = rewire('./mermaid'); global.mermaid_config = {startOnLoad: false} document.body.innerHTML = '
graph TD;\na;
' spyOn(global.mermaid, 'init') - // log.debug(mermaid); mermaid.contentLoaded() expect(global.mermaid.init).not.toHaveBeenCalled() }) it('should not start rendering with mermaid.startOnLoad set to false', function () { - // mermaid = rewire('./mermaid'); global.mermaid.startOnLoad = false global.mermaid_config = {startOnLoad: true} @@ -49,7 +29,6 @@ describe('when using mermaid and ', function () { }) it('should start rendering with both startOnLoad set', function () { - // mermaid = rewire('./mermaid'); global.mermaid.startOnLoad = true global.mermaid_config = {startOnLoad: true} document.body.innerHTML = '
graph TD;\na;
' @@ -59,7 +38,6 @@ describe('when using mermaid and ', function () { }) it('should start rendering with mermaid.startOnLoad set and no mermaid_config defined', function () { - // mermaid = rewire('./mermaid'); global.mermaid.startOnLoad = true document.body.innerHTML = '
graph TD;\na;
' spyOn(global.mermaid, 'init') @@ -68,7 +46,6 @@ describe('when using mermaid and ', function () { }) it('should start rendering as a default with no changes performed', function () { - // mermaid = rewire('./mermaid'); document.body.innerHTML = '
graph TD;\na;
' spyOn(global.mermaid, 'init') mermaid.contentLoaded() @@ -83,12 +60,8 @@ describe('when using mermaid and ', function () { beforeEach(function () { global.mermaid_config = {startOnLoad: false} - // var MockBrowser = require('mock-browser').mocks.MockBrowser; - // var mock = new MockBrowser(); flow.parser.yy = graph graph.clear() - // global.document = mock.getDocument(); - // mermaid = rewire('./mermaid'); }) it('it should handle edges with text', function () { flow.parser.parse('graph TD;A-->|text ex|B;')