-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbaseOnReact.html
468 lines (436 loc) · 14.5 KB
/
baseOnReact.html
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<link rel="stylesheet"
type="text/css"
href="reset.css" />
<link rel="stylesheet"
type="text/css"
href="./jquery.ganttView.css" />
<link href="./lib/jquery-ui.min.css"
rel="stylesheet">
<script src="./lib/jquery.min.js"></script>
<script src="./lib/jquery-ui.min.js"></script>
<script src="./lib/babel.min.js"></script>
<script src="./lib/react.development.js"></script>
<script src="./lib/react-dom.development.js"></script>
<script src="./lib/moment.min.js"></script>
<script type="text/javascript"
src="data.js"></script>
<style type="text/css">
body {
font-family: tahoma, verdana, helvetica;
font-size: 0.8em;
padding: 10px;
}
</style>
<title>React and jQuery Gantt</title>
</head>
<body>
<div id="ganttChart"></div>
<script type="text/babel">
const defaults = {
cellWidth: 21,
cellHeight: 31,
start: moment(),
end: moment().add(3, 'd').add(12,'h').add(1, 's'),
};
class Gantt extends React.Component{
constructor(props) {
super(props);
this.state = {
datas: ganttData,
props: defaults,
hitBlock: false,
};
}
toNumArray = (num) => {
let temp=[];
for(let i = 0; i< num;i++){
temp.push(i)
}
return temp;
}
//天数差,小时差
dateDiff = () => {
let temp = [];
let {start,end} = this.state.props;
start = start.clone();
end = end.format("MM/DD");
for (; start.format("MM/DD")!= end; start.add(1, 'd')) {
temp.push(start.format("MM/DD"));
}
temp.push(start.format("MM/DD"));
return temp;
}
//获取小时列表
hourList = () => {
let temp = [];
let start = this.state.props.start.clone()
let end = this.state.props.end.clone()
while (start.isBefore(end)) {
temp.push(start.hour());
start.add(1, 'h');
}
//temp.push(start.hour());
return temp;
}
//其中2代表前后各加一个小时
hzheaderTotalWidth = () => {
let { start, end, cellWidth }=this.state.props;
return ((end.diff(start,'h') + 1) * cellWidth)
}
//计算每天的MM/DD的block长度
getHzHeaderMonthWith = (date) => {
let width;
let { start, end, cellWidth }=this.state.props;
if (date.format("MM/DD") == start.format("MM/DD")) {
width = (24 - (start.hour() - 0)) * cellWidth;
} else if (date.format("MM/DD") == end.format(
"MM/DD")) {
width = (end.hour() + 1) * cellWidth
} else {
width = 24 *cellWidth
}
return width - 1
}
allRow = () => {
let temp = [];
for (let item of this.state.datas) {
temp.push(...item.series)
}
return temp;
}
//计算block的width
calBlockWidth = (data) => {
let { cellWidth }=this.state.props;
return data.end.diff(data.start, 'h', true) * cellWidth
}
//计算block的margin-left
calLeft = (data) => {
let { start, cellWidth }=this.state.props;
return data.start.diff(start, 'h', true) * cellWidth
}
//test
calLeftRandom = (data) => {
let { start, cellWidth }=this.state.props;
let w = this.calBlockWidth(data)
return (data.start.diff(start, 'h', true) *
cellWidth) + w + (parseInt((Math.random() * (0 -
100) + 100)
.toFixed(
0)))
}
//随机颜色
randomColor = () => {
return '#' + (~~(Math.random() * (1 << 24))).toString(16)
}
componentDidMount() {
//$(window).resize(this.resizeWidth);
this.initBind();
}
componentDidUpdate(){
this.initBind();
}
//计算ganttganttview-slide-container容器的宽度
resizeWidth = () => {
console.log('go')
let ganttViewW = $('.ganttview').innerWidth();
let ganttVtHeaderW = $('.ganttview-vtheader').outerWidth();
$('.ganttview-slide-container').outerWidth(ganttViewW -
ganttVtHeaderW)
}
//以下绑定事件
initBind =()=> {
//可以带一个回调函数参数
this.bindBlockResize();
this.bindBlockDrag();
this.bindToolTip();
this.bindBlockClick();
}
bindBlockResize = (callback) => {
let borderSize = 2;
let prev; //前一个兄弟节点
let next; //后一个兄弟节点
let self; //自身
//记录碰撞时的最大width
let maxWidth = null;
let hitLeft = false
//锁定碰撞时的数据
let lock = false;
let maxLeftMargin = null; //null代表不限制
let minLeftMargin = null;
jQuery("div.ganttview-block").resizable({
handles: "e,w",
containment: 'parent',
start() {
prev = jQuery(this).prev()[0] ? jQuery(jQuery(this).prev()[
0]) : null;
next = jQuery(this).next()[0] ? jQuery(jQuery(this).next()[
0]) : null;
self = jQuery(this);
if (prev) {
let pleft = parseFloat(prev.css("left")) || 0;
let pwidth = parseFloat(prev.width()) + borderSize;
minLeftMargin = pleft + pwidth
} else {
minLeftMargin = null;
}
if (next) {
let nleft = parseFloat(next.css("left")) || 0;
maxLeftMargin = nleft;
} else {
maxLeftMargin = null;
}
},
resize(event, ui) {
let {
left = 0
} = ui.position;
let {
width
} = ui.size;
let hit = false;
if (minLeftMargin) {
if (left < minLeftMargin) {
console.log('left hit');
hit = true;
hitLeft = true;
}
}
if (maxLeftMargin) { //计算右边是否碰撞
if ((left + width + borderSize) > maxLeftMargin) {
console.log('right hit');
hit = true;
hitLeft = false;
}
}
if (hit) {
if (!lock) {
if (hitLeft) {
maxWidth = ui.originalPosition.left + ui.originalSize
.width - minLeftMargin;
} else {
maxWidth = maxLeftMargin - ui.originalPosition.left -
borderSize
}
lock = true;
}
//修正左缩放的位置
if (hitLeft) {
let fixRight = minLeftMargin - left;
self.css("margin-left", fixRight)
}
event.preventDefault();
//TOFIXED:固定最大宽度,因为event.preventDefault()不起暂停作用
self.css("max-width", maxWidth)
} else {
self.css('max-width', '');
self.css("margin-left", "");
maxWidth = null;
lock = false;
}
},
stop() {
if (maxWidth) {
self.width(maxWidth);
if (hitLeft) {
self.css("left", minLeftMargin)
}
}
//重置
self.css('max-width', '');
self.css("margin-left", "");
maxWidth = null;
lock = false;
if (callback) {
callback(jQuery(this))
}
}
});
}
bindBlockDrag = (callback) => {
let borderSize = 2;
let prev; //前一个兄弟节点
let next; //后一个兄弟节点
let self; //自身
let maxLeftMargin = 0; //-1值代表不限制
let minLeftMargin = 0;
jQuery("div.ganttview-block").draggable({
axis: "x",
scroll: true,
containment: 'parent',
start() {
prev = jQuery(this).prev()[0] ? jQuery(jQuery(this).prev()[
0]) : null;
next = jQuery(this).next()[0] ? jQuery(jQuery(this).next()[
0]) : null;
self = jQuery(this);
if (prev) {
let pleft = parseFloat(prev.css("left")) || 0;
let pwidth = parseFloat(prev.width()) + borderSize;
minLeftMargin = pleft + pwidth
} else {
minLeftMargin = null;
}
if (next) {
let nleft = parseFloat(next.css("left")) || 0;
maxLeftMargin = nleft;
} else {
maxLeftMargin = null;
}
},
drag(event, ui) {
let {
left = 0
} = ui.position;
let width = self.width();
let hit = false;
let hitLeft = false;
//计算左边是否碰撞
if (minLeftMargin) {
if (left < minLeftMargin) {
console.log('left hit');
hit = true;
hitLeft = true;
}
}
//计算右边是否碰撞
if (maxLeftMargin) {
if ((left + width + borderSize) > maxLeftMargin) {
console.log('right hit');
hit = true;
}
}
if (hit) {
event.preventDefault();
//因为获取的left不准,需要手动修正
let fixLeft;
if (hitLeft) {
fixLeft = minLeftMargin
} else {
fixLeft = maxLeftMargin - width - borderSize
}
self.css("left", fixLeft)
}
},
stop() {
if (callback) {
callback(jQuery(this))
}
}
});
}
bindBlockClick = (callback) => {
jQuery("div.ganttview-block").on("click", function() {
if (callback) {
callback(jQuery(this))
}
});
}
bindToolTip =()=> {
jQuery("div.ganttview-block").tooltip();
}
test = () =>{
let { hitBlock } =this.state;
hitBlock =! hitBlock;
this.setState({hitBlock})
}
render(){
let {datas,hitBlock} = this.state;
let {start} = this.state.props
return (
<div className="ganttview">
{/*左半边项目条*/}
<div className="ganttview-vtheader">
{datas.map((item)=>
<div className="ganttview-vtheader-item" key={item.id}>
<div className="ganttview-vtheader-item-name">{item.name}</div>
<div className="ganttview-vtheader-series">
{item.series.map((p)=>
<div className="ganttview-vtheader-series-name" key={p.id} >{p.name}</div>
)}
</div>
</div>
)}
</div>
{/*左半边项目条*/}
<div className="ganttview-slide-container">
{/* 日期和小时部分块 */}
<div className="ganttview-hzheader"
style={{width:this.hzheaderTotalWidth()+'px'}}>
<div className="ganttview-hzheader-months">
{
this.dateDiff().map((i,index)=>(
<div className="ganttview-hzheader-month"
style={{width: this.getHzHeaderMonthWith(start.clone().add(index,'d'))+'px'}}>{i}</div>
))
}
</div>
<div className="ganttview-hzheader-days">
{
this.hourList().map((i)=>(
<div className="ganttview-hzheader-day"
>{i}</div>
))
}
</div>
</div>
{/*日期和小时部分块*/}
{ /* <!-- 网格部分 --> */}
<div style={{clear: 'both'}}></div>
<div className="ganttview-grid"
style={{width:this.hzheaderTotalWidth()+'px'}}>
{
this.toNumArray(this.allRow().length).map(() =>(
<div className="ganttview-grid-row">
{
this.hourList().map((i)=>(
<div className={i==0?'ganttview-grid-row-cell ganttview-weekend':'ganttview-grid-row-cell'}></div>
))
}
</div>
))
}
</div>
{/* <!-- 网格部分 --> */}
{/* !-- 可操作的容器块部分 --> */}
<div className="ganttview-blocks">
{
this.allRow().map((item)=>(
<div className="ganttview-block-container"
style={{position:"relative",width:this.hzheaderTotalWidth()+'px'}}>
<div className="ganttview-block"
style={{position: "absolute",opacity: 0.5,width:this.calBlockWidth(item)+'px','backgroundColor':this.randomColor(),'left':this.calLeft(item)+'px'}}
title="Planned, 16 days">
<div className="ganttview-block-text">15</div>
</div>
{/* <!-- 生成碰撞用的block --> */}
{
hitBlock?(
<div className="ganttview-block"
style={{position: "absolute",opacity: 0.5,width:this.calBlockWidth(item)+'px','backgroundColor':this.randomColor(),'left':this.calLeftRandom(item)+'px'}}
title="Planned, 16 days">
<div className="ganttview-block-text">16</div>
</div>):""
}
{/* <!-- 生成碰撞用的block --> */}
</div>
))
}
</div>
{/* <!-- 可操作的容器块部分 --> */}
</div>
<button onClick={this.test}>开启碰撞块</button>
</div>
)
}
}
ReactDOM.render(
<Gantt/>,
document.getElementById('ganttChart')
);
</script>
</body>
</html>