Timeline.js 30 KB

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