-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageFactory.js
More file actions
246 lines (208 loc) · 7.52 KB
/
ImageFactory.js
File metadata and controls
246 lines (208 loc) · 7.52 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
const _ = require('lodash')
const CircleType = require('./imageTypes/circle.imagetype')
const EllipseType = require('./imageTypes/ellipse.imagetype')
const SquareType = require('./imageTypes/square.imagetype')
const RectangleType = require('./imageTypes/rectangle.imagetype')
const RecoloredExternalSvg = require('./imageTypes/recoloredExternalSvg.imagetype')
const UrlType = require('./imageTypes/url.imagetype')
const ClipFactory = require('./ClipFactory')
class ImageFactory {
static get types () {
return {
circle: CircleType,
square: SquareType,
url: UrlType,
rect: RectangleType,
ellipse: EllipseType,
data: UrlType,
recoloredExternalSvg: RecoloredExternalSvg,
}
}
static get basicShapes () {
return ['circle', 'ellipse', 'square', 'rect']
}
static get scalingStrategies () {
return {
vertical: { clip: 'frombottom' },
horizontal: { clip: 'fromleft' },
fromleft: { clip: 'fromleft' },
fromright: { clip: 'fromright' },
frombottom: { clip: 'frombottom' },
fromtop: { clip: 'fromtop' },
scale: 'scale',
radialclip: { clip: 'radial' },
radial: { clip: 'radial' },
pie: { clip: 'radial' },
}
}
static get validScalingStrategyStrings () {
return _.keys(ImageFactory.scalingStrategies)
}
static get validScalingStrategyKeys () {
return ['clip', 'scale']
}
static validateAspectRatioString (candidateAspectRatio) {
// https://developer.mozilla.org/en/docs/Web/SVG/Attribute/preserveAspectRatio
const validAlignStrings = [
'none',
'xMinYMin',
'xMidYMin',
'xMaxYMin',
'xMinYMid',
'xMidYMid',
'xMaxYMid',
'xMinYMax',
'xMidYMax',
'xMaxYMax',
]
const validMeetOrSlice = ['meet', 'slice']
const [align, meetOrSlice] = candidateAspectRatio.split(' ')
if (!validAlignStrings.includes(align)) {
throw new Error(`Invalid preserveAspectRatio string '${candidateAspectRatio}'`)
}
if (meetOrSlice && !validMeetOrSlice.includes(meetOrSlice)) {
throw new Error(`Invalid preserveAspectRatio string '${candidateAspectRatio}'`)
}
}
static parseConfig (newConfig) {
let config = {}
if (!_.isString(newConfig)) {
if (!(newConfig.type in ImageFactory.types)) {
throw new Error(`Invalid image creation config : unknown image type ${newConfig.type}`)
}
config = newConfig
} else {
config = ImageFactory.parseConfigString(newConfig)
}
if (config.color && config.url) {
if (config.url.match(/\.svg$/)) {
config.type = 'recoloredExternalSvg'
} else {
throw new Error(`Cannot recolor ${config.url}: unsupported image type for recoloring`)
}
}
if (_.has(config, 'preserveAspectRatio')) {
ImageFactory.validateAspectRatioString(config.preserveAspectRatio)
}
return config
}
static parseConfigString (configString) {
const config = {}
if (configString.length <= 0) {
throw new Error("Invalid image creation configString '' : empty string")
}
let configParts = []
const httpRegex = new RegExp('^(.*?):?(https?://.*)$')
const matchesHttp = configString.match(httpRegex)
if (matchesHttp) {
configParts = _.without(matchesHttp[1].split(':'), 'url')
config.type = 'url'
config.url = matchesHttp[2]
} else {
configParts = configString.split(':')
// NB TODO ! converting to const breaks flow !
config.type = configParts.shift()
if (!(config.type in ImageFactory.types)) {
throw new Error(`Invalid image creation configString '${configString}' : unknown image type ${config.type}`)
}
}
if (['url'].includes(config.type) && (config.url == null)) {
config.url = configParts.pop()
const hasDot = new RegExp(/\./)
if (!config.url || !config.url.match(hasDot)) {
throw new Error(`Invalid image creation configString '${configString}' : url string must end with a url`)
}
}
if (['data'].includes(config.type)) {
config.url = `data:${configParts.pop()}`
// TODO this logic needs to check there is something after data:
if (!config.url) {
throw new Error(`Invalid image creation configString '${configString}' : data string must have a data url as last string part`)
}
}
const unknownParts = []
let part = configParts.shift()
while (part) {
if (part in ImageFactory.scalingStrategies) {
const handler = ImageFactory.scalingStrategies[part]
if (_.isString(handler)) {
config[handler] = true
} else {
_.extend(config, handler)
}
} else if (part.match(/opacity=/)) {
const [, opacityValueString] = part.split('=')
const opacityValue = parseFloat(opacityValueString)
if (_.isNaN(opacityValue) || opacityValue < 0 || opacityValue > 1) {
throw new Error(`Invalid opacity '${opacityValue}': Must be between 0 and 1`)
}
config.opacity = opacityValue
} else {
unknownParts.push(part)
}
part = configParts.shift()
}
if (unknownParts.length > 1) {
throw new Error(`Invalid image creation configString '${configString}' : too many unknown parts: [${unknownParts.join(',')}]`)
}
if (unknownParts.length === 1) {
config.color = unknownParts[0]
}
return config
}
static isInternetExplorer () {
const userAgentString = window.navigator.userAgent
const oldIe = userAgentString.indexOf('MSIE ')
const newIe = userAgentString.indexOf('Trident/')
if ((oldIe > -1) || (newIe > -1)) {
return true
}
return false
}
constructor ({ definitionManager }) {
this.definitionManager = definitionManager
}
addBaseImageTo (d3Node, config, width, height, dataAttributes) {
config = ImageFactory.parseConfig(config)
// VIS-121 - Prevent base svgs from peeking out over the variable images (only for basic shapes)
if (_.includes(ImageFactory.basicShapes, config.type) && ImageFactory.isInternetExplorer()) {
config.baseShapeScale = 0.98
}
return this.addImageTo(d3Node, config, width, height, dataAttributes)
}
addVarImageTo (d3Node, config, width, height, dataAttributes) {
config = ImageFactory.parseConfig(config)
return this.addImageTo(d3Node, config, width, height, dataAttributes)
}
addImageTo (d3Node, config, width, height, dataAttributes) {
const imageInstance = this.createInstance(d3Node, config, width, height, dataAttributes)
return Promise.resolve()
.then(imageInstance.calculateImageDimensions.bind(imageInstance))
.then((imageDimensions) => {
if (config.clip) {
const newClipId = ClipFactory.addClipPath(config.clip, d3Node, imageDimensions)
return newClipId
} else {
return null
}
})
.then((clipId) => {
const imageHandle = imageInstance.appendToSvg()
if (clipId) {
imageHandle.attr('clip-path', `url(#${clipId})`)
}
})
}
calculateAspectRatio (config) {
config = ImageFactory.parseConfig(config)
const instance = this.createInstance(null, config, null, null, null)
return instance.calculateDesiredAspectRatio()
}
createInstance (d3Node, config, width, height, dataAttributes) {
if (!_.has(ImageFactory.types, config.type)) {
throw new Error(`Invalid image type '${config.type}'`)
}
return new ImageFactory.types[config.type](d3Node, config, width, height, dataAttributes, this.definitionManager)
}
}
module.exports = ImageFactory