lucos_search_component 0.0.3 → 0.0.4

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/README.md CHANGED
@@ -16,6 +16,19 @@ Include the following in your html:
16
16
  <lucos-search></lucos-search>
17
17
  ```
18
18
 
19
+ Include the following in the project's webpack.config.js:
20
+ ```
21
+
22
+ module: {
23
+ rules: [
24
+ {
25
+ test: /\.css$/i,
26
+ use: ["css-loader"],
27
+ },
28
+ ],
29
+ },
30
+ ```
31
+
19
32
  ## Manual Testing
20
33
 
21
34
  Expects a `.env` file in the root directory with the following environment variables:
@@ -1,3 +1 @@
1
1
  import '../index.js';
2
-
3
- document.querySelector('lucos-search').searchAPI('luke')
@@ -7,5 +7,13 @@ export default {
7
7
  filename: 'built.js',
8
8
  path: new URL('.', import.meta.url).pathname,
9
9
  },
10
+ module: {
11
+ rules: [
12
+ {
13
+ test: /\.css$/i,
14
+ use: ["css-loader"],
15
+ },
16
+ ],
17
+ },
10
18
  mode: 'development',
11
19
  };
package/index.js CHANGED
@@ -1,30 +1,116 @@
1
+ import TomSelect from 'tom-select';
2
+ import tomSelectStylesheet from 'tom-select/dist/css/tom-select.default.css';
3
+
1
4
  class LucosSearchComponent extends HTMLElement {
2
5
  static get observedAttributes() {
3
6
  return ['api-key'];
4
7
  }
5
8
  constructor() {
6
9
  super();
7
- const shadow = this.attachShadow({mode: 'closed'});
10
+ const component = this;
11
+ const shadow = component.attachShadow({mode: 'open'});
8
12
 
9
- const results = document.createElement("span");
10
- this.updateResults = resultData => {
11
- results.innerText = JSON.stringify(resultData);
12
- }
13
- shadow.appendChild(results);
14
- }
15
- async searchAPI(query) {
16
- const key = this.getAttribute("api-key");
17
- if (!key) throw new Error("No `api-key` attribute set on `lucos-search` component");
13
+ const mainStyle = document.createElement('style');
14
+ mainStyle.textContent = `
15
+ .lozenge {
16
+ align-items: center;
17
+ vertical-align: baseline;
18
+ border-radius: 3px;
19
+ background-repeat: repeat-x;
20
+ border-style: solid;
21
+ border-width: 1px;
22
+ text-shadow: 0 1px 0 rgba(0, 51, 83, 0.3);
23
+ box-shadow: 0 1px 0 rgba(0, 0, 0, 0.2), inset 0 1px rgba(255, 255, 255, 0.03);
18
24
 
19
- const queryParams = new URLSearchParams({
20
- q: query,
21
- });
22
- const response = await fetch("https://arachne.l42.eu/basic-search?"+queryParams.toString(), {
23
- headers: { Authorization: `key ${key}` },
24
- signal: AbortSignal.timeout(900),
25
+ /** Make the colour settings !important so they override the tom-select default style **/
26
+ background-image: linear-gradient(to bottom, #ffffff63, #24232347) !important;
27
+ background-color: var(--lozenge-background) !important;
28
+ border-color: var(--lozenge-border) !important;
29
+ color: var(--lozenge-text) !important;
30
+ }
31
+ .lozenge .remove {
32
+ border-left-color: var(--lozenge-border) !important;
33
+ }
34
+
35
+ /* Default colour to greys, but override based on type */
36
+ .lozenge {
37
+ --lozenge-background: #555;
38
+ --lozenge-border: #6d6d6d;
39
+ --lozenge-text: #fff;
40
+ }
41
+ /* Items from lucos_eolas have many types. For now, count any type which isn't specified later as part of eolas. */
42
+ .lozenge[data-type] {
43
+ --lozenge-background: #6a00c2;
44
+ --lozenge-border: #44265d;
45
+ }
46
+ .lozenge[data-type="Track"] {
47
+ --lozenge-background: #000060;
48
+ --lozenge-border: #000020;
49
+ }
50
+ .lozenge[data-type="Person"] {
51
+ --lozenge-background: #044E00;
52
+ --lozenge-border: #033100;
53
+ }
54
+ .lozenge.active {
55
+ --lozenge-border: #b00;
56
+ }
57
+ .type {
58
+ margin: 0 3px;
59
+ padding: 2px 6px;
60
+ }
61
+ `;
62
+ shadow.appendChild(mainStyle);
63
+
64
+ const tomStyle = document.createElement('style');
65
+ tomStyle.textContent = tomSelectStylesheet[0][1];
66
+ shadow.appendChild(tomStyle);
67
+
68
+ const select = document.createElement("select");
69
+ select.setAttribute("multiple", "multiple");
70
+ shadow.appendChild(select);
71
+ const tomSelect = new TomSelect(select, {
72
+ valueField: 'id',
73
+ labelField: 'pref_label',
74
+ searchField: [],
75
+ load: async function(query, callback) {
76
+ const key = component.getAttribute("api-key");
77
+ if (!key) throw new Error("No `api-key` attribute set on `lucos-search` component");
78
+ const queryParams = new URLSearchParams({
79
+ q: query,
80
+ });
81
+ const response = await fetch("https://arachne.l42.eu/basic-search?"+queryParams.toString(), {
82
+ headers: { Authorization: `key ${key}` },
83
+ signal: AbortSignal.timeout(900),
84
+ });
85
+ const data = await response.json();
86
+ const results = data.hits.map(result => {
87
+ return {...result, ...result.document}
88
+ });
89
+ this.clearOptions();
90
+ callback(results);
91
+ },
92
+ plugins: {
93
+ remove_button:{
94
+ title:'Remove this item',
95
+ }
96
+ },
97
+ onItemAdd: function() { // Workaround until https://github.com/orchidjs/tom-select/issues/854 is merged/released
98
+ this.setTextboxValue('');
99
+ this.clearOptions();
100
+ this.refreshOptions();
101
+ },
102
+ onFocus: function() {
103
+ this.clearOptions();
104
+ },
105
+ render:{
106
+ option: function(data, escape) {
107
+ return `<div>${escape(data.pref_label)}<span class="type lozenge" data-type="${escape(data.type)}">${escape(data.type)}</span></div>`;
108
+ },
109
+ item: function(data, escape) {
110
+ return `<div class="lozenge" data-type="${escape(data.type)}">${escape(data.pref_label)}</div>`;
111
+ },
112
+ },
25
113
  });
26
- const data = await response.json();
27
- this.updateResults(data);
28
114
  }
29
115
  }
30
116
  customElements.define('lucos-search', LucosSearchComponent);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lucos_search_component",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "Web Component for searching lucOS data",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -23,5 +23,9 @@
23
23
  "homepage": "https://github.com/lucas42/lucos_search_component#readme",
24
24
  "devDependencies": {
25
25
  "webpack-cli": "^6.0.1"
26
+ },
27
+ "dependencies": {
28
+ "tom-select": "^2.4.3",
29
+ "css-loader": "^7.1.2"
26
30
  }
27
31
  }