aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/playlist.js
blob: 4739efc9b310f31685805393bc1ff62d053b6936 (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
import { load as load_template } from './templates';
import Mustache from 'mustache';

import { mediaplayer } from 'agl-js-api';

var template;
var root;
var currentTrackIndex;
var page = {
    playlist: {},
    isPlaying: function() {
        return this.selected;
    }
};

function load() {
    mediaplayer.playlist().then(function(playlist){
        page.playlist = playlist.list;
        show();
    });
}

export function show() {
    root.innerHTML = Mustache.render(template, page);
}


export function init(node) {

    mediaplayer.on_playlist_changes(function(playlist){
        page.playlist = playlist.list;
        show();
    });

    mediaplayer.on_metadata_changes(function(metadata){
        if( metadata && metadata.track && metadata.track.index !== currentTrackIndex ) {
            currentTrackIndex = metadata.track.index;
            load();
        } 
    });
    load_template('playlist.template.html').then(function(result) {
        template = result;
        root = node;
        Mustache.parse(template);
        show();
    }, function(error) {
        console.error('ERRROR loading main template', error);
    });
}