Timeline.js 29 KB

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