mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-14 06:43:25 +08:00
Get rid of "var "
This commit is contained in:
parent
daba43dcba
commit
f16143de7c
@ -2,21 +2,21 @@ import _ from 'lodash'
|
|||||||
|
|
||||||
import { logger } from '../../logger'
|
import { logger } from '../../logger'
|
||||||
|
|
||||||
var commits = {}
|
let commits = {}
|
||||||
var head = null
|
let head = null
|
||||||
var branches = { 'master': head }
|
let branches = { 'master': head }
|
||||||
var curBranch = 'master'
|
let curBranch = 'master'
|
||||||
var direction = 'LR'
|
let direction = 'LR'
|
||||||
var seq = 0
|
let seq = 0
|
||||||
|
|
||||||
function getRandomInt (min, max) {
|
function getRandomInt (min, max) {
|
||||||
return Math.floor(Math.random() * (max - min)) + min
|
return Math.floor(Math.random() * (max - min)) + min
|
||||||
}
|
}
|
||||||
|
|
||||||
function getId () {
|
function getId () {
|
||||||
var pool = '0123456789abcdef'
|
const pool = '0123456789abcdef'
|
||||||
var id = ''
|
let id = ''
|
||||||
for (var i = 0; i < 7; i++) {
|
for (let i = 0; i < 7; i++) {
|
||||||
id += pool[getRandomInt(0, 16)]
|
id += pool[getRandomInt(0, 16)]
|
||||||
}
|
}
|
||||||
return id
|
return id
|
||||||
@ -40,8 +40,8 @@ function isfastforwardable (currentCommit, otherCommit) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function isReachableFrom (currentCommit, otherCommit) {
|
function isReachableFrom (currentCommit, otherCommit) {
|
||||||
var currentSeq = currentCommit.seq
|
const currentSeq = currentCommit.seq
|
||||||
var otherSeq = otherCommit.seq
|
const otherSeq = otherCommit.seq
|
||||||
if (currentSeq > otherSeq) return isfastforwardable(otherCommit, currentCommit)
|
if (currentSeq > otherSeq) return isfastforwardable(otherCommit, currentCommit)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -49,7 +49,7 @@ function isReachableFrom (currentCommit, otherCommit) {
|
|||||||
export const setDirection = function (dir) {
|
export const setDirection = function (dir) {
|
||||||
direction = dir
|
direction = dir
|
||||||
}
|
}
|
||||||
var options = {}
|
let options = {}
|
||||||
export const setOptions = function (rawOptString) {
|
export const setOptions = function (rawOptString) {
|
||||||
logger.debug('options str', rawOptString)
|
logger.debug('options str', rawOptString)
|
||||||
rawOptString = rawOptString && rawOptString.trim()
|
rawOptString = rawOptString && rawOptString.trim()
|
||||||
@ -66,7 +66,7 @@ export const getOptions = function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const commit = function (msg) {
|
export const commit = function (msg) {
|
||||||
var commit = {
|
const commit = {
|
||||||
id: getId(),
|
id: getId(),
|
||||||
message: msg,
|
message: msg,
|
||||||
seq: seq++,
|
seq: seq++,
|
||||||
@ -84,8 +84,8 @@ export const branch = function (name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const merge = function (otherBranch) {
|
export const merge = function (otherBranch) {
|
||||||
var currentCommit = commits[branches[curBranch]]
|
const currentCommit = commits[branches[curBranch]]
|
||||||
var otherCommit = commits[branches[otherBranch]]
|
const otherCommit = commits[branches[otherBranch]]
|
||||||
if (isReachableFrom(currentCommit, otherCommit)) {
|
if (isReachableFrom(currentCommit, otherCommit)) {
|
||||||
logger.debug('Already merged')
|
logger.debug('Already merged')
|
||||||
return
|
return
|
||||||
@ -95,7 +95,7 @@ export const merge = function (otherBranch) {
|
|||||||
head = commits[branches[curBranch]]
|
head = commits[branches[curBranch]]
|
||||||
} else {
|
} else {
|
||||||
// create merge commit
|
// create merge commit
|
||||||
var commit = {
|
const commit = {
|
||||||
id: getId(),
|
id: getId(),
|
||||||
message: 'merged branch ' + otherBranch + ' into ' + curBranch,
|
message: 'merged branch ' + otherBranch + ' into ' + curBranch,
|
||||||
seq: seq++,
|
seq: seq++,
|
||||||
@ -112,21 +112,21 @@ export const merge = function (otherBranch) {
|
|||||||
export const checkout = function (branch) {
|
export const checkout = function (branch) {
|
||||||
logger.debug('in checkout')
|
logger.debug('in checkout')
|
||||||
curBranch = branch
|
curBranch = branch
|
||||||
var id = branches[curBranch]
|
const id = branches[curBranch]
|
||||||
head = commits[id]
|
head = commits[id]
|
||||||
}
|
}
|
||||||
|
|
||||||
export const reset = function (commitRef) {
|
export const reset = function (commitRef) {
|
||||||
logger.debug('in reset', commitRef)
|
logger.debug('in reset', commitRef)
|
||||||
var ref = commitRef.split(':')[0]
|
const ref = commitRef.split(':')[0]
|
||||||
var parentCount = parseInt(commitRef.split(':')[1])
|
let parentCount = parseInt(commitRef.split(':')[1])
|
||||||
var commit = ref === 'HEAD' ? head : commits[branches[ref]]
|
let commit = ref === 'HEAD' ? head : commits[branches[ref]]
|
||||||
logger.debug(commit, parentCount)
|
logger.debug(commit, parentCount)
|
||||||
while (parentCount > 0) {
|
while (parentCount > 0) {
|
||||||
commit = commits[commit.parent]
|
commit = commits[commit.parent]
|
||||||
parentCount--
|
parentCount--
|
||||||
if (!commit) {
|
if (!commit) {
|
||||||
var err = 'Critical error - unique parent commit not found during reset'
|
const err = 'Critical error - unique parent commit not found during reset'
|
||||||
logger.error(err)
|
logger.error(err)
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
@ -145,8 +145,8 @@ function upsert (arr, key, newval) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function prettyPrintCommitHistory (commitArr) {
|
function prettyPrintCommitHistory (commitArr) {
|
||||||
var commit = _.maxBy(commitArr, 'seq')
|
const commit = _.maxBy(commitArr, 'seq')
|
||||||
var line = ''
|
let line = ''
|
||||||
commitArr.forEach(function (c) {
|
commitArr.forEach(function (c) {
|
||||||
if (c === commit) {
|
if (c === commit) {
|
||||||
line += '\t*'
|
line += '\t*'
|
||||||
@ -154,19 +154,19 @@ function prettyPrintCommitHistory (commitArr) {
|
|||||||
line += '\t|'
|
line += '\t|'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
var label = [line, commit.id, commit.seq]
|
const label = [line, commit.id, commit.seq]
|
||||||
_.each(branches, function (value, key) {
|
_.each(branches, function (value, key) {
|
||||||
if (value === commit.id) label.push(key)
|
if (value === commit.id) label.push(key)
|
||||||
})
|
})
|
||||||
logger.debug(label.join(' '))
|
logger.debug(label.join(' '))
|
||||||
if (Array.isArray(commit.parent)) {
|
if (Array.isArray(commit.parent)) {
|
||||||
var newCommit = commits[commit.parent[0]]
|
const newCommit = commits[commit.parent[0]]
|
||||||
upsert(commitArr, commit, newCommit)
|
upsert(commitArr, commit, newCommit)
|
||||||
commitArr.push(commits[commit.parent[1]])
|
commitArr.push(commits[commit.parent[1]])
|
||||||
} else if (commit.parent == null) {
|
} else if (commit.parent == null) {
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
var nextCommit = commits[commit.parent]
|
const nextCommit = commits[commit.parent]
|
||||||
upsert(commitArr, commit, nextCommit)
|
upsert(commitArr, commit, nextCommit)
|
||||||
}
|
}
|
||||||
commitArr = _.uniqBy(commitArr, 'id')
|
commitArr = _.uniqBy(commitArr, 'id')
|
||||||
@ -175,7 +175,7 @@ function prettyPrintCommitHistory (commitArr) {
|
|||||||
|
|
||||||
export const prettyPrint = function () {
|
export const prettyPrint = function () {
|
||||||
logger.debug(commits)
|
logger.debug(commits)
|
||||||
var node = getCommitsArray()[0]
|
const node = getCommitsArray()[0]
|
||||||
prettyPrintCommitHistory([node])
|
prettyPrintCommitHistory([node])
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,7 +197,7 @@ export const getBranchesAsObjArray = function () {
|
|||||||
export const getBranches = function () { return branches }
|
export const getBranches = function () { return branches }
|
||||||
export const getCommits = function () { return commits }
|
export const getCommits = function () { return commits }
|
||||||
export const getCommitsArray = function () {
|
export const getCommitsArray = function () {
|
||||||
var commitArr = Object.keys(commits).map(function (key) {
|
const commitArr = Object.keys(commits).map(function (key) {
|
||||||
return commits[key]
|
return commits[key]
|
||||||
})
|
})
|
||||||
commitArr.forEach(function (o) { logger.debug(o.id) })
|
commitArr.forEach(function (o) { logger.debug(o.id) })
|
||||||
|
@ -5,9 +5,9 @@ import gitGraphParser from './parser/gitGraph'
|
|||||||
import d3 from '../../d3'
|
import d3 from '../../d3'
|
||||||
import { logger } from '../../logger'
|
import { logger } from '../../logger'
|
||||||
|
|
||||||
var allCommitsDict = {}
|
let allCommitsDict = {}
|
||||||
var branchNum
|
let branchNum
|
||||||
var config = {
|
let config = {
|
||||||
nodeSpacing: 150,
|
nodeSpacing: 150,
|
||||||
nodeFillColor: 'yellow',
|
nodeFillColor: 'yellow',
|
||||||
nodeStrokeWidth: 2,
|
nodeStrokeWidth: 2,
|
||||||
@ -25,7 +25,7 @@ var config = {
|
|||||||
y: 0
|
y: 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var apiConfig = {}
|
let apiConfig = {}
|
||||||
export const setConf = function (c) {
|
export const setConf = function (c) {
|
||||||
apiConfig = c
|
apiConfig = c
|
||||||
}
|
}
|
||||||
@ -53,8 +53,8 @@ function svgCreateDefs (svg) {
|
|||||||
|
|
||||||
function svgDrawLine (svg, points, colorIdx, interpolate) {
|
function svgDrawLine (svg, points, colorIdx, interpolate) {
|
||||||
interpolate = interpolate || 'basis'
|
interpolate = interpolate || 'basis'
|
||||||
var color = config.branchColors[colorIdx % config.branchColors.length]
|
const color = config.branchColors[colorIdx % config.branchColors.length]
|
||||||
var lineGen = d3.svg.line()
|
const lineGen = d3.svg.line()
|
||||||
.x(function (d) {
|
.x(function (d) {
|
||||||
return Math.round(d.x)
|
return Math.round(d.x)
|
||||||
})
|
})
|
||||||
@ -73,9 +73,9 @@ function svgDrawLine (svg, points, colorIdx, interpolate) {
|
|||||||
// Pass in the element and its pre-transform coords
|
// Pass in the element and its pre-transform coords
|
||||||
function getElementCoords (element, coords) {
|
function getElementCoords (element, coords) {
|
||||||
coords = coords || element.node().getBBox()
|
coords = coords || element.node().getBBox()
|
||||||
var ctm = element.node().getCTM()
|
const ctm = element.node().getCTM()
|
||||||
var xn = ctm.e + coords.x * ctm.a
|
const xn = ctm.e + coords.x * ctm.a
|
||||||
var yn = ctm.f + coords.y * ctm.d
|
const yn = ctm.f + coords.y * ctm.d
|
||||||
return {
|
return {
|
||||||
left: xn,
|
left: xn,
|
||||||
top: yn,
|
top: yn,
|
||||||
@ -86,16 +86,16 @@ function getElementCoords (element, coords) {
|
|||||||
|
|
||||||
function svgDrawLineForCommits (svg, fromId, toId, direction, color) {
|
function svgDrawLineForCommits (svg, fromId, toId, direction, color) {
|
||||||
logger.debug('svgDrawLineForCommits: ', fromId, toId)
|
logger.debug('svgDrawLineForCommits: ', fromId, toId)
|
||||||
var fromBbox = getElementCoords(svg.select('#node-' + fromId + ' circle'))
|
const fromBbox = getElementCoords(svg.select('#node-' + fromId + ' circle'))
|
||||||
var toBbox = getElementCoords(svg.select('#node-' + toId + ' circle'))
|
const toBbox = getElementCoords(svg.select('#node-' + toId + ' circle'))
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case 'LR':
|
case 'LR':
|
||||||
// (toBbox)
|
// (toBbox)
|
||||||
// +--------
|
// +--------
|
||||||
// + (fromBbox)
|
// + (fromBbox)
|
||||||
if (fromBbox.left - toBbox.left > config.nodeSpacing) {
|
if (fromBbox.left - toBbox.left > config.nodeSpacing) {
|
||||||
var lineStart = { x: fromBbox.left - config.nodeSpacing, y: toBbox.top + toBbox.height / 2 }
|
const lineStart = { x: fromBbox.left - config.nodeSpacing, y: toBbox.top + toBbox.height / 2 }
|
||||||
var lineEnd = { x: toBbox.left + toBbox.width, y: toBbox.top + toBbox.height / 2 }
|
const lineEnd = { x: toBbox.left + toBbox.width, y: toBbox.top + toBbox.height / 2 }
|
||||||
svgDrawLine(svg, [lineStart, lineEnd], color, 'linear')
|
svgDrawLine(svg, [lineStart, lineEnd], color, 'linear')
|
||||||
svgDrawLine(svg, [
|
svgDrawLine(svg, [
|
||||||
{ x: fromBbox.left, y: fromBbox.top + fromBbox.height / 2 },
|
{ x: fromBbox.left, y: fromBbox.top + fromBbox.height / 2 },
|
||||||
@ -124,8 +124,8 @@ function svgDrawLineForCommits (svg, fromId, toId, direction, color) {
|
|||||||
// |
|
// |
|
||||||
// + (toBbox)
|
// + (toBbox)
|
||||||
if (toBbox.top - fromBbox.top > config.nodeSpacing) {
|
if (toBbox.top - fromBbox.top > config.nodeSpacing) {
|
||||||
lineStart = { x: toBbox.left + toBbox.width / 2, y: fromBbox.top + fromBbox.height + config.nodeSpacing }
|
const lineStart = { x: toBbox.left + toBbox.width / 2, y: fromBbox.top + fromBbox.height + config.nodeSpacing }
|
||||||
lineEnd = { x: toBbox.left + toBbox.width / 2, y: toBbox.top }
|
const lineEnd = { x: toBbox.left + toBbox.width / 2, y: toBbox.top }
|
||||||
svgDrawLine(svg, [lineStart, lineEnd], color, 'linear')
|
svgDrawLine(svg, [lineStart, lineEnd], color, 'linear')
|
||||||
svgDrawLine(svg, [
|
svgDrawLine(svg, [
|
||||||
{ x: fromBbox.left + fromBbox.width / 2, y: fromBbox.top + fromBbox.height },
|
{ x: fromBbox.left + fromBbox.width / 2, y: fromBbox.top + fromBbox.height },
|
||||||
@ -156,8 +156,8 @@ function cloneNode (svg, selector) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function renderCommitHistory (svg, commitid, branches, direction) {
|
function renderCommitHistory (svg, commitid, branches, direction) {
|
||||||
var commit
|
let commit
|
||||||
var numCommits = Object.keys(allCommitsDict).length
|
const numCommits = Object.keys(allCommitsDict).length
|
||||||
if (_.isString(commitid)) {
|
if (_.isString(commitid)) {
|
||||||
do {
|
do {
|
||||||
commit = allCommitsDict[commitid]
|
commit = allCommitsDict[commitid]
|
||||||
@ -187,7 +187,7 @@ function renderCommitHistory (svg, commitid, branches, direction) {
|
|||||||
.attr('stroke', config.nodeStrokeColor)
|
.attr('stroke', config.nodeStrokeColor)
|
||||||
.attr('stroke-width', config.nodeStrokeWidth)
|
.attr('stroke-width', config.nodeStrokeWidth)
|
||||||
|
|
||||||
var branch = _.find(branches, ['commit', commit])
|
const branch = _.find(branches, ['commit', commit])
|
||||||
if (branch) {
|
if (branch) {
|
||||||
logger.debug('found branch ', branch.name)
|
logger.debug('found branch ', branch.name)
|
||||||
svg.select('#node-' + commit.id + ' p')
|
svg.select('#node-' + commit.id + ' p')
|
||||||
@ -237,8 +237,7 @@ function renderLines (svg, commit, direction, branchColor) {
|
|||||||
|
|
||||||
export const draw = function (txt, id, ver) {
|
export const draw = function (txt, id, ver) {
|
||||||
try {
|
try {
|
||||||
var parser
|
const parser = gitGraphParser.parser
|
||||||
parser = gitGraphParser.parser
|
|
||||||
parser.yy = db
|
parser.yy = db
|
||||||
|
|
||||||
logger.debug('in gitgraph renderer', txt, id, ver)
|
logger.debug('in gitgraph renderer', txt, id, ver)
|
||||||
@ -247,15 +246,15 @@ export const draw = function (txt, id, ver) {
|
|||||||
|
|
||||||
config = _.extend(config, apiConfig, db.getOptions())
|
config = _.extend(config, apiConfig, db.getOptions())
|
||||||
logger.debug('effective options', config)
|
logger.debug('effective options', config)
|
||||||
var direction = db.getDirection()
|
const direction = db.getDirection()
|
||||||
allCommitsDict = db.getCommits()
|
allCommitsDict = db.getCommits()
|
||||||
var branches = db.getBranchesAsObjArray()
|
const branches = db.getBranchesAsObjArray()
|
||||||
if (direction === 'BT') {
|
if (direction === 'BT') {
|
||||||
config.nodeLabel.x = branches.length * config.branchOffset
|
config.nodeLabel.x = branches.length * config.branchOffset
|
||||||
config.nodeLabel.width = '100%'
|
config.nodeLabel.width = '100%'
|
||||||
config.nodeLabel.y = -1 * 2 * config.nodeRadius
|
config.nodeLabel.y = -1 * 2 * config.nodeRadius
|
||||||
}
|
}
|
||||||
var svg = d3.select('#' + id)
|
const svg = d3.select('#' + id)
|
||||||
svgCreateDefs(svg)
|
svgCreateDefs(svg)
|
||||||
branchNum = 1
|
branchNum = 1
|
||||||
_.each(branches, function (v) {
|
_.each(branches, function (v) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user