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

weru
30.04.2020 dfad1ec43b34f84ddcee2b94ea2641fe0d798a6c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
function isObj(obj) {
  return (obj && typeof obj === 'object' && obj !== null) ? true : false;
}
 
function createEl(element = 'div') {
  return document.createElement(element);
}
 
function emptyEl(el) {
  while(el.firstChild)
  el.removeChild(el.firstChild);
}
 
function elem(selector, parent = document){
  let elem = isObj(parent) ? parent.querySelector(selector) : false;
  return elem ? elem : false;
}
 
function elems(selector, parent = document) {
  let elems = isObj(parent) ?parent.querySelectorAll(selector) : [];
  return elems.length ? elems : false;
}
 
function pushClass(el, targetClass) {
  if (isObj(el) && targetClass) {
    let elClass = el.classList;
    elClass.contains(targetClass) ? false : elClass.add(targetClass);
  }
}
 
function deleteClass(el, targetClass) {
  if (isObj(el) && targetClass) {
    let elClass = el.classList;
    elClass.contains(targetClass) ? elClass.remove(targetClass) : false;
  }
}
 
function modifyClass(el, targetClass) {
  if (isObj(el) && targetClass) {
    const elClass = el.classList;
    elClass.contains(targetClass) ? elClass.remove(targetClass) : elClass.add(targetClass);
  }
}
 
function containsClass(el, targetClass) {
  if (isObj(el) && targetClass && el !== document ) {
    return el.classList.contains(targetClass) ? true : false;
  }
}
 
function isChild(node, parentClass) {
  let objectsAreValid = isObj(node) && parentClass && typeof parentClass == 'string';
  return (objectsAreValid && node.closest(parentClass)) ? true : false;
}
 
function elemAttribute(elem, attr, value = null) {
  if (value) {
    elem.setAttribute(attr, value);
  } else {
    value = elem.getAttribute(attr);
    return value ? value : false;
  }
}
 
function deleteChars(str, subs) {
  let newStr = str;
  if (Array.isArray(subs)) {
    for (let i = 0; i < subs.length; i++) {
      newStr = newStr.replace(subs[i], '');
    }
  } else {
    newStr = newStr.replace(subs, '');
  }
  return newStr;
}
 
function isBlank(str) {
  return (!str || str.trim().length === 0);
}
 
function isMatch(element, selectors) {
  if(isObj(element)) {
    if(selectors.isArray) {
      let matching = selectors.map(function(selector){
        return element.matches(selector)
      })
      return matching.includes(true);
    }
    return element.matches(selectors)
  }
}
 
function closestInt(goal, collection) {
  const closest = collection.reduce(function(prev, curr) {
    return (Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);
  });
  return closest
}
 
function activeHeading(position, listLinks) {
  let active = 'active';
 
  let linksToModify = Object.create(null);
  linksToModify.active = listLinks.filter(function(link) {
    return containsClass(link, active);
  })[0];
 
  // activeTocLink ? deleteClass
 
  linksToModify.new = listLinks.filter(function(link){
    return parseInt(link.dataset.position) === position
  })[0];
 
  console.log(linksToModify.new)
 
  if (linksToModify.active != linksToModify.new) {
    linksToModify.active ? deleteClass(linksToModify.active, active): false;
    pushClass(linksToModify.new, active);
  }
};
 
function loadActions() {
 
  (function updateDate() {
    const date = new Date();
    const year = date.getFullYear();
    const yearEl = elem('.year');
    yearEl ? year.innerHTML = year : false;
  })();
 
  (function customizeSidebar(){
    const tocActive = 'toc_active';
    const aside = elem('aside');
    const tocs = elems('nav', aside);
    if(tocs) {
      tocs.forEach(function(toc){
        toc.id = "";
        pushClass(toc, 'toc');
        if(toc.children.length >= 1) {
          const tocItems = Array.from(toc.children[0].children);
  
          const previousHeading = toc.previousElementSibling;
          previousHeading.matches('.active') ? pushClass(toc, tocActive) : false;
    
          tocItems.forEach(function(item){
            pushClass(item, 'toc_item');
            pushClass(item.firstElementChild, 'toc_link');
          })
        }
      });
 
      const currentToc = elem(`.${tocActive}`);
 
      if(currentToc) {
        const pageInternalLinks = Array.from(elems('a', currentToc));
 
        const pageIds = pageInternalLinks.map(function(link){
          return link.hash;
        });
        
        const linkPositions = pageIds.map(function(id){
          const heading = elem(id);
          const position = heading.offsetTop;
          return position;
        });
        
        pageInternalLinks.forEach(function(link, index){
          link.dataset.position = linkPositions[index]
        });
        console.log(linkPositions);
 
        window.addEventListener('scroll', function(e) {
          // this.setTimeout(function(){
          let position = window.scrollY;
          let active = closestInt(position, linkPositions);
          console.log(active);
          activeHeading(active, pageInternalLinks);
          // }, 1500)
        });
      }
    }
  })();
 
  function searchResults(results=[], order =[]) {
    let resultsFragment = new DocumentFragment();
    let showResults = elem('.search_results');
    emptyEl(showResults);
    let index = 0
    results.forEach(function(result){
      let item = createEl('a');
      item.href = result.link;
      item.className = 'search_result';
      item.textContent = result.title;
      item.style.order = order[index];
      resultsFragment.appendChild(item);
      index += 1
    });
    
    showResults.appendChild(resultsFragment);
  }
  
  (function search(){
    const searchField = elem('.search_field');
    
    if (searchField) {
      searchField.addEventListener('input', function() {
        console.log('typing')
        let rawResults = idx.search(`${ this.value }`).slice(0,6);
        let refs = rawResults.map(function(ref){
          // return id and score in a single string
          return `${ref.ref}:${ref.score}`;
        });
        
        let ids = refs.map(function(id){
          let positionOfSeparator = id.indexOf(":");
          id = id.substring(0,positionOfSeparator)
          return Number(id);
        });
        
        let scores = refs.map(function(score){
          let positionOfSeparator = score.indexOf(":");
          score = score.substring((positionOfSeparator + 1), (score.length - 1));
          return (parseFloat(score) * 50).toFixed(0);
        });
        
        let matchedDocuments = simpleIndex.filter(function(doc){
          return ids.includes(doc.id);
        });
        
        matchedDocuments.length >= 1 ? searchResults(matchedDocuments, scores) : false;
      });
    }
    
  })();
}
 
window.addEventListener('load', loadActions());