Timeline.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. "use strict";
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  4. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  5. 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;
  6. return c > 3 && r && Object.defineProperty(target, key, r), r;
  7. };
  8. var __importDefault = (this && this.__importDefault) || function (mod) {
  9. return (mod && mod.__esModule) ? mod : { "default": mod };
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. exports.HorizontalResizer = void 0;
  13. const dayjs_1 = __importDefault(require("dayjs"));
  14. const lit_element_1 = require("lit-element");
  15. const style_map_1 = require("lit-html/directives/style-map");
  16. const simplebar_1 = __importDefault(require("simplebar"));
  17. const SimplbeBar_styles_1 = require("./styles/SimplbeBar.styles");
  18. const Event_1 = require("./Event");
  19. const Ressource_1 = require("./Ressource");
  20. var horizontal_resizer_1 = require("./components/horizontal-resizer");
  21. Object.defineProperty(exports, "HorizontalResizer", { enumerable: true, get: function () { return horizontal_resizer_1.HorizontalResizer; } });
  22. const syncroScroll_1 = __importDefault(require("./utils/syncroScroll"));
  23. const Timeline_style_1 = require("./styles/Timeline.style");
  24. let Timeline = class Timeline extends lit_element_1.LitElement {
  25. constructor(options = {}) {
  26. super();
  27. this._slotDuration = 30;
  28. this._legendSpan = 2;
  29. this.rowHeight = 32;
  30. this.slotWidth = 20;
  31. this.legendUnitFormat = { "y": "YYYY", "M": "MMMM", "d": 'D', "h": "H[h]", "m": "m'", 's': "s[s]" };
  32. this._clearSelectionHandler = (_e) => {
  33. this._clearSelectedItems();
  34. window.removeEventListener("click", this._clearSelectionHandler);
  35. };
  36. this.rows = [];
  37. this.items = [];
  38. this._start = dayjs_1.default().startOf("day");
  39. this._end = this._start.endOf("day");
  40. this.rowsTitle = "Ressources";
  41. this.ressourceWidth = 200;
  42. this.selectedList = [];
  43. this.legend = [];
  44. this.defaultBackground = "";
  45. if (options.ressources) {
  46. this.addRessources(options.ressources);
  47. }
  48. if (options.items) {
  49. this.addEvents(options.items);
  50. }
  51. this.updateLegend();
  52. this.render();
  53. }
  54. get start() {
  55. return this._start.toISOString();
  56. }
  57. set start(value) {
  58. const d = dayjs_1.default(value);
  59. if (d.isValid()) {
  60. this._start = d < this._end ? d : this._end;
  61. this.updateLegend();
  62. }
  63. else {
  64. console.log("Invalid Date :", value);
  65. }
  66. }
  67. get end() {
  68. return this._end.toISOString();
  69. }
  70. set end(value) {
  71. const d = dayjs_1.default(value);
  72. if (d.isValid()) {
  73. this._end = this._start < d ? d : this._start;
  74. this.updateLegend();
  75. }
  76. }
  77. get slotDuration() {
  78. return this._slotDuration;
  79. }
  80. set slotDuration(value) {
  81. this._slotDuration = value;
  82. this.updateLegend();
  83. }
  84. get legendSpan() {
  85. return this._legendSpan;
  86. }
  87. set legendSpan(value) {
  88. this._legendSpan = value;
  89. this.updateLegend();
  90. }
  91. set defaultBackground(value) {
  92. this.style.setProperty("--default-background", value);
  93. }
  94. get defaultBackground() {
  95. return this.style.getPropertyValue("--default-background");
  96. }
  97. setLegendUnitFormatAll(legend) {
  98. this.legendUnitFormat = Object.assign(Object.assign({}, this.legendUnitFormat), legend);
  99. this.updateLegend();
  100. }
  101. setLegendUnitFormat(unit, format) {
  102. this.legendUnitFormat[unit] = format;
  103. this.updateLegend();
  104. }
  105. addRessources(list) {
  106. return list.map(r => this.addRessource(r));
  107. }
  108. addRessource(ressource) {
  109. var _a;
  110. const existingRessource = this.getRessourceFromId(ressource.id);
  111. if (existingRessource) {
  112. return existingRessource;
  113. }
  114. const r = Ressource_1.Ressource.toRessource(ressource);
  115. if (r.parent !== undefined) {
  116. r.parent = (_a = this.getRessourceFromId(r.parent.id)) !== null && _a !== void 0 ? _a : this.addRessource(r.parent);
  117. const idx = this.rows.indexOf(r.parent);
  118. if (idx > -1) {
  119. this.rows[idx].children.push(r);
  120. this.rows.splice(idx + 1, 0, r);
  121. }
  122. else {
  123. throw new Error("Not able to create ressource parent.\n" + r.id);
  124. }
  125. }
  126. else {
  127. this.rows = [...this.rows, r];
  128. }
  129. this.addRessources(r.children);
  130. this._clearSelectedItems();
  131. this._updateEventPosition(r);
  132. return r;
  133. }
  134. removeRessourceById(id) {
  135. return this._removeRessourceById(id);
  136. }
  137. _removeRessourceById(id, depth = 0) {
  138. const output = { ressources: [], items: [], index: 0 };
  139. for (let i = 0; i < this.rows.length; i) {
  140. const ressource = this.rows[i];
  141. if (ressource.id === id) {
  142. output.index = i;
  143. output.ressources.push(ressource);
  144. if (ressource.parent && depth === 0) {
  145. ressource.parent.children = ressource.parent.children.filter(o => o.id !== id);
  146. }
  147. this.rows.splice(i, 1);
  148. }
  149. else if (ressource.parentId === id) {
  150. const partialOutput = this._removeRessourceById(ressource.id, depth + 1);
  151. output.ressources.push(...partialOutput.ressources);
  152. output.items.push(...partialOutput.items);
  153. }
  154. else {
  155. i++;
  156. }
  157. }
  158. output.items.push(...this.items.filter(i => i.ressourceId === id));
  159. this.items = this.items.filter(i => i.ressourceId !== id);
  160. return output;
  161. }
  162. getRessources() {
  163. return this.rows;
  164. }
  165. getRessourceFromId(id) {
  166. const tmp = this.rows.filter(r => r.id === id);
  167. return tmp.length > 0 ? tmp[0] : null;
  168. }
  169. updateRessource(id, key, value) {
  170. const ressource = this.getRessourceFromId(id);
  171. if (ressource) {
  172. if (key == "parent") {
  173. const content = this.removeRessourceById(id);
  174. ressource[key] = value;
  175. this.addRessource(ressource);
  176. this.addRessources(content.ressources);
  177. this.addEvents(content.items);
  178. }
  179. else {
  180. ressource[key] = value;
  181. this.rows = this.rows.map(r => r.id == ressource.id ? ressource : r);
  182. }
  183. this._updateEventPosition(ressource);
  184. return ressource;
  185. }
  186. return null;
  187. }
  188. setRowsTitle(title) {
  189. this.rowsTitle = title;
  190. }
  191. getEventById(id) {
  192. return this.items.find(o => o.id == id);
  193. }
  194. addEvents(list) {
  195. return list.map((e) => this.addEvent(e));
  196. }
  197. addEvent(event) {
  198. const existingEvent = this.getEventById(event.id);
  199. if (existingEvent) {
  200. return existingEvent;
  201. }
  202. const ressource = this.rows.find(r => r.id === event.ressourceId);
  203. if (ressource === undefined) {
  204. return undefined;
  205. }
  206. const timeslot = Event_1.Event.toTimeSlot(event);
  207. this.items = [...this.items, timeslot];
  208. timeslot.isDisplayed = timeslot.end > this._start.toDate() || timeslot.start < this._end.toDate();
  209. this._updateEventPosition(ressource);
  210. this._clearSelectedItems();
  211. return timeslot;
  212. }
  213. removeEventById(id) {
  214. const output = this.items.filter(o => o.id === id);
  215. this.items = this.items.filter(o => o.id !== id);
  216. return output;
  217. }
  218. updateEventById(id) {
  219. const output = this.removeEventById(id);
  220. if (output.length > 0) {
  221. this.addEvent(output[0]);
  222. return output[0];
  223. }
  224. else {
  225. return null;
  226. }
  227. }
  228. _updateEventPosition(ressource) {
  229. const timeslots = this.items.filter(i => i.ressourceId === ressource.id);
  230. if (timeslots.length === 0) {
  231. ressource.height = this.rowHeight + (ressource.collapseChildren ? 5 : 0);
  232. return;
  233. }
  234. const start = this._start.toDate().getTime();
  235. const end = this._end.toDate().getTime();
  236. const points = [start, end];
  237. const populateInterval = (d) => {
  238. const t = d.getTime();
  239. if (start < t && t < end && !points.includes(t)) {
  240. points.push(t);
  241. }
  242. };
  243. timeslots.forEach(element => {
  244. populateInterval(element.start);
  245. populateInterval(element.end);
  246. });
  247. points.sort();
  248. const intervals = [];
  249. for (let i = 0; i < points.length - 1; i++) {
  250. const startTime = points[i];
  251. const endTime = points[i + 1];
  252. intervals.push({
  253. start: points[i],
  254. end: points[i + 1],
  255. slots: timeslots.filter(slot => (slot.start.getTime() <= startTime && endTime <= slot.end.getTime()))
  256. });
  257. }
  258. const lineCount = intervals.reduce((acc, interval) => Math.max(acc, interval.slots.length), 0);
  259. ressource.height = this.rowHeight * Math.max(lineCount, 1) + (ressource.collapseChildren ? 5 : 0);
  260. const sortTimeslots = (a, b) => {
  261. const t = a.start.getTime() - b.start.getTime();
  262. if (t === 0) {
  263. const tend = b.end.getTime() - a.end.getTime();
  264. return tend === 0 ? ('' + a.id).localeCompare(b.id) : tend;
  265. }
  266. return t;
  267. };
  268. timeslots.forEach(slot => slot.offset = -1);
  269. timeslots.sort(sortTimeslots);
  270. timeslots[0].offset = 0;
  271. const potentialOffset = [];
  272. for (let i = 0; i < lineCount; i++) {
  273. potentialOffset.push(i);
  274. }
  275. intervals.forEach(intervals => {
  276. intervals.slots.sort(sortTimeslots);
  277. const usedOffset = intervals.slots.map(o => o.offset).filter(i => i > -1);
  278. const availableOffset = potentialOffset.filter(i => !usedOffset.includes(i));
  279. intervals.slots.forEach(slot => {
  280. if (slot.offset === -1) {
  281. slot.offset = availableOffset.shift() || 0;
  282. }
  283. });
  284. });
  285. }
  286. getEvents() {
  287. return this.items;
  288. }
  289. updateLegend() {
  290. const legend = [];
  291. const legendUnitList = ["y", "M", "d", "h", "m", 's'];
  292. const legendMinUnitSpan = this.slotDuration * this.legendSpan;
  293. for (const legendUnit of legendUnitList) {
  294. let currentDate = dayjs_1.default(this._start);
  295. let nextColumn = currentDate.add(legendMinUnitSpan, "m");
  296. const isLegendPossible = this._end.diff(this._start, legendUnit) > 0 &&
  297. (nextColumn.format(this.legendUnitFormat[legendUnit]) !== currentDate.format(this.legendUnitFormat[legendUnit])
  298. || currentDate.add(1, legendUnit).diff(currentDate, "m") >= legendMinUnitSpan);
  299. if (isLegendPossible) {
  300. const row = [];
  301. let i = 0;
  302. while (currentDate.isBefore(this._end)) {
  303. i += this.legendSpan;
  304. if (nextColumn.diff(currentDate, legendUnit) > 0) {
  305. row.push({ colspan: i, title: '' + currentDate.format(this.legendUnitFormat[legendUnit]) });
  306. i = 0;
  307. currentDate = nextColumn;
  308. }
  309. nextColumn = nextColumn.add(legendMinUnitSpan, "m");
  310. }
  311. legend.push(row);
  312. }
  313. }
  314. this.legend = legend;
  315. }
  316. _handleResizeX(e) {
  317. e.stopPropagation();
  318. this.ressourceWidth += e.detail;
  319. if (this.ressourceWidth < 0) {
  320. this.ressourceWidth = 0;
  321. }
  322. }
  323. _grabHeader(e) {
  324. const root = this.shadowRoot;
  325. if (root !== null) {
  326. const gridContainer = root.querySelector(".jc-timeline-grid-container");
  327. const headerContainer = root.querySelector(".jc-timeline-grid-title-container");
  328. let lastPosX = e.clientX;
  329. const scroll = function (e) {
  330. const scrollLeft = (lastPosX - e.clientX);
  331. headerContainer.scrollLeft += scrollLeft;
  332. gridContainer.scrollLeft += scrollLeft;
  333. lastPosX = e.clientX;
  334. };
  335. const mouseUpListener = function (_e) {
  336. window.removeEventListener("mousemove", scroll);
  337. window.removeEventListener("mouseup", mouseUpListener);
  338. };
  339. window.addEventListener("mousemove", scroll);
  340. window.addEventListener("mouseup", mouseUpListener);
  341. }
  342. }
  343. _getEventResizerHandler(slot, direction) {
  344. return (evt) => {
  345. evt.stopPropagation();
  346. evt.preventDefault();
  347. const startPos = evt.clientX;
  348. const localSlot = slot;
  349. const localDir = direction;
  350. const initialDate = slot[direction];
  351. const resizeListener = (e) => {
  352. const newDate = dayjs_1.default(initialDate).add(Math.round((e.clientX - startPos) / this.slotWidth) * this.slotDuration, "m").toDate();
  353. if (direction === "start" ? (newDate < localSlot.end) : (localSlot.start < newDate)) {
  354. if (localSlot[localDir] !== newDate) {
  355. localSlot[localDir] = newDate;
  356. this.updateEventById(slot.id);
  357. }
  358. }
  359. };
  360. const mouseUpListener = (_e) => {
  361. window.removeEventListener("mousemove", resizeListener);
  362. window.removeEventListener("mouseup", mouseUpListener);
  363. localSlot.moving = false;
  364. this.updateEventById(localSlot.id);
  365. if (initialDate !== localSlot[localDir]) {
  366. const cEvt = new CustomEvent("event-change", {
  367. detail: { items: [localSlot] }
  368. });
  369. this.dispatchEvent(cEvt);
  370. }
  371. };
  372. localSlot.moving = true;
  373. window.addEventListener("mousemove", resizeListener);
  374. window.addEventListener("mouseup", mouseUpListener);
  375. };
  376. }
  377. _getEventGrabHandler(slot, editable, ressourceEditable, callback) {
  378. return (evt) => {
  379. evt.stopPropagation();
  380. evt.preventDefault();
  381. const startPos = evt.clientX;
  382. let hasChanged = false;
  383. const localSlot = slot;
  384. let localSlots = this.selectedList.filter(s => s instanceof Event_1.Event).map(s => s);
  385. if (!localSlots.includes(localSlot)) {
  386. localSlots = [localSlot];
  387. }
  388. const startDates = localSlots.map(slot => slot.start);
  389. const endDates = localSlots.map(slot => slot.end);
  390. const updatePosition = editable ? (e) => {
  391. const changeTime = Math.round((e.clientX - startPos) / this.slotWidth) * this.slotDuration;
  392. return localSlots.map((slot, index) => {
  393. const prevStart = slot.start;
  394. slot.start = dayjs_1.default(startDates[index]).add(changeTime, "m").toDate();
  395. slot.end = dayjs_1.default(endDates[index]).add(changeTime, "m").toDate();
  396. return prevStart.getTime() !== slot.start.getTime();
  397. }).reduce((prev, curr) => prev || curr);
  398. } : (_e) => { return false; };
  399. const updateRessource = ressourceEditable ? (e) => {
  400. var _a, _b, _c;
  401. 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');
  402. if (rowId) {
  403. const ressourceId = this.rows[Number(rowId)].id;
  404. if (ressourceId !== localSlot.ressourceId) {
  405. const oldRessource = this.getRessourceFromId(localSlot.ressourceId);
  406. localSlot.ressourceId = ressourceId;
  407. this._updateEventPosition(oldRessource);
  408. return true;
  409. }
  410. }
  411. return false;
  412. } : (_e) => { return false; };
  413. const moveListener = (e) => {
  414. const a = updatePosition(e);
  415. if (updateRessource(e) || a) {
  416. hasChanged = true;
  417. this.updateEventById(localSlot.id);
  418. }
  419. };
  420. const mouseUpListener = (e) => {
  421. window.removeEventListener("mousemove", moveListener);
  422. window.removeEventListener("mouseup", mouseUpListener);
  423. localSlot.moving = false;
  424. this.updateEventById(slot.id);
  425. if (hasChanged) {
  426. const cEvt = new CustomEvent("event-change", {
  427. detail: { items: localSlots }
  428. });
  429. this.dispatchEvent(cEvt);
  430. }
  431. callback(e, hasChanged);
  432. };
  433. localSlot.moving = true;
  434. window.addEventListener("mousemove", moveListener);
  435. window.addEventListener("mouseup", mouseUpListener);
  436. };
  437. }
  438. _clearSelectedItems() {
  439. this.items.forEach((selectable) => {
  440. selectable.selected = false;
  441. this.updateEventById(selectable.id);
  442. });
  443. this.rows.forEach((selectable) => selectable.selected = false);
  444. this.selectedList = [];
  445. }
  446. _getEventClickHandler(clickedItem) {
  447. const item = clickedItem;
  448. return (e, wasModified = false) => {
  449. e.stopPropagation();
  450. const idx = this.selectedList.indexOf(item);
  451. if (idx > -1) {
  452. if (!wasModified) {
  453. if (e.ctrlKey) {
  454. this.selectedList.splice(idx, 1);
  455. item.selected = false;
  456. this.updateEventById(item.id);
  457. }
  458. else {
  459. this._clearSelectedItems();
  460. }
  461. }
  462. }
  463. else {
  464. if (this.selectedList.length > 0 && !e.ctrlKey) {
  465. this._clearSelectedItems();
  466. }
  467. item.selected = true;
  468. this.selectedList.push(item);
  469. this.updateEventById(item.id);
  470. }
  471. const myEvent = new CustomEvent('item-selected', {
  472. detail: { items: this.selectedList },
  473. bubbles: true,
  474. composed: true
  475. });
  476. this.dispatchEvent(myEvent);
  477. };
  478. }
  479. firstUpdated() {
  480. const root = this.shadowRoot;
  481. if (root !== null) {
  482. const gridContainer = root.querySelector(".jc-timeline-grid-container");
  483. const simplebar = new simplebar_1.default(gridContainer).getScrollElement();
  484. syncroScroll_1.default([simplebar, root.querySelector(".jc-timeline-grid-title-container")], "h");
  485. syncroScroll_1.default([simplebar, root.querySelector(".jc-timeline-rows")], "v");
  486. }
  487. if (this.defaultBackground === "") {
  488. this.style.setProperty("--default-background", "SteelBlue");
  489. }
  490. }
  491. renderTimeslot(evt) {
  492. if (!evt.isDisplayed) {
  493. return lit_element_1.html ``;
  494. }
  495. let rowTop = 0;
  496. let ressource;
  497. let i;
  498. for (i = 0; i < this.rows.length && this.rows[i].id !== evt.ressourceId; i++) {
  499. ressource = this.rows[i];
  500. if (ressource.show) {
  501. rowTop += ressource.height ? ressource.height : this.rowHeight;
  502. }
  503. }
  504. ressource = this.rows[i];
  505. const minute2pixel = this.slotWidth / this.slotDuration;
  506. const left = dayjs_1.default(evt.start).diff(this._start, "m") * minute2pixel;
  507. const right = -dayjs_1.default(evt.end).diff(this._end, "m") * minute2pixel;
  508. const style = {
  509. height: this.rowHeight - 4 + "px",
  510. top: rowTop + evt.offset * this.rowHeight + "px",
  511. left: left + "px",
  512. right: right + "px",
  513. backgroundColor: ""
  514. };
  515. const bgColor = evt.bgColor ? evt.bgColor : ressource.eventBgColor;
  516. if (bgColor) {
  517. style.backgroundColor = bgColor;
  518. }
  519. if (!ressource.show) {
  520. style.height = "";
  521. style.top = rowTop - 6 + "px";
  522. return lit_element_1.html `<div class="jc-timeslot empty" style="${style_map_1.styleMap(style)}"></div>`;
  523. }
  524. let content = lit_element_1.html `${evt.content}`;
  525. const resizer = evt.editable === null ? ressource.eventEditable : evt.editable;
  526. const editableRessource = evt.ressourceEditable === null ? ressource.eventRessourceEditable : evt.ressourceEditable;
  527. if (resizer) {
  528. content = lit_element_1.html `<div class="jc-timeslot-resizer-start" @mousedown="${this._getEventResizerHandler(evt, "start")}"></div>${content}
  529. <div class="jc-timeslot-resizer-end" @mousedown="${this._getEventResizerHandler(evt, "end")}"></div>`;
  530. }
  531. return lit_element_1.html `<div class="jc-timeslot ${evt.moving ? "moving" : ""} ${evt.selected ? "selected" : ""}"
  532. start="${evt.start.getHours()}"
  533. end="${evt.end.getHours()}"
  534. style="${style_map_1.styleMap(style)}"
  535. @mousedown="${this._getEventGrabHandler(evt, resizer, editableRessource, this._getEventClickHandler(evt))}"
  536. >${content}</div>`;
  537. }
  538. _getCollapseRessourceHandler(item) {
  539. return (_e) => {
  540. item.collapseChildren = !item.collapseChildren;
  541. this._updateEventPosition(item);
  542. this.rows = [...this.rows];
  543. };
  544. }
  545. _onRessourceDragStart(item) {
  546. return (event) => {
  547. var _a;
  548. (_a = event.dataTransfer) === null || _a === void 0 ? void 0 : _a.setData("text", item.id);
  549. };
  550. }
  551. _onRessourceDragEnter(event) {
  552. if (event.target instanceof HTMLElement) {
  553. const tgt = event.target;
  554. tgt.classList.add("target");
  555. }
  556. }
  557. _onRessourceDragLeave(event) {
  558. if (event.target instanceof HTMLElement) {
  559. event.target.classList.remove("target");
  560. }
  561. }
  562. _onRessourceDrop(event) {
  563. var _a, _b;
  564. event.preventDefault();
  565. if (event.target instanceof HTMLElement) {
  566. event.target.classList.remove("target");
  567. const srcId = (_a = event.dataTransfer) === null || _a === void 0 ? void 0 : _a.getData("text");
  568. const destinationId = (_b = event.target.parentElement) === null || _b === void 0 ? void 0 : _b.getAttribute("ressourceId");
  569. if (srcId && destinationId && (destinationId !== srcId)) {
  570. const src = this.getRessourceFromId(srcId);
  571. const destination = this.getRessourceFromId(destinationId);
  572. if (destination.descendantOf(src)) {
  573. return;
  574. }
  575. const movedContent = this.removeRessourceById(src.id);
  576. if (event.target.classList.contains("jc-ressource")) {
  577. movedContent.ressources[0].parent = destination;
  578. }
  579. else {
  580. movedContent.ressources[0].parent = destination.parent;
  581. let idx = this.rows.findIndex(v => v.id === destinationId);
  582. if (event.target.classList.contains("jc-ressource-below")) {
  583. idx += 1;
  584. while ((idx < this.rows.length)
  585. && this.rows[idx].descendantOf(destination)) {
  586. idx += 1;
  587. }
  588. }
  589. const arr = this.rows;
  590. this.rows = [...arr.splice(0, idx), src, ...arr];
  591. }
  592. this.addRessources(movedContent.ressources);
  593. this.addEvents(movedContent.items);
  594. this.dispatchEvent(new CustomEvent("reorder-ressource", {
  595. detail: { ressources: this.rows }
  596. }));
  597. }
  598. }
  599. }
  600. renderRessource(item) {
  601. const depth = item.depth;
  602. const style = `--depth:${depth};` + (item.height ? `height:${item.height}px;` : "");
  603. const hasChild = item.children.length > 0;
  604. const collapseHandler = this._getCollapseRessourceHandler(item);
  605. return lit_element_1.html `<tr>
  606. <td class="${item.selected ? "jc-ressource-selected" : ""}" style="${style}" ressourceId="${item.id}" @click="${this._getEventClickHandler(item)}">
  607. <div class="jc-ressource-above"></div>
  608. <div class="jc-ressource" draggable="true" @dragstart="${this._onRessourceDragStart(item)}">
  609. ${Array(depth).fill(0).map(_i => lit_element_1.html `<i class="jc-spacer"></i>`)}${hasChild ? lit_element_1.html `<i class="jc-spacer ${item.collapseChildren ? "extend" : "collapse"}" @click="${collapseHandler}"></i>` : lit_element_1.html `<i class="jc-spacer"></i>`}
  610. <span>${item.title}</span>
  611. </div>
  612. <div class="jc-ressource-below"></div>
  613. </td>
  614. </tr>`;
  615. }
  616. renderGridRow(columns, rowId = -1, height = 30) {
  617. return lit_element_1.html `<tr row-id="${rowId}">${columns.map((d, i) => lit_element_1.html `<td style="height:${height}px;" class="jc-slot ${(i % this.legendSpan) === 0 ? "jc-major-slot" : ""}" start="${d.toISOString()}">&nbsp;</td>`)}</tr>`;
  618. }
  619. render() {
  620. const nCol = Math.floor(this._end.diff(this._start, 'm') / this.slotDuration) + 1;
  621. const columns = [];
  622. for (let i = 0; i < nCol; i++) {
  623. columns.push(this._start.add(this.slotDuration * i, 'm'));
  624. }
  625. const displayedRows = this.rows.map((r, i) => { return { i: i, r: r }; }).filter(o => o.r.show);
  626. return lit_element_1.html `
  627. <div class="jc-timeline-header">
  628. <div class="jc-timeline-rows-title" style=${style_map_1.styleMap({ minWidth: this.ressourceWidth + "px", width: this.ressourceWidth + "px" })}>${this.rowsTitle}</div>
  629. <horizontal-resizer @resize-x="${this._handleResizeX}"></horizontal-resizer>
  630. <div class="jc-timeline-grid-title-container">
  631. <table @mousedown="${this._grabHeader}" style="width:${nCol * this.slotWidth}px;">
  632. <colgroup>${columns.map(_o => lit_element_1.html `<col style="min-width:${this.slotWidth}px">`)}</colgroup>
  633. <tbody>
  634. ${this.legend.map(arr => lit_element_1.html `<tr class="jc-timeline-grid-title">${arr.map(o => lit_element_1.html `<th colspan="${o.colspan}">${o.title}</th>`)}</tr>`)}
  635. </tbody>
  636. </table>
  637. </div>
  638. </div>
  639. <div class="jc-timeline-content">
  640. <table class="jc-timeline-rows"
  641. style="${style_map_1.styleMap({ "--width": this.ressourceWidth + "px" })}"
  642. @dragover="${(e) => e.preventDefault()}"
  643. @dragenter="${this._onRessourceDragEnter}"
  644. @dragleave="${this._onRessourceDragLeave}"
  645. @drop="${this._onRessourceDrop}">
  646. ${this.rows.length > 0 ? displayedRows.map(o => this.renderRessource(o.r)) : lit_element_1.html `<tr class="empty"><td>No ressource</td></tr>`}
  647. </table>
  648. <horizontal-resizer @resize-x="${this._handleResizeX}"></horizontal-resizer>
  649. <div class="jc-timeline-grid-container">
  650. <table style="width:${nCol * this.slotWidth}px;">
  651. <colgroup>${columns.map(_o => lit_element_1.html `<col style="min-width:${this.slotWidth}px">`)}</colgroup>
  652. <tbody>
  653. ${this.rows.length > 0 ? displayedRows.map(o => this.renderGridRow(columns, o.i, o.r.height)) : this.renderGridRow(columns)}
  654. </tbody>
  655. </table>
  656. <div class="jc-timeslots" style="width:${nCol * this.slotWidth}px;">
  657. ${this.items.map(slot => this.renderTimeslot(slot))}
  658. </div>
  659. </div>
  660. </div>
  661. `;
  662. }
  663. };
  664. Timeline.styles = [Timeline_style_1.TimelineStyle, SimplbeBar_styles_1.SimpleBarStyle];
  665. __decorate([
  666. lit_element_1.internalProperty()
  667. ], Timeline.prototype, "rows", void 0);
  668. __decorate([
  669. lit_element_1.internalProperty()
  670. ], Timeline.prototype, "items", void 0);
  671. __decorate([
  672. lit_element_1.property({ type: Number })
  673. ], Timeline.prototype, "ressourceWidth", void 0);
  674. __decorate([
  675. lit_element_1.property({ type: Object })
  676. ], Timeline.prototype, "_start", void 0);
  677. __decorate([
  678. lit_element_1.property({ type: String })
  679. ], Timeline.prototype, "start", null);
  680. __decorate([
  681. lit_element_1.property({ type: String })
  682. ], Timeline.prototype, "end", null);
  683. __decorate([
  684. lit_element_1.property({ type: Number })
  685. ], Timeline.prototype, "slotDuration", null);
  686. __decorate([
  687. lit_element_1.property({ type: Number })
  688. ], Timeline.prototype, "legendSpan", null);
  689. __decorate([
  690. lit_element_1.property({ type: Number })
  691. ], Timeline.prototype, "rowHeight", void 0);
  692. __decorate([
  693. lit_element_1.property({ type: Number })
  694. ], Timeline.prototype, "slotWidth", void 0);
  695. __decorate([
  696. lit_element_1.property({ type: String })
  697. ], Timeline.prototype, "rowsTitle", void 0);
  698. __decorate([
  699. lit_element_1.property({ type: Array })
  700. ], Timeline.prototype, "legend", void 0);
  701. __decorate([
  702. lit_element_1.property({ type: String })
  703. ], Timeline.prototype, "defaultBackground", null);
  704. Timeline = __decorate([
  705. lit_element_1.customElement('jc-timeline')
  706. ], Timeline);
  707. exports.default = Timeline;
  708. //# sourceMappingURL=Timeline.js.map