@teipublisher/pb-components 1.43.5 → 1.44.1

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.
@@ -1,219 +1,219 @@
1
- import { LitElement, html, css } from 'lit-element';
2
- import { Grid } from "gridjs";
3
- import { pbMixin } from './pb-mixin.js';
4
- import { resolveURL } from './utils.js';
5
- import { loadStylesheets, importStyles } from "./theming.js";
6
- import '@polymer/paper-input/paper-input';
7
- import '@polymer/iron-icons';
8
- import '@polymer/iron-form';
9
- import '@polymer/paper-icon-button';
10
- import './pb-table-column.js';
11
-
12
- /**
13
- * A table grid based on [gridjs](https://gridjs.io/), which loads its data from a server endpoint
14
- * specified in `source`. If `source` is a relative URI, it will be resolved relative to the
15
- * TEI Publisher endpoint.
16
- *
17
- * The JSON data returned by the endpoint should be an object with two properties:
18
- *
19
- * * `count`: the overall number of rows available on the server
20
- * * `results`: an array containing each record as an object
21
- *
22
- * The parameters send to the server are as follows:
23
- *
24
- *
25
- * Parameter | Description
26
- * ---------|----------
27
- * limit | number of records to return for each page
28
- * start | start offset from which to return records
29
- * order | the id of the column to sort by
30
- * dir | sort direction: either 'asc' or 'desc'
31
- * search | an optional search string entered by the user
32
- *
33
- * Table columns are configured via nested `<pb-table-column>` elements:
34
- *
35
- * ```html
36
- * <pb-table-column label="Name" property="name" sort width="33%"></pb-table-column>
37
- * <pb-table-column label="Born" property="birth"></pb-table-column>
38
- * <pb-table-column label="Died" property="death"></pb-table-column>
39
- * ```
40
- */
41
- export class PbTableGrid extends pbMixin(LitElement) {
42
- static get properties() {
43
- return {
44
- /**
45
- * URI of the server-side endpoint to retrieve data from.
46
- * Relative URIs are resolved relative to the configured TEI Publisher endpoint.
47
- */
48
- source: {
49
- type: String
50
- },
51
- /**
52
- * Path to the gridjs theme CSS files.
53
- */
54
- cssPath: {
55
- type: String,
56
- attribute: 'css-path'
57
- },
58
- /**
59
- * If specified, columns (without a fixed width) will be resizable.
60
- */
61
- resizable: {
62
- type: Boolean
63
- },
64
- perPage: {
65
- type: Number,
66
- attribute: 'per-page'
67
- },
68
- height: {
69
- type: String
70
- },
71
- /**
72
- * If specified, enable server-side search.
73
- */
74
- search: {
75
- type: Boolean
76
- },
77
- _params: {
78
- type: Object
79
- },
80
- ...super.properties
81
- };
82
- }
83
-
84
- constructor() {
85
- super();
86
- this.cssPath = '../css/gridjs';
87
- this._params = {};
88
- this.resizable = false;
89
- this.search = false;
90
- this.perPage = 10;
91
- this.height = null;
92
- this.fixedHeader = false;
93
- }
94
-
95
- async connectedCallback() {
96
- super.connectedCallback();
97
-
98
- this.subscribeTo('pb-search-resubmit', (ev) => {
99
- this._params = Object.assign({}, ev.detail.params);
100
- this._submit();
101
- });
102
-
103
- window.addEventListener('popstate', (ev) => {
104
- this._params = ev.state;
105
- this._submit();
106
- });
107
-
108
- if (!this.height) {
109
- const property = getComputedStyle(this).getPropertyValue('--pb-table-grid-height');
110
- if (property) {
111
- this.height = property;
112
- } else {
113
- this.height = 'auto';
114
- }
115
- }
116
-
117
- const gridjsTheme = await loadStylesheets([`${resolveURL(this.cssPath)}/mermaid.min.css`]);
118
- const theme = importStyles(this);
119
- const sheets = [...this.shadowRoot.adoptedStyleSheets, gridjsTheme];
120
- if (theme) {
121
- sheets.push(theme);
122
- }
123
- this.shadowRoot.adoptedStyleSheets = sheets;
124
- }
125
-
126
- firstUpdated() {
127
- const table = this.shadowRoot.getElementById('table');
128
-
129
- const pbColumns = this.querySelectorAll('pb-table-column');
130
- const columns = [];
131
- pbColumns.forEach((column) => columns.push(column.data()));
132
- PbTableGrid.waitOnce('pb-page-ready', () => {
133
- this._params = this.getParameters();
134
- const url = this.toAbsoluteURL(this.source);
135
- const config = {
136
- height: this.height,
137
- fixedHeader: true,
138
- columns,
139
- resizable: this.resizable,
140
- server: {
141
- url,
142
- then: data => data.results,
143
- total: data => data.count
144
- },
145
- sort: {
146
- multiColumn: false,
147
- enabled: true,
148
- server: {
149
- url: (prev, cols) => {
150
- if (!cols.length) return prev;
151
- const col = cols[0];
152
- return `${prev}${prev.indexOf('?') > -1 ? '&' : '?'}order=${columns[col.index].id}&dir=${col.direction === 1 ? 'asc' : 'desc'}`;
153
- }
154
- }
155
- },
156
- pagination: {
157
- enabled: true,
158
- limit: this.perPage,
159
- server: {
160
- url: (prev, page, limit) => {
161
- const form = this.shadowRoot.getElementById('form');
162
- if (form) {
163
- Object.assign(this._params, form.serializeForm());
164
- }
165
- this._params.limit = limit;
166
- this._params.start = page * limit + 1;
167
- this.setParameters(this._params);
168
- this.pushHistory('grid', this._params);
169
-
170
- return `${prev}${prev.indexOf('?') > -1 ? '&' : '?'}${new URLSearchParams(this._params).toString()}`;
171
- }
172
- }
173
- }
174
- };
175
-
176
- this.grid = new Grid(config);
177
- this.grid.on('load', () => {
178
- this.emitTo('pb-results-received', {
179
- "params": this._params
180
- });
181
- });
182
-
183
- this.grid.render(table);
184
- });
185
- }
186
-
187
- _submit() {
188
- this.grid.forceRender();
189
- }
190
-
191
- render() {
192
- return html`
193
- ${
194
- this.search ? html`
195
- <iron-form id="form">
196
- <form action="">
197
- <paper-input id="search" name="search" label="Search" @keyup="${(e) => e.keyCode == 13 ? this._submit() : null}">
198
- <paper-icon-button icon="search" @click="${this._submit}" slot="suffix"></paper-icon-button>
199
- </paper-input>
200
- </form>
201
- </iron-form>
202
- ` : null
203
- }
204
- <div id="table"></div>
205
- `;
206
- }
207
-
208
- static get styles() {
209
- return css`
210
- :host {
211
- display: block;
212
- }
213
- button {
214
- border: 0;
215
- }
216
- `;
217
- }
218
- }
1
+ import { LitElement, html, css } from 'lit-element';
2
+ import { Grid } from "gridjs";
3
+ import { pbMixin } from './pb-mixin.js';
4
+ import { resolveURL } from './utils.js';
5
+ import { loadStylesheets, importStyles } from "./theming.js";
6
+ import '@polymer/paper-input/paper-input';
7
+ import '@polymer/iron-icons';
8
+ import '@polymer/iron-form';
9
+ import '@polymer/paper-icon-button';
10
+ import './pb-table-column.js';
11
+
12
+ /**
13
+ * A table grid based on [gridjs](https://gridjs.io/), which loads its data from a server endpoint
14
+ * specified in `source`. If `source` is a relative URI, it will be resolved relative to the
15
+ * TEI Publisher endpoint.
16
+ *
17
+ * The JSON data returned by the endpoint should be an object with two properties:
18
+ *
19
+ * * `count`: the overall number of rows available on the server
20
+ * * `results`: an array containing each record as an object
21
+ *
22
+ * The parameters send to the server are as follows:
23
+ *
24
+ *
25
+ * Parameter | Description
26
+ * ---------|----------
27
+ * limit | number of records to return for each page
28
+ * start | start offset from which to return records
29
+ * order | the id of the column to sort by
30
+ * dir | sort direction: either 'asc' or 'desc'
31
+ * search | an optional search string entered by the user
32
+ *
33
+ * Table columns are configured via nested `<pb-table-column>` elements:
34
+ *
35
+ * ```html
36
+ * <pb-table-column label="Name" property="name" sort width="33%"></pb-table-column>
37
+ * <pb-table-column label="Born" property="birth"></pb-table-column>
38
+ * <pb-table-column label="Died" property="death"></pb-table-column>
39
+ * ```
40
+ */
41
+ export class PbTableGrid extends pbMixin(LitElement) {
42
+ static get properties() {
43
+ return {
44
+ /**
45
+ * URI of the server-side endpoint to retrieve data from.
46
+ * Relative URIs are resolved relative to the configured TEI Publisher endpoint.
47
+ */
48
+ source: {
49
+ type: String
50
+ },
51
+ /**
52
+ * Path to the gridjs theme CSS files.
53
+ */
54
+ cssPath: {
55
+ type: String,
56
+ attribute: 'css-path'
57
+ },
58
+ /**
59
+ * If specified, columns (without a fixed width) will be resizable.
60
+ */
61
+ resizable: {
62
+ type: Boolean
63
+ },
64
+ perPage: {
65
+ type: Number,
66
+ attribute: 'per-page'
67
+ },
68
+ height: {
69
+ type: String
70
+ },
71
+ /**
72
+ * If specified, enable server-side search.
73
+ */
74
+ search: {
75
+ type: Boolean
76
+ },
77
+ _params: {
78
+ type: Object
79
+ },
80
+ ...super.properties
81
+ };
82
+ }
83
+
84
+ constructor() {
85
+ super();
86
+ this.cssPath = '../css/gridjs';
87
+ this._params = {};
88
+ this.resizable = false;
89
+ this.search = false;
90
+ this.perPage = 10;
91
+ this.height = null;
92
+ this.fixedHeader = false;
93
+ }
94
+
95
+ async connectedCallback() {
96
+ super.connectedCallback();
97
+
98
+ this.subscribeTo('pb-search-resubmit', (ev) => {
99
+ this._params = Object.assign({}, ev.detail.params);
100
+ this._submit();
101
+ });
102
+
103
+ window.addEventListener('popstate', (ev) => {
104
+ this._params = ev.state;
105
+ this._submit();
106
+ });
107
+
108
+ if (!this.height) {
109
+ const property = getComputedStyle(this).getPropertyValue('--pb-table-grid-height');
110
+ if (property) {
111
+ this.height = property;
112
+ } else {
113
+ this.height = 'auto';
114
+ }
115
+ }
116
+
117
+ const gridjsTheme = await loadStylesheets([`${resolveURL(this.cssPath)}/mermaid.min.css`]);
118
+ const theme = importStyles(this);
119
+ const sheets = [...this.shadowRoot.adoptedStyleSheets, gridjsTheme];
120
+ if (theme) {
121
+ sheets.push(theme);
122
+ }
123
+ this.shadowRoot.adoptedStyleSheets = sheets;
124
+ }
125
+
126
+ firstUpdated() {
127
+ const table = this.shadowRoot.getElementById('table');
128
+
129
+ const pbColumns = this.querySelectorAll('pb-table-column');
130
+ const columns = [];
131
+ pbColumns.forEach((column) => columns.push(column.data()));
132
+ PbTableGrid.waitOnce('pb-page-ready', () => {
133
+ this._params = this.getParameters();
134
+ const url = this.toAbsoluteURL(this.source);
135
+ const config = {
136
+ height: this.height,
137
+ fixedHeader: true,
138
+ columns,
139
+ resizable: this.resizable,
140
+ server: {
141
+ url,
142
+ then: data => data.results,
143
+ total: data => data.count
144
+ },
145
+ sort: {
146
+ multiColumn: false,
147
+ enabled: true,
148
+ server: {
149
+ url: (prev, cols) => {
150
+ if (!cols.length) return prev;
151
+ const col = cols[0];
152
+ return `${prev}${prev.indexOf('?') > -1 ? '&' : '?'}order=${columns[col.index].id}&dir=${col.direction === 1 ? 'asc' : 'desc'}`;
153
+ }
154
+ }
155
+ },
156
+ pagination: {
157
+ enabled: true,
158
+ limit: this.perPage,
159
+ server: {
160
+ url: (prev, page, limit) => {
161
+ const form = this.shadowRoot.getElementById('form');
162
+ if (form) {
163
+ Object.assign(this._params, form.serializeForm());
164
+ }
165
+ this._params.limit = limit;
166
+ this._params.start = page * limit + 1;
167
+ this.setParameters(this._params);
168
+ this.pushHistory('grid', this._params);
169
+
170
+ return `${prev}${prev.indexOf('?') > -1 ? '&' : '?'}${new URLSearchParams(this._params).toString()}`;
171
+ }
172
+ }
173
+ }
174
+ };
175
+
176
+ this.grid = new Grid(config);
177
+ this.grid.on('load', () => {
178
+ this.emitTo('pb-results-received', {
179
+ "params": this._params
180
+ });
181
+ });
182
+
183
+ this.grid.render(table);
184
+ });
185
+ }
186
+
187
+ _submit() {
188
+ this.grid.forceRender();
189
+ }
190
+
191
+ render() {
192
+ return html`
193
+ ${
194
+ this.search ? html`
195
+ <iron-form id="form">
196
+ <form action="">
197
+ <paper-input id="search" name="search" label="Search" @keyup="${(e) => e.keyCode == 13 ? this._submit() : null}">
198
+ <paper-icon-button icon="search" @click="${this._submit}" slot="suffix"></paper-icon-button>
199
+ </paper-input>
200
+ </form>
201
+ </iron-form>
202
+ ` : null
203
+ }
204
+ <div id="table"></div>
205
+ `;
206
+ }
207
+
208
+ static get styles() {
209
+ return css`
210
+ :host {
211
+ display: block;
212
+ }
213
+ button {
214
+ border: 0;
215
+ }
216
+ `;
217
+ }
218
+ }
219
219
  customElements.define('pb-table-grid', PbTableGrid);
@@ -25,7 +25,37 @@ import { translate } from "./pb-i18n.js";
25
25
  * The value associated with each entry
26
26
  * should either correspond to a count of resources or an object with properties `count` and `info`.
27
27
  * `info` should be an array, containing HTML to be shown in a list within the tooltips.
28
- *
28
+ * Expected JSON:
29
+ * ```javascript
30
+ * {
31
+ * "1852-01-14": {
32
+ * "count": 1,
33
+ * "info": [
34
+ * "<a href='/briefe/B0977' part='tooltip-link'>Alfred Escher an Joseph Wolfgang von Deschwanden, Belvoir (Enge, Zürich), Mittwoch, 14. Januar 1852</a>"
35
+ * ]
36
+ * },
37
+ * "1874-01-25": {
38
+ * "count": 3,
39
+ * "info": [
40
+ * "<a href='/briefe/B8140' part='tooltip-link'>Alfred Escher an Gustav von Mevissen, Zürich, Sonntag, 25. Januar 1874</a>",
41
+ * "<a href='/briefe/B8139' part='tooltip-link'>Alfred Escher an Theodor Weishaupt, Zürich, Sonntag, 25. Januar 1874</a>",
42
+ * "<a href='/briefe/B8143' part='tooltip-link'>Alfred Escher an Ludwig August Parcus, Zürich, Sonntag, 25. Januar 1874</a>"
43
+ * ]
44
+ * }
45
+ * }
46
+ * ```
47
+ * Sample Usage:
48
+ * ```xml
49
+ * <pb-timeline url="api/timeline" scopes="['D', 'M', 'Y', '5Y', '10Y']"
50
+ * resettable=""
51
+ * subscribe="docs" emit="timeline">
52
+ * <span slot="label">Angezeigter Zeitraum: </span>
53
+ * </pb-timeline>
54
+ * ```
55
+ * See https://www.briefedition.alfred-escher.ch/briefe/ for a running sample. The source code of the webpage is here: https://github.com/stazh/briefedition-escher. Relevant files are:
56
+ * - [templates/people.html](https://github.com/stazh/briefedition-escher/blob/master/templates/people.html#L91) - usage of pb-timeline
57
+ * - [modules/custom-api.json](https://github.com/stazh/briefedition-escher/blob/master/modules/custom-api.json#L1080) - `/api/timeline` endpoint delivering required JSON object
58
+ *
29
59
  * @slot label - Inserted before the label showing the currently displayed time range
30
60
  *
31
61
  * @fires pb-timeline-date-changed - Triggered when user clicks on a single entry