mirror of https://github.com/onweru/compose.git

weru
30.03.2020 72117c20ecb0e08eca67a03081ad9e3cb6a41f14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const idx = [
  {{- range .Site.Pages }}
  {
    "link": "{{ .Permalink }}",
    "title": "{{ .Title }}",
    "body": "{{ .PlainWords }}".toLowerCase(),
  },
  {{- end }}
];
 
const searchKeys = ['title', 'link', 'body', 'id'];
 
const searchPageElement = elem('#searchpage');
 
const searchOptions = {
  ignoreLocation: true,
  findAllMatches: true,
  includeScore: true,
  shouldSort: true,
  keys: searchKeys,
  threshold: 0.0
};
 
const index = new Fuse(idx, searchOptions);
 
function searchResults(results=[], query="") {
  let resultsFragment = new DocumentFragment();
  let showResults = elem('.search_results');
  if(searchPageElement) {
    showResults = searchPageElement;
  }
  emptyEl(showResults);
  if(results.length) {
    let resultsTitle = createEl('h3');
    resultsTitle.className = 'search_title';
    resultsTitle.innerText = 'Quick Links';
    if(searchPageElement) {
      resultsTitle.innerText = 'Search Results';
    }
    resultsFragment.appendChild(resultsTitle);
    results.slice(0,4).forEach(function(result){
      let item = createEl('a');
      item.href = `${result.link}?query=${query}`;
      item.className = 'search_result';
      item.style.order = result.score;
      if(searchPageElement) {
        pushClass(item, 'passive');
        let itemTitle = createEl('h3');
        itemTitle.textContent = result.title;
        item.appendChild(itemTitle);
 
        let itemDescription = createEl('p');
        // position of first search term instance
        let queryInstance = result.body.indexOf(query);
        itemDescription.textContent = `... ${result.body.substring(queryInstance, queryInstance + 200)} ...`;
        item.appendChild(itemDescription);
      } else {
        item.textContent = result.title;
      }
      resultsFragment.appendChild(item);
    });
  } else {
    showResults.innerHTML = (query.length >= 3) ? `<span class="search_result">No Results</span>` : "";
  }
  showResults.appendChild(resultsFragment);
}
 
function search(searchTerm) {
  // check if searchTerm can be cast as float
  const isFloat = parseFloat(searchTerm);
  const minimumSearchTermLength = isFloat ? 2 : 3;
  if(searchTerm.length >= minimumSearchTermLength) {
    let rawResults = index.search(searchTerm);
    rawResults = rawResults.map(function(result){
      const score = result.score;
      const resultItem = result.item;
      resultItem.score = (parseFloat(score) * 50).toFixed(0);
      return resultItem;
    });
    searchResults(rawResults, searchTerm);
  } else {
    searchResults();
  }
}
 
function liveSearch() {
  const searchField = elem('.search_field');
 
  if (searchField) {
    searchField.addEventListener('input', function() {
      const searchTerm = searchField.value.trim().toLowerCase();
      search(searchTerm);
    });
    searchField.addEventListener('search', function(){
      const searchTerm = searchField.value.trim().toLowerCase();
      window.location.href = `${parentURL}/search/?query=${searchTerm}`;
    });
  }
}
 
function findQuery(query = 'query') {
  const urlParams = new URLSearchParams(window.location.search);
  if(urlParams.has(query)){
    let c = urlParams.get(query);
    // window.find(c);
    cc = `${c.charAt(0).toUpperCase()}${c.substring(1,c.length)}`;
    // window.find(cc);
    return [c, cc];
  }
  return ["",""];
}
 
function passiveSearch() {
  if(searchPageElement) {
    const searchTerm = findQuery()[0];
    search(searchTerm);
  }
}
 
let main = elem('main');
if(!main) {
  main = elem('.main');
}
 
window.addEventListener('load', function() {
  searchPageElement ? false : liveSearch();
  passiveSearch();
 
  const searchQuery = findQuery();
  wrapText(searchQuery[0],main);
  wrapText(searchQuery[1],main);
});