aboutsummaryrefslogtreecommitdiffhomepage
path: root/app/Resources/static/themes/_global/index.js
blob: bb3e95b6d4bceec69e6e208298a38553d7de9352 (plain) (blame)
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
/* jQuery */
import $ from 'jquery';

/* Annotations */
import annotator from 'annotator';

/* Fonts */
import 'material-design-icons-iconfont/dist/material-design-icons.css';
import 'lato-font/css/lato-font.css';
import './global.scss';

/* Shortcuts */
import './js/shortcuts/entry';
import './js/shortcuts/main';

/* Hightlight */
import './js/highlight';

import { savePercent, retrievePercent } from './js/tools';

/* ==========================================================================
 Annotations & Remember position
 ========================================================================== */

$(document).ready(() => {
  if ($('article').length) {
    const app = new annotator.App();

    app.include(annotator.ui.main, {
      element: document.querySelector('article'),
    });

    const authorization = {
      permits() { return true; },
    };
    app.registry.registerUtility(authorization, 'authorizationPolicy');

    const x = JSON.parse($('#annotationroutes').html());
    app.include(annotator.storage.http, $.extend({}, x, {
      onError(msg, xhr) {
        if (!Object.prototype.hasOwnProperty.call(xhr, 'responseJSON')) {
          annotator.notification.banner('An error occurred', 'error');
          return;
        }
        $.each(xhr.responseJSON.children, (k, v) => {
          if (v.errors) {
            $.each(v.errors, (n, errorText) => {
              annotator.notification.banner(errorText, 'error');
            });
          }
        });
      },
    }));

    app.start().then(() => {
      app.annotations.load({ entry: x.entryId });
    });

    $(window).scroll(() => {
      const scrollTop = $(window).scrollTop();
      const docHeight = $(document).height();
      const scrollPercent = (scrollTop) / (docHeight);
      const scrollPercentRounded = Math.round(scrollPercent * 100) / 100;
      savePercent(x.entryId, scrollPercentRounded);
    });

    retrievePercent(x.entryId);

    $(window).resize(() => {
      retrievePercent(x.entryId, true);
    });
  }

  document.querySelectorAll('[data-handler=tag-rename]').forEach((item) => {
    const current = item;
    current.wallabag_edit_mode = false;
    current.onclick = (event) => {
      const target = event.currentTarget;

      if (target.wallabag_edit_mode === false) {
        $(target.parentNode.querySelector('[data-handle=tag-link]')).addClass('hidden');
        $(target.parentNode.querySelector('[data-handle=tag-rename-form]')).removeClass('hidden');
        target.parentNode.querySelector('[data-handle=tag-rename-form] input').focus();
        target.querySelector('.material-icons').innerHTML = 'done';

        target.wallabag_edit_mode = true;
      } else {
        target.parentNode.querySelector('[data-handle=tag-rename-form]').submit();
      }
    };
  });
});