index.vue 21 KB

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