Timeline.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import dayjs from 'dayjs';
  8. import { Event } from './Event';
  9. import { Ressource } from './Ressource';
  10. import { LitElement, html, customElement, property, css } from 'lit-element';
  11. import { styleMap } from 'lit-html/directives/style-map';
  12. import syncronizeElementsScrolling from './utils/syncroScroll';
  13. let Timeline = class Timeline extends LitElement {
  14. constructor(options = {}) {
  15. super();
  16. this._slotDuration = 30;
  17. this._legendSpan = 2;
  18. this.rowHeight = 32;
  19. this.slotWidth = 20;
  20. this.legendUnitFormat = { "y": "YYYY", "M": "MMMM", "d": 'D', "h": "H[h]", "m": "m'", 's': "s[s]" };
  21. this._clearSelectionhandler = (_e) => {
  22. this._clearSelectedItems();
  23. window.removeEventListener("click", this._clearSelectionhandler);
  24. };
  25. this.rows = options.ressources ? options.ressources.map(Ressource.toRessource) : [];
  26. this.items = options.items ? options.items.map(Event.toTimeSlot) : [];
  27. this._start = dayjs().startOf("day");
  28. this._end = this._start.endOf("day");
  29. this.rowsTitle = "Ressources";
  30. this.ressourceWidth = 200;
  31. this.selectedList = [];
  32. this.legend = [];
  33. this.defaultBackground = "";
  34. this.updateLegend();
  35. this.render();
  36. }
  37. get start() {
  38. return this._start.toISOString();
  39. }
  40. set start(value) {
  41. this._start = dayjs(value);
  42. this.updateLegend();
  43. }
  44. get end() {
  45. return this._end.toISOString();
  46. }
  47. set end(value) {
  48. this._end = dayjs(value);
  49. this.updateLegend();
  50. }
  51. get slotDuration() {
  52. return this._slotDuration;
  53. }
  54. set slotDuration(value) {
  55. this._slotDuration = value;
  56. this.updateLegend();
  57. }
  58. get legendSpan() {
  59. return this._legendSpan;
  60. }
  61. set legendSpan(value) {
  62. this._legendSpan = value;
  63. this.updateLegend();
  64. }
  65. set defaultBackground(value) {
  66. this.style.setProperty("--default-background", value);
  67. }
  68. get defaultBackground() {
  69. return this.style.getPropertyValue("--default-background");
  70. }
  71. setLegendUnitFormatAll(legend) {
  72. this.legendUnitFormat = Object.assign(Object.assign({}, this.legendUnitFormat), legend);
  73. this.updateLegend();
  74. }
  75. setLegendUnitFormat(unit, format) {
  76. this.legendUnitFormat[unit] = format;
  77. this.updateLegend();
  78. }
  79. addRessources(list) {
  80. return list.map(r => this.addRessource(r));
  81. }
  82. addRessource(ressource) {
  83. if (this.rows.filter(o => o.id == ressource.id).length > 0) {
  84. return;
  85. }
  86. const r = Ressource.toRessource(ressource);
  87. if (r.parent !== undefined) {
  88. const idx = this.rows.indexOf(r.parent);
  89. if (idx > -1) {
  90. this.rows[idx].children.push(r);
  91. this.rows.splice(idx + 1, 0, r);
  92. }
  93. else {
  94. return;
  95. }
  96. }
  97. else {
  98. this.rows = [...this.rows, r];
  99. }
  100. this.updateTimeslotPosition(r);
  101. return r;
  102. }
  103. removeRessourceById(id) {
  104. return this._removeRessourceById(id);
  105. }
  106. _removeRessourceById(id, depth = 0) {
  107. const output = { ressources: [], items: [] };
  108. for (let i = 0; i < this.rows.length; i) {
  109. const ressource = this.rows[i];
  110. if (ressource.id === id) {
  111. output.ressources.push(ressource);
  112. if (ressource.parent && depth === 0) {
  113. ressource.parent.children = ressource.parent.children.filter(o => o.id !== ressource.id);
  114. }
  115. this.rows.splice(i, 1);
  116. }
  117. else if (ressource.parentId === id) {
  118. const partialOutput = this._removeRessourceById(ressource.id, depth + 1);
  119. output.ressources.push(...partialOutput.ressources);
  120. output.items.push(...partialOutput.items);
  121. }
  122. else {
  123. i++;
  124. }
  125. }
  126. output.items.push(...this.items.filter(i => i.ressourceId === id));
  127. this.items = this.items.filter(i => i.ressourceId !== id);
  128. return output;
  129. }
  130. getRessources() {
  131. return this.rows;
  132. }
  133. getRessourceFromId(id) {
  134. const tmp = this.rows.filter(r => r.id === id);
  135. return tmp.length > 0 ? tmp[0] : null;
  136. }
  137. setRowsTitle(title) {
  138. this.rowsTitle = title;
  139. }
  140. addTimeSlots(list) {
  141. return list.map((e) => this.addTimeSlot(e));
  142. }
  143. addTimeSlot(slot) {
  144. if (this.items.filter(o => o.id == slot.id).length > 0) {
  145. return undefined;
  146. }
  147. const ressource = this.rows.find(r => r.id === slot.ressourceId);
  148. if (ressource === undefined) {
  149. return undefined;
  150. }
  151. const timeslot = Event.toTimeSlot(slot);
  152. this.items = [...this.items, timeslot];
  153. timeslot.isDisplayed = timeslot.end > this._start.toDate() || timeslot.start < this._end.toDate();
  154. this.updateTimeslotPosition(ressource);
  155. return timeslot;
  156. }
  157. removeTimeslotById(id) {
  158. const output = this.items.filter(o => o.id === id);
  159. this.items = this.items.filter(o => o.id !== id);
  160. return output;
  161. }
  162. updateTimeslotById(id) {
  163. const output = this.removeTimeslotById(id);
  164. if (output.length > 0) {
  165. this.addTimeSlot(output[0]);
  166. return output[0];
  167. }
  168. else {
  169. return null;
  170. }
  171. }
  172. updateTimeslotPosition(ressource) {
  173. const timeslots = this.items.filter(i => i.ressourceId === ressource.id);
  174. if (timeslots.length === 0) {
  175. ressource.height = this.rowHeight + (ressource.collapseChildren ? 5 : 0);
  176. return;
  177. }
  178. const start = this._start.toDate().getTime();
  179. const end = this._end.toDate().getTime();
  180. const points = [start, end];
  181. const populateInterval = (d) => {
  182. const t = d.getTime();
  183. if (start < t && t < end && !points.includes(t)) {
  184. points.push(t);
  185. }
  186. };
  187. timeslots.forEach(element => {
  188. populateInterval(element.start);
  189. populateInterval(element.end);
  190. });
  191. points.sort();
  192. const intervals = [];
  193. for (let i = 0; i < points.length - 1; i++) {
  194. const startTime = points[i];
  195. const endTime = points[i + 1];
  196. intervals.push({
  197. start: points[i],
  198. end: points[i + 1],
  199. slots: timeslots.filter(slot => (slot.start.getTime() <= startTime && endTime <= slot.end.getTime()))
  200. });
  201. }
  202. const lineCount = intervals.reduce((acc, interval) => Math.max(acc, interval.slots.length), 0);
  203. ressource.height = this.rowHeight * Math.max(lineCount, 1) + (ressource.collapseChildren ? 5 : 0);
  204. const sortTimeslots = (a, b) => {
  205. const t = a.start.getTime() - b.start.getTime();
  206. if (t === 0) {
  207. const tend = b.end.getTime() - a.end.getTime();
  208. return tend === 0 ? ('' + a.id).localeCompare(b.id) : tend;
  209. }
  210. return t;
  211. };
  212. timeslots.forEach(slot => slot.offset = -1);
  213. timeslots.sort(sortTimeslots);
  214. timeslots[0].offset = 0;
  215. const potentialOffset = [];
  216. for (let i = 0; i < lineCount; i++) {
  217. potentialOffset.push(i);
  218. }
  219. intervals.forEach(intervals => {
  220. intervals.slots.sort(sortTimeslots);
  221. const usedOffset = intervals.slots.map(o => o.offset).filter(i => i > -1);
  222. const availableOffset = potentialOffset.filter(i => !usedOffset.includes(i));
  223. intervals.slots.forEach(slot => {
  224. if (slot.offset === -1) {
  225. slot.offset = availableOffset.shift() || 0;
  226. }
  227. });
  228. });
  229. }
  230. getTimeSlots() {
  231. return this.items;
  232. }
  233. updateLegend() {
  234. const legend = [];
  235. const legendUnitList = ["y", "M", "d", "h", "m", 's'];
  236. const legendMinUnitSpan = this.slotDuration * this.legendSpan;
  237. for (const legendUnit of legendUnitList) {
  238. let currentDate = dayjs(this._start);
  239. let nextColumn = currentDate.add(legendMinUnitSpan, "m");
  240. const isLegendPossible = this._end.diff(this._start, legendUnit) > 0 &&
  241. (nextColumn.format(this.legendUnitFormat[legendUnit]) !== currentDate.format(this.legendUnitFormat[legendUnit])
  242. || currentDate.add(1, legendUnit).diff(currentDate, "m") >= legendMinUnitSpan);
  243. if (isLegendPossible) {
  244. const row = [];
  245. let i = 0;
  246. while (currentDate.isBefore(this._end)) {
  247. i += this.legendSpan;
  248. if (nextColumn.diff(currentDate, legendUnit) > 0) {
  249. row.push({ colspan: i, title: '' + currentDate.format(this.legendUnitFormat[legendUnit]) });
  250. i = 0;
  251. currentDate = nextColumn;
  252. }
  253. nextColumn = nextColumn.add(legendMinUnitSpan, "m");
  254. }
  255. legend.push(row);
  256. }
  257. }
  258. this.legend = legend;
  259. }
  260. _handleResizeX(e) {
  261. e.stopPropagation();
  262. this.ressourceWidth += e.detail;
  263. if (this.ressourceWidth < 0) {
  264. this.ressourceWidth = 0;
  265. }
  266. }
  267. _grabHeader(e) {
  268. const root = this.shadowRoot;
  269. if (root !== null) {
  270. const gridContainer = root.querySelector(".jc-timeline-grid-container");
  271. const headerContainer = root.querySelector(".jc-timeline-grid-title-container");
  272. let lastPosX = e.clientX;
  273. const scroll = function (e) {
  274. const scrollLeft = (lastPosX - e.clientX);
  275. headerContainer.scrollLeft += scrollLeft;
  276. gridContainer.scrollLeft += scrollLeft;
  277. lastPosX = e.clientX;
  278. };
  279. const mouseUpListener = function (_e) {
  280. window.removeEventListener("mousemove", scroll);
  281. window.removeEventListener("mouseup", mouseUpListener);
  282. };
  283. window.addEventListener("mousemove", scroll);
  284. window.addEventListener("mouseup", mouseUpListener);
  285. }
  286. }
  287. _getEventResizerHandler(slot, direction) {
  288. return (evt) => {
  289. evt.stopPropagation();
  290. evt.preventDefault();
  291. const startPos = evt.clientX;
  292. const localSlot = slot;
  293. const localDir = direction;
  294. const startDate = slot[direction];
  295. const resizeListener = (e) => {
  296. const newDate = dayjs(startDate).add(Math.round((e.clientX - startPos) / this.slotWidth) * this.slotDuration, "m").toDate();
  297. if (direction === "start" ? (newDate < localSlot.end) : (localSlot.start < newDate)) {
  298. localSlot[localDir] = newDate;
  299. this.updateTimeslotById(slot.id);
  300. }
  301. };
  302. const mouseUpListener = (_e) => {
  303. window.removeEventListener("mousemove", resizeListener);
  304. window.removeEventListener("mouseup", mouseUpListener);
  305. localSlot.moving = false;
  306. this.updateTimeslotById(slot.id);
  307. };
  308. localSlot.moving = true;
  309. window.addEventListener("mousemove", resizeListener);
  310. window.addEventListener("mouseup", mouseUpListener);
  311. };
  312. }
  313. _getEventGrabHandler(slot, editable, ressourceEditable, callback) {
  314. return (evt) => {
  315. evt.stopPropagation();
  316. evt.preventDefault();
  317. const startPos = evt.clientX;
  318. let hasChanged = false;
  319. const localSlot = slot;
  320. let localSlots = this.selectedList.filter(s => s instanceof Event).map(s => s);
  321. if (!localSlots.includes(localSlot)) {
  322. localSlots = [localSlot];
  323. }
  324. const startDates = localSlots.map(slot => slot.start);
  325. const endDates = localSlots.map(slot => slot.end);
  326. const updatePosition = editable ? (e) => {
  327. const changeTime = Math.round((e.clientX - startPos) / this.slotWidth) * this.slotDuration;
  328. return localSlots.map((slot, index) => {
  329. const prevStart = slot.start;
  330. slot.start = dayjs(startDates[index]).add(changeTime, "m").toDate();
  331. slot.end = dayjs(endDates[index]).add(changeTime, "m").toDate();
  332. return prevStart.getTime() !== slot.start.getTime();
  333. }).reduce((prev, curr) => prev || curr);
  334. } : (_e) => { return false; };
  335. const updateRessource = ressourceEditable ? (e) => {
  336. var _a, _b, _c;
  337. const rowId = (_c = (_b = (_a = this.shadowRoot) === null || _a === void 0 ? void 0 : _a.elementsFromPoint(e.clientX, e.clientY).find((e) => e.tagName == "TD")) === null || _b === void 0 ? void 0 : _b.parentElement) === null || _c === void 0 ? void 0 : _c.getAttribute('row-id');
  338. if (rowId) {
  339. const ressourceId = this.rows[Number(rowId)].id;
  340. if (ressourceId !== localSlot.ressourceId) {
  341. const oldRessource = this.getRessourceFromId(localSlot.ressourceId);
  342. localSlot.ressourceId = ressourceId;
  343. this.updateTimeslotPosition(oldRessource);
  344. return true;
  345. }
  346. }
  347. return false;
  348. } : (_e) => { return false; };
  349. const moveListener = (e) => {
  350. const a = updatePosition(e);
  351. if (updateRessource(e) || a) {
  352. hasChanged = true;
  353. this.updateTimeslotById(localSlot.id);
  354. }
  355. };
  356. const mouseUpListener = (e) => {
  357. window.removeEventListener("mousemove", moveListener);
  358. window.removeEventListener("mouseup", mouseUpListener);
  359. localSlot.moving = false;
  360. this.updateTimeslotById(slot.id);
  361. callback(e, hasChanged);
  362. };
  363. localSlot.moving = true;
  364. window.addEventListener("mousemove", moveListener);
  365. window.addEventListener("mouseup", mouseUpListener);
  366. };
  367. }
  368. _clearSelectedItems() {
  369. this.selectedList.map(selectable => {
  370. selectable.selected = false;
  371. this.updateTimeslotById(selectable.id);
  372. });
  373. this.selectedList = [];
  374. }
  375. _getEventClickHandler(clickedItem) {
  376. const item = clickedItem;
  377. return (e, wasModified = false) => {
  378. e.stopPropagation();
  379. const idx = this.selectedList.indexOf(item);
  380. if (idx > -1) {
  381. if (!wasModified) {
  382. if (e.ctrlKey) {
  383. this.selectedList.splice(idx, 1);
  384. item.selected = false;
  385. this.updateTimeslotById(item.id);
  386. }
  387. else {
  388. this._clearSelectedItems();
  389. }
  390. }
  391. }
  392. else {
  393. if (this.selectedList.length > 0 && !e.ctrlKey) {
  394. this._clearSelectedItems();
  395. }
  396. item.selected = true;
  397. this.selectedList.push(item);
  398. this.updateTimeslotById(item.id);
  399. }
  400. const myEvent = new CustomEvent('item-selected', {
  401. detail: { items: this.selectedList },
  402. bubbles: true,
  403. composed: true
  404. });
  405. this.dispatchEvent(myEvent);
  406. };
  407. }
  408. firstUpdated() {
  409. const root = this.shadowRoot;
  410. if (root !== null) {
  411. const gridContainer = root.querySelector(".jc-timeline-grid-container");
  412. syncronizeElementsScrolling([gridContainer, root.querySelector(".jc-timeline-grid-title-container")], "h");
  413. syncronizeElementsScrolling([gridContainer, root.querySelector(".jc-timeline-rows")], "v");
  414. }
  415. if (this.defaultBackground === "") {
  416. this.style.setProperty("--default-background", "SteelBlue");
  417. }
  418. }
  419. renderTimeslot(slot) {
  420. if (!slot.isDisplayed) {
  421. return html ``;
  422. }
  423. let rowTop = 0;
  424. let ressource;
  425. let i;
  426. for (i = 0; i < this.rows.length && this.rows[i].id !== slot.ressourceId; i++) {
  427. ressource = this.rows[i];
  428. if (ressource.show) {
  429. rowTop += ressource.height ? ressource.height : this.rowHeight;
  430. }
  431. }
  432. ressource = this.rows[i];
  433. const minute2pixel = this.slotWidth / this.slotDuration;
  434. const left = dayjs(slot.start).diff(this._start, "m") * minute2pixel;
  435. const right = -dayjs(slot.end).diff(this._end, "m") * minute2pixel;
  436. const style = {
  437. height: this.rowHeight - 4 + "px",
  438. top: rowTop + slot.offset * this.rowHeight + "px",
  439. left: left + "px",
  440. right: right + "px",
  441. backgroundColor: ""
  442. };
  443. const bgColor = slot.bgColor ? slot.bgColor : ressource.eventBgColor;
  444. if (bgColor) {
  445. style.backgroundColor = bgColor;
  446. }
  447. if (!ressource.show) {
  448. style.height = "";
  449. style.top = rowTop - 6 + "px";
  450. return html `<div class="jc-timeslot empty" style="${styleMap(style)}"></div>`;
  451. }
  452. let content = html `${slot.title}`;
  453. const resizer = slot.editable === null ? ressource.eventEditable : slot.editable;
  454. const editableRessource = slot.ressourceEditable === null ? ressource.eventRessourceEditable : slot.ressourceEditable;
  455. if (resizer) {
  456. content = html `<div class="jc-timeslot-resizer-start" @mousedown="${this._getEventResizerHandler(slot, "start")}"></div>${content}
  457. <div class="jc-timeslot-resizer-end" @mousedown="${this._getEventResizerHandler(slot, "end")}"></div>`;
  458. }
  459. return html `<div class="jc-timeslot ${slot.moving ? "moving" : ""} ${slot.selected ? "selected" : ""}"
  460. start="${slot.start.getHours()}"
  461. end="${slot.end.getHours()}"
  462. style="${styleMap(style)}"
  463. @mousedown="${this._getEventGrabHandler(slot, resizer, editableRessource, this._getEventClickHandler(slot))}"
  464. >${content}</div>`;
  465. }
  466. _getCollapseRessourceHandler(item) {
  467. return (_e) => {
  468. item.collapseChildren = !item.collapseChildren;
  469. this.updateTimeslotPosition(item);
  470. this.rows = [...this.rows];
  471. };
  472. }
  473. _onRessourceDragStart(item) {
  474. return (event) => {
  475. var _a;
  476. (_a = event.dataTransfer) === null || _a === void 0 ? void 0 : _a.setData("text", item.id);
  477. };
  478. }
  479. _onRessourceDragEnter(event) {
  480. if (event.target instanceof HTMLElement) {
  481. const tgt = event.target;
  482. tgt.classList.add("target");
  483. }
  484. }
  485. _onRessourceDragLeave(event) {
  486. if (event.target instanceof HTMLElement) {
  487. event.target.classList.remove("target");
  488. }
  489. }
  490. _onRessourceDrop(event) {
  491. var _a, _b;
  492. event.preventDefault();
  493. if (event.target instanceof HTMLElement) {
  494. event.target.classList.remove("target");
  495. const srcId = (_a = event.dataTransfer) === null || _a === void 0 ? void 0 : _a.getData("text");
  496. const destinationId = (_b = event.target.parentElement) === null || _b === void 0 ? void 0 : _b.getAttribute("ressourceId");
  497. if (srcId && destinationId && (destinationId !== srcId)) {
  498. const src = this.getRessourceFromId(srcId);
  499. const destination = this.getRessourceFromId(destinationId);
  500. if (destination.childOf(src)) {
  501. return;
  502. }
  503. const movedContent = this.removeRessourceById(src.id);
  504. if (event.target.classList.contains("jc-ressource")) {
  505. movedContent.ressources[0].parent = destination;
  506. }
  507. else {
  508. movedContent.ressources[0].parent = destination.parent;
  509. let idx = this.rows.findIndex(v => v.id === destinationId);
  510. if (event.target.classList.contains("jc-ressource-below")) {
  511. idx += 1;
  512. while ((idx < this.rows.length)
  513. && this.rows[idx].childOf(destination)) {
  514. idx += 1;
  515. }
  516. }
  517. const arr = this.rows;
  518. this.rows = [...arr.splice(0, idx), src, ...arr];
  519. }
  520. this.addRessources(movedContent.ressources);
  521. this.addTimeSlots(movedContent.items);
  522. }
  523. }
  524. }
  525. renderRessource(item) {
  526. const depth = item.depth;
  527. const style = `--depth:${depth};` + (item.height ? `height:${item.height}px;` : "");
  528. const hasChild = item.children.length > 0;
  529. const collapseHandler = this._getCollapseRessourceHandler(item);
  530. return html `<tr>
  531. <td class="${item.selected ? "jc-ressource-selected" : ""}" style="${style}" ressourceId="${item.id}" @click="${this._getEventClickHandler(item)}">
  532. <div class="jc-ressource-above"></div>
  533. <div class="jc-ressource" draggable="true" @dragstart="${this._onRessourceDragStart(item)}">
  534. ${Array(depth).fill(0).map(_i => html `<i class="jc-spacer"></i>`)}${hasChild ? html `<i class="jc-spacer ${item.collapseChildren ? "extend" : "collapse"}" @click="${collapseHandler}"></i>` : html `<i class="jc-spacer"></i>`}
  535. <span>${item.title}</span>
  536. </div>
  537. <div class="jc-ressource-below"></div>
  538. </td>
  539. </tr>`;
  540. }
  541. renderGridRow(columns, rowId = -1, height = 30) {
  542. return html `<tr row-id="${rowId}">${columns.map((d, i) => html `<td style="height:${height}px;" class="jc-slot ${(i % this.legendSpan) === 0 ? "jc-major-slot" : ""}" start="${d.toISOString()}">&nbsp;</td>`)}</tr>`;
  543. }
  544. render() {
  545. const nCol = Math.floor(this._end.diff(this._start, 'm') / this.slotDuration) + 1;
  546. const columns = [];
  547. for (let i = 0; i < nCol; i++) {
  548. columns.push(this._start.add(this.slotDuration * i, 'm'));
  549. }
  550. const displayedRows = this.rows.map((r, i) => { return { i: i, r: r }; }).filter(o => o.r.show);
  551. return html `
  552. <div class="jc-timeline-header">
  553. <div class="jc-timeline-rows-title" style=${styleMap({ minWidth: this.ressourceWidth + "px", width: this.ressourceWidth + "px" })}>${this.rowsTitle}</div>
  554. <horizontal-resizer @resize-x="${this._handleResizeX}"></horizontal-resizer>
  555. <div class="jc-timeline-grid-title-container">
  556. <table @mousedown="${this._grabHeader}" style="width:${nCol * this.slotWidth}px;">
  557. <colgroup>${columns.map(_o => html `<col style="min-width:${this.slotWidth}px">`)}</colgroup>
  558. <tbody>
  559. ${this.legend.map(arr => html `<tr class="jc-timeline-grid-title">${arr.map(o => html `<th colspan="${o.colspan}">${o.title}</th>`)}</tr>`)}
  560. </tbody>
  561. </table>
  562. </div>
  563. </div>
  564. <div class="jc-timeline-content">
  565. <table class="jc-timeline-rows"
  566. style="${styleMap({ "--width": this.ressourceWidth + "px" })}"
  567. @dragover="${(e) => e.preventDefault()}"
  568. @dragenter="${this._onRessourceDragEnter}"
  569. @dragleave="${this._onRessourceDragLeave}"
  570. @drop="${this._onRessourceDrop}">
  571. ${this.rows.length > 0 ? displayedRows.map(o => this.renderRessource(o.r)) : html `<tr class="empty"><td>No ressource</td></tr>`}
  572. </table>
  573. <horizontal-resizer @resize-x="${this._handleResizeX}"></horizontal-resizer>
  574. <div class="jc-timeline-grid-container">
  575. <table style="width:${nCol * this.slotWidth}px;">
  576. <colgroup>${columns.map(_o => html `<col style="min-width:${this.slotWidth}px">`)}</colgroup>
  577. <tbody>
  578. ${this.rows.length > 0 ? displayedRows.map(o => this.renderGridRow(columns, o.i, o.r.height)) : this.renderGridRow(columns)}
  579. </tbody>
  580. </table>
  581. <div class="jc-timeslots" style="width:${nCol * this.slotWidth}px;">
  582. ${this.items.map(slot => this.renderTimeslot(slot))}
  583. </div>
  584. </div>
  585. </div>
  586. `;
  587. }
  588. };
  589. Timeline.styles = css `
  590. body{
  591. font-family:Roboto;
  592. }
  593. div {
  594. box-sizing: border-box;
  595. }
  596. .jc-timeline-content,
  597. .jc-timeline-header{
  598. width:100%;
  599. position:relative;
  600. display: flex;
  601. flex-direction:row;
  602. height:max-content;
  603. align-items: stretch;
  604. }
  605. .jc-timeline-rows-title,
  606. .jc-timeline-rows > tr > td{
  607. padding: 8px;
  608. min-width:40px;
  609. }
  610. .jc-timeline-rows > tr > td {
  611. max-width:calc( var(--width) - 8px );
  612. padding: 0px;
  613. vertical-align:top;
  614. }
  615. .jc-timeline-rows > tr.empty > td{
  616. padding: 6px 0px 4px 8px;
  617. }
  618. i.jc-spacer {
  619. display:inline-block;
  620. width : 1rem;
  621. height: 1rem;
  622. position:relative;
  623. box-sizing: border-box;
  624. }
  625. i.jc-spacer:after{
  626. content: " ";
  627. position:absolute;
  628. background-repeat: no-repeat;
  629. background-size: 1.05rem;
  630. width: 1.05rem;
  631. height: 1.05rem;
  632. }
  633. .jc-spacer.extend,
  634. .jc-spacer.collapse {
  635. cursor:pointer;
  636. }
  637. i.jc-spacer.extend:after{
  638. background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0iYmxhY2siIHdpZHRoPSIxOHB4IiBoZWlnaHQ9IjE4cHgiPjxwYXRoIGQ9Ik0wIDBoMjR2MjRIMFYweiIgZmlsbD0ibm9uZSIvPjxwYXRoIGQ9Ik0xOSAzSDVjLTEuMTEgMC0yIC45LTIgMnYxNGMwIDEuMS44OSAyIDIgMmgxNGMxLjEgMCAyLS45IDItMlY1YzAtMS4xLS45LTItMi0yem0wIDE2SDVWNWgxNHYxNHptLTgtMmgydi00aDR2LTJoLTRWN2gtMnY0SDd2Mmg0eiIvPjwvc3ZnPg==")
  639. }
  640. i.jc-spacer.collapse:after{
  641. background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgd2lkdGg9IjI0Ij48cGF0aCBkPSJNMCAwaDI0djI0SDB6IiBmaWxsPSJub25lIi8+PHBhdGggZD0iTTE5IDNINWMtMS4xIDAtMiAuOS0yIDJ2MTRjMCAxLjEuOSAyIDIgMmgxNGMxLjEgMCAyLS45IDItMlY1YzAtMS4xLS45LTItMi0yem0wIDE2SDVWNWgxNHYxNHpNNyAxMWgxMHYySDd6Ii8+PC9zdmc+")
  642. }
  643. .jc-timeline-rows > tr{
  644. box-sizing: border-box;
  645. white-space: nowrap;
  646. border: 1px solid grey;
  647. border-style: solid none;
  648. }
  649. .jc-timeline-rows,
  650. .jc-timeline-rows-title{
  651. width:var(--width, 200px);
  652. overflow: hidden;
  653. border-collapse:collapse;
  654. }
  655. .jc-timeline-grid-title-container,
  656. .jc-timeline-grid-container{
  657. position:relative;
  658. width: 600px;
  659. display: block;
  660. overflow: hidden;
  661. }
  662. .jc-timeline-grid-container{
  663. overflow-x: auto;
  664. }
  665. .jc-timeline-grid-title-container > table,
  666. .jc-timeline-grid-container > table {
  667. width:100%;
  668. table-layout: fixed;
  669. border-collapse: collapse;
  670. box-sizing: border-box;
  671. }
  672. .jc-timeline-grid-title-container {
  673. white-space: nowrap;
  674. cursor: grab;
  675. user-select: none; /* supported by Chrome and Opera */
  676. -webkit-user-select: none; /* Safari */
  677. -khtml-user-select: none; /* Konqueror HTML */
  678. -moz-user-select: none; /* Firefox */
  679. -ms-user-select: none; /* Internet Explorer/Edge */
  680. }
  681. .jc-timeline-grid-title:first-child > th{
  682. border-top:0;
  683. }
  684. .jc-timeline-grid-title:first-child > th:before,
  685. .jc-timeline-grid-title:first-child > th:last-child:after {
  686. content:" ";
  687. display: block;
  688. position:absolute;
  689. left:-1px;
  690. top:0px;
  691. height:calc( 100% - 8px);
  692. border-left: 2px solid white;
  693. z-index:2;
  694. }
  695. .jc-timeline-grid-title:first-child > th:last-child:after
  696. {
  697. left:auto;
  698. right:-1px;
  699. }
  700. .jc-timeline-grid-title:first-child:last-child >th{
  701. padding:8px 0;
  702. }
  703. .jc-timeline-grid-title:last-child > th{
  704. border-bottom:none;
  705. }
  706. .jc-timeline-grid-title > th,
  707. .jc-slot {
  708. height:100%;
  709. border: solid 1px lightgray;
  710. border-left-style: dotted;
  711. border-right:0;
  712. text-align: center;
  713. position:relative;
  714. box-sizing: border-box;
  715. }
  716. .jc-timeline-grid-title > th:last-child,
  717. .jc-slot:last-child{
  718. border-right:solid 1px lightgray;
  719. }
  720. .jc-timeline-grid-title > th,
  721. .jc-major-slot{
  722. border-left-style: solid;
  723. }
  724. .jc-timeslots{
  725. position:absolute;
  726. top:0px;
  727. left:0px;
  728. bottom:0px;
  729. overflow: hidden;
  730. }
  731. .jc-timeslot{
  732. position:absolute;
  733. white-space: nowrap;
  734. overflow-x:hidden;
  735. background-color:var(--default-background);
  736. color:#fff;
  737. border-radius:3px;
  738. padding:4px;
  739. margin:2px 0px;
  740. z-index:1;
  741. cursor:auto;
  742. }
  743. .jc-timeslot.empty{
  744. height:5px;
  745. padding:2px 2px;
  746. margin:0px;
  747. cursor:pointer;
  748. }
  749. .jc-timeslot.moving{
  750. opacity:0.7;
  751. cursor:grabbing;
  752. }
  753. .jc-timeslot.selected:before{
  754. border:solid 2px black;
  755. position:absolute;
  756. top:0;
  757. bottom:0;
  758. left:0;
  759. right:0;
  760. content:" ";
  761. }
  762. .jc-timeslot-resizer-start,
  763. .jc-timeslot-resizer-end{
  764. position:absolute;
  765. top:0;
  766. bottom:0;
  767. width:4px;
  768. min-width:4px;
  769. display:block;
  770. cursor: ew-resize;
  771. }
  772. .jc-timeslot-resizer-start,
  773. .jc-timeslot-resizer-end{
  774. display:block;
  775. }
  776. .jc-timeslot-resizer-start{
  777. left:0px;
  778. }
  779. .jc-timeslot-resizer-end{
  780. right:0px;
  781. }
  782. .jc-timeline-rows > tr >td{
  783. height:100%;
  784. }
  785. .jc-ressource{
  786. padding-top: 2px;
  787. height: calc( 100% - 8px);
  788. }
  789. .jc-ressource > span {
  790. pointer-events: none;
  791. }
  792. .jc-ressource.target{
  793. background-color: lightgrey;
  794. }
  795. .jc-ressource-selected{
  796. border:1px solid var(--default-background, SteelBlue);
  797. border-right:0;
  798. border-left:0;
  799. background-color:#4682b46b;
  800. }
  801. .jc-ressource-above{
  802. height:4px;
  803. }
  804. .jc-ressource-above.target{
  805. margin-left: calc( var(--depth) * 16px );
  806. background-color: var(--default-background, SteelBlue);
  807. border-radius: 0 0 0 4px;
  808. }
  809. .jc-ressource-below{
  810. height:4px;
  811. }
  812. .jc-ressource-below.target{
  813. margin-left: calc( var(--depth) * 16px);
  814. background-color: var(--default-background, SteelBlue);
  815. border-radius: 4px 0 0 0;
  816. }
  817. `;
  818. __decorate([
  819. property({ type: Array })
  820. ], Timeline.prototype, "rows", void 0);
  821. __decorate([
  822. property({ type: Array })
  823. ], Timeline.prototype, "items", void 0);
  824. __decorate([
  825. property({ type: Number })
  826. ], Timeline.prototype, "ressourceWidth", void 0);
  827. __decorate([
  828. property({ type: Object })
  829. ], Timeline.prototype, "_start", void 0);
  830. __decorate([
  831. property({ type: String })
  832. ], Timeline.prototype, "start", null);
  833. __decorate([
  834. property({ type: String })
  835. ], Timeline.prototype, "end", null);
  836. __decorate([
  837. property({ type: Number })
  838. ], Timeline.prototype, "slotDuration", null);
  839. __decorate([
  840. property({ type: Number })
  841. ], Timeline.prototype, "legendSpan", null);
  842. __decorate([
  843. property({ type: Number })
  844. ], Timeline.prototype, "rowHeight", void 0);
  845. __decorate([
  846. property({ type: Number })
  847. ], Timeline.prototype, "slotWidth", void 0);
  848. __decorate([
  849. property({ type: String })
  850. ], Timeline.prototype, "rowsTitle", void 0);
  851. __decorate([
  852. property({ type: Array })
  853. ], Timeline.prototype, "legend", void 0);
  854. __decorate([
  855. property({ type: String })
  856. ], Timeline.prototype, "defaultBackground", null);
  857. Timeline = __decorate([
  858. customElement('jc-timeline')
  859. ], Timeline);
  860. export default Timeline;
  861. //# sourceMappingURL=Timeline.js.map