]> git.immae.eu Git - github/wallabag/wallabag.git/blame - app/Resources/static/themes/baggy/index.js
Set progress
[github/wallabag/wallabag.git] / app / Resources / static / themes / baggy / index.js
CommitLineData
af61cb80
TC
1import $ from 'jquery';
2
64f81bc3
TC
3/* Global imports */
4import '../_global/index';
af61cb80 5
5637a26e 6/* Shortcuts */
64f81bc3
TC
7import './js/shortcuts/main';
8import './js/shortcuts/entry';
5637a26e
TC
9
10/* Tools */
64f81bc3 11import toggleSaveLinkForm from './js/uiTools';
5fe9aadc
TC
12import {savePercent, retrievePercent, throttle} from '../_global/js/tools';
13
14global.jquery = $;
5ecdfcd0 15
64f81bc3
TC
16/* Theme style */
17import './css/index.scss';
19f2f11e 18
64f81bc3 19$(document).ready(() => {
19f2f11e
NL
20 /* ==========================================================================
21 Menu
22 ========================================================================== */
23
c146f694 24 $('#menu').click(() => {
0743287f 25 $('#links').toggleClass('menu--open');
c146f694
TC
26 const content = $('#content');
27 if (content.hasClass('opacity03')) {
28 content.removeClass('opacity03');
19f2f11e
NL
29 }
30 });
31
56349e47
TC
32 /* ==========================================================================
33 Add tag panel
34 ========================================================================== */
35
c146f694 36 $('#nav-btn-add-tag').on('click', () => {
64f81bc3 37 $('.baggy-add-tag').toggle(100);
0743287f
TC
38 $('.nav-panel-menu').addClass('hidden');
39 $('#tag_label').focus();
40 return false;
41 });
56349e47 42
c146f694
TC
43 /**
44 * Filters & Export
45 */
46 // no display if filters not available
47 if ($('div').is('#filters')) {
48 $('#button_filters').show();
49 $('#clear_form_filters').on('click', () => {
50 $('#filters input').val('');
51 $('#filters :checked').removeAttr('checked');
52 return false;
53 });
54 }
55
5fe9aadc
TC
56 /* ==========================================================================
57 Annotations & Remember position
58 ========================================================================== */
59
60 if ($('article').length) {
61 const app = new annotator.App();
62
63 app.include(annotator.ui.main, {
64 element: document.querySelector('article'),
65 });
66
67 const x = JSON.parse($('#annotationroutes').html());
68 app.include(annotator.storage.http, x);
69
70 app.start().then(() => {
71 app.annotations.load({ entry: x.entryId });
72 });
73
74 window.addEventListener('unload', () => {
75 const scrollTop = $(window).scrollTop();
76 const docHeight = $(document).height();
77 const scrollPercent = (scrollTop) / (docHeight);
78 const scrollPercentRounded = Math.round(scrollPercent * 100) / 100;
79 savePercent(x.entryId, scrollPercentRounded);
80 });
81
82 retrievePercent(x.entryId);
83
84 $(window).resize(() => {
85 retrievePercent(x.entryId);
86 });
87 }
88
c146f694
TC
89 /**
90 * Close window after adding entry if popup
91 */
92 const currentUrl = window.location.href;
93 if (currentUrl.match('&closewin=true')) {
94 window.close();
95 }
96
64f81bc3
TC
97 /**
98 if ($('article').size() > 0) {
99 const waypoint = new Waypoint({
100 element: $('.wallabag-title').get(0),
101 handler: (direction) => {
102 console.log(direction);
103 if (direction === 'down') {
104 $('aside.tags').fadeIn('slow');
105 } else {
106 $('aside.tags').fadeOut('slow');
107 }
108 },
109 offset: 250,
110 });
111 }
112 */
113
c146f694
TC
114 /**
115 * Tags autocomplete
116 */
8f234d01
TC
117 /**
118 * Not working on v2
119 *
120
c146f694
TC
121 $('#value').bind('keydown', (event) => {
122 if (event.keyCode === $.ui.keyCode.TAB && $(this).data('ui-autocomplete').menu.active) {
123 event.preventDefault();
124 }
125 }).autocomplete({
126 source: function source(request, response) {
127 $.getJSON('./?view=tags', {
128 term: extractLast(request.term),
129 //id: $(':hidden#entry_id').val()
130 }, response);
131 },
132 search: function search() {
133 // custom minLength
134 const term = extractLast(this.value);
135 return term.length >= 1;
136 },
137 focus: function focus() {
138 // prevent value inserted on focus
139 return false;
140 },
141 select: function select(event, ui) {
142 const terms = split(this.value);
143 // remove the current input
144 terms.pop();
145 // add the selected item
146 terms.push(ui.item.value);
147 // add placeholder to get the comma-and-space at the end
148 terms.push('');
149 this.value = terms.join(', ');
150 return false;
151 },
152 });
8f234d01 153 */
c146f694
TC
154
155 //---------------------------------------------------------------------------
156 // Close the message box when the user clicks the close icon
157 //---------------------------------------------------------------------------
158 $('a.closeMessage').on('click', () => {
159 $(this).parents('div.messages').slideUp(300, () => { $(this).remove(); });
160 return false;
161 });
162
163 $('#search-form').hide();
164 $('#bagit-form').hide();
165 $('#filters').hide();
166 $('#download-form').hide();
5ecdfcd0 167
c146f694
TC
168 //---------------------------------------------------------------------------
169 // Toggle the 'Search' popup in the sidebar
170 //---------------------------------------------------------------------------
171 function toggleSearch() {
172 $('#search-form').toggle();
173 $('#search').toggleClass('current');
174 $('#search').toggleClass('active-current');
175 $('#search-arrow').toggleClass('arrow-down');
176 if ($('#search').hasClass('current')) {
177 $('#content').addClass('opacity03');
178 } else {
179 $('#content').removeClass('opacity03');
180 }
181 }
182
183 //---------------------------------------------------------------------------
184 // Toggle the 'Filter' popup on entries list
185 //---------------------------------------------------------------------------
186 function toggleFilter() {
187 $('#filters').toggle();
188 }
189
190 //---------------------------------------------------------------------------
191 // Toggle the 'Download' popup on entries list
192 //---------------------------------------------------------------------------
193 function toggleDownload() {
194 $('#download-form').toggle();
195 }
196
197 //---------------------------------------------------------------------------
198 // Toggle the 'Save a Link' popup in the sidebar
199 //---------------------------------------------------------------------------
200 function toggleBagit() {
201 $('#bagit-form').toggle();
202 $('#bagit').toggleClass('current');
203 $('#bagit').toggleClass('active-current');
204 $('#bagit-arrow').toggleClass('arrow-down');
205 if ($('#bagit').hasClass('current')) {
206 $('#content').addClass('opacity03');
207 } else {
208 $('#content').removeClass('opacity03');
5ecdfcd0 209 }
c146f694
TC
210 }
211
212 //---------------------------------------------------------------------------
213 // Close all #links popups in the sidebar
214 //---------------------------------------------------------------------------
215 function closePopups() {
216 $('#links .messages').hide();
217 $('#links > li > a').removeClass('active-current');
218 $('#links > li > a').removeClass('current');
219 $('[id$=-arrow]').removeClass('arrow-down');
220 $('#content').removeClass('opacity03');
221 }
222
223 $('#search').click(() => {
224 closePopups();
225 toggleSearch();
226 $('#searchfield').focus();
227 });
228
229 $('.filter-btn').click(() => {
230 closePopups();
231 toggleFilter();
232 });
233
234 $('.download-btn').click(() => {
235 closePopups();
236 toggleDownload();
237 });
238
239 $('#bagit').click(() => {
240 closePopups();
241 toggleBagit();
242 $('#plainurl').focus();
243 });
244
245 $('#search-form-close').click(() => {
246 toggleSearch();
247 });
248
249 $('#filter-form-close').click(() => {
250 toggleFilter();
251 });
252
253 $('#download-form-close').click(() => {
254 toggleDownload();
255 });
256
257 $('#bagit-form-close').click(() => {
258 toggleBagit();
259 });
260
64f81bc3 261 const bagitFormForm = $('#bagit-form-form');
c146f694
TC
262
263 /* ==========================================================================
264 bag it link and close button
265 ========================================================================== */
266
267 // send 'bag it link' form request via ajax
64f81bc3 268 bagitFormForm.submit((event) => {
c146f694
TC
269 $('body').css('cursor', 'wait');
270 $('#add-link-result').empty();
271
272 $.ajax({
64f81bc3
TC
273 type: bagitFormForm.attr('method'),
274 url: bagitFormForm.attr('action'),
275 data: bagitFormForm.serialize(),
c146f694
TC
276 success: function success() {
277 $('#add-link-result').html('Done!');
64f81bc3 278 $('#plainurl').val('').blur('');
c146f694
TC
279 $('body').css('cursor', 'auto');
280 },
281 error: function error() {
282 $('#add-link-result').html('Failed!');
283 $('body').css('cursor', 'auto');
284 },
285 });
286
287 event.preventDefault();
288 });
289
290 /* ==========================================================================
291 Process all links inside an article
292 ========================================================================== */
293
294 $('article a[href^="http"]').after(
f99ebec0 295 () => `<a href="${$(this).attr('href')}" class="add-to-wallabag-link-after" title="add to wallabag"></a>`,
c146f694
TC
296 );
297
298 $('.add-to-wallabag-link-after').click((event) => {
299 toggleSaveLinkForm($(this).attr('href'), event);
300 event.preventDefault();
301 });
19f2f11e 302});