-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathflex-box.js
126 lines (111 loc) · 3.32 KB
/
flex-box.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
import Line from './line'
import { isExact, isAuto } from './utils'
import STYLES from './constants'
const KEY = {
[STYLES.FLEX_DIRECTION.ROW]: {
width: 'width',
contentWidth: 'contentWidth',
x: 'x',
y: 'y',
contentX: 'contentX',
height: 'height',
contentHeight: 'contentHeight'
},
[STYLES.FLEX_DIRECTION.COLUMN]: {
width: 'height',
contentWidth: 'contentHeight',
x: 'y',
y: 'x',
contentX: 'contentY',
height: 'width',
contentHeight: 'contentWidth'
}
}
// 目前flex是基于inline-block的简单实现,只支持row方向width + flex混用
export default class FlexBox extends Line {
constructor() {
super()
this.exactValue = 0
this.flexTotal = 0
this.key = null
}
closeLine() {
super.closeLine()
this.calcFlex()
}
bind(el) {
this.container = el.parent
if (el.parent) {
this.key = KEY[el.parent.renderStyles.flexDirection]
}
this.initHeight(el)
this.outerWidth = el.parent && isAuto(el.parent.styles[this.key.width]) ? Infinity : el.parent.renderStyles[this.key.contentWidth]
this.start = el
this.add(el)
}
add(el) {
if (isExact(el.styles[this.key.width])) {
this.exactValue += el.renderStyles[this.key.width]
} else if (isExact(el.styles.flex)) {
this.flexTotal += el.renderStyles.flex
}
this.elements.push(el)
el.line = this
this.refreshWidthHeight(el)
if (!el.next) {
this.closeLine()
}
}
initHeight() {
this[this.key.height] = 0
}
refreshWidthHeight(el) {
if (el.renderStyles[this.key.height] > this[this.key.height]) {
this[this.key.height] = el.renderStyles[this.key.height]
}
this[this.key.width] += el.renderStyles[this.key.width]
}
initLayout(el) {
this.right = el._getContainerLayout()[this.key.contentX]
this[this.key.x] = el._getContainerLayout()[this.key.contentX]
this[this.key.y] = this.getPreLine(el)[this.key.y]
}
refreshElementPosition(el) {
if (this.start === el) {
this.initLayout(el)
}
// 刷新位置,首先以左边计算
el[this.key.x] = this.right + this.offsetX
el[this.key.y] = this[this.key.y] + this.getOffsetY(el)
// + (this.height - el.renderStyles.height) / 2
this.right += el.renderStyles[this.key.width]
}
calcFlex() {
const { [this.key.contentWidth]: containerWidth } = this.container.renderStyles
this.elements.forEach(child => {
if (isExact(child.styles.flex)) {
child.renderStyles[this.key.width] = (child.styles.flex / this.flexTotal) * (containerWidth - this.exactValue)
child._refreshContentWithLayout()
}
})
}
refreshXAlign() {
if (!this.end.parent) return
let offsetX = this.outerWidth - this[this.key.width]
if (this.end.parent.renderStyles.justifyContent === 'center') {
offsetX = offsetX / 2
} else if (this.end.parent.renderStyles.justifyContent === 'flex-start') {
offsetX = 0
}
this.offsetX = offsetX
}
getOffsetY(el) {
if (el.renderStyles.alignSelf === 'flex-end') {
return (this.container.renderStyles[this.key.contentHeight] - el.renderStyles[this.key.height])
} else if (el.renderStyles.alignSelf === 'center') {
return (this.container.renderStyles[this.key.contentHeight] - el.renderStyles[this.key.height]) / 2
} else {
return 0
}
}
}