Timeline.js 30 KB

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