Timeline.js 30 KB

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