Passed
Push — master ( 998e10...1efe83 )
by Marcel
06:10
created

js/sidebar.js (49 issues)

1
/**
2
 * Audio Player
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the LICENSE.md file.
6
 *
7
 * @author Marcel Scherello <[email protected]>
8
 * @copyright 2016-2019 Marcel Scherello
9
 */
10
11
'use strict';
0 ignored issues
show
Use the function form of "use strict".
Loading history...
12
13
if (!OCA.Audioplayer) {
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
14
    /**
15
     * @namespace
16
     */
17
    OCA.Audioplayer = {};
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
18
}
19
if (!OCA.Audioplayer.Sidebar) {
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
20
    /**
21
     * @namespace
22
     */
23
    OCA.Audioplayer.Sidebar = {};
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
24
}
25
26
/**
27
 * @namespace OCA.Audioplayer.Sidebar
28
 */
29
OCA.Audioplayer.Sidebar = {
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
30
    sidebar_tabs: {},
31
32
    showSidebar: function (evt) {
33
34
        var trackid = $(evt.target).closest('li').attr('data-trackid');
0 ignored issues
show
$ does not seem to be defined.
Loading history...
35
        var $appsidebar = $('#app-sidebar');
0 ignored issues
show
$ does not seem to be defined.
Loading history...
36
37
        if ($appsidebar.data('trackid') === trackid) {
38
            OCA.Audioplayer.Sidebar.hideSidebar();
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
39
        } else {
40
            var getcoverUrl = OC.generateUrl('apps/audioplayer/getcover/');
41
            var trackData = $('li[data-trackid=\'' + trackid + '\']');
0 ignored issues
show
$ does not seem to be defined.
Loading history...
42
            var cover = trackData.attr('data-cover');
43
            var sidebarThumbnail = $('#sidebarThumbnail');
0 ignored issues
show
$ does not seem to be defined.
Loading history...
44
            $('.thumbnailContainer').addClass('portrait large');
0 ignored issues
show
$ does not seem to be defined.
Loading history...
45
46
            if (cover !== '') {
47
                sidebarThumbnail.attr({
48
                    'style': 'background-image:url(' + getcoverUrl + cover + ')'
49
                });
50
            } else {
51
                sidebarThumbnail.attr({
52
                    'style': 'display: none;'
53
                });
54
            }
55
56
            $('#sidebarTitle').html(decodeURIComponent(trackData.attr('data-path')));
0 ignored issues
show
$ does not seem to be defined.
Loading history...
57
            $('#sidebarMime').html(trackData.attr('data-mimetype'));
0 ignored issues
show
$ does not seem to be defined.
Loading history...
58
59
            var starIcon = $('#sidebarFavorite').attr({'data-trackid': trackid});
0 ignored issues
show
$ does not seem to be defined.
Loading history...
60
            starIcon.off();
61
            starIcon.on('click',
62
                OCA.Audioplayer.audiosInstance.favoriteUpdate.bind(OCA.Audioplayer.audiosInstance)
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
63
            );
64
65
            if ($appsidebar.data('trackid') === '') {
66
                $('#sidebarClose').on('click', OCA.Audioplayer.Sidebar.hideSidebar);
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
$ does not seem to be defined.
Loading history...
67
68
                OCA.Audioplayer.Sidebar.constructTabs();
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
69
                $('#tabHeaderMetadata').addClass('selected');
0 ignored issues
show
$ does not seem to be defined.
Loading history...
70
                OC.Apps.showAppSidebar();
71
            }
72
73
            $appsidebar.data('trackid', trackid);
74
            document.getElementById('app-sidebar').dataset.trackid = trackid; //start moving to vanilla js
75
            $('.tabHeader.selected').trigger('click');
0 ignored issues
show
$ does not seem to be defined.
Loading history...
76
        }
77
    },
78
79
    registerSidebarTab: function (tab) {
80
        var id = tab.id;
81
        this.sidebar_tabs[id] = tab;
82
    },
83
84
    constructTabs: function () {
85
        var tab = {};
86
87
        $('.tabHeaders').empty();
0 ignored issues
show
$ does not seem to be defined.
Loading history...
88
        $('.tabsContainer').empty();
0 ignored issues
show
$ does not seem to be defined.
Loading history...
89
90
        OCA.Audioplayer.Sidebar.registerSidebarTab({
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
91
            id: 'tabHeaderAddons',
92
            class: 'addonsTabView',
93
            tabindex: '9',
94
            name: t('audioplayer', 'Add-ons'),
95
            action: OCA.Audioplayer.Sidebar.addonsTabView,
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
96
        });
97
98
        OCA.Audioplayer.Sidebar.registerSidebarTab({
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
99
            id: 'tabHeaderMetadata',
100
            class: 'metadataTabView',
101
            tabindex: '1',
102
            name: t('audioplayer', 'Metadata'),
103
            action: OCA.Audioplayer.Sidebar.metadataTabView,
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
104
        });
105
106
        OCA.Audioplayer.Sidebar.registerSidebarTab({
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
107
            id: 'tabHeaderPlaylists',
108
            class: 'playlistsTabView',
109
            tabindex: '2',
110
            name: t('audioplayer', 'Playlists'),
111
            action: OCA.Audioplayer.Sidebar.playlistsTabView,
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
112
        });
113
114
        var items = _.map(OCA.Audioplayer.Sidebar.sidebar_tabs, function (item) {
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
115
            return item;
116
        });
117
        items.sort(OCA.Audioplayer.Sidebar.sortByName);
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
118
119
        for (tab in items) {
120
            var li = $('<li/>').addClass('tabHeader')
0 ignored issues
show
$ does not seem to be defined.
Loading history...
121
                .attr({
122
                    'id': items[tab].id,
123
                    'tabindex': items[tab].tabindex
124
                });
125
            var atag = $('<a/>').text(items[tab].name);
0 ignored issues
show
$ does not seem to be defined.
Loading history...
126
            li.append(atag);
127
            $('.tabHeaders').append(li);
0 ignored issues
show
$ does not seem to be defined.
Loading history...
128
129
            var div = $('<div/>').addClass('tab ' + items[tab].class)
0 ignored issues
show
$ does not seem to be defined.
Loading history...
130
                .attr({
131
                    'id': items[tab].class
132
                });
133
            $('.tabsContainer').append(div);
0 ignored issues
show
$ does not seem to be defined.
Loading history...
134
            $('#' + items[tab].id).on('click', items[tab].action);
0 ignored issues
show
$ does not seem to be defined.
Loading history...
135
        }
136
    },
137
138
    hideSidebar: function () {
139
        $('#app-sidebar').data('trackid', '');
0 ignored issues
show
$ does not seem to be defined.
Loading history...
140
        OC.Apps.hideAppSidebar();
141
        $('.tabHeaders').empty();
0 ignored issues
show
$ does not seem to be defined.
Loading history...
142
        $('.tabsContainer').empty();
0 ignored issues
show
$ does not seem to be defined.
Loading history...
143
    },
144
145
    metadataTabView: function () {
146
        var trackid = $('#app-sidebar').data('trackid');
0 ignored issues
show
$ does not seem to be defined.
Loading history...
147
148
        OCA.Audioplayer.Sidebar.resetView();
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
149
        $('#tabHeaderMetadata').addClass('selected');
0 ignored issues
show
$ does not seem to be defined.
Loading history...
There were too many errors found in this file; checking aborted after 43%.

If JSHint finds too many errors in a file, it aborts checking altogether because it suspects a configuration issue.

Further Reading:

Loading history...
150
        $('#metadataTabView').removeClass('hidden').html('<div style="text-align:center; word-wrap:break-word;" class="get-metadata"><p><img src="' + OC.imagePath('core', 'loading.gif') + '"><br><br></p><p>' + t('audioplayer', 'Reading data') + '</p></div>');
151
152
        $.ajax({
153
            type: 'GET',
154
            url: OC.generateUrl('apps/audioplayer/getaudioinfo'),
155
            data: {trackid: trackid},
156
            success: function (jsondata) {
157
                var table;
158
                if (jsondata.status === 'success') {
159
160
                    table = $('<div>').css('display', 'table').addClass('table');
161
                    var tablerow;
162
                    var m;
163
                    var tablekey;
164
                    var tablevalue;
165
166
                    var audioinfo = jsondata.data;
167
                    for (m in audioinfo) {
168
                        tablerow = $('<div>').css('display', 'table-row');
169
                        tablekey = $('<div>').addClass('key').text(t('audioplayer', m));
170
                        tablevalue = $('<div>').addClass('value')
171
                            .text(audioinfo[m]);
172
                        tablerow.append(tablekey).append(tablevalue);
173
174
                        if (m === 'fav' && audioinfo[m] === 't') {
175
                            $('#sidebarFavorite').removeClass('icon-star')
176
                                .addClass('icon-starred')
177
                                .prop('title', t('files', 'Favorited'));
178
                            audioinfo[m] = '';
179
                        } else if (m === 'fav') {
180
                            $('#sidebarFavorite').removeClass('icon-starred')
181
                                .addClass('icon-star')
182
                                .prop('title', t('files', 'Favorite'));
183
                            audioinfo[m] = '';
184
                        }
185
186
                        if (audioinfo[m] !== '' && audioinfo[m] !== null) {
187
                            table.append(tablerow);
188
                        }
189
                    }
190
                } else {
191
                    table = '<div style="margin-left: 2em;" class="get-metadata"><p>' + t('audioplayer', 'No data') + '</p></div>';
192
                }
193
194
                $('#metadataTabView').html(table);
195
            }
196
        });
197
    },
198
199
    playlistsTabView: function () {
200
        var trackid = $('#app-sidebar').data('trackid');
201
202
        OCA.Audioplayer.Sidebar.resetView();
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
203
        $('#tabHeaderPlaylists').addClass('selected');
204
        $('#playlistsTabView').removeClass('hidden').html('<div style="text-align:center; word-wrap:break-word;" class="get-metadata"><p><img src="' + OC.imagePath('core', 'loading.gif') + '"><br><br></p><p>' + t('audioplayer', 'Reading data') + '</p></div>');
205
206
        $.ajax({
207
            type: 'POST',
208
            url: OC.generateUrl('apps/audioplayer/getplaylists'),
209
            data: {trackid: trackid},
210
            success: function (jsondata) {
211
                var table;
212
                if (jsondata.status === 'success') {
213
214
                    table = $('<div>').css('display', 'table').addClass('table');
215
                    var tablerow;
216
                    var m;
217
                    var tablekey;
218
                    var tablevalue;
219
220
                    var audioinfo = jsondata.data;
221
                    for (m in audioinfo) {
222
                        var spanDelete = $('<a/>').attr({
223
                            'class': 'icon icon-delete toolTip',
224
                            'data-listid': audioinfo[m].playlist_id,
225
                            'data-trackid': trackid,
226
                            'title': t('audioplayer', 'Remove')
227
                        }).on('click', OCA.Audioplayer.audiosInstance.removeSongFromPlaylist.bind(OCA.Audioplayer.audiosInstance));
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
228
229
                        tablerow = $('<div>').css('display', 'table-row').attr({'data-id': audioinfo[m].playlist_id});
230
                        tablekey = $('<div>').addClass('key').append(spanDelete);
231
232
                        tablevalue = $('<div>').addClass('value')
233
                            .text(audioinfo[m].name);
234
                        tablerow.append(tablekey).append(tablevalue);
235
                        table.append(tablerow);
236
                    }
237
                } else {
238
                    table = '<div style="margin-left: 2em;" class="get-metadata"><p>' + t('audioplayer', 'No playlist entry') + '</p></div>';
239
                }
240
241
                $('#playlistsTabView').html(table);
242
            }
243
        });
244
245
    },
246
247
    addonsTabView: function () {
248
        OCA.Audioplayer.Sidebar.resetView();
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
249
        $('#tabHeaderAddons').addClass('selected');
250
        var html = '<div style="margin-left: 2em; background-position: initial;" class="icon-info">';
251
        html += '<p style="margin-left: 2em;">' + t('audioplayer', 'Available Audio Player Add-Ons:') + '</p>';
252
        html += '<p style="margin-left: 2em;"><br></p>';
253
        html += '<a href="https://github.com/rello/audioplayer_editor"  target="_blank" >';
254
        html += '<p style="margin-left: 2em;">- ' + t('audioplayer', 'ID3 editor') + '</p>';
255
        html += '</a>';
256
        html += '<a href="https://github.com/rello/audioplayer_sonos"  target="_blank" >';
257
        html += '<p style="margin-left: 2em;">- ' + t('audioplayer', 'SONOS playback') + '</p>';
258
        html += '</a></div>';
259
        $('#addonsTabView').removeClass('hidden').html(html);
260
    },
261
262
    resetView: function () {
263
        $('.tabHeader.selected').removeClass('selected');
264
        $('.tab').addClass('hidden');
265
    },
266
267
    removeSongFromPlaylist: function (evt) {
268
269
        var trackid = $(evt.target).attr('data-trackid');
270
        var playlist = $(evt.target).attr('data-listid');
271
272
        $.ajax({
273
            type: 'POST',
274
            url: OC.generateUrl('apps/audioplayer/removetrackfromplaylist'),
275
            data: {
276
                'playlistid': playlist,
277
                'songid': trackid
278
            },
279
            success: function (jsondata) {
280
                if (jsondata === true) {
281
                    var currentCount = $('#myCategory li[data-id="' + playlist + '"]').find('.counter');
282
                    currentCount.text(currentCount.text() - 1);
283
                    $('#playlistsTabView div[data-id="' + playlist + '"]').remove();
284
                }
285
            }
286
        });
287
    },
288
289
    sortByName: function (a, b) {
290
        var aName = a.tabindex;
291
        var bName = b.tabindex;
292
        return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
293
    },
294
295
    registerSONOSTab: function () {
296
        var li = $('<li/>').addClass('tabHeader')
297
            .attr({
298
                'id': 'tabHeaderSONOS',
299
                'data-tabid': '4',
300
                'data-tabindex': '4'
301
            });
302
        var atag = $('<a/>').text(t('audioplayer', 'SONOS'));
303
        li.append(atag);
304
        $('.tabHeaders').append(li);
305
306
        var div = $('<div/>').addClass('tab SONOSTabView')
307
            .attr({
308
                'id': 'SONOSTabView'
309
            });
310
        $('.tabsContainer').append(div);
311
312
        $('#tabHeaderSONOS').on('click', OCA.Audioplayer.Sidebar.SONOSTabView);
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
313
    },
314
    SONOSTabView: function () {
315
        var trackid = $('#app-sidebar').data('trackid');
316
        OCA.Audioplayer.audiosInstance.resetView();
0 ignored issues
show
OCA does not seem to be defined.
Loading history...
317
        $('#tabHeaderSONOS').addClass('selected');
318
319
        var html = '<div style="margin-left: 2em; background-position: initial;" class="icon-info">';
320
        html += '<p style="margin-left: 2em;">' + t('audioplayer', 'Details for error analysis') + '</p>';
321
        html += '<br>';
322
        html += '</div>';
323
        $('#SONOSTabView').removeClass('hidden').html(html);
324
325
        $.ajax({
326
            type: 'POST',
327
            url: OC.generateUrl('apps/audioplayer/sonosdebug'),
328
            data: {'trackid': trackid},
329
            success: function (jsondata) {
330
                html = $('#SONOSTabView').html();
331
                html += '<p style="margin-left: 2em;">' + t('audioplayer', 'SMB link from user settings:') + '</p>';
332
                html += '<p style="margin-left: 2em;">' + jsondata.smb + '</p>';
333
                html += '<br>';
334
                html += '<p style="margin-left: 2em;">' + t('audioplayer', 'Combined link for your SONOS controller:') + '</p>';
335
                html += '<p style="margin-left: 2em;">' + jsondata.sonos + '</p>';
336
                $('#SONOSTabView').html(html);
337
            }
338
        });
339
    },
340
341
};
342