Welcome to mirror list, hosted at ThFree Co, Russian Federation.

index.js « js « assets - github.com/onweru/hugo-swift-theme.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9e971834a98227a3c4cb0d81a9b165ca16a831b9 (plain)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
function fileClosure(){ 
  // everything in this file should be declared within this closure (function).

  // global variables
  let hidden;
  hidden = 'hidden';

  const doc = document.documentElement;
  const parentURL = '{{ .Site.BaseURL }}';
  const staticman = Object.create(null);
  {{ with .Site.Params.staticman -}}
  const endpoint = '{{ .endpoint | default "https://staticman3.herokuapp.com" }}';
  const gitProvider = '{{ .gitprovider }}';
  const username = '{{ .username }}';
  const repository = '{{ .repository }}';
  const branch = '{{ .branch }}';

  // store reCAPTCHA v2 site key and secret
  {{ with .recaptcha -}}
  staticman.siteKey = '{{ .sitekey }}';
  staticman.secret = '{{ .secret }}';
  {{ end -}}
  {{ end -}}

  const translations = {
    success: {
      title: '{{ i18n "successTitle" }}',
      text: '{{ i18n "successMsg" }}',
      close: '{{ i18n "close" }}'
    },
    error: {
      title: '{{ i18n "errTitle" }}',
      text: '{{ i18n "errMsg" }}',
      close: '{{ i18n "close" }}'
    },
    discard: {
      title: '{{ i18n "discardComment" }}',
      button: '{{ i18n "discard" }}'
    },
    submit: '{{ i18n "btnSubmit" }}',
    submitted: '{{ i18n "btnSubmitted" }}'
  };

  function isObj(obj) {
    return (obj && typeof obj === 'object' && obj !== null) ? true : false;
  }

  function createEl(element = 'div') {
    return document.createElement(element);
  }

  function elem(selector, parent = document){
    let elem = parent.querySelector(selector);
    return elem != false ? elem : false;
  }

  function elems(selector, parent = document) {
    let elems = parent.querySelectorAll(selector);
    return elems.length ? elems : false;
  }

  function pushClass(el, targetClass) {
    if (isObj(el) && targetClass) {
      elClass = el.classList;
      elClass.contains(targetClass) ? false : elClass.add(targetClass);
    }
  }

  function deleteClass(el, targetClass) {
    if (isObj(el) && targetClass) {
      elClass = el.classList;
      elClass.contains(targetClass) ? elClass.remove(targetClass) : false;
    }
  }

  function modifyClass(el, targetClass) {
    if (isObj(el) && targetClass) {
      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 updateDate() {
    var date = new Date();
    var year = date.getFullYear();
    elem('.year').innerHTML = year;
  })();

  (function() {
    let bar = 'nav_bar-wrap';
    let navBar = elem(`.${bar}`);
    let nav = elem('.nav-body');
    let open = 'nav-open';
    let exit = 'nav-exit';
    let drop = 'nav-drop';
    let pop = 'nav-pop';
    let navDrop = elem(`.${drop}`);

    function toggleMenu(){
      let menuOpen, menuPulled, status;
      modifyClass(navDrop, pop);
      modifyClass(navBar, hidden);
      menuOpen = containsClass(nav, open);
      menuPulled = containsClass(nav, exit);

      status = menuOpen || menuPulled ? true : false;

      status ? modifyClass(nav, exit) : modifyClass(nav, open);
      status ? modifyClass(nav, open) : modifyClass(nav, exit);
    }

    navBar.addEventListener('click', function() {
      toggleMenu();
    });
    elem('.nav-close').addEventListener('click', function() {
      toggleMenu();
    });

    elem('.nav-drop').addEventListener('click', function(e) {
      e.target === this ? toggleMenu() : false;
    });

  })();

  function convertToUnderScoreCase(str) {
    let char, newChar, newStr;
    newStr = '';
    if (typeof str == 'string') {
      for (let x = 0; x < str.length; x++) {
        char = str.charAt(x);
        if (char.match(/^[A-Z]*$/)) {
          char = char.toLowerCase();
          newChar = `_${char}`
          newStr += newChar;
        } else {
          newStr += char;
        }
      }
      return newStr;
    }
  }

  function createModal(children, parent) {
    let body, modal, title;

    modal = createEl();
    pushClass(modal, 'modal');
    body = createEl();
    pushClass(body, 'modal_inner');
    title = createEl('h3');
    pushClass(title, 'modal_title');
    body.appendChild(title);
    // add html specific to modal
    if (isObj(children)) {   
      if (Array.isArray(children)) {
        children.map(function(child){
          body.appendChild(child);
        });
      } else {
        body.appendChild(children);
      }
    }
    modal.appendChild(body);
    parent.append(modal);
    pushClass(doc, 'modal_show');
  }

  function fillModal(obj) {
    let el, targetClass, modal;
    modal = elem('.modal');
    const entries = Object.entries(obj)
    for (const [element, content] of entries) {
      targetClass = `.${convertToUnderScoreCase(element)}`;
      el = elem(targetClass, modal);
      el.innerHTML = content;
    }
  }

  (function comments(){
    let comments, form, replyNotice, open;

    comments = elem('.comments');
    form = elem('.form');
    button = elem('.form_toggle');
    replyNotice = elem('.reply_notice')
    open = 'form_open';

    let successOutput, errorOutput;

    successOutput = {
      modalTitle: translations.success.title,
      modalText: translations.success.text,
      modalClose: translations.success.close
    };
    errorOutput = {
      modalTitle: translations.error.title,
      modalText: translations.error.text,
      modalClose: translations.error.close
    };

    function feedbackModal() {
      let body, button, children;
      body = createEl();
      pushClass(body, 'modal_text');
      button = createEl();
      pushClass(button, 'btn');
      pushClass(button, 'modal_close');
      children = [
        body,
        button
      ];
      return children;
    }

    function confirmModal() {
      // confirm if you want to exit form
      let group, button, cancel;
      group = createEl();
      pushClass(group, 'btn_group');
      button = createEl();
      pushClass(button, 'btn');
      pushClass(button, 'modal_close')
      pushClass(button, 'form_close')
      cancel = createEl();
      pushClass(cancel, 'modal_close')
      pushClass(cancel, 'btn_close');
      pushClass(cancel, 'icon');
      group.appendChild(button);
      group.appendChild(cancel);
      return group;
    }

    function handleForm(form) {

      function formValues() {
        // returns an object with form field values
        let deadWeight, fields, fieldAreas, obj;
        fieldAreas = elems('.form_input', form);
        fields = Array.from(fieldAreas);
        obj = Object.create(null);
        deadWeight = ['fields', 'options', '[' , ']', 'undefined'];

        fields.map(function(field) {
          let key, value;
          key = deleteChars(field.name, deadWeight);
          key = convertToUnderScoreCase(key);
          value = field.value;
          obj[key] = value;
        });
        return obj;
      }

      (function submitForm() {
        form.addEventListener('submit', function (event) {
          event.preventDefault();

          let fields, recaptchaResponse, submit, url;
          url = [endpoint, 'v3/entry', gitProvider, username, repository, branch, 'comments'].join('/');
          fields = formValues();
          submit = elem('.form_submit', form);
          recaptchaResponse = elem('[name="g-recaptcha-response"]', form);

          submit.value = translations.submitted;

          function formActions(info) {
            showModal(info);
            submit.value = translations.submit;
          }

          let data = {
            fields: {
              name: fields.name,
              email: fields.email,
              comment: fields.comment,
              replyID: fields.reply_id,
              replyName: fields.reply_name,
              replyThread: fields.reply_thread
            },
            options: {
              slug: fields.slug
            }
          };

          if (staticman.secret){
            data.options.reCaptcha = {};
            data.options.reCaptcha.siteKey = staticman.siteKey;
            data.options.reCaptcha.secret = staticman.secret;
            data["g-recaptcha-response"] = recaptchaResponse.value;
          }

          fetch(url, {
            method: "POST",
            body: JSON.stringify(data),
            headers: {
              "Content-Type": "application/json"
            }
          }).then(function(res) {
            if(res.ok) {
              formActions(successOutput);
            } else {
              formActions(errorOutput);
            }
          }).catch(function(error) {
            formActions(errorOutput);
            console.error('Error:', error);
          });
        });
      })();

      function getHiddenFields() {
        let reply_id, reply_name, reply_thread, reply_to, obj;

        reply_id = elem('.reply_id', form);
        reply_name = elem('.reply_name', form);
        reply_thread = elem('.reply_thread', form);
        reply_to = elem('.reply_to', form);

        obj = {
          id: reply_id,
          name: reply_name,
          thread: reply_thread,
          to: reply_to
        }

        return obj;
      }

      function setReplyValues(trigger) {
        let comment, id, name, thread;

        let reply_fields = getHiddenFields();

        comment = trigger.parentNode;
        id = comment.id;
        name = elem('.comment_name_span', comment);
        thread = elem('.comment_thread', comment);
        reply_fields.thread.value = thread.textContent;
        reply_fields.id.value = id;
        reply_fields.name.value = name.textContent;
        reply_fields.to.textContent = name.textContent;
      }

      function resetReplyValues() {
        let reply_fields;
        reply_fields = getHiddenFields();
        const values = Object.entries(reply_fields);
        for (const [key, element] of values) {
          if (key == 'to') {
            element.textContent = '';
          } else {
            element.value = '';
          }
        }
      }

      function toggleForm(action = true) {
        let reply_to, toggle_btn;
        action = action ? pushClass : deleteClass;
        toggle_btn = elem('.form_toggle');
        action(form, open);
        action(toggle_btn, hidden);
        reply_to = getHiddenFields().to.textContent;
        isBlank(reply_to) ? pushClass(replyNotice, hidden) : deleteClass(replyNotice, hidden) ;
      }

      comments.addEventListener('click', function (event){
        let confirm, fields, modal, target, obj, formIsEmpty, hiddenValuesEmpty;

        // buttons
        let isFormCloseBtn, isFormToggleBtn, isModalCloseBtn, isResetFormBtn, isReplyBt;

        target = event.target;
        fields = formValues();
        formIsEmpty = isBlank(fields.name) && isBlank(fields.comment) && isBlank(fields.email) ? true : false;
        hiddenValuesEmpty = isBlank(fields.reply_id) ? true : false;

        isFormCloseBtn = containsClass(target, 'form_close');
        isFormToggleBtn = containsClass(target, 'form_toggle');
        isModalCloseBtn = containsClass(target, 'modal_close');
        isResetFormBtn = containsClass(target, 'form_reset');
        isReplyBtn = containsClass(target, 'reply_btn');

        isReplyBtn ? setReplyValues(target) : false;
        isReplyBtn || isFormToggleBtn ? toggleForm() : false;
        isFormCloseBtn ? toggleForm(false) : false;

        if (isFormCloseBtn) {
          form.reset();
        } 

        if (isResetFormBtn) {
          if (formIsEmpty) {
            hiddenValuesEmpty ? false : resetReplyValues();
            toggleForm(false);
          } else {
            obj = {
              modalTitle: translations.discard.title,
              modalClose: translations.discard.button
            }
            confirm = confirmModal();
            createModal(confirm, comments);
            fillModal(obj);
          }
        } 

        if (isModalCloseBtn) {
          modal = target.closest('.modal');
          modal.remove();
          deleteClass(doc, 'modal_show');    
        }
      });
    }

    form ? handleForm(form) : false;

    function showModal(obj) {
      let feedbackBody;
      feedback = feedbackModal();
      createModal(feedback, comments);
      fillModal(obj);
    }

  })();

  (function makeExternalLinks(){
    let links = elems('a');
    if(links) {
      Array.from(links).forEach(function(link){
        let target, rel, blank, noopener, attr1, attr2, url, isExternal;
        url = elemAttribute(link, 'href');
        isExternal = (url && typeof url == 'string' && url.startsWith('http')) && !url.startsWith(parentURL) ? true : false;
        if(isExternal) {
          target = 'target';
          rel = 'rel';
          blank = '_blank';
          noopener = 'noopener';
          attr1 = elemAttribute(link, target);
          attr2 = elemAttribute(link, noopener);

          attr1 ? false : elemAttribute(link, target, blank);
          attr2 ? false : elemAttribute(link, rel, noopener);
        }
      });
    }
  })();

  let headingNodes = [], results, link, icon, current, id,
  tags = ['h2', 'h3', 'h4', 'h5', 'h6'];

  current = document.URL;

  tags.forEach(function(tag){
    results = document.getElementsByTagName(tag);
    Array.prototype.push.apply(headingNodes, results);
  });

  headingNodes.forEach(function(node){
    link = createEl('a');
    icon = createEl('img');
    icon.src = '{{ absURL "images/icons/link.svg" }}';
    link.className = 'link';
    link.appendChild(icon);
    id = node.getAttribute('id');
    if(id) {
      link.href = `${current}#${id}`;
      node.appendChild(link);
      pushClass(node, 'link_owner');
    }
  });

  let inlineListItems = elems('ol li');
  if(inlineListItems) {
    inlineListItems.forEach(function(listItem){
      let firstChild = listItem.children[0]
      let containsHeading = isMatch(firstChild, tags);
      containsHeading ? pushClass(listItem, 'align') : false;
    })
  }

  const copyToClipboard = str => {
    let copy, selection, selected;
    copy = createEl('textarea');
    copy.value = str;
    copy.setAttribute('readonly', '');
    copy.style.position = 'absolute';
    copy.style.left = '-9999px';
    selection = document.getSelection();
    doc.appendChild(copy);
    // check if there is any selected content
    selected = selection.rangeCount > 0 ? selection.getRangeAt(0) : false;
    copy.select();
    document.execCommand('copy');
    doc.removeChild(copy);
    if (selected) { // if a selection existed before copying
      selection.removeAllRanges(); // unselect existing selection
      selection.addRange(selected); // restore the original selection
    }
  }

  (function copyHeadingLink() {
    let deeplink, deeplinks, newLink, parent, target;
    deeplink = 'link';
    deeplinks = elems(`.${deeplink}`);
    if(deeplinks) {
      document.addEventListener('click', function(event)
      {
        target = event.target;
        parent = target.parentNode;
        if (target && containsClass(target, deeplink) || containsClass(parent, deeplink)) {
          event.preventDefault();
          newLink = target.href != undefined ? target.href : target.parentNode.href;
          copyToClipboard(newLink);
        }
      });
    }
  })();

  (function copyLinkToShare() {
    let  copy, copied, excerpt, isCopyIcon, isInExcerpt, link, postCopy, postLink, target;
    copy = 'copy';
    copied = 'copy_done';
    excerpt = 'excerpt';
    postLink = 'post_card';

    doc.addEventListener('click', function(event) {
      target = event.target;
      isCopyIcon = containsClass(target, copy);
      let isWithinCopyIcon = target.closest(`.${copy}`);
      if (isCopyIcon || isWithinCopyIcon) {
        let icon = isCopyIcon ? isCopyIcon : isWithinCopyIcon;
        isInExcerpt = icon.closest(`.${excerpt}`);
        if (isInExcerpt) {
          link = target.closest(`.${excerpt}`).previousElementSibling;
          link = containsClass(link, postLink)? elemAttribute(link, 'href') : false;
        } else {
          link = window.location.href;
        }
        if(link) {
          copyToClipboard(link);
          pushClass(icon, copied);
        }
      }
    });
  })();

  (function hideAside(){
    let aside, title, posts;
    aside = elem('.aside');
    title = aside ? aside.previousElementSibling : null;
    if(aside && title.nodeName.toLowerCase() === 'h3') {
      posts = Array.from(aside.children);
      posts.length < 1 ? title.remove() : false;
    }
  })();

  (function goBack() {
    let backBtn = elem('.btn_back');
    let history = window.history;
    if (backBtn) {
      backBtn.addEventListener('click', function(){
        history.back();
      });
    }
  })();

  (function postsPager(){
    const pager = elem('.pagination');
    if (pager) {
      pushClass(pager, 'pager');
      const pagerItems = elems('li', pager);
      const pagerLinks = Array.from(pagerItems).map(function(item){
        return item.firstElementChild;
      });

      pagerLinks.forEach(function(link){
        pushClass(link, 'pager_link')
      });

      pagerItems.forEach(function(item){
        pushClass(item, 'pager_item')
      });
    }
  })();

  (function lazy() {
    function lazyLoadMedia(element) {
      let mediaItems = elems(element);
      if(mediaItems) {
        Array.from(mediaItems).forEach(function(item, index) {
        item.loading = "lazy";
        });
      }
    }
    lazyLoadMedia('iframe');
    lazyLoadMedia('img');
  })();

  // add new code above this line
}
window.addEventListener('load', fileClosure());