lucos_search_component 0.1.1 → 0.1.2

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/dist/index.js CHANGED
@@ -5869,6 +5869,9 @@ class LucosLangComponent extends HTMLSpanElement {
5869
5869
  color: inherit;
5870
5870
  text-decoration: none;
5871
5871
  }
5872
+ .optgroup-header {
5873
+ text-transform: capitalize;
5874
+ }
5872
5875
  `;
5873
5876
  shadow.appendChild(mainStyle);
5874
5877
 
@@ -5880,6 +5883,7 @@ class LucosLangComponent extends HTMLSpanElement {
5880
5883
  valueField: 'code',
5881
5884
  labelField: 'label',
5882
5885
  searchField: ['code','label'],
5886
+ optgroupField: 'family',
5883
5887
  closeAfterSelect: true,
5884
5888
  plugins: {
5885
5889
  remove_button:{
@@ -5893,10 +5897,15 @@ class LucosLangComponent extends HTMLSpanElement {
5893
5897
  },
5894
5898
  // On startup, update any existing options with latest data from search
5895
5899
  onInitialize: async function() {
5896
- const results = await component.searchRequest();
5897
- results.forEach(result => {
5898
- this.updateOption(result.code, result); // Updates any existing options which are selected with the correct label
5899
- this.addOption(result); // Makes the option available for new selections
5900
+ const families = await component.getLanguageFamilies();
5901
+ //this.clearOptionGroups();
5902
+ families.forEach(family => {
5903
+ this.addOptionGroup(family.code, family);
5904
+ });
5905
+ const languages = await component.getLanguages();
5906
+ languages.forEach(language => {
5907
+ this.updateOption(language.code, language); // Updates any existing options which are selected with the correct label
5908
+ this.addOption(language); // Makes the option available for new selections
5900
5909
  });
5901
5910
  },
5902
5911
  onItemSelect: function (item) {
@@ -5913,17 +5922,9 @@ class LucosLangComponent extends HTMLSpanElement {
5913
5922
  shadow.append(selector.nextElementSibling);
5914
5923
  }
5915
5924
  }
5916
- async searchRequest() {
5925
+ async searchRequest(searchParams) {
5917
5926
  const key = this.getAttribute("data-api-key");
5918
5927
  if (!key) throw new Error("No `data-api-key` attribute set on `lucos-search` component");
5919
- const searchParams = new URLSearchParams({
5920
- filter_by: 'type:=Language',
5921
- query_by: "pref_label",
5922
- include_fields: "id,pref_label",
5923
- sort_by: "pref_label:asc",
5924
- enable_highlight_v1: false,
5925
- per_page: 200,
5926
- });
5927
5928
  const response = await fetch("https://arachne.l42.eu/search?"+searchParams.toString(), {
5928
5929
  headers: { 'X-TYPESENSE-API-KEY': key },
5929
5930
  signal: AbortSignal.timeout(900),
@@ -5932,14 +5933,44 @@ class LucosLangComponent extends HTMLSpanElement {
5932
5933
  if (!response.ok) {
5933
5934
  throw new Error(`Recieved ${response.status} error from search endpoint: ${data["message"]}`);
5934
5935
  }
5935
- const results = data.hits.map(result => {
5936
+ return data;
5937
+ }
5938
+ async getLanguages() {
5939
+ const searchParams = new URLSearchParams({
5940
+ filter_by: 'type:=Language',
5941
+ query_by: "pref_label",
5942
+ include_fields: "id,pref_label,lang_family",
5943
+ sort_by: "pref_label:asc",
5944
+ enable_highlight_v1: false,
5945
+ per_page: 200,
5946
+ });
5947
+ const data = await this.searchRequest(searchParams);
5948
+ return data.hits.map(result => {
5936
5949
  return {
5937
5950
  code: result.document.id.split("/").reverse()[1],
5938
5951
  label: result.document.pref_label,
5939
5952
  url: result.document.id,
5953
+ family: result.document.lang_family || 'qli', // If no language family is given, using `qli` code as language isolate
5954
+ }
5955
+ });
5956
+ }
5957
+ async getLanguageFamilies() {
5958
+ const searchParams = new URLSearchParams({
5959
+ filter_by: 'type:=Language Family',
5960
+ query_by: "pref_label",
5961
+ include_fields: "id,pref_label",
5962
+ sort_by: "pref_label:asc",
5963
+ enable_highlight_v1: false,
5964
+ per_page: 200,
5965
+ });
5966
+ const data = await this.searchRequest(searchParams);
5967
+ return data.hits.map(result => {
5968
+ return {
5969
+ code: result.document.id.split("/").pop() || 'qli',
5970
+ label: result.document.pref_label,
5971
+ url: result.document.id,
5940
5972
  }
5941
5973
  });
5942
- return results;
5943
5974
  }
5944
5975
  }
5945
5976
  customElements.define('lucos-lang', LucosLangComponent, { extends: "span" });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lucos_search_component",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Web Components for searching lucOS data",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -49,6 +49,9 @@ class LucosLangComponent extends HTMLSpanElement {
49
49
  color: inherit;
50
50
  text-decoration: none;
51
51
  }
52
+ .optgroup-header {
53
+ text-transform: capitalize;
54
+ }
52
55
  `;
53
56
  shadow.appendChild(mainStyle);
54
57
 
@@ -60,6 +63,7 @@ class LucosLangComponent extends HTMLSpanElement {
60
63
  valueField: 'code',
61
64
  labelField: 'label',
62
65
  searchField: ['code','label'],
66
+ optgroupField: 'family',
63
67
  closeAfterSelect: true,
64
68
  plugins: {
65
69
  remove_button:{
@@ -73,10 +77,15 @@ class LucosLangComponent extends HTMLSpanElement {
73
77
  },
74
78
  // On startup, update any existing options with latest data from search
75
79
  onInitialize: async function() {
76
- const results = await component.searchRequest();
77
- results.forEach(result => {
78
- this.updateOption(result.code, result); // Updates any existing options which are selected with the correct label
79
- this.addOption(result); // Makes the option available for new selections
80
+ const families = await component.getLanguageFamilies();
81
+ //this.clearOptionGroups();
82
+ families.forEach(family => {
83
+ this.addOptionGroup(family.code, family);
84
+ });
85
+ const languages = await component.getLanguages();
86
+ languages.forEach(language => {
87
+ this.updateOption(language.code, language); // Updates any existing options which are selected with the correct label
88
+ this.addOption(language); // Makes the option available for new selections
80
89
  });
81
90
  },
82
91
  onItemSelect: function (item) {
@@ -93,17 +102,9 @@ class LucosLangComponent extends HTMLSpanElement {
93
102
  shadow.append(selector.nextElementSibling);
94
103
  }
95
104
  }
96
- async searchRequest() {
105
+ async searchRequest(searchParams) {
97
106
  const key = this.getAttribute("data-api-key");
98
107
  if (!key) throw new Error("No `data-api-key` attribute set on `lucos-search` component");
99
- const searchParams = new URLSearchParams({
100
- filter_by: 'type:=Language',
101
- query_by: "pref_label",
102
- include_fields: "id,pref_label",
103
- sort_by: "pref_label:asc",
104
- enable_highlight_v1: false,
105
- per_page: 200,
106
- });
107
108
  const response = await fetch("https://arachne.l42.eu/search?"+searchParams.toString(), {
108
109
  headers: { 'X-TYPESENSE-API-KEY': key },
109
110
  signal: AbortSignal.timeout(900),
@@ -112,14 +113,44 @@ class LucosLangComponent extends HTMLSpanElement {
112
113
  if (!response.ok) {
113
114
  throw new Error(`Recieved ${response.status} error from search endpoint: ${data["message"]}`);
114
115
  }
115
- const results = data.hits.map(result => {
116
+ return data;
117
+ }
118
+ async getLanguages() {
119
+ const searchParams = new URLSearchParams({
120
+ filter_by: 'type:=Language',
121
+ query_by: "pref_label",
122
+ include_fields: "id,pref_label,lang_family",
123
+ sort_by: "pref_label:asc",
124
+ enable_highlight_v1: false,
125
+ per_page: 200,
126
+ });
127
+ const data = await this.searchRequest(searchParams);
128
+ return data.hits.map(result => {
116
129
  return {
117
130
  code: result.document.id.split("/").reverse()[1],
118
131
  label: result.document.pref_label,
119
132
  url: result.document.id,
133
+ family: result.document.lang_family || 'qli', // If no language family is given, using `qli` code as language isolate
134
+ }
135
+ });
136
+ }
137
+ async getLanguageFamilies() {
138
+ const searchParams = new URLSearchParams({
139
+ filter_by: 'type:=Language Family',
140
+ query_by: "pref_label",
141
+ include_fields: "id,pref_label",
142
+ sort_by: "pref_label:asc",
143
+ enable_highlight_v1: false,
144
+ per_page: 200,
145
+ });
146
+ const data = await this.searchRequest(searchParams);
147
+ return data.hits.map(result => {
148
+ return {
149
+ code: result.document.id.split("/").pop() || 'qli',
150
+ label: result.document.pref_label,
151
+ url: result.document.id,
120
152
  }
121
153
  });
122
- return results;
123
154
  }
124
155
  }
125
156
  customElements.define('lucos-lang', LucosLangComponent, { extends: "span" });