-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
137 lines (121 loc) · 3.17 KB
/
utils.js
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
import {
isObject,
isUnset,
isSet,
} from 'mytoolkit'
export function getData(arr, index) {
let item = arr[index]
return item && isSet(item.value) ? item.value : item
}
export function maybePercentValue(value, target) {
if (/^\d+(\.\d+)?%$/.test(value)) {
if (isUnset(target)) {
target = 1
}
return parsePercent(value) * target
}
return value
}
export function parsePercent(p) {
if (!/^\d+(\.\d+)?%$/.test(p)) return p
return parseFloat(p) / 100
}
export function isInBound(bound, x, y) {
let bl = bound[0], br = bound[1]
if (bl[0] - x > 0) return false
if (br[0] - x < 0) return false
if (bl[1] - y > 0) return false
if (br[1] - y < 0) return false
return true
}
export function deepExtend(source, target = {}) {
Object.keys(target).forEach(tk => {
if (isObject(source[tk]) && isObject(target[tk])) {
deepExtend(source[tk], target[tk])
} else {
source[tk] = target[tk]
}
})
return source
}
export function getBoundingRect(doc) {
if (doc && doc.getBoundingClientRect) {
return doc.getBoundingClientRect()
}
return { left: 0, top: 0, width: 0, height: 0 }
}
export function d3Augment(d3) {
if (!d3.transition.prototype.attrs) {
let attrs = function (name) {
if (isObject(name)) {
Object.keys(name).forEach(k => {
this.attr(k, name[k])
})
}
return this
}
d3.transition.prototype.attrs = attrs
}
if (!d3.transition.prototype.styles) {
let styles = function (name) {
if (isObject(name)) {
Object.keys(name).forEach(k => {
this.style(k, name[k])
})
}
return this
}
d3.transition.prototype.styles = styles
}
if (!d3.selection.prototype.attrs) {
let attrs = function (name) {
if (isObject(name)) {
Object.keys(name).forEach(k => {
this.attr(k, name[k])
})
}
return this
}
d3.selection.prototype.attrs = attrs
}
if (!d3.selection.prototype.styles) {
let styles = function (name) {
if (isObject(name)) {
Object.keys(name).forEach(k => {
this.style(k, name[k])
})
}
return this
}
d3.selection.prototype.styles = styles
}
// make selection.append can accept [tag][class] synctax , it can also accept an initial attributes, an initial styles
let appendProto = d3.selection.prototype.append
if (!appendProto.lc_extended) {
let append = function (name, attrs, styles) {
if (isUnset(name)) {
return appendProto.call(this, name)
}
let [tagName, className] = name.split(/[.#]/)
let s = appendProto.call(this, tagName)
if (className) {
name.includes('#') && s.attr('id', className)
name.includes('.') && s.classed(className, 'true')
}
isObject(attrs) && s.attr(attrs)
isObject(styles) && s.style(styles)
return s
}
append.lc_extended = true
d3.selection.prototype.append = append
}
if (!d3.selection.prototype.safeSelect) {
d3.selection.prototype.safeSelect = function (selector) {
let s = this.select(selector)
if (s.empty()) {
s = this.append(selector)
}
return s
}
}
}