AdvancedTable.vue 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241
  1. <template>
  2. <div>
  3. <div
  4. class="table-outer-container"
  5. @mousemove="columnResizing($event)"
  6. @touchmove="columnResizing($event)"
  7. >
  8. <div class="table-header">
  9. <div>
  10. <tippy
  11. v-if="filters.length > 0"
  12. :touch="true"
  13. :interactive="true"
  14. placement="bottom-start"
  15. theme="search"
  16. ref="search"
  17. trigger="click"
  18. @show="
  19. () => {
  20. showFiltersDropdown = true;
  21. }
  22. "
  23. @hide="
  24. () => {
  25. showFiltersDropdown = false;
  26. }
  27. "
  28. >
  29. <div class="control has-addons" ref="trigger">
  30. <button class="button is-primary">
  31. <i class="material-icons icon-with-button"
  32. >filter_list</i
  33. >
  34. Filters
  35. </button>
  36. <button class="button">
  37. <i class="material-icons">
  38. {{
  39. showFiltersDropdown
  40. ? "expand_more"
  41. : "expand_less"
  42. }}
  43. </i>
  44. </button>
  45. </div>
  46. <template #content>
  47. <div class="control is-grouped input-with-button">
  48. <p class="control select is-expanded">
  49. <select v-model="addFilterValue">
  50. <option
  51. v-for="type in filters"
  52. :key="type.name"
  53. :value="type"
  54. >
  55. {{ type.displayName }}
  56. </option>
  57. </select>
  58. </p>
  59. <p class="control">
  60. <button
  61. :disabled="!addFilterValue"
  62. class="button material-icons is-success"
  63. @click="addFilterItem()"
  64. >
  65. control_point
  66. </button>
  67. </p>
  68. </div>
  69. <div
  70. v-for="(filter, index) in editingFilters"
  71. :key="`filter-${index}`"
  72. class="
  73. advanced-filter
  74. control
  75. is-grouped is-expanded
  76. "
  77. >
  78. <div class="control select">
  79. <select
  80. v-model="filter.filter"
  81. @change="changeFilterType(index)"
  82. >
  83. <option
  84. v-for="type in filters"
  85. :key="type.name"
  86. :value="type"
  87. >
  88. {{ type.displayName }}
  89. </option>
  90. </select>
  91. </div>
  92. <div class="control select">
  93. <select
  94. v-model="filter.filterType"
  95. :disabled="!filter.filterType"
  96. >
  97. <option
  98. v-for="filterType in filterTypes(
  99. filter.filter
  100. )"
  101. :key="filterType.name"
  102. :value="filterType"
  103. :selected="
  104. filter.filter
  105. .defaultFilterType ===
  106. filterType.name
  107. "
  108. >
  109. {{ filterType.displayName }}
  110. </option>
  111. </select>
  112. </div>
  113. <div
  114. v-if="
  115. filter.filterType.name &&
  116. filter.filterType.name === 'exact' &&
  117. filter.filter.filterValues
  118. "
  119. class="control is-expanded select"
  120. >
  121. <select
  122. v-model="filter.data"
  123. :disabled="!filter.filterType"
  124. >
  125. <option
  126. v-for="filterValue in filter.filter
  127. .filterValues"
  128. :key="filterValue"
  129. :value="filterValue"
  130. >
  131. {{ filterValue }}
  132. </option>
  133. </select>
  134. </div>
  135. <div
  136. v-else-if="
  137. filter.filterType.name &&
  138. filter.filterType.name === 'boolean'
  139. "
  140. class="control is-expanded select"
  141. >
  142. <select
  143. v-model="filter.data"
  144. :disabled="!filter.filterType"
  145. >
  146. <option :value="true">true</option>
  147. <option :value="false">false</option>
  148. </select>
  149. </div>
  150. <div v-else class="control is-expanded">
  151. <input
  152. v-if="
  153. filter.filterType.name &&
  154. filter.filterType.name.startsWith(
  155. 'datetime'
  156. )
  157. "
  158. v-model="filter.data"
  159. class="input"
  160. type="datetime-local"
  161. />
  162. <input
  163. v-else-if="
  164. filter.filterType.name &&
  165. filter.filterType.name.startsWith(
  166. 'number'
  167. )
  168. "
  169. v-model="filter.data"
  170. class="input"
  171. type="number"
  172. :disabled="!filter.filterType"
  173. @keydown.enter="applyFilterAndGetData()"
  174. />
  175. <input
  176. v-else
  177. v-model="filter.data"
  178. class="input"
  179. type="text"
  180. placeholder="Search value"
  181. :disabled="!filter.filterType"
  182. @keydown.enter="applyFilterAndGetData()"
  183. @blur="blurFilterInput(filter)"
  184. @focus="focusFilterInput(filter)"
  185. @keydown="keydownFilterInput(filter)"
  186. />
  187. <div
  188. class="autosuggest-container"
  189. v-if="
  190. filter.filter.autosuggest &&
  191. (autosuggest.inputFocussed[
  192. filter.filter.name
  193. ] ||
  194. autosuggest.containerFocussed[
  195. filter.filter.name
  196. ]) &&
  197. autosuggest.items[
  198. filter.filter.name
  199. ]?.length > 0
  200. "
  201. @mouseover="
  202. focusFilterAutosuggestContainer(
  203. filter
  204. )
  205. "
  206. @mouseleave="
  207. blurFilterAutosuggestContainer(
  208. filter
  209. )
  210. "
  211. >
  212. <span
  213. class="autosuggest-item"
  214. tabindex="0"
  215. @click="
  216. selectAutosuggestItem(
  217. index,
  218. filter,
  219. item
  220. )
  221. "
  222. v-for="item in autosuggest.items[
  223. filter.filter.name
  224. ]"
  225. :key="item"
  226. >{{ item }}
  227. </span>
  228. </div>
  229. </div>
  230. <div class="control">
  231. <button
  232. class="button material-icons is-danger"
  233. @click="removeFilterItem(index)"
  234. >
  235. remove_circle_outline
  236. </button>
  237. </div>
  238. </div>
  239. <div
  240. v-if="editingFilters.length > 0"
  241. class="control is-expanded is-grouped"
  242. >
  243. <label class="control label"
  244. >Filter operator</label
  245. >
  246. <div class="control select is-expanded">
  247. <select v-model="filterOperator">
  248. <option
  249. v-for="operator in filterOperators"
  250. :key="operator.name"
  251. :value="operator.name"
  252. >
  253. {{ operator.displayName }}
  254. </option>
  255. </select>
  256. </div>
  257. </div>
  258. <div
  259. class="advanced-filter-bottom"
  260. v-if="editingFilters.length > 0"
  261. >
  262. <div class="control is-expanded">
  263. <button
  264. class="button is-info"
  265. @click="applyFilterAndGetData()"
  266. >
  267. <i
  268. class="
  269. material-icons
  270. icon-with-button
  271. "
  272. >filter_list</i
  273. >
  274. Apply filters
  275. </button>
  276. </div>
  277. </div>
  278. <div
  279. class="advanced-filter-bottom"
  280. v-else-if="editingFilters.length === 0"
  281. >
  282. <div class="control is-expanded">
  283. <button
  284. class="button is-info"
  285. @click="applyFilterAndGetData()"
  286. >
  287. <i
  288. class="
  289. material-icons
  290. icon-with-button
  291. "
  292. >filter_list</i
  293. >
  294. Apply filters
  295. </button>
  296. </div>
  297. </div>
  298. </template>
  299. </tippy>
  300. <tippy
  301. v-if="appliedFilters.length > 0"
  302. :touch="true"
  303. :interactive="true"
  304. theme="info"
  305. ref="activeFilters"
  306. >
  307. <div class="filters-indicator">
  308. {{ appliedFilters.length }}
  309. <i class="material-icons" @click.prevent="true"
  310. >filter_list</i
  311. >
  312. </div>
  313. <template #content>
  314. <p
  315. v-for="(filter, index) in appliedFilters"
  316. :key="`filter-${index}`"
  317. >
  318. {{ filter.filter.displayName }}
  319. {{
  320. appliedFilters.length === 1 &&
  321. appliedFilterOperator === "nor"
  322. ? "not"
  323. : ""
  324. }}
  325. {{
  326. filter.filterType.displayName.toLowerCase()
  327. }}
  328. "{{ filter.data }}"
  329. {{
  330. appliedFilters.length === index + 1
  331. ? ""
  332. : appliedFilterOperator
  333. }}
  334. </p>
  335. </template>
  336. </tippy>
  337. <i
  338. v-else
  339. class="filters-indicator material-icons"
  340. content="No active filters"
  341. v-tippy="{ theme: 'info' }"
  342. >
  343. filter_list_off
  344. </i>
  345. </div>
  346. <div>
  347. <tippy
  348. v-if="hidableSortedColumns.length > 0"
  349. :touch="true"
  350. :interactive="true"
  351. placement="bottom-end"
  352. theme="dropdown"
  353. ref="editColumns"
  354. trigger="click"
  355. @show="
  356. () => {
  357. showColumnsDropdown = true;
  358. }
  359. "
  360. @hide="
  361. () => {
  362. showColumnsDropdown = false;
  363. }
  364. "
  365. >
  366. <div class="control has-addons" ref="trigger">
  367. <button class="button is-primary">
  368. <i class="material-icons icon-with-button"
  369. >tune</i
  370. >
  371. Columns
  372. </button>
  373. <button class="button">
  374. <i class="material-icons">
  375. {{
  376. showColumnsDropdown
  377. ? "expand_more"
  378. : "expand_less"
  379. }}
  380. </i>
  381. </button>
  382. </div>
  383. <template #content>
  384. <draggable
  385. item-key="name"
  386. v-model="orderedColumns"
  387. v-bind="columnDragOptions"
  388. tag="div"
  389. draggable=".item-draggable"
  390. class="nav-dropdown-items"
  391. @change="columnOrderChanged"
  392. >
  393. <template #item="{ element: column }">
  394. <button
  395. v-if="
  396. column.name !== 'select' &&
  397. column.name !== 'placeholder' &&
  398. column.name !== 'updatedPlaceholder'
  399. "
  400. :class="{
  401. sortable: column.sortable,
  402. 'item-draggable': column.draggable,
  403. 'nav-item': true
  404. }"
  405. @click.prevent="
  406. toggleColumnVisibility(column)
  407. "
  408. >
  409. <p
  410. class="
  411. control
  412. is-expanded
  413. checkbox-control
  414. "
  415. >
  416. <label class="switch">
  417. <input
  418. type="checkbox"
  419. :id="index"
  420. :checked="
  421. shownColumns.indexOf(
  422. column.name
  423. ) !== -1
  424. "
  425. @click="
  426. toggleColumnVisibility(
  427. column
  428. )
  429. "
  430. />
  431. <span
  432. :class="{
  433. slider: true,
  434. round: true,
  435. disabled:
  436. !column.hidable
  437. }"
  438. ></span>
  439. </label>
  440. <label :for="index">
  441. <span></span>
  442. <p>{{ column.displayName }}</p>
  443. </label>
  444. </p>
  445. </button>
  446. </template>
  447. </draggable>
  448. </template>
  449. </tippy>
  450. </div>
  451. </div>
  452. <div class="table-container">
  453. <table
  454. :class="{
  455. table: true,
  456. 'has-checkboxes': hasCheckboxes
  457. }"
  458. >
  459. <thead>
  460. <draggable
  461. item-key="name"
  462. v-model="orderedColumns"
  463. v-bind="columnDragOptions"
  464. tag="tr"
  465. handle=".handle"
  466. draggable=".item-draggable"
  467. @change="columnOrderChanged"
  468. >
  469. <template #item="{ element: column }">
  470. <th
  471. v-if="
  472. !(
  473. column.name === 'select' &&
  474. rows.length === 0
  475. ) &&
  476. shownColumns.indexOf(column.name) !== -1
  477. "
  478. :class="{
  479. sortable: column.sortable,
  480. 'item-draggable': column.draggable
  481. }"
  482. :style="{
  483. minWidth: Number.isNaN(column.minWidth)
  484. ? column.minWidth
  485. : `${column.minWidth}px`,
  486. width: Number.isNaN(column.width)
  487. ? column.width
  488. : `${column.width}px`,
  489. maxWidth: Number.isNaN(column.maxWidth)
  490. ? column.maxWidth
  491. : `${column.maxWidth}px`
  492. }"
  493. >
  494. <div v-if="column.name === 'select'">
  495. <p class="checkbox">
  496. <input
  497. type="checkbox"
  498. :checked="
  499. rows.filter(
  500. row => !row.removed
  501. ).length ===
  502. selectedRows.length
  503. "
  504. @click="toggleAllRows()"
  505. />
  506. </p>
  507. </div>
  508. <div v-else class="handle">
  509. <span>
  510. {{ column.displayName }}
  511. </span>
  512. <span
  513. v-if="column.sortable"
  514. :content="`Sort by ${column.displayName}`"
  515. v-tippy
  516. >
  517. <span
  518. v-if="
  519. !sort[column.sortProperty]
  520. "
  521. class="material-icons"
  522. @click="changeSort(column)"
  523. >
  524. unfold_more
  525. </span>
  526. <span
  527. v-if="
  528. sort[
  529. column.sortProperty
  530. ] === 'ascending'
  531. "
  532. class="material-icons active"
  533. @click="changeSort(column)"
  534. >
  535. expand_more
  536. </span>
  537. <span
  538. v-if="
  539. sort[
  540. column.sortProperty
  541. ] === 'descending'
  542. "
  543. class="material-icons active"
  544. @click="changeSort(column)"
  545. >
  546. expand_less
  547. </span>
  548. </span>
  549. </div>
  550. <div
  551. class="resizer"
  552. v-if="column.resizable"
  553. @mousedown.prevent.stop="
  554. columnResizingStart(column, $event)
  555. "
  556. @touchstart.prevent.stop="
  557. columnResizingStart(column, $event)
  558. "
  559. @mouseup="columnResizingStop()"
  560. @touchend="columnResizingStop()"
  561. @dblclick="columnResetWidth(column)"
  562. ></div>
  563. </th>
  564. </template>
  565. </draggable>
  566. </thead>
  567. <tbody>
  568. <tr
  569. v-for="(item, itemIndex) in rows"
  570. :key="item._id"
  571. :class="{
  572. selected: item.selected,
  573. highlighted: item.highlighted,
  574. updated: item.updated,
  575. removed: item.removed
  576. }"
  577. >
  578. <td
  579. v-for="column in sortedFilteredColumns"
  580. :key="`${item._id}-${column.name}`"
  581. >
  582. <slot
  583. :name="`column-${column.name}`"
  584. :item="item"
  585. v-if="
  586. column.properties.length === 0 ||
  587. column.properties.every(property =>
  588. property
  589. .split('.')
  590. .reduce(
  591. (previous, current) =>
  592. (previous !== null &&
  593. previous[
  594. current
  595. ]) !== null || null,
  596. item
  597. )
  598. )
  599. "
  600. ></slot>
  601. <div
  602. v-if="
  603. column.name === 'updatedPlaceholder' &&
  604. item.updated
  605. "
  606. class="updated-tooltip"
  607. content="Row updated"
  608. v-tippy="{
  609. theme: 'info',
  610. placement: 'right'
  611. }"
  612. ></div>
  613. <p
  614. class="checkbox"
  615. v-if="column.name === 'select'"
  616. >
  617. <input
  618. type="checkbox"
  619. :checked="item.selected"
  620. @click="
  621. toggleSelectedRow(itemIndex, $event)
  622. "
  623. :disabled="item.removed"
  624. />
  625. </p>
  626. <span
  627. v-if="item.removed"
  628. class="removed-overlay"
  629. content="Item removed"
  630. v-tippy="{ theme: 'info' }"
  631. ></span>
  632. <div
  633. class="resizer"
  634. v-if="column.resizable"
  635. @mousedown.prevent.stop="
  636. columnResizingStart(column, $event)
  637. "
  638. @touchstart.prevent.stop="
  639. columnResizingStart(column, $event)
  640. "
  641. @mouseup="columnResizingStop()"
  642. @touchend="columnResizingStop()"
  643. @dblclick="columnResetWidth(column)"
  644. ></div>
  645. </td>
  646. </tr>
  647. </tbody>
  648. </table>
  649. </div>
  650. <div class="table-footer">
  651. <div class="page-controls">
  652. <button
  653. :class="{ disabled: page === 1 }"
  654. class="button is-primary material-icons"
  655. :disabled="page === 1"
  656. @click="changePage(1)"
  657. content="First Page"
  658. v-tippy
  659. >
  660. skip_previous
  661. </button>
  662. <button
  663. :class="{ disabled: page === 1 }"
  664. class="button is-primary material-icons"
  665. :disabled="page === 1"
  666. @click="changePage(page - 1)"
  667. content="Previous Page"
  668. v-tippy
  669. >
  670. fast_rewind
  671. </button>
  672. <p>Page {{ page }} / {{ lastPage }}</p>
  673. <button
  674. :class="{ disabled: page === lastPage }"
  675. class="button is-primary material-icons"
  676. :disabled="page === lastPage"
  677. @click="changePage(page + 1)"
  678. content="Next Page"
  679. v-tippy
  680. >
  681. fast_forward
  682. </button>
  683. <button
  684. :class="{ disabled: page === lastPage }"
  685. class="button is-primary material-icons"
  686. :disabled="page === lastPage"
  687. @click="changePage(lastPage)"
  688. content="Last Page"
  689. v-tippy
  690. >
  691. skip_next
  692. </button>
  693. </div>
  694. <div class="page-size">
  695. <div class="control">
  696. <label class="label">Items per page</label>
  697. <p class="control select">
  698. <select
  699. v-model.number="pageSize"
  700. @change="changePageSize()"
  701. >
  702. <option value="10">10</option>
  703. <option value="25">25</option>
  704. <option value="50">50</option>
  705. <option value="100">100</option>
  706. <option value="250">250</option>
  707. <option value="500">500</option>
  708. <option value="1000">1000</option>
  709. </select>
  710. </p>
  711. </div>
  712. </div>
  713. </div>
  714. </div>
  715. <div
  716. v-if="hasCheckboxes && selectedRows.length > 0"
  717. class="bulk-popup"
  718. :style="{
  719. top: bulkPopup.top + 'px',
  720. left: bulkPopup.left + 'px'
  721. }"
  722. >
  723. <button
  724. class="button is-primary"
  725. :content="
  726. selectedRows.length === 1
  727. ? `${selectedRows.length} row selected`
  728. : `${selectedRows.length} rows selected`
  729. "
  730. v-tippy="{ theme: 'info' }"
  731. >
  732. {{ selectedRows.length }}
  733. </button>
  734. <slot name="bulk-actions" :item="selectedRows" />
  735. <div class="right">
  736. <slot name="bulk-actions-right" :item="selectedRows" />
  737. <span
  738. class="material-icons drag-icon"
  739. @mousedown.left="onDragBox"
  740. @touchstart="onDragBox"
  741. @dblclick="resetBulkActionsPosition()"
  742. >
  743. drag_indicator
  744. </span>
  745. </div>
  746. </div>
  747. </div>
  748. </template>
  749. <script>
  750. import { mapGetters } from "vuex";
  751. import draggable from "vuedraggable";
  752. import Toast from "toasters";
  753. import ws from "@/ws";
  754. export default {
  755. components: {
  756. draggable
  757. },
  758. props: {
  759. /*
  760. Column properties:
  761. name: Unique lowercase name
  762. displayName: Nice name for the column header
  763. properties: The properties this column needs to show data
  764. sortable: Boolean for whether the order of a particular column can be changed
  765. sortProperty: The property the backend will sort on if this column gets sorted, e.g. title
  766. hidable: Boolean for whether a column can be hidden
  767. defaultVisibility: Default visibility for a column, either "shown" or "hidden"
  768. draggable: Boolean for whether a column can be dragged/reordered,
  769. resizable: Boolean for whether a column can be resized
  770. minWidth: Minimum width of column, e.g. 50px
  771. width: Width of column, e.g. 100px
  772. maxWidth: Maximum width of column, e.g. 150px
  773. */
  774. columnDefault: { type: Object, default: () => {} },
  775. columns: { type: Array, default: null },
  776. filters: { type: Array, default: null },
  777. dataAction: { type: String, default: null },
  778. name: { type: String, default: null },
  779. maxWidth: { type: Number, default: 1880 },
  780. query: { type: Boolean, default: true },
  781. events: { type: Object, default: () => {} }
  782. },
  783. data() {
  784. return {
  785. page: 1,
  786. pageSize: 10,
  787. rows: [],
  788. count: 0, // TODO Rename
  789. sort: {},
  790. orderedColumns: [],
  791. shownColumns: [],
  792. columnDragOptions() {
  793. return {
  794. animation: 200,
  795. group: "columns",
  796. disabled: false,
  797. ghostClass: "draggable-list-ghost",
  798. filter: ".ignore-elements",
  799. fallbackTolerance: 50
  800. };
  801. },
  802. editingFilters: [],
  803. appliedFilters: [],
  804. filterOperator: "or",
  805. appliedFilterOperator: "or",
  806. filterOperators: [
  807. {
  808. name: "or",
  809. displayName: "OR"
  810. },
  811. {
  812. name: "and",
  813. displayName: "AND"
  814. },
  815. {
  816. name: "nor",
  817. displayName: "NOR"
  818. }
  819. ],
  820. resizing: {},
  821. allFilterTypes: {
  822. contains: {
  823. name: "contains",
  824. displayName: "Contains"
  825. },
  826. exact: {
  827. name: "exact",
  828. displayName: "Exact"
  829. },
  830. regex: {
  831. name: "regex",
  832. displayName: "Regex"
  833. },
  834. datetimeBefore: {
  835. name: "datetimeBefore",
  836. displayName: "Before"
  837. },
  838. datetimeAfter: {
  839. name: "datetimeAfter",
  840. displayName: "After"
  841. },
  842. numberLesserEqual: {
  843. name: "numberLesserEqual",
  844. displayName: "<="
  845. },
  846. numberLesser: {
  847. name: "numberLesser",
  848. displayName: "<"
  849. },
  850. numberGreater: {
  851. name: "numberGreater",
  852. displayName: ">"
  853. },
  854. numberGreaterEqual: {
  855. name: "numberGreaterEqual",
  856. displayName: ">="
  857. },
  858. numberEquals: {
  859. name: "numberEquals",
  860. displayName: "=="
  861. },
  862. boolean: {
  863. name: "boolean",
  864. displayName: "Boolean"
  865. }
  866. },
  867. bulkPopup: {
  868. top: 0,
  869. left: 0,
  870. pos1: 0,
  871. pos2: 0,
  872. pos3: 0,
  873. pos4: 0
  874. },
  875. addFilterValue: null,
  876. showFiltersDropdown: false,
  877. showColumnsDropdown: false,
  878. lastColumnResizerTapped: null,
  879. lastColumnResizerTappedDate: 0,
  880. lastBulkActionsTappedDate: 0,
  881. autosuggest: {
  882. inputFocussed: {},
  883. containerFocussed: {},
  884. keydownInputTimeout: {},
  885. items: {},
  886. allItems: {
  887. genres: ["edm", "pop", "test"]
  888. }
  889. }
  890. };
  891. },
  892. computed: {
  893. properties() {
  894. return Array.from(
  895. new Set(
  896. this.sortedFilteredColumns.flatMap(
  897. column => column.properties
  898. )
  899. )
  900. );
  901. },
  902. lastPage() {
  903. return Math.ceil(this.count / this.pageSize);
  904. },
  905. sortedFilteredColumns() {
  906. return this.orderedColumns.filter(
  907. column => this.shownColumns.indexOf(column.name) !== -1
  908. );
  909. },
  910. hidableSortedColumns() {
  911. return this.orderedColumns.filter(column => column.hidable);
  912. },
  913. lastSelectedItemIndex() {
  914. return this.rows.findIndex(item => item.highlighted);
  915. },
  916. selectedRows() {
  917. return this.rows.filter(row => row.selected);
  918. },
  919. hasCheckboxes() {
  920. return (
  921. this.$slots["bulk-actions"] != null ||
  922. this.$slots["bulk-actions-right"] != null
  923. );
  924. },
  925. ...mapGetters({
  926. socket: "websockets/getSocket"
  927. })
  928. },
  929. watch: {
  930. selectedRows(newSelectedRows, oldSelectedRows) {
  931. // If selected rows goes from zero to one or more selected, trigger onWindowResize, as otherwise the popup could be out of bounds
  932. if (oldSelectedRows.length === 0 && newSelectedRows.length > 0)
  933. this.onWindowResize();
  934. }
  935. },
  936. mounted() {
  937. const tableSettings = this.getTableSettings();
  938. this.orderedColumns = [
  939. {
  940. name: "updatedPlaceholder",
  941. displayName: "",
  942. properties: [],
  943. sortable: false,
  944. hidable: false,
  945. draggable: false,
  946. resizable: false,
  947. minWidth: 5,
  948. width: 5,
  949. maxWidth: 5
  950. },
  951. {
  952. name: "select",
  953. displayName: "",
  954. properties: [],
  955. sortable: false,
  956. hidable: false,
  957. draggable: false,
  958. resizable: false,
  959. minWidth: 47,
  960. defaultWidth: 47,
  961. maxWidth: 47
  962. },
  963. ...this.columns.map(column => ({
  964. ...this.columnDefault,
  965. ...column
  966. })),
  967. {
  968. name: "placeholder",
  969. displayName: "",
  970. properties: [],
  971. sortable: false,
  972. hidable: false,
  973. draggable: false,
  974. resizable: false,
  975. minWidth: "auto",
  976. width: "auto",
  977. maxWidth: "auto"
  978. }
  979. ].sort((columnA, columnB) => {
  980. // Always places updatedPlaceholder column in the first position
  981. if (columnA.name === "updatedPlaceholder") return -1;
  982. // Always places select column in the first position
  983. // if (columnA.name === "select") return -1;
  984. // Always places placeholder column in the last position
  985. if (columnB.name === "placeholder") return -1;
  986. // If there are no table settings stored, use default ordering
  987. if (!tableSettings || !tableSettings.columnOrder) return 0;
  988. const indexA = tableSettings.columnOrder.indexOf(columnA.name);
  989. const indexB = tableSettings.columnOrder.indexOf(columnB.name);
  990. // If either of the columns is not stored in the table settings, use default ordering
  991. if (indexA === -1 || indexB === -1) return 0;
  992. return indexA - indexB;
  993. });
  994. this.shownColumns = this.orderedColumns
  995. .filter(column => {
  996. // If table settings exist, use shownColumns from settings to determine which columns to show
  997. if (tableSettings && tableSettings.shownColumns)
  998. return (
  999. tableSettings.shownColumns.indexOf(column.name) !== -1
  1000. );
  1001. // Table settings don't exist, only show if the default visibility isn't hidden
  1002. return column.defaultVisibility !== "hidden";
  1003. })
  1004. .map(column => column.name);
  1005. this.recalculateWidths();
  1006. if (tableSettings) {
  1007. // If table settings' page is an integer, use it for the page
  1008. if (Number.isInteger(tableSettings?.page))
  1009. this.page = tableSettings.page;
  1010. // If table settings' pageSize is an integer, use it for the pageSize
  1011. if (Number.isInteger(tableSettings?.pageSize))
  1012. this.pageSize = tableSettings.pageSize;
  1013. // If table settings' columnSort exists, sort all still existing columns based on table settings' columnSort object
  1014. if (tableSettings.columnSort) {
  1015. Object.entries(tableSettings.columnSort).forEach(
  1016. ([columnName, sortDirection]) => {
  1017. if (
  1018. this.columns.find(
  1019. column => column.name === columnName
  1020. )
  1021. )
  1022. this.sort[columnName] = sortDirection;
  1023. }
  1024. );
  1025. }
  1026. // If table settings' columnWidths exists, load the stored widths into the columns
  1027. if (tableSettings.columnWidths) {
  1028. this.orderedColumns = this.orderedColumns.map(orderedColumn => {
  1029. const columnWidth = tableSettings.columnWidths.find(
  1030. column => column.name === orderedColumn.name
  1031. )?.width;
  1032. if (columnWidth)
  1033. return { ...orderedColumn, width: columnWidth };
  1034. return orderedColumn;
  1035. });
  1036. }
  1037. if (
  1038. tableSettings.filter &&
  1039. tableSettings.filter.appliedFilters &&
  1040. tableSettings.filter.appliedFilterOperator
  1041. ) {
  1042. const { appliedFilters, appliedFilterOperator } =
  1043. tableSettings.filter;
  1044. // Set the applied filter operator and filter operator to the value stored in table settings
  1045. this.appliedFilterOperator = this.filterOperator =
  1046. appliedFilterOperator;
  1047. // Set the applied filters and editing filters to the value stored in table settings, for all filters that are allowed
  1048. this.appliedFilters = appliedFilters.filter(appliedFilter =>
  1049. this.filters.find(
  1050. filter => appliedFilter.filter.name === filter.name
  1051. )
  1052. );
  1053. this.editingFilters = appliedFilters.filter(appliedFilter =>
  1054. this.filters.find(
  1055. filter => appliedFilter.filter.name === filter.name
  1056. )
  1057. );
  1058. }
  1059. }
  1060. this.resetBulkActionsPosition();
  1061. this.$nextTick(() => {
  1062. this.onWindowResize();
  1063. window.addEventListener("resize", this.onWindowResize);
  1064. });
  1065. ws.onConnect(this.init);
  1066. if (this.events && this.events.updated)
  1067. this.socket.on(`event:${this.events.updated.event}`, res => {
  1068. const index = this.rows
  1069. .map(row => row._id)
  1070. .indexOf(
  1071. this.events.updated.id
  1072. .split(".")
  1073. .reduce(
  1074. (previous, current) =>
  1075. (previous && previous[current]) || null,
  1076. res.data
  1077. )
  1078. );
  1079. const row = this.events.updated.item
  1080. .split(".")
  1081. .reduce(
  1082. (previous, current) =>
  1083. (previous && previous[current]) || null,
  1084. res.data
  1085. );
  1086. this.updateData(index, row);
  1087. });
  1088. if (this.events && this.events.removed)
  1089. this.socket.on(`event:${this.events.removed.event}`, res => {
  1090. const index = this.rows
  1091. .map(row => row._id)
  1092. .indexOf(
  1093. this.events.removed.id
  1094. .split(".")
  1095. .reduce(
  1096. (previous, current) =>
  1097. (previous && previous[current]) || null,
  1098. res.data
  1099. )
  1100. );
  1101. this.removeData(index);
  1102. });
  1103. },
  1104. unmounted() {
  1105. window.removeEventListener("resize", this.onWindowResize);
  1106. if (this.storeTableSettingsDebounceTimeout)
  1107. clearTimeout(this.storeTableSettingsDebounceTimeout);
  1108. },
  1109. methods: {
  1110. init() {
  1111. this.getData();
  1112. if (this.query) this.setQuery();
  1113. if (this.events) {
  1114. if (this.events.room)
  1115. this.socket.dispatch(
  1116. "apis.joinRoom",
  1117. this.events.room,
  1118. () => {}
  1119. );
  1120. if (this.events.adminRoom)
  1121. this.socket.dispatch(
  1122. "apis.joinAdminRoom",
  1123. this.events.adminRoom,
  1124. () => {}
  1125. );
  1126. }
  1127. this.filters.forEach(filter => {
  1128. if (filter.autosuggest && filter.autosuggestDataAction) {
  1129. this.socket.dispatch(filter.autosuggestDataAction, res => {
  1130. if (res.status === "success") {
  1131. const { items } = res.data;
  1132. this.autosuggest.allItems[filter.name] = items;
  1133. } else {
  1134. new Toast(res.message);
  1135. }
  1136. });
  1137. }
  1138. });
  1139. },
  1140. getData() {
  1141. this.socket.dispatch(
  1142. this.dataAction,
  1143. this.page,
  1144. this.pageSize,
  1145. this.properties,
  1146. this.sort,
  1147. this.appliedFilters.map(filter => ({
  1148. ...filter,
  1149. filterType: filter.filterType.name
  1150. })),
  1151. this.appliedFilterOperator,
  1152. res => {
  1153. console.log(111, res);
  1154. if (res.status === "success") {
  1155. const { data, count } = res.data;
  1156. this.rows = data.map(row => ({
  1157. ...row,
  1158. selected: false
  1159. }));
  1160. this.count = count;
  1161. } else {
  1162. new Toast(res.message);
  1163. }
  1164. }
  1165. );
  1166. },
  1167. changePageSize() {
  1168. this.page = 1;
  1169. this.getData();
  1170. this.storeTableSettings();
  1171. },
  1172. changePage(page) {
  1173. if (page < 1) return;
  1174. if (page > this.lastPage) return;
  1175. if (page === this.page) return;
  1176. this.page = page;
  1177. this.getData();
  1178. if (this.query) this.setQuery();
  1179. },
  1180. changeSort(column) {
  1181. if (column.sortable) {
  1182. const { sortProperty } = column;
  1183. if (this.sort[sortProperty] === undefined)
  1184. this.sort[sortProperty] = "ascending";
  1185. else if (this.sort[sortProperty] === "ascending")
  1186. this.sort[sortProperty] = "descending";
  1187. else if (this.sort[sortProperty] === "descending")
  1188. delete this.sort[sortProperty];
  1189. this.getData();
  1190. this.storeTableSettings();
  1191. }
  1192. },
  1193. toggleColumnVisibility(column) {
  1194. if (!column.hidable) return false;
  1195. if (this.shownColumns.indexOf(column.name) !== -1) {
  1196. if (this.shownColumns.length <= 3)
  1197. return new Toast(
  1198. `Unable to hide column ${column.displayName}, there must be at least 1 visibile column`
  1199. );
  1200. this.shownColumns.splice(
  1201. this.shownColumns.indexOf(column.name),
  1202. 1
  1203. );
  1204. } else {
  1205. this.shownColumns.push(column.name);
  1206. }
  1207. this.recalculateWidths();
  1208. this.getData();
  1209. return this.storeTableSettings();
  1210. },
  1211. toggleSelectedRow(itemIndex, event) {
  1212. const { shiftKey, ctrlKey } = event;
  1213. // Shift was pressed, so attempt to select all items between the clicked item and last clicked item
  1214. if (shiftKey) {
  1215. // If the clicked item is already selected, prevent default, otherwise the checkbox will be unchecked
  1216. if (this.rows[itemIndex].selected) event.preventDefault();
  1217. // If there is a last clicked item
  1218. if (this.lastSelectedItemIndex >= 0) {
  1219. // Clicked item is lower than last item, so select upwards until it reaches the last selected item
  1220. if (itemIndex > this.lastSelectedItemIndex) {
  1221. for (
  1222. let itemIndexUp = itemIndex;
  1223. itemIndexUp > this.lastSelectedItemIndex;
  1224. itemIndexUp -= 1
  1225. ) {
  1226. this.rows[itemIndexUp].selected = true;
  1227. }
  1228. }
  1229. // Clicked item is higher than last item, so select downwards until it reaches the last selected item
  1230. else if (itemIndex < this.lastSelectedItemIndex) {
  1231. for (
  1232. let itemIndexDown = itemIndex;
  1233. itemIndexDown < this.lastSelectedItemIndex;
  1234. itemIndexDown += 1
  1235. ) {
  1236. this.rows[itemIndexDown].selected = true;
  1237. }
  1238. }
  1239. }
  1240. }
  1241. // Ctrl was pressed, so attempt to unselect all items between the clicked item and last clicked item
  1242. else if (ctrlKey) {
  1243. // If the clicked item is already unselected, prevent default, otherwise the checkbox will be checked
  1244. if (!this.rows[itemIndex].selected) event.preventDefault();
  1245. // If there is a last clicked item
  1246. if (this.lastSelectedItemIndex >= 0) {
  1247. // Clicked item is lower than last item, so unselect upwards until it reaches the last selected item
  1248. if (itemIndex > this.lastSelectedItemIndex) {
  1249. for (
  1250. let itemIndexUp = itemIndex;
  1251. itemIndexUp >= this.lastSelectedItemIndex;
  1252. itemIndexUp -= 1
  1253. ) {
  1254. this.rows[itemIndexUp].selected = false;
  1255. }
  1256. }
  1257. // Clicked item is higher than last item, so unselect downwards until it reaches the last selected item
  1258. else if (itemIndex < this.lastSelectedItemIndex) {
  1259. for (
  1260. let itemIndexDown = itemIndex;
  1261. itemIndexDown <= this.lastSelectedItemIndex;
  1262. itemIndexDown += 1
  1263. ) {
  1264. this.rows[itemIndexDown].selected = false;
  1265. }
  1266. }
  1267. }
  1268. }
  1269. // Neither ctrl nor shift were pressed, so toggle clicked item
  1270. else {
  1271. this.rows[itemIndex].selected = !this.rows[itemIndex].selected;
  1272. }
  1273. // Set the last clicked item to no longer be highlighted, if it exists
  1274. if (this.lastSelectedItemIndex >= 0)
  1275. this.rows[this.lastSelectedItemIndex].highlighted = false;
  1276. // Set the clicked item to be highlighted
  1277. this.rows[itemIndex].highlighted = true;
  1278. },
  1279. toggleAllRows() {
  1280. if (
  1281. this.rows.filter(row => !row.removed).length >
  1282. this.selectedRows.length
  1283. ) {
  1284. this.rows = this.rows.map(row => {
  1285. if (row.removed) return row;
  1286. return { ...row, selected: true };
  1287. });
  1288. } else {
  1289. this.rows = this.rows.map(row => {
  1290. if (row.removed) return row;
  1291. return { ...row, selected: false };
  1292. });
  1293. }
  1294. },
  1295. addFilterItem() {
  1296. this.editingFilters.push({
  1297. data: "",
  1298. filter: this.addFilterValue,
  1299. filterType:
  1300. this.allFilterTypes[this.addFilterValue.defaultFilterType]
  1301. });
  1302. },
  1303. removeFilterItem(index) {
  1304. this.editingFilters.splice(index, 1);
  1305. },
  1306. columnResizingStart(column, event) {
  1307. const eventIsTouch = event.type === "touchstart";
  1308. if (eventIsTouch) {
  1309. // Handle double click from touch (if this method is called for the same column twice in a row within one second)
  1310. if (
  1311. this.lastColumnResizerTapped === column &&
  1312. Date.now() - this.lastColumnResizerTappedDate <= 1000
  1313. ) {
  1314. this.columnResetWidth(column);
  1315. this.lastColumnResizerTapped = null;
  1316. this.lastColumnResizerTappedDate = 0;
  1317. return;
  1318. }
  1319. this.lastColumnResizerTapped = column;
  1320. this.lastColumnResizerTappedDate = Date.now();
  1321. }
  1322. this.resizing.resizing = true;
  1323. this.resizing.resizingColumn = column;
  1324. this.resizing.width = event.target.parentElement.offsetWidth;
  1325. this.resizing.lastX = eventIsTouch
  1326. ? event.targetTouches[0].clientX
  1327. : event.x;
  1328. },
  1329. columnResizing(event) {
  1330. if (this.resizing.resizing) {
  1331. const eventIsTouch = event.type === "touchmove";
  1332. if (!eventIsTouch && event.buttons !== 1) {
  1333. this.resizing.resizing = false;
  1334. this.storeTableSettings();
  1335. }
  1336. const x = eventIsTouch
  1337. ? event.changedTouches[0].clientX
  1338. : event.x;
  1339. this.resizing.width -= this.resizing.lastX - x;
  1340. this.resizing.lastX = x;
  1341. if (
  1342. this.resizing.resizingColumn.minWidth &&
  1343. this.resizing.resizingColumn.maxWidth
  1344. ) {
  1345. this.resizing.resizingColumn.width = Math.max(
  1346. Math.min(
  1347. this.resizing.resizingColumn.maxWidth,
  1348. this.resizing.width
  1349. ),
  1350. this.resizing.resizingColumn.minWidth
  1351. );
  1352. } else if (this.resizing.resizingColumn.minWidth) {
  1353. this.resizing.resizingColumn.width = Math.max(
  1354. this.resizing.width,
  1355. this.resizing.resizingColumn.minWidth
  1356. );
  1357. } else if (this.resizing.resizingColumn.maxWidth) {
  1358. this.resizing.resizingColumn.width = Math.min(
  1359. this.resizing.resizingColumn.maxWidth,
  1360. this.resizing.width
  1361. );
  1362. } else {
  1363. this.resizing.resizingColumn.width = this.resizing.width;
  1364. }
  1365. this.resizing.width = this.resizing.resizingColumn.width;
  1366. console.log(`New width: ${this.resizing.width}px`);
  1367. this.storeTableSettings();
  1368. }
  1369. },
  1370. columnResizingStop() {
  1371. this.resizing.resizing = false;
  1372. this.storeTableSettings();
  1373. },
  1374. columnResetWidth(column) {
  1375. const index = this.orderedColumns.indexOf(column);
  1376. if (column.defaultWidth && !Number.isNaN(column.defaultWidth))
  1377. this.orderedColumns[index].width = column.defaultWidth;
  1378. else if (
  1379. column.calculatedWidth &&
  1380. !Number.isNaN(column.calculatedWidth)
  1381. )
  1382. this.orderedColumns[index].width = column.calculatedWidth;
  1383. this.storeTableSettings();
  1384. },
  1385. filterTypes(filter) {
  1386. if (!filter || !filter.filterTypes) return [];
  1387. return filter.filterTypes.map(
  1388. filterType => this.allFilterTypes[filterType]
  1389. );
  1390. },
  1391. changeFilterType(index) {
  1392. this.editingFilters[index].filterType =
  1393. this.allFilterTypes[
  1394. this.editingFilters[index].filter.defaultFilterType
  1395. ];
  1396. },
  1397. onDragBox(e) {
  1398. const e1 = e || window.event;
  1399. const e1IsTouch = e1.type === "touchstart";
  1400. e1.preventDefault();
  1401. if (e1IsTouch) {
  1402. // Handle double click from touch (if this method is twice in a row within one second)
  1403. if (Date.now() - this.lastBulkActionsTappedDate <= 1000) {
  1404. this.resetBulkActionsPosition();
  1405. this.lastBulkActionsTappedDate = 0;
  1406. return;
  1407. }
  1408. this.lastBulkActionsTappedDate = Date.now();
  1409. }
  1410. this.bulkPopup.pos3 = e1IsTouch
  1411. ? e1.changedTouches[0].clientX
  1412. : e1.clientX;
  1413. this.bulkPopup.pos4 = e1IsTouch
  1414. ? e1.changedTouches[0].clientY
  1415. : e1.clientY;
  1416. document.onmousemove = document.ontouchmove = e => {
  1417. const e2 = e || window.event;
  1418. const e2IsTouch = e2.type === "touchmove";
  1419. if (!e2IsTouch) e2.preventDefault();
  1420. // Get the clientX and clientY
  1421. const e2ClientX = e2IsTouch
  1422. ? e2.changedTouches[0].clientX
  1423. : e2.clientX;
  1424. const e2ClientY = e2IsTouch
  1425. ? e2.changedTouches[0].clientY
  1426. : e2.clientY;
  1427. // calculate the new cursor position:
  1428. this.bulkPopup.pos1 = this.bulkPopup.pos3 - e2ClientX;
  1429. this.bulkPopup.pos2 = this.bulkPopup.pos4 - e2ClientY;
  1430. this.bulkPopup.pos3 = e2ClientX;
  1431. this.bulkPopup.pos4 = e2ClientY;
  1432. // set the element's new position:
  1433. this.bulkPopup.top -= this.bulkPopup.pos2;
  1434. this.bulkPopup.left -= this.bulkPopup.pos1;
  1435. if (this.bulkPopup.top < 0) this.bulkPopup.top = 0;
  1436. if (this.bulkPopup.top > document.body.clientHeight - 50)
  1437. this.bulkPopup.top = document.body.clientHeight - 50;
  1438. if (this.bulkPopup.left < 0) this.bulkPopup.left = 0;
  1439. if (this.bulkPopup.left > document.body.clientWidth - 400)
  1440. this.bulkPopup.left = document.body.clientWidth - 400;
  1441. };
  1442. document.onmouseup = document.ontouchend = () => {
  1443. document.onmouseup = null;
  1444. document.ontouchend = null;
  1445. document.onmousemove = null;
  1446. document.ontouchmove = null;
  1447. };
  1448. },
  1449. resetBulkActionsPosition() {
  1450. this.bulkPopup.top = document.body.clientHeight - 56;
  1451. this.bulkPopup.left = document.body.clientWidth / 2 - 200;
  1452. },
  1453. applyFilterAndGetData() {
  1454. this.appliedFilters = JSON.parse(
  1455. JSON.stringify(this.editingFilters)
  1456. );
  1457. this.appliedFilterOperator = this.filterOperator;
  1458. this.page = 1;
  1459. this.getData();
  1460. this.storeTableSettings();
  1461. },
  1462. recalculateWidths() {
  1463. let noWidthCount = 0;
  1464. let calculatedWidth = 0;
  1465. this.orderedColumns.forEach(column => {
  1466. if (
  1467. this.shownColumns.indexOf(column.name) !== -1 &&
  1468. (column.name !== "select" || this.hasCheckboxes)
  1469. )
  1470. if (
  1471. Number.isFinite(column.width) &&
  1472. !Number.isFinite(column.calculatedWidth)
  1473. ) {
  1474. calculatedWidth += column.width;
  1475. } else if (Number.isFinite(column.defaultWidth)) {
  1476. calculatedWidth += column.defaultWidth;
  1477. } else {
  1478. noWidthCount += 1;
  1479. }
  1480. });
  1481. calculatedWidth = Math.floor(
  1482. (Math.min(this.maxWidth, document.body.clientWidth) -
  1483. calculatedWidth) /
  1484. (noWidthCount - 1)
  1485. );
  1486. this.orderedColumns = this.orderedColumns.map(column => {
  1487. const orderedColumn = column;
  1488. if (this.shownColumns.indexOf(orderedColumn.name) !== -1) {
  1489. let newWidth;
  1490. if (Number.isFinite(orderedColumn.defaultWidth)) {
  1491. newWidth = orderedColumn.defaultWidth;
  1492. } else {
  1493. // eslint-disable-next-line no-param-reassign
  1494. newWidth = orderedColumn.calculatedWidth = Math.min(
  1495. Math.max(
  1496. orderedColumn.minWidth || 100, // fallback 100px min width
  1497. calculatedWidth
  1498. ),
  1499. orderedColumn.maxWidth || 1000 // fallback 1000px max width
  1500. );
  1501. }
  1502. if (newWidth && !Number.isFinite(orderedColumn.width))
  1503. orderedColumn.width = newWidth;
  1504. }
  1505. return orderedColumn;
  1506. });
  1507. },
  1508. columnOrderChanged() {
  1509. this.storeTableSettings();
  1510. },
  1511. getTableSettings() {
  1512. const urlTableSettings = {};
  1513. if (this.query) {
  1514. if (this.$route.query.page)
  1515. urlTableSettings.page = Number.parseInt(
  1516. this.$route.query.page
  1517. );
  1518. if (this.$route.query.pageSize)
  1519. urlTableSettings.pageSize = Number.parseInt(
  1520. this.$route.query.pageSize
  1521. );
  1522. if (this.$route.query.shownColumns)
  1523. urlTableSettings.shownColumns = JSON.parse(
  1524. this.$route.query.shownColumns
  1525. );
  1526. if (this.$route.query.columnOrder)
  1527. urlTableSettings.columnOrder = JSON.parse(
  1528. this.$route.query.columnOrder
  1529. );
  1530. if (this.$route.query.columnWidths)
  1531. urlTableSettings.columnWidths = JSON.parse(
  1532. this.$route.query.columnWidths
  1533. );
  1534. if (this.$route.query.columnSort)
  1535. urlTableSettings.columnSort = JSON.parse(
  1536. this.$route.query.columnSort
  1537. );
  1538. if (this.$route.query.filter)
  1539. urlTableSettings.filter = JSON.parse(
  1540. this.$route.query.filter
  1541. );
  1542. }
  1543. const localStorageTableSettings = JSON.parse(
  1544. localStorage.getItem(`advancedTableSettings:${this.name}`)
  1545. );
  1546. return {
  1547. ...localStorageTableSettings,
  1548. ...urlTableSettings
  1549. };
  1550. },
  1551. storeTableSettings() {
  1552. // Clear debounce timeout
  1553. if (this.storeTableSettingsDebounceTimeout)
  1554. clearTimeout(this.storeTableSettingsDebounceTimeout);
  1555. // Resizing calls this function a lot, so rather than saving dozens of times a second, use debouncing
  1556. this.storeTableSettingsDebounceTimeout = setTimeout(() => {
  1557. if (this.query) this.setQuery();
  1558. this.setLocalStorage();
  1559. }, 250);
  1560. },
  1561. setQuery() {
  1562. const queryObject = {
  1563. ...this.$route.query,
  1564. page: this.page,
  1565. pageSize: this.pageSize,
  1566. filter: JSON.stringify({
  1567. appliedFilters: this.appliedFilters,
  1568. appliedFilterOperator: this.appliedFilterOperator
  1569. }),
  1570. columnSort: JSON.stringify(this.sort),
  1571. columnOrder: JSON.stringify(
  1572. this.orderedColumns.map(column => column.name)
  1573. ),
  1574. columnWidths: JSON.stringify(
  1575. this.orderedColumns.map(column => ({
  1576. name: column.name,
  1577. width: column.width
  1578. }))
  1579. ),
  1580. shownColumns: JSON.stringify(this.shownColumns)
  1581. };
  1582. const queryString = `?${Object.keys(queryObject)
  1583. .map(key => `${key}=${queryObject[key]}`)
  1584. .join("&")}`;
  1585. window.history.replaceState(null, null, queryString);
  1586. },
  1587. setLocalStorage() {
  1588. localStorage.setItem(
  1589. `advancedTableSettings:${this.name}`,
  1590. JSON.stringify({
  1591. pageSize: this.pageSize,
  1592. filter: {
  1593. appliedFilters: this.appliedFilters,
  1594. appliedFilterOperator: this.appliedFilterOperator
  1595. },
  1596. columnSort: this.sort,
  1597. columnOrder: this.orderedColumns.map(column => column.name),
  1598. columnWidths: this.orderedColumns.map(column => ({
  1599. name: column.name,
  1600. width: column.width
  1601. })),
  1602. shownColumns: this.shownColumns
  1603. })
  1604. );
  1605. },
  1606. onWindowResize() {
  1607. // Only change the position if the popup is actually visible
  1608. if (this.selectedRows.length === 0) return;
  1609. if (this.bulkPopup.top < 0) this.bulkPopup.top = 0;
  1610. if (this.bulkPopup.top > document.body.clientHeight - 50)
  1611. this.bulkPopup.top = document.body.clientHeight - 50;
  1612. if (this.bulkPopup.left < 0) this.bulkPopup.left = 0;
  1613. if (this.bulkPopup.left > document.body.clientWidth - 400)
  1614. this.bulkPopup.left = document.body.clientWidth - 400;
  1615. },
  1616. updateData(index, data) {
  1617. this.rows[index] = { ...this.rows[index], ...data, updated: true };
  1618. },
  1619. removeData(index) {
  1620. this.rows[index] = {
  1621. ...this.rows[index],
  1622. selected: false,
  1623. removed: true
  1624. };
  1625. },
  1626. blurFilterInput(filter) {
  1627. this.autosuggest.inputFocussed[filter.filter.name] = false;
  1628. },
  1629. focusFilterInput(filter) {
  1630. this.autosuggest.inputFocussed[filter.filter.name] = true;
  1631. },
  1632. keydownFilterInput(filter) {
  1633. const { name } = filter.filter;
  1634. clearTimeout(this.autosuggest.keydownInputTimeout[name]);
  1635. this.autosuggest.keydownInputTimeout[name] = setTimeout(() => {
  1636. if (filter.data.length > 1) {
  1637. this.autosuggest.items[name] = this.autosuggest.allItems[
  1638. name
  1639. ]?.filter(item =>
  1640. item.toLowerCase().startsWith(filter.data.toLowerCase())
  1641. );
  1642. } else this.autosuggest.items[name] = [];
  1643. }, 1000);
  1644. },
  1645. focusFilterAutosuggestContainer(filter) {
  1646. this.autosuggest.containerFocussed[filter.filter.name] = true;
  1647. },
  1648. blurFilterAutosuggestContainer(filter) {
  1649. this.autosuggest.containerFocussed[filter.filter.name] = false;
  1650. },
  1651. selectAutosuggestItem(index, filter, item) {
  1652. this.editingFilters[index].data = item;
  1653. this.autosuggest.items[filter.filter.name] = [];
  1654. }
  1655. }
  1656. };
  1657. </script>
  1658. <style lang="scss" scoped>
  1659. .night-mode {
  1660. .table-outer-container {
  1661. .table-container .table {
  1662. &,
  1663. thead th {
  1664. background-color: var(--dark-grey-3) !important;
  1665. color: var(--light-grey-2);
  1666. }
  1667. tr {
  1668. th,
  1669. td {
  1670. border-color: var(--dark-grey) !important;
  1671. &:first-child {
  1672. background-color: var(--dark-grey-3) !important;
  1673. }
  1674. }
  1675. &:nth-child(even) {
  1676. &,
  1677. td:first-child {
  1678. background-color: var(--dark-grey-2) !important;
  1679. }
  1680. }
  1681. &:hover,
  1682. &:focus,
  1683. &.highlighted {
  1684. th,
  1685. td {
  1686. &,
  1687. &:first-child {
  1688. background-color: var(--dark-grey-4) !important;
  1689. }
  1690. }
  1691. }
  1692. &.updated td:first-child {
  1693. background-color: var(--primary-color) !important;
  1694. }
  1695. }
  1696. &.has-checkboxes tbody tr {
  1697. td:nth-child(2) {
  1698. background-color: var(--dark-grey-3) !important;
  1699. }
  1700. &:nth-child(even) td:nth-child(2) {
  1701. background-color: var(--dark-grey-2) !important;
  1702. }
  1703. &.updated td:first-child {
  1704. background-color: var(--primary-color) !important;
  1705. }
  1706. &:hover,
  1707. &:focus,
  1708. &.highlighted {
  1709. th,
  1710. td {
  1711. &:nth-child(2) {
  1712. background-color: var(--dark-grey-4) !important;
  1713. }
  1714. }
  1715. }
  1716. }
  1717. }
  1718. .table-header,
  1719. .table-footer {
  1720. background-color: var(--dark-grey-3);
  1721. color: var(--light-grey-2);
  1722. }
  1723. .label.control {
  1724. background-color: var(--dark-grey) !important;
  1725. border-color: var(--grey-3) !important;
  1726. color: var(--white) !important;
  1727. }
  1728. }
  1729. .bulk-popup {
  1730. border: 0;
  1731. background-color: var(--dark-grey-2);
  1732. color: var(--white);
  1733. .material-icons {
  1734. color: var(--white);
  1735. }
  1736. }
  1737. .autosuggest-container {
  1738. background-color: var(--dark-grey) !important;
  1739. .autosuggest-item {
  1740. background-color: var(--dark-grey) !important;
  1741. color: var(--white) !important;
  1742. border-color: var(--dark-grey) !important;
  1743. }
  1744. .autosuggest-item:hover,
  1745. .autosuggest-item:focus {
  1746. background-color: var(--dark-grey-2) !important;
  1747. }
  1748. }
  1749. }
  1750. .table-outer-container {
  1751. border-radius: 5px;
  1752. box-shadow: 0 2px 3px rgba(10, 10, 10, 0.1), 0 0 0 1px rgba(10, 10, 10, 0.1);
  1753. margin: 10px 0;
  1754. overflow: hidden;
  1755. .table-container {
  1756. overflow-x: auto;
  1757. table {
  1758. border-collapse: separate;
  1759. table-layout: fixed;
  1760. thead {
  1761. tr {
  1762. th {
  1763. height: 40px;
  1764. line-height: 40px;
  1765. border: 1px solid var(--light-grey-2);
  1766. border-width: 1px 1px 1px 0;
  1767. padding: 0;
  1768. &:last-child {
  1769. border-width: 1px 0 1px;
  1770. }
  1771. &.sortable {
  1772. cursor: pointer;
  1773. }
  1774. & > div {
  1775. display: flex;
  1776. white-space: nowrap;
  1777. padding: 8px 10px;
  1778. & > span {
  1779. margin-left: 5px;
  1780. &:first-child {
  1781. margin-left: 0;
  1782. margin-right: auto;
  1783. }
  1784. & > .material-icons {
  1785. font-size: 22px;
  1786. position: relative;
  1787. top: 6px;
  1788. cursor: pointer;
  1789. &.active {
  1790. color: var(--primary-color);
  1791. }
  1792. &:hover,
  1793. &:focus {
  1794. filter: brightness(90%);
  1795. }
  1796. }
  1797. }
  1798. }
  1799. }
  1800. }
  1801. }
  1802. tbody {
  1803. tr {
  1804. &.highlighted {
  1805. background-color: var(--light-grey);
  1806. }
  1807. &.updated {
  1808. td:first-child {
  1809. background-color: var(--primary-color) !important;
  1810. }
  1811. }
  1812. td {
  1813. border: 1px solid var(--light-grey-2);
  1814. border-width: 0 1px 1px 0;
  1815. &:last-child {
  1816. border-width: 0 0 1px;
  1817. }
  1818. /deep/ .row-options {
  1819. display: flex;
  1820. justify-content: space-between;
  1821. .icon-with-button {
  1822. height: 30px;
  1823. width: 30px;
  1824. }
  1825. }
  1826. }
  1827. &.removed {
  1828. filter: grayscale(100%);
  1829. cursor: not-allowed;
  1830. user-select: none;
  1831. td .removed-overlay {
  1832. position: absolute;
  1833. top: 0;
  1834. left: 0;
  1835. bottom: 0;
  1836. right: 5px;
  1837. z-index: 5;
  1838. }
  1839. }
  1840. }
  1841. }
  1842. }
  1843. table {
  1844. thead tr,
  1845. tbody tr {
  1846. th,
  1847. td {
  1848. position: relative;
  1849. white-space: nowrap;
  1850. text-overflow: ellipsis;
  1851. overflow: hidden;
  1852. &:first-child {
  1853. display: table-cell;
  1854. position: sticky;
  1855. left: 0;
  1856. background-color: var(--white);
  1857. z-index: 2;
  1858. & > .updated-tooltip {
  1859. position: absolute;
  1860. top: 0;
  1861. left: 0;
  1862. bottom: 0;
  1863. right: 0;
  1864. }
  1865. }
  1866. &:nth-child(2) {
  1867. display: none;
  1868. }
  1869. .resizer {
  1870. height: 100%;
  1871. width: 5px;
  1872. background-color: transparent;
  1873. cursor: col-resize;
  1874. position: absolute;
  1875. right: 0;
  1876. top: 0;
  1877. }
  1878. }
  1879. &:nth-child(even) td:first-child {
  1880. background-color: #fafafa;
  1881. }
  1882. &:hover,
  1883. &:focus,
  1884. &.highlighted {
  1885. th,
  1886. td {
  1887. &,
  1888. &:first-child {
  1889. background-color: var(--light-grey);
  1890. }
  1891. }
  1892. }
  1893. }
  1894. &.has-checkboxes {
  1895. thead,
  1896. tbody {
  1897. tr {
  1898. th,
  1899. td {
  1900. &:nth-child(2) {
  1901. display: table-cell;
  1902. position: sticky;
  1903. left: 5px;
  1904. background-color: var(--white);
  1905. z-index: 2;
  1906. }
  1907. }
  1908. &:hover,
  1909. &:focus,
  1910. &.highlighted {
  1911. th,
  1912. td {
  1913. &:nth-child(2) {
  1914. background-color: var(--light-grey);
  1915. }
  1916. }
  1917. }
  1918. &.updated td:first-child {
  1919. background-color: var(--primary-color);
  1920. }
  1921. }
  1922. }
  1923. }
  1924. }
  1925. }
  1926. .table-header,
  1927. .table-footer {
  1928. display: flex;
  1929. flex-direction: row;
  1930. flex-wrap: wrap;
  1931. justify-content: space-between;
  1932. line-height: 36px;
  1933. background-color: var(--white);
  1934. }
  1935. .table-header > div {
  1936. display: flex;
  1937. flex-direction: row;
  1938. > span > .control {
  1939. margin: 5px;
  1940. }
  1941. .filters-indicator {
  1942. line-height: 46px;
  1943. display: flex;
  1944. align-items: center;
  1945. column-gap: 4px;
  1946. }
  1947. }
  1948. .table-footer {
  1949. .page-controls,
  1950. .page-size > .control {
  1951. display: flex;
  1952. flex-direction: row;
  1953. margin-bottom: 0 !important;
  1954. button {
  1955. margin: 5px;
  1956. font-size: 20px;
  1957. }
  1958. p,
  1959. label {
  1960. margin: 5px;
  1961. font-size: 14px;
  1962. font-weight: 600;
  1963. }
  1964. &.select::after {
  1965. top: 18px;
  1966. }
  1967. }
  1968. }
  1969. }
  1970. .control.is-grouped {
  1971. display: flex;
  1972. & > .control {
  1973. &.label {
  1974. height: 36px;
  1975. background-color: var(--white);
  1976. border: 1px solid var(--light-grey-2);
  1977. color: var(--dark-grey-2);
  1978. appearance: none;
  1979. border-radius: 3px;
  1980. font-size: 14px;
  1981. line-height: 34px;
  1982. padding-left: 8px;
  1983. padding-right: 8px;
  1984. }
  1985. &.select.is-expanded > select {
  1986. width: 100%;
  1987. }
  1988. & > input,
  1989. & > select,
  1990. & > .button,
  1991. &.label {
  1992. border-radius: 0;
  1993. }
  1994. &:first-child {
  1995. & > input,
  1996. & > select,
  1997. & > .button,
  1998. &.label {
  1999. border-radius: 5px 0 0 5px;
  2000. }
  2001. }
  2002. &:last-child {
  2003. & > input,
  2004. & > select,
  2005. & > .button,
  2006. &.label {
  2007. border-radius: 0 5px 5px 0;
  2008. }
  2009. }
  2010. & > .button {
  2011. font-size: 22px;
  2012. }
  2013. }
  2014. @media screen and (max-width: 500px) {
  2015. &.advanced-filter {
  2016. flex-wrap: wrap;
  2017. .control.select {
  2018. width: 50%;
  2019. select {
  2020. width: 100%;
  2021. }
  2022. }
  2023. .control {
  2024. margin-bottom: 0 !important;
  2025. &:nth-child(1) select {
  2026. border-radius: 5px 0 0 0;
  2027. }
  2028. &:nth-child(2) select {
  2029. border-radius: 0 5px 0 0;
  2030. }
  2031. &:nth-child(3) input {
  2032. border-radius: 0 0 0 5px;
  2033. }
  2034. &:nth-child(4) button {
  2035. border-radius: 0 0 5px 0;
  2036. }
  2037. }
  2038. }
  2039. }
  2040. }
  2041. .autosuggest-container {
  2042. position: absolute;
  2043. background: var(--white);
  2044. width: calc(100% + 1px);
  2045. top: 35px;
  2046. z-index: 200;
  2047. overflow: auto;
  2048. max-height: 98px;
  2049. clear: both;
  2050. .autosuggest-item {
  2051. padding: 8px;
  2052. display: block;
  2053. border: 1px solid var(--light-grey-2);
  2054. margin-top: -1px;
  2055. line-height: 16px;
  2056. cursor: pointer;
  2057. -webkit-user-select: none;
  2058. -ms-user-select: none;
  2059. -moz-user-select: none;
  2060. user-select: none;
  2061. }
  2062. .autosuggest-item:hover,
  2063. .autosuggest-item:focus {
  2064. background-color: var(--light-grey);
  2065. }
  2066. .autosuggest-item:first-child {
  2067. border-top: none;
  2068. }
  2069. .autosuggest-item:last-child {
  2070. border-radius: 0 0 3px 3px;
  2071. }
  2072. }
  2073. .advanced-filter {
  2074. .control {
  2075. position: relative;
  2076. }
  2077. }
  2078. .advanced-filter-bottom {
  2079. display: flex;
  2080. .button {
  2081. font-size: 16px !important;
  2082. width: 100%;
  2083. }
  2084. .control {
  2085. margin: 0 !important;
  2086. }
  2087. }
  2088. /deep/ .bulk-popup {
  2089. display: flex;
  2090. position: fixed;
  2091. flex-direction: row;
  2092. width: 100%;
  2093. max-width: 400px;
  2094. line-height: 36px;
  2095. z-index: 5;
  2096. border: 1px solid var(--light-grey-3);
  2097. border-radius: 5px;
  2098. box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
  2099. background-color: var(--white);
  2100. color: var(--dark-grey);
  2101. padding: 5px;
  2102. .right {
  2103. display: flex;
  2104. flex-direction: row;
  2105. margin-left: auto;
  2106. }
  2107. .drag-icon {
  2108. position: relative;
  2109. top: 6px;
  2110. color: var(--dark-grey);
  2111. cursor: move;
  2112. }
  2113. .bulk-actions {
  2114. display: flex;
  2115. flex-direction: row;
  2116. width: 100%;
  2117. justify-content: space-evenly;
  2118. .material-icons {
  2119. position: relative;
  2120. top: 6px;
  2121. margin-left: 5px;
  2122. cursor: pointer;
  2123. color: var(--primary-color);
  2124. &:hover,
  2125. &:focus {
  2126. filter: brightness(90%);
  2127. }
  2128. }
  2129. .delete-icon {
  2130. color: var(--dark-red);
  2131. }
  2132. }
  2133. }
  2134. </style>