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