-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathinject.js
More file actions
79 lines (69 loc) · 1.85 KB
/
inject.js
File metadata and controls
79 lines (69 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const fs = require('fs')
const path = require('path')
const isUrl = require('is-url')
const semver = require('semver')
const isNonEmptyString = require('../../util/is-non-empty-string')
const cst = require('./constants')
/** plotly-graph inject
*
* @param {object} opts : component options
* - plotlyJS
* - mathjax
* - topojson
* @return {array}
*/
function inject (opts = {}) {
const plotlyJS = opts.plotlyJS
const mathjax = opts.mathjax
const topojson = opts.topojson
const parts = []
if (isNonEmptyString(mathjax)) {
let src = resolve(mathjax)
if (src) {
parts.push(script(src + cst.mathJaxConfigQuery))
parts.push(scriptMathJaxFont(cst.mathJaxFontQuery))
} else {
throw new Error('Provided path to MathJax files does not exists')
}
}
if (isNonEmptyString(topojson)) {
let src = resolve(topojson)
if (src) {
parts.push(script(src))
} else {
throw new Error('Provided path to topojson files does not exists')
}
}
if (isNonEmptyString(plotlyJS)) {
let src = resolve(plotlyJS)
if (src) {
parts.push(script(src))
} else if (plotlyJS === 'latest' || semver.valid(plotlyJS)) {
parts.push(script(cdnSrc(plotlyJS)))
} else {
throw new Error('Provided path to plotly.js bundle does not exist and does not correspond to a release version')
}
} else {
parts.push(script(cdnSrc('latest')))
}
return parts
}
function resolve (v) {
if (isUrl(v)) {
return v
} else {
const p = path.resolve(v)
return fs.existsSync(p) ? p : false
}
}
function script (src) {
return `<script src="${src}"></script>`
}
function scriptMathJaxFont (opts) {
return `<script type="text/javascript">${opts}</script>`
}
function cdnSrc (v) {
v = v.charAt(0) === 'v' ? v.slice(1) : v
return `https://cdn.plot.ly/plotly-${v}.min.js`
}
module.exports = inject