index.vue 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. <template>
  2. <div class="wook-gantt">
  3. <div class="gantt-left" :style="{width:menuWidth+'px'}">
  4. <div class="gantt-title">
  5. <div class="gantt-title-text">{{$L('任务名称')}}</div>
  6. </div>
  7. <ul ref="ganttItem"
  8. class="gantt-item"
  9. @scroll="itemScrollListener"
  10. @mouseenter="mouseType='item'">
  11. <li v-for="(item, key) in lists" :key="key">
  12. <div class="item-title" @click="clickItem(item)">{{item.label}}</div>
  13. <Icon class="item-icon" type="ios-locate-outline" @click="scrollPosition(key)"/>
  14. </li>
  15. </ul>
  16. </div>
  17. <div ref="ganttRight" class="gantt-right">
  18. <div class="gantt-chart">
  19. <ul class="gantt-month">
  20. <li v-for="(item, key) in monthNum" :key="key" :style="monthStyle(key)">
  21. <div class="month-format">{{monthFormat(key)}}</div>
  22. </li>
  23. </ul>
  24. <ul class="gantt-date" @mousedown="dateMouseDown">
  25. <li v-for="(item, key) in dateNum" :key="key" :style="dateStyle(key)">
  26. <div class="date-format">
  27. <div class="format-day">{{dateFormat(key, 'day')}}</div>
  28. <div v-if="dateWidth > 46" class="format-wook">{{dateFormat(key, 'wook')}}</div>
  29. </div>
  30. </li>
  31. </ul>
  32. <ul ref="ganttTimeline"
  33. class="gantt-timeline"
  34. @scroll="timelineScrollListener"
  35. @mouseenter="mouseType='timeline'">
  36. <li v-for="(item, key) in lists" :key="key">
  37. <div
  38. class="timeline-item"
  39. :style="itemStyle(item)"
  40. @mousedown="itemMouseDown($event, item)">
  41. <div class="timeline-title">{{item.label}}</div>
  42. <div class="timeline-resizer"></div>
  43. </div>
  44. </li>
  45. </ul>
  46. </div>
  47. </div>
  48. </div>
  49. </template>
  50. <script>
  51. export default {
  52. name: 'GanttView',
  53. props: {
  54. lists: {
  55. type: Array
  56. },
  57. menuWidth: {
  58. type: Number,
  59. default: 300
  60. },
  61. itemWidth: {
  62. type: Number,
  63. default: 100
  64. }
  65. },
  66. data() {
  67. return {
  68. mouseType: '',
  69. mouseWidth: 0,
  70. mouseScaleWidth: 0,
  71. dateWidth: 100,
  72. ganttWidth: 0,
  73. mouseItem: null,
  74. mouseBak: {},
  75. dateMove: null
  76. }
  77. },
  78. mounted() {
  79. this.dateWidth = this.itemWidth;
  80. this.$refs.ganttRight.addEventListener('mousewheel', this.handleScroll, false);
  81. document.addEventListener('mousemove', this.itemMouseMove);
  82. document.addEventListener('mouseup', this.itemMouseUp);
  83. window.addEventListener("resize", this.handleResize, false);
  84. this.handleResize();
  85. },
  86. beforeDestroy() {
  87. this.$refs.ganttRight.removeEventListener('mousewheel', this.handleScroll, false);
  88. document.removeEventListener('mousemove', this.itemMouseMove);
  89. document.removeEventListener('mouseup', this.itemMouseUp);
  90. window.removeEventListener("resize", this.handleResize, false);
  91. },
  92. watch: {
  93. itemWidth(val) {
  94. this.dateWidth = val;
  95. }
  96. },
  97. computed: {
  98. monthNum() {
  99. const {ganttWidth, dateWidth} = this;
  100. return Math.floor(ganttWidth / dateWidth / 30) + 2
  101. },
  102. monthStyle() {
  103. const {mouseWidth, dateWidth} = this;
  104. return function(index) {
  105. let mouseDay = mouseWidth == 0 ? 0 : mouseWidth / dateWidth;
  106. let date = new Date();
  107. //今天00:00:00
  108. let nowDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
  109. //当前时间
  110. let curDay = new Date(nowDay.getTime() + mouseDay * 86400000);
  111. //当月最后一天
  112. let lastDay = new Date(curDay.getFullYear(), curDay.getMonth() + 1, 0, 23, 59, 59);
  113. //相差天数
  114. let diffDay = (lastDay - curDay) / 1000 / 60 / 60 / 24;
  115. //
  116. let width = dateWidth * diffDay;
  117. if (index > 0) {
  118. lastDay = new Date(curDay.getFullYear(), curDay.getMonth() + 1 + index, 0);
  119. width = lastDay.getDate() * dateWidth;
  120. }
  121. return {
  122. width: width + 'px',
  123. }
  124. }
  125. },
  126. monthFormat() {
  127. const {mouseWidth, dateWidth} = this;
  128. return function(index) {
  129. let mouseDay = mouseWidth == 0 ? 0 : mouseWidth / dateWidth;
  130. let date = new Date();
  131. //开始位置时间(今天00:00:00)
  132. let nowDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
  133. //当前时间
  134. let curDay = new Date(nowDay.getTime() + mouseDay * 86400000);
  135. //
  136. if (index > 0) {
  137. curDay = new Date(curDay.getFullYear(), curDay.getMonth() + 1 + index, 0);
  138. }
  139. return $A.formatDate("Y-m", curDay)
  140. }
  141. },
  142. dateNum() {
  143. const {ganttWidth, dateWidth} = this;
  144. return Math.floor(ganttWidth / dateWidth) + 2
  145. },
  146. dateStyle() {
  147. const {mouseWidth, dateWidth} = this;
  148. return function(index) {
  149. const style = {};
  150. //
  151. let mouseDay = mouseWidth == 0 ? 0 : mouseWidth / dateWidth;
  152. let mouseData = Math.floor(mouseDay) + index;
  153. if (mouseDay == Math.floor(mouseDay)) {
  154. mouseData--;
  155. }
  156. let j = mouseWidth == 0 ? index - 1 : mouseData;
  157. let date = new Date(new Date().getTime() + j * 86400000);
  158. if ([0, 6].indexOf(date.getDay()) !== -1) {
  159. style.backgroundColor = '#f9fafb';
  160. }
  161. //
  162. let width = dateWidth;
  163. if (index == 0) {
  164. width = Math.abs((mouseWidth % width - width) % width);
  165. }
  166. style.width = width + 'px';
  167. return style
  168. }
  169. },
  170. dateFormat() {
  171. const {mouseWidth, dateWidth} = this;
  172. return function(index, type) {
  173. let mouseDay = mouseWidth == 0 ? 0 : mouseWidth / dateWidth;
  174. let mouseData = Math.floor(mouseDay) + index;
  175. if (mouseDay == Math.floor(mouseDay)) {
  176. mouseData--;
  177. }
  178. let j = mouseWidth == 0 ? index - 1 : mouseData;
  179. let date = new Date(new Date().getTime() + j * 86400000)
  180. if (type == 'day') {
  181. return date.getDate();
  182. } else if (type == 'wook') {
  183. return this.$L(`星期${'日一二三四五六'.charAt(date.getDay())}`);
  184. } else {
  185. return date;
  186. }
  187. }
  188. },
  189. itemStyle() {
  190. const {mouseWidth, dateWidth, ganttWidth} = this;
  191. return function(item) {
  192. const {start, end} = item.time;
  193. const {style, moveX, moveW} = item;
  194. let date = new Date();
  195. //开始位置时间戳(今天00:00:00时间戳)
  196. let nowTime = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0).getTime();
  197. //距离开始位置多少天
  198. let diffStartDay = (start - nowTime) / 1000 / 60 / 60 / 24;
  199. let diffEndDay = (end - nowTime) / 1000 / 60 / 60 / 24;
  200. //
  201. let left = dateWidth * diffStartDay + (mouseWidth * -1);
  202. let width = dateWidth * (diffEndDay - diffStartDay);
  203. if (typeof moveX === "number") {
  204. left+= moveX;
  205. }
  206. if (typeof moveW === "number") {
  207. width+= moveW;
  208. }
  209. //
  210. const customStyle = {
  211. left: Math.min(Math.max(left, width * -1.2), ganttWidth * 1.2).toFixed(2) + 'px',
  212. width: width.toFixed(2) + 'px',
  213. };
  214. if (left < 0 && Math.abs(left) < width) {
  215. customStyle.paddingLeft = Math.abs(left).toFixed(2) + 'px'
  216. }
  217. if (left + width > ganttWidth && left < ganttWidth) {
  218. customStyle.paddingRight = Math.abs(left + width - ganttWidth).toFixed(2) + 'px'
  219. }
  220. if (typeof style === "object") {
  221. return Object.assign(customStyle, style);
  222. }
  223. return customStyle
  224. }
  225. }
  226. },
  227. methods: {
  228. itemScrollListener(e) {
  229. if (this.mouseType == 'timeline') {
  230. return;
  231. }
  232. this.$refs.ganttTimeline.scrollTop = e.target.scrollTop;
  233. },
  234. timelineScrollListener(e) {
  235. if (this.mouseType == 'item') {
  236. return;
  237. }
  238. this.$refs.ganttItem.scrollTop = e.target.scrollTop;
  239. },
  240. handleScroll(e) {
  241. e.preventDefault();
  242. if (e.ctrlKey) {
  243. //缩放
  244. this.dateWidth = Math.min(600, Math.max(24, this.dateWidth - Math.floor(e.deltaY)));
  245. this.mouseWidth = this.ganttWidth / 2 * ((this.dateWidth - 100) / 100) + this.dateWidth / 100 * this.mouseScaleWidth;
  246. return;
  247. }
  248. if (e.deltaY != 0) {
  249. const ganttTimeline = this.$refs.ganttTimeline;
  250. let newTop = ganttTimeline.scrollTop + e.deltaY;
  251. if (newTop < 0) {
  252. newTop = 0;
  253. } else if (newTop > ganttTimeline.scrollHeight - ganttTimeline.clientHeight) {
  254. newTop = ganttTimeline.scrollHeight - ganttTimeline.clientHeight;
  255. }
  256. if (ganttTimeline.scrollTop != newTop) {
  257. this.mouseType = 'timeline';
  258. ganttTimeline.scrollTop = newTop;
  259. }
  260. }
  261. if (e.deltaX != 0) {
  262. this.mouseWidth+= e.deltaX;
  263. this.mouseScaleWidth+= e.deltaX * (100 / this.dateWidth);
  264. }
  265. },
  266. handleResize() {
  267. this.ganttWidth = this.$refs.ganttTimeline.clientWidth;
  268. },
  269. dateMouseDown(e) {
  270. e.preventDefault();
  271. this.mouseItem = null;
  272. this.dateMove = {
  273. clientX: e.clientX
  274. };
  275. },
  276. itemMouseDown(e, item) {
  277. e.preventDefault();
  278. let type = 'moveX';
  279. if (e.target.className == 'timeline-resizer') {
  280. type = 'moveW';
  281. }
  282. if (typeof item[type] !== "number") {
  283. this.$set(item, type, 0);
  284. }
  285. this.mouseBak = {
  286. type: type,
  287. clientX: e.clientX,
  288. value: item[type],
  289. };
  290. this.mouseItem = item;
  291. this.dateMove = null;
  292. },
  293. itemMouseMove(e) {
  294. if (this.mouseItem != null) {
  295. e.preventDefault();
  296. var diff = e.clientX - this.mouseBak.clientX;
  297. this.$set(this.mouseItem, this.mouseBak.type, this.mouseBak.value + diff);
  298. } else if (this.dateMove != null) {
  299. e.preventDefault();
  300. let moveX = (this.dateMove.clientX - e.clientX) * 5;
  301. this.dateMove.clientX = e.clientX;
  302. this.mouseWidth+= moveX;
  303. this.mouseScaleWidth+= moveX * (100 / this.dateWidth);
  304. }
  305. },
  306. itemMouseUp(e) {
  307. if (this.mouseItem != null) {
  308. const {start, end} = this.mouseItem.time;
  309. let isM = false;
  310. //一个宽度的时间
  311. let oneWidthTime = 86400000 / this.dateWidth;
  312. //修改起止时间
  313. if (typeof this.mouseItem.moveX === "number" && this.mouseItem.moveX != 0) {
  314. let moveTime = this.mouseItem.moveX * oneWidthTime;
  315. this.$set(this.mouseItem.time, 'start', start + moveTime);
  316. this.$set(this.mouseItem.time, 'end', end + moveTime);
  317. this.$set(this.mouseItem, 'moveX', 0);
  318. isM = true;
  319. }
  320. //修改结束时间
  321. if (typeof this.mouseItem.moveW === "number" && this.mouseItem.moveW != 0) {
  322. let moveTime = this.mouseItem.moveW * oneWidthTime;
  323. this.$set(this.mouseItem.time, 'end', end + moveTime);
  324. this.$set(this.mouseItem, 'moveW', 0);
  325. isM = true;
  326. }
  327. //
  328. if (isM) {
  329. this.$emit("on-change", this.mouseItem)
  330. } else if (e.target.className == 'timeline-title') {
  331. this.clickItem(this.mouseItem);
  332. }
  333. this.mouseItem = null;
  334. } else if (this.dateMove != null) {
  335. this.dateMove = null;
  336. }
  337. },
  338. scrollPosition(pos) {
  339. let date = new Date();
  340. //今天00:00:00
  341. let nowDay = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
  342. //一个宽度的时间
  343. let oneWidthTime = 86400000 / this.dateWidth;
  344. //
  345. let moveWidth = (this.lists[pos].time.start - nowDay) / oneWidthTime - this.dateWidth - this.mouseWidth;
  346. this.mouseWidth+= moveWidth;
  347. this.mouseScaleWidth+= moveWidth * (100 / this.dateWidth);
  348. },
  349. clickItem(item) {
  350. this.$emit("on-click", item)
  351. }
  352. }
  353. }
  354. </script>
  355. <style lang="scss" scoped>
  356. .wook-gantt {
  357. position: absolute;
  358. top: 0;
  359. left: 0;
  360. right: 0;
  361. bottom: 0;
  362. display: flex;
  363. flex-direction: row;
  364. align-items: self-start;
  365. color: #747a81;
  366. * {
  367. box-sizing: border-box;
  368. }
  369. .gantt-left {
  370. flex-grow:0;
  371. flex-shrink:0;
  372. height: 100%;
  373. background-color: #ffffff;
  374. position: relative;
  375. display: flex;
  376. flex-direction: column;
  377. &:after {
  378. content: "";
  379. position: absolute;
  380. top: 0;
  381. right: 0;
  382. bottom: 0;
  383. width: 1px;
  384. background-color: rgba(237, 241, 242, 0.75);
  385. }
  386. .gantt-title {
  387. height: 76px;
  388. flex-grow: 0;
  389. flex-shrink: 0;
  390. background-color: #F9FAFB;
  391. padding-left: 12px;
  392. overflow: hidden;
  393. .gantt-title-text {
  394. line-height: 100px;
  395. max-width: 200px;
  396. overflow: hidden;
  397. text-overflow: ellipsis;
  398. white-space: nowrap;
  399. font-weight: 600;
  400. }
  401. }
  402. .gantt-item {
  403. transform: translateZ(0);
  404. max-height: 100%;
  405. overflow: auto;
  406. -ms-overflow-style: none;
  407. &::-webkit-scrollbar {
  408. display: none;
  409. }
  410. > li {
  411. height: 40px;
  412. border-bottom: 1px solid rgba(237, 241, 242, 0.75);
  413. position: relative;
  414. display: flex;
  415. align-items: center;
  416. &:hover {
  417. .item-icon {
  418. display: flex;
  419. }
  420. }
  421. .item-title {
  422. flex: 1;
  423. padding: 0 12px;
  424. cursor: default;
  425. overflow: hidden;
  426. text-overflow: ellipsis;
  427. white-space: nowrap;
  428. }
  429. .item-icon {
  430. display: none;
  431. align-items: center;
  432. justify-content: center;
  433. width: 32px;
  434. margin-right: 2px;
  435. font-size: 16px;
  436. color: #888888;
  437. }
  438. }
  439. }
  440. }
  441. .gantt-right {
  442. flex: 1;
  443. height: 100%;
  444. background-color: #ffffff;
  445. position: relative;
  446. overflow: hidden;
  447. .gantt-chart {
  448. position: absolute;
  449. top: 0;
  450. left: 0;
  451. right: 0;
  452. bottom: 0;
  453. transform: translateZ(0);
  454. .gantt-month {
  455. display: flex;
  456. align-items: center;
  457. position: absolute;
  458. top: 0;
  459. left: 0;
  460. right: 0;
  461. z-index: 1;
  462. height: 26px;
  463. line-height: 20px;
  464. font-size: 14px;
  465. background-color: #F9FAFB;
  466. > li {
  467. flex-grow: 0;
  468. flex-shrink: 0;
  469. height: 100%;
  470. position: relative;
  471. overflow: hidden;
  472. &:after {
  473. content: "";
  474. position: absolute;
  475. top: 0;
  476. right: 0;
  477. width: 1px;
  478. height: 100%;
  479. background-color: rgba(237, 241, 242, 0.75);
  480. }
  481. .month-format {
  482. overflow: hidden;
  483. white-space: nowrap;
  484. padding: 6px 6px 0;
  485. }
  486. }
  487. }
  488. .gantt-date {
  489. display: flex;
  490. align-items: center;
  491. position: absolute;
  492. top: 26px;
  493. left: 0;
  494. right: 0;
  495. bottom: 0;
  496. z-index: 2;
  497. cursor: move;
  498. &:before {
  499. content: "";
  500. position: absolute;
  501. top: 0;
  502. left: 0;
  503. right: 0;
  504. height: 50px;
  505. background-color: #F9FAFB;
  506. }
  507. > li {
  508. flex-grow: 0;
  509. flex-shrink: 0;
  510. height: 100%;
  511. position: relative;
  512. overflow: hidden;
  513. &:after {
  514. content: "";
  515. position: absolute;
  516. top: 0;
  517. right: 0;
  518. width: 1px;
  519. height: 100%;
  520. background-color: rgba(237, 241, 242, 0.75);
  521. }
  522. .date-format {
  523. overflow: hidden;
  524. white-space: nowrap;
  525. display: flex;
  526. flex-direction: column;
  527. align-items: center;
  528. justify-content: center;
  529. height: 44px;
  530. .format-day {
  531. line-height: 28px;
  532. font-size: 18px;
  533. }
  534. .format-wook {
  535. line-height: 16px;
  536. font-weight: 300;
  537. font-size: 13px;
  538. }
  539. }
  540. }
  541. }
  542. .gantt-timeline {
  543. position: absolute;
  544. top: 76px;
  545. left: 0;
  546. right: 0;
  547. bottom: 0;
  548. z-index: 3;
  549. overflow-x: hidden;
  550. overflow-y: auto;
  551. > li {
  552. cursor: default;
  553. height: 40px;
  554. border-bottom: 1px solid rgba(237, 241, 242, 0.75);
  555. position: relative;
  556. .timeline-item {
  557. position: absolute;
  558. top: 0;
  559. touch-action: none;
  560. pointer-events: auto;
  561. padding: 4px;
  562. margin-top: 4px;
  563. background: #e74c3c;
  564. border-radius: 18px;
  565. color: #fff;
  566. display: flex;
  567. align-items: center;
  568. will-change: contents;
  569. height: 32px;
  570. .timeline-title {
  571. touch-action: none;
  572. flex-grow: 1;
  573. overflow: hidden;
  574. text-overflow: ellipsis;
  575. white-space: nowrap;
  576. margin-left: 4px;
  577. margin-right: 10px;
  578. }
  579. .timeline-resizer {
  580. height: 22px;
  581. touch-action: none;
  582. width: 8px;
  583. background: rgba(255,255,255,0.1);
  584. cursor: ew-resize;
  585. flex-shrink: 0;
  586. will-change: visibility;
  587. position: absolute;
  588. top: 5px;
  589. right: 5px;
  590. }
  591. }
  592. }
  593. }
  594. }
  595. }
  596. }
  597. </style>