summaryrefslogtreecommitdiffstats
path: root/js/gui/KeyboardShortcuts.js
blob: ad0943cdeb6063b59b74443a623b75959bdfedc3 (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
/**
 * ownCloud - News
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Bernhard Posselt <dev@bernhard-posselt.com>
 * @copyright Bernhard Posselt 2014
 */

/**
 * Code in here acts only as a click shortcut mechanism. That's why its not
 * being put into a directive since it has to be tested with protractor
 * anyways and theres no benefit from wiring it into the angular app
 */
(function (window, document, $) {
    'use strict';

    const noInputFocused = (element) => {
        return !(
            element.is('input') ||
            element.is('select') ||
            element.is('textarea') ||
            element.is('checkbox')
        );
    };

    const noModifierKey = (event) => {
        return !(
            event.shiftKey ||
            event.altKey ||
            event.ctrlKey ||
            event.metaKey
        );
    };

    const scrollToItem = (item, scrollArea) => {
        scrollArea.scrollTop(
            item.offset().top - scrollArea.offset().top + scrollArea.scrollTop()
        );
    };

    const scrollToNextItem = (scrollArea) => {
        const items = scrollArea.find('.item');

        for (let item of items) {
            item = $(item);

            if (item.position().top > 1) {
                scrollToItem(scrollArea, item);
                return;
            }
        }

        // in case this is the last item it should still scroll below the top
        scrollArea.scrollTop(scrollArea.prop('scrollHeight'));

    };

    const scrollToPreviousItem = (scrollArea) => {
        const items = scrollArea.find('.item');

        for (var item of items) {
            item = $(item);

            if (item.position().top >= 0) {
                let previous = item.prev();

                // if there are no items before the current one
                if (previous.length > 0) {
                    scrollToItem(scrollArea, previous);
                }

                return;
            }
        }

        // if there was no jump jump to the last element
        if (items.length > 0) {
            scrollToItem(scrollArea, items.last());
        }
    };

    const getActiveItem = (scrollArea) => {
        const items = scrollArea.find('.item');

        for (let item of items) {
            item = $(item);

            // 130px of the item should be visible
            if ((item.height() + item.position().top) > 30) {
                return item;
            }
        }
    };

    const toggleUnread = (scrollArea) => {
        const item = getActiveItem(scrollArea);
        item.find('.keep_unread').trigger('click');
    };

    const toggleStar = (scrollArea) => {
        const item = getActiveItem(scrollArea);
        item.find('.item_utils .star').trigger('click');
    };

    const expandItem = (scrollArea) => {
        const item = getActiveItem(scrollArea);
        item.find('.item_heading a').trigger('click');
    };

    const openLink = (scrollArea) => {
        const item = getActiveItem(scrollArea).find('.item_title a');
        item.trigger('click');  // mark read
        window.open(item.attr('href'), '_blank');
    };

    $(document).keyup((event) => {
        if (noInputFocused($(':focus')) && noModifierKey(event)) {
            const keyCode = event.keyCode;
            const scrollArea = $('#app-content');

            // j, n, right arrow
            if ([74, 78, 33].indexOf(keyCode) >= 0) {

                event.preventDefault();
                scrollToNextItem(scrollArea);

            // k, p, left arrow
            } else if ([75, 80, 37].indexOf(keyCode) >= 0) {

                event.preventDefault();
                scrollToPreviousItem(scrollArea);

            // u
            } else if ([85].indexOf(keyCode) >= 0) {

                event.preventDefault();
                toggleUnread(scrollArea);

            // e
            } else if ([69].indexOf(keyCode) >= 0) {

                event.preventDefault();
                expandItem(scrollArea);

            // s, i, l
            } else if ([73, 83, 76].indexOf(keyCode) >= 0) {

                event.preventDefault();
                toggleStar(scrollArea);

            // h
            } else if ([72].indexOf(keyCode) >= 0) {

                event.preventDefault();
                toggleStar(scrollArea);
                scrollToNextItem(scrollArea);

            // o
            } else if ([79].indexOf(keyCode) >= 0) {

                event.preventDefault();
                openLink(scrollArea);

            }

        }
    });

}(window, document, jQuery));