@operato/data-grist 2.0.0-alpha.115 → 2.0.0-alpha.117

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/data-grist.ts CHANGED
@@ -29,6 +29,7 @@ import {
29
29
  GristRecord,
30
30
  GristSelectFunction,
31
31
  PaginationConfig,
32
+ PersonalGristPreference,
32
33
  SortersConfig
33
34
  } from './types'
34
35
  import { convertListParamToSearchString, convertSearchStringToListParam } from './utils'
@@ -170,7 +171,14 @@ export class DataGrist extends LitElement implements DataConsumer {
170
171
  */
171
172
  @property({ type: Boolean, attribute: 'url-params-sensitive' }) urlParamsSensitive?: boolean
172
173
 
173
- @property({ type: Object }) personalConfig?: {
174
+ @property({ type: Object }) personalConfigProvider?: {
175
+ load(): PersonalGristPreference | Promise<PersonalGristPreference | undefined> | undefined
176
+ save(
177
+ preference?: PersonalGristPreference
178
+ ): PersonalGristPreference | Promise<PersonalGristPreference | undefined> | undefined
179
+ }
180
+
181
+ @state() personalConfig?: {
174
182
  columns?: Partial<ColumnConfig>[]
175
183
  [key: string]: any
176
184
  }
@@ -552,10 +560,19 @@ export class DataGrist extends LitElement implements DataConsumer {
552
560
  await this.requestUpdate()
553
561
  }
554
562
 
555
- if (changes.has('config') || changes.has('personalConfig')) {
563
+ if (changes.has('personalConfigProvider')) {
564
+ if (this.personalConfigProvider) {
565
+ this.personalConfig = await this.personalConfigProvider.load()
566
+ this.applyUpdatedConfiguration()
567
+ }
568
+ } else if (changes.has('config') || changes.has('personalConfig')) {
556
569
  this.applyUpdatedConfiguration()
557
570
  }
558
571
 
572
+ // if (changes.has('config') || changes.has('personalConfig')) {
573
+ // this.applyUpdatedConfiguration()
574
+ // }
575
+
559
576
  if (changes.has('fetchHandler')) {
560
577
  this.dataProvider && (this.dataProvider.fetchHandler = this.fetchHandler)
561
578
  needToSetPullToRefresh = true
@@ -0,0 +1,122 @@
1
+ import '@material/web/icon/icon.js'
2
+
3
+ import { css, html, LitElement } from 'lit'
4
+ import { customElement, property, state } from 'lit/decorators.js'
5
+
6
+ import { OxPopupList } from '@operato/popup'
7
+
8
+ import { GristConfig, PersonalGristPreference } from '../types'
9
+ import { DataGrist } from '../data-grist'
10
+
11
+ @customElement('ox-grist-personalizer')
12
+ export class OxGristPersonalizer extends LitElement {
13
+ static styles = [
14
+ css`
15
+ md-icon {
16
+ --md-icon-size: 14px;
17
+ width: 16px;
18
+ height: 16px;
19
+ background-color: rgba(var(--secondary-color-rgb), 0.6);
20
+ color: var(--primary-color);
21
+ border-radius: 0px 0px 7px 7px;
22
+ cursor: pointer;
23
+
24
+ &:hover {
25
+ color: var(--theme-white-color);
26
+ }
27
+ }
28
+ `
29
+ ]
30
+
31
+ @property({ type: String }) page!: string
32
+ @property({ type: String }) element!: string
33
+
34
+ @state() private preference?: PersonalGristPreference
35
+
36
+ render() {
37
+ return html`
38
+ <md-icon
39
+ @click=${(e: MouseEvent) => {
40
+ const grist = this.closest('ox-grist') as DataGrist
41
+
42
+ const { compiledConfig: config } = grist
43
+ const columns = (config as GristConfig).columns.filter(
44
+ column => column.name && column.type !== 'gutter' && column.name != 'id'
45
+ )
46
+
47
+ this.preference = columns.map(column => {
48
+ return {
49
+ name: column.name,
50
+ hidden: column.hidden,
51
+ width: column.width
52
+ }
53
+ })
54
+
55
+ const template = html`
56
+ <div class="personalizer-header" slot="header">
57
+ select to show<md-icon
58
+ style="margin-left: auto;"
59
+ @click=${async (e: MouseEvent) => {
60
+ if (grist.personalConfigProvider) {
61
+ this.preference = await grist.personalConfigProvider.save(this.preference)
62
+ }
63
+ ;((e.target as HTMLElement).closest('ox-popup-list') as OxPopupList)?.close()
64
+ }}
65
+ >save</md-icon
66
+ >
67
+ </div>
68
+
69
+ ${columns.map(
70
+ column =>
71
+ html`<ox-checkbox label="checkbox" ?checked=${!column.hidden} value=${column.name} option />${column.name}</ox-checkbox>`
72
+ )}
73
+ `
74
+
75
+ const popup = OxPopupList.open({
76
+ template,
77
+ multiple: true,
78
+ attrSelected: 'checked',
79
+ top: e.pageY,
80
+ left: e.pageX,
81
+ styles: css`
82
+ :host {
83
+ width: 200px;
84
+ max-height: 300px;
85
+ overflow: scroll;
86
+ }
87
+
88
+ ::slotted(.personalizer-header) {
89
+ --md-icon-size: 1.2em;
90
+
91
+ display: flex;
92
+ flex-direction: row;
93
+ align-items: center;
94
+ text-transform: capitalize;
95
+ }
96
+ `
97
+ })
98
+
99
+ popup.onselect = (e: Event) => {
100
+ const selected = (e as CustomEvent).detail
101
+
102
+ const pconfig: PersonalGristPreference = grist.personalConfig || {}
103
+ pconfig.columns = columns.map(column => {
104
+ return {
105
+ name: column.name,
106
+ hidden: selected.indexOf(column.name) == -1,
107
+ width: column.width
108
+ }
109
+ })
110
+
111
+ this.preference = pconfig
112
+
113
+ grist.personalConfig = this.preference
114
+
115
+ grist.applyUpdatedConfiguration()
116
+ }
117
+ }}
118
+ >settings</md-icon
119
+ >
120
+ `
121
+ }
122
+ }
package/src/types.ts CHANGED
@@ -761,3 +761,17 @@ export type GristData = {
761
761
  * @returns {boolean} - `true` if the record should be selected, `false` otherwise.
762
762
  */
763
763
  export type GristSelectFunction = (record: GristRecord) => boolean
764
+
765
+ /**
766
+ * Defines a type for personalized grid settings. It includes individual column settings and additional configurations specific to a user's grid view.
767
+ *
768
+ * @typedef {Object} PersonalGristPreference
769
+ * @property {Partial<ColumnConfig>[]} [columns] - Partially defines the configuration for each column in the grid.
770
+ * Each element can include only some properties of the `ColumnConfig`, used for optionally overriding column settings.
771
+ * @property {any} [key] - Allows for storing additional user-defined properties with dynamic keys.
772
+ * This property can include various custom settings beyond the grid configuration, and the keys can be freely defined.
773
+ */
774
+ export type PersonalGristPreference = {
775
+ columns?: Partial<ColumnConfig>[]
776
+ [key: string]: any
777
+ }
@@ -1,25 +1,25 @@
1
+ import '@material/web/icon/icon.js'
2
+ import '@operato/popup/ox-popup-list.js'
3
+
1
4
  import '../src/index.js'
2
5
  import '../src/filters/filters-form.js'
3
6
  import '../src/sorters/sorters-control.js'
4
7
  import '../src/record-view/record-creator.js'
5
- import '@operato/popup/ox-popup-list.js'
6
- import '@material/web/icon/icon.js'
8
+ import '../src/personalizer/ox-grist-personalizer.js'
7
9
 
8
10
  import { html, TemplateResult } from 'lit'
9
11
 
10
- import { OxPopupList } from '@operato/popup'
11
12
  import { CommonHeaderStyles, CommonGristStyles } from '@operato/styles'
12
13
 
13
14
  import {
14
15
  ColumnConfig,
15
16
  FetchHandler,
16
17
  GristClassifier,
17
- GristConfig,
18
18
  GristEventHandlerSet,
19
19
  GristRecord,
20
+ PersonalGristPreference,
20
21
  ValidationCallback
21
22
  } from '../src/types.js'
22
- import { DataGrist } from '../src/index.js'
23
23
 
24
24
  const fetchHandler: FetchHandler = async ({ page, limit }) => {
25
25
  var total = 120993
@@ -382,48 +382,23 @@ const Template: Story<ArgTypes> = ({ config, mode = 'GRID', urlParamsSensitive =
382
382
  ${CommonGristStyles.cssText} ${CommonHeaderStyles.cssText}
383
383
  </style>
384
384
 
385
- <style>
386
- ox-popup-list {
387
- width: 200px;
388
- max-height: 300px;
389
- overflow: scroll;
390
- }
391
-
392
- [slot='setting'] {
393
- --md-icon-size: 14px;
394
- width: 16px;
395
- height: 16px;
396
- background-color: rgba(var(--secondary-color-rgb), 0.6);
397
- border-radius: 0px 0px 7px 7px;
398
- color: var(--theme-white-color);
399
- cursor: pointer;
400
- }
401
-
402
- [slot='setting']:hover {
403
- color: var(--primary-color);
404
- }
405
-
406
- ox-popup-list {
407
- .header {
408
- display: flex;
409
- flex-direction: row;
410
- align-items: center;
411
- text-transform: capitalize;
412
- }
413
-
414
- .header md-icon {
415
- --md-icon-size: 1.2em;
416
- margin-left: auto;
417
- }
418
- }
419
- </style>
420
-
421
385
  <ox-grist
422
386
  .mode=${mode}
423
387
  .config=${config}
424
388
  .fetchHandler=${fetchHandler}
425
- .personalConfig=${{
426
- columns: [{ name: 'description', hidden: true }]
389
+ .personalConfigProvider=${{
390
+ load() {
391
+ return {
392
+ columns: [
393
+ { name: 'name', hidden: false, width: 200 },
394
+ { name: 'description', hidden: true }
395
+ ]
396
+ }
397
+ },
398
+ save(preference: PersonalGristPreference) {
399
+ console.log('saving preference', preference)
400
+ return preference
401
+ }
427
402
  }}
428
403
  ?url-params-sensitive=${urlParamsSensitive}
429
404
  @filters-change=${(e: Event) => console.log('filters', (e.target as any).filters)}
@@ -461,57 +436,7 @@ const Template: Story<ArgTypes> = ({ config, mode = 'GRID', urlParamsSensitive =
461
436
  </ox-record-creator>
462
437
  </div>
463
438
 
464
- <md-icon
465
- title="personal setting"
466
- slot="setting"
467
- @click=${(e: MouseEvent) => {
468
- const grist = (e.target as HTMLElement).closest('ox-grist') as DataGrist
469
-
470
- const { compiledConfig: config } = grist
471
- const columns = (config as GristConfig).columns.filter(
472
- column => column.name && column.type !== 'gutter' && column.name != 'id'
473
- )
474
-
475
- const template = html`
476
- <div class="header">
477
- select to show<md-icon
478
- @click=${(e: MouseEvent) =>
479
- ((e.target as HTMLElement).closest('ox-popup-list') as OxPopupList)?.close()}
480
- >save</md-icon
481
- >
482
- </div>
483
- ${columns.map(
484
- column =>
485
- html`<ox-checkbox label="checkbox" ?checked=${!column.hidden} value=${column.name} option />${column.name}</ox-checkbox>`
486
- )}
487
- `
488
-
489
- const popup = OxPopupList.open({
490
- template,
491
- multiple: true,
492
- attrSelected: 'checked',
493
- top: e.pageY,
494
- left: e.pageX
495
- })
496
-
497
- popup.onselect = (e: Event) => {
498
- console.log('select', e.target, (e as CustomEvent).detail, (e.target as OxPopupList).value)
499
- const selected = (e as CustomEvent).detail
500
-
501
- const pconfig = grist.personalConfig || {}
502
- pconfig.columns = columns.map(column => {
503
- return {
504
- name: column.name,
505
- hidden: selected.indexOf(column.name) == -1
506
- }
507
- })
508
- grist.personalConfig = pconfig
509
-
510
- grist.applyUpdatedConfiguration()
511
- }
512
- }}
513
- >tune</md-icon
514
- >
439
+ <ox-grist-personalizer slot="setting"></ox-grist-personalizer>
515
440
  </ox-grist>`
516
441
 
517
442
  export const Regular = Template.bind({})