jekyll_ranked_search 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/search.js +54 -12
  3. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a717e9e1526e49b484e6b84067aa22daef311e21160e80a5c87a5a18fd6aeeb
4
- data.tar.gz: 5f6403859df289ecd20971ecb775d08bcc88487616df544ebacc1c2e74805fe6
3
+ metadata.gz: 81e9541ca2a4139827dfb10a8ad23bb3f493f9465d95c8c0d8f18f55affeb10f
4
+ data.tar.gz: cc861e099846391ab537624f4a509e7bf97987c5f68851335a613fe8fbadb6e9
5
5
  SHA512:
6
- metadata.gz: 7c268065e1ffe5dbd646feca161ed346dfc649c52d2b4f7269e1cab98e17a3a6180cb70c81a82a8c9b081b4b3a4aa7a6d5dd4d7d9b3ea10610a20210610e0f46
7
- data.tar.gz: 73822909d296b0d24a711cbffe27ceb906d6319bc419932ce5dc8be94774d1840339aba7eb58634c126c75a836bc4b4279275eab8ae431d47b17ffc9df0127d4
6
+ metadata.gz: cb47470cf5b035428a768d83804dd92703e754e3776f85655094f136e02d1523e0995c884595caf02716a29fe8cf7f416fd05f4549b18ca121c13fbad0ec252e
7
+ data.tar.gz: 5258f44e86d40cc44666aae5841ffd7e50c7a51b5c60dcf7666300177b278ffff0c4e89f5b4a6b2d62c6117f21edea8f8a0ee75ed076a8def7b010a572472a1d
data/lib/search.js CHANGED
@@ -4,16 +4,38 @@ import 'https://cdn.jsdelivr.net/npm/@github/relative-time-element';
4
4
 
5
5
  class SearchBox extends LitElement {
6
6
  static properties = {
7
- _data: {state: true, type: Array},
7
+ _isLoading: {state: true, type: Boolean},
8
+ _data: {state: false, type: Array},
8
9
  _results : {state: true, type: Array},
9
10
  _open: {state: true, type: Boolean},
11
+ _placeholder: {state: true, type: String},
12
+
13
+ // Lazy loading
14
+ lazy: {type: Boolean, attribute: true},
10
15
  };
11
16
 
17
+ /**
18
+ * Constructor. Sets up default values.
19
+ */
12
20
  constructor() {
13
21
  super();
22
+
23
+ // No data initially loaded
14
24
  this._data = [];
25
+
26
+ // Results are initially empty
15
27
  this._results = [];
28
+
29
+ // Start in closed state, show no results
16
30
  this._open = false;
31
+
32
+ // Default to not loading
33
+ this._isLoading = false;
34
+
35
+ // Disable lazy loading by default
36
+ this.lazy = false;
37
+
38
+ this._placeholder = "Search...";
17
39
  }
18
40
 
19
41
  static styles = css`
@@ -77,16 +99,17 @@ class SearchBox extends LitElement {
77
99
 
78
100
  connectedCallback() {
79
101
  super.connectedCallback();
80
- this.loadData();
102
+
103
+ // Load data if lazy loading is disabled
104
+ if (!this.lazy) {
105
+ this.loadData();
106
+ }
81
107
 
82
108
  document.addEventListener('click', (event) => {
83
109
  if (!event.composedPath().includes(this) && this._open) {
84
110
  this.toggle();
85
111
  }
86
- });
87
-
88
- // Register arrow keys
89
-
112
+ });
90
113
  }
91
114
 
92
115
  toggle() {
@@ -104,12 +127,20 @@ class SearchBox extends LitElement {
104
127
  }
105
128
 
106
129
  async loadData() {
130
+ // Set state during loading
131
+ this._isLoading = true;
132
+ this.updatePlaceholder();
133
+
107
134
  const response = await fetch("/search.json");
108
135
  const jsonData = await response.json();
109
136
  jsonData.word2doc = new Map(Object.entries(jsonData.word2doc));
110
137
  jsonData.bow = new Map(Object.entries(jsonData.bow));
111
138
  jsonData.tfidf = new Map(Object.entries(jsonData.tfidf));
112
139
  this._data = jsonData;
140
+
141
+ // Cleanup state
142
+ this._isLoading = false;
143
+ this.updatePlaceholder();
113
144
  }
114
145
 
115
146
  disconnectedCallback() {
@@ -158,7 +189,6 @@ class SearchBox extends LitElement {
158
189
  for (const tokenId of tokenIds.slice(1)) {
159
190
  // Find document candidates
160
191
  const docCandidates = new Set(this._data.word2doc.get(tokenId.toString()));
161
- // console.log("intersection", docCandidates, docs);
162
192
  docs = new Set([...docs].filter((x) => docCandidates.has(x)));
163
193
  }
164
194
 
@@ -182,21 +212,33 @@ class SearchBox extends LitElement {
182
212
  this._open = true;
183
213
  }
184
214
 
185
- placeholder() {
215
+ updatePlaceholder() {
216
+ if (this._isLoading) {
217
+ this._placeholder = "Loading...";
218
+ return;
219
+ }
186
220
  if (this._data && this._data.docs && this._data.docs.length > 0) {
187
221
  let plural = "";
188
222
  if (this._data.docs.length !== 1) {
189
223
  plural = "s";
190
224
  }
191
- return "Search in " + this._data.docs.length + ` post${plural}...`;
192
- } else {
193
- return "Loading...";
225
+ this._placeholder = "Search in " + this._data.docs.length + ` post${plural}...`;
226
+ return;
227
+ }
228
+ }
229
+
230
+ /**
231
+ * Event triggered on search box focus.
232
+ */
233
+ focus(_) {
234
+ if (this.lazy && this._data.length === 0 && !this._isLoading) {
235
+ this.loadData();
194
236
  }
195
237
  }
196
238
 
197
239
  render() {
198
240
  return html`<div>
199
- <input id="q" type="text" placeholder="${this.placeholder()}" @keyup="${this.search}" @click=${this.openIfResults}>
241
+ <input id="q" type="text" placeholder="${this._placeholder}" @keyup="${this.search}" @click=${this.openIfResults} @focus=${this.focus}>
200
242
  ${this._open ? html`
201
243
  <div id="results">
202
244
  ${this._results.map((result) => html`
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll_ranked_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Friedrich Ewald
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-24 00:00:00.000000000 Z
11
+ date: 2023-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redcarpet
@@ -46,7 +46,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
- version: '0'
49
+ version: 2.5.0
50
50
  required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
@@ -56,5 +56,5 @@ requirements: []
56
56
  rubygems_version: 3.4.13
57
57
  signing_key:
58
58
  specification_version: 4
59
- summary: TF-IDF search for Jekyll posts
59
+ summary: TF-IDF offline search for Jekyll posts
60
60
  test_files: []