@uwdata/mosaic-inputs 0.2.0 → 0.3.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uwdata/mosaic-inputs",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Mosaic input components.",
5
5
  "keywords": [
6
6
  "inputs",
@@ -25,9 +25,9 @@
25
25
  "prepublishOnly": "npm run test && npm run lint && npm run build"
26
26
  },
27
27
  "dependencies": {
28
- "@uwdata/mosaic-core": "^0.2.0",
29
- "@uwdata/mosaic-sql": "^0.2.0",
28
+ "@uwdata/mosaic-core": "^0.3.0",
29
+ "@uwdata/mosaic-sql": "^0.3.0",
30
30
  "isoformat": "^0.2.1"
31
31
  },
32
- "gitHead": "e53cd914c807f99aabe78dcbe618dd9543e2f438"
32
+ "gitHead": "a8dd23fed4c7a24c0a2ee5261d1aabe4239ce574"
33
33
  }
package/src/Menu.js CHANGED
@@ -1,12 +1,16 @@
1
1
  import { MosaicClient, isParam, isSelection } from '@uwdata/mosaic-core';
2
2
  import { Query, eq, literal } from '@uwdata/mosaic-sql';
3
+ import { input } from './input.js';
3
4
 
4
5
  const isObject = v => {
5
6
  return v && typeof v === 'object' && !Array.isArray(v);
6
7
  };
7
8
 
9
+ export const menu = options => input(Menu, options);
10
+
8
11
  export class Menu extends MosaicClient {
9
12
  constructor({
13
+ element,
10
14
  filterBy,
11
15
  from,
12
16
  column,
@@ -22,7 +26,7 @@ export class Menu extends MosaicClient {
22
26
  this.selection = as;
23
27
  this.format = format;
24
28
 
25
- this.element = document.createElement('div');
29
+ this.element = element ?? document.createElement('div');
26
30
  this.element.setAttribute('class', 'input');
27
31
  this.element.value = this;
28
32
 
package/src/Search.js CHANGED
@@ -2,17 +2,21 @@ import { MosaicClient, isParam, isSelection } from '@uwdata/mosaic-core';
2
2
  import {
3
3
  Query, regexp_matches, contains, prefix, suffix, literal
4
4
  } from '@uwdata/mosaic-sql';
5
+ import { input } from './input.js';
5
6
 
6
7
  const FUNCTIONS = { contains, prefix, suffix, regexp: regexp_matches };
7
8
  let _id = 0;
8
9
 
10
+ export const search = options => input(Search, options);
11
+
9
12
  export class Search extends MosaicClient {
10
13
  constructor({
14
+ element,
11
15
  filterBy,
12
16
  from,
13
17
  column,
14
18
  label,
15
- type,
19
+ type = 'contains',
16
20
  as
17
21
  } = {}) {
18
22
  super(filterBy);
@@ -22,7 +26,7 @@ export class Search extends MosaicClient {
22
26
  this.column = column;
23
27
  this.selection = as;
24
28
 
25
- this.element = document.createElement('div');
29
+ this.element = element ?? document.createElement('div');
26
30
  this.element.setAttribute('class', 'input');
27
31
  this.element.value = this;
28
32
 
package/src/Slider.js CHANGED
@@ -1,10 +1,14 @@
1
1
  import { MosaicClient, isParam, isSelection } from '@uwdata/mosaic-core';
2
2
  import { Query, eq, literal, max, min } from '@uwdata/mosaic-sql';
3
+ import { input } from './input.js';
3
4
 
4
5
  let _id = 0;
5
6
 
7
+ export const slider = options => input(Slider, options);
8
+
6
9
  export class Slider extends MosaicClient {
7
10
  constructor({
11
+ element,
8
12
  filterBy,
9
13
  as,
10
14
  min,
@@ -25,7 +29,7 @@ export class Slider extends MosaicClient {
25
29
  this.max = max;
26
30
  this.step = step;
27
31
 
28
- this.element = document.createElement('div');
32
+ this.element = element || document.createElement('div');
29
33
  this.element.setAttribute('class', 'input');
30
34
  this.element.value = this;
31
35
 
package/src/Table.js CHANGED
@@ -1,11 +1,15 @@
1
- import { MosaicClient } from '@uwdata/mosaic-core';
1
+ import { MosaicClient, coordinator } from '@uwdata/mosaic-core';
2
2
  import { Query, column, desc } from '@uwdata/mosaic-sql';
3
3
  import { formatDate, formatLocaleAuto, formatLocaleNumber } from './util/format.js';
4
+ import { input } from './input.js';
4
5
 
5
6
  let _id = -1;
6
7
 
8
+ export const table = options => input(Table, options);
9
+
7
10
  export class Table extends MosaicClient {
8
11
  constructor({
12
+ element,
9
13
  filterBy,
10
14
  from,
11
15
  columns = ['*'],
@@ -32,7 +36,7 @@ export class Table extends MosaicClient {
32
36
  this.sortColumn = null;
33
37
  this.sortDesc = false;
34
38
 
35
- this.element = document.createElement('div');
39
+ this.element = element || document.createElement('div');
36
40
  this.element.setAttribute('id', this.id);
37
41
  this.element.value = this;
38
42
  if (typeof width === 'number') this.element.style.width = `${width}px`;
@@ -51,9 +55,7 @@ export class Table extends MosaicClient {
51
55
 
52
56
  if (scrollHeight - scrollTop < 2 * clientHeight) {
53
57
  this.pending = true;
54
- this.offset += this.limit;
55
- const query = this.queryInternal(this.filterBy?.predicate(this));
56
- this.requestQuery(query);
58
+ this.requestData(this.offset + this.limit);
57
59
  }
58
60
  });
59
61
 
@@ -70,6 +72,17 @@ export class Table extends MosaicClient {
70
72
  this.element.appendChild(this.style);
71
73
  }
72
74
 
75
+ requestData(offset = 0) {
76
+ this.offset = offset;
77
+
78
+ // request next data batch
79
+ const query = this.query(this.filterBy?.predicate(this));
80
+ this.requestQuery(query);
81
+
82
+ // prefetch subsequent data batch
83
+ coordinator().prefetch(query.clone().offset(offset + this.limit));
84
+ }
85
+
73
86
  fields() {
74
87
  return this.columns.map(name => column(this.from, name));
75
88
  }
@@ -102,12 +115,7 @@ export class Table extends MosaicClient {
102
115
  return this;
103
116
  }
104
117
 
105
- query(filter) {
106
- this.offset = 0;
107
- return this.queryInternal(filter);
108
- }
109
-
110
- queryInternal(filter = []) {
118
+ query(filter = []) {
111
119
  const { from, limit, offset, schema, sortColumn, sortDesc } = this;
112
120
  return Query.from(from)
113
121
  .select(schema.map(s => s.column))
@@ -174,8 +182,7 @@ export class Table extends MosaicClient {
174
182
  }
175
183
 
176
184
  // issue query for sorted data
177
- const query = this.query(this.filterBy?.predicate(this));
178
- this.requestQuery(query);
185
+ this.requestData();
179
186
  }
180
187
  }
181
188
 
package/src/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { Menu } from './Menu.js';
2
- export { Search } from './Search.js';
3
- export { Slider } from './Slider.js';
4
- export { Table } from './Table.js';
1
+ export { Menu, menu } from './Menu.js';
2
+ export { Search, search } from './Search.js';
3
+ export { Slider, slider } from './Slider.js';
4
+ export { Table, table } from './Table.js';
package/src/input.js ADDED
@@ -0,0 +1,7 @@
1
+ import { coordinator } from '@uwdata/mosaic-core';
2
+
3
+ export function input(InputClass, options) {
4
+ const input = new InputClass(options);
5
+ coordinator().connect(input);
6
+ return input.element;
7
+ }