root/src/public/js/MissingFilesPage.js

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
function MissingFilesPage() {
}

MissingFilesPage.prototype.createNodes = function() {
    var self = this;
    this.page = document.createElement("div");
    this.page.innerHTML = "<h1>Missing Files</h1>";

    this.content = document.createElement("div");
    this.page.appendChild(this.content);

    this.loadList(function() {

    });
    return this.page;
}

MissingFilesPage.prototype.removeNodes = function() {
    $(this.page).remove();
}

MissingFilesPage.prototype.groupData = function(data) {
    // TODO group by directory
    return data.missingFiles.reduce(function(acc, file) {
        acc[file.showName] = acc[file.showName] || [];
        acc[file.showName].push(file);
        return acc;
    }, {});
}

MissingFilesPage.prototype.loadList = function(callback) {
    var self = this;
    $.getJSON("/api/library/filter/missingFiles", function(data) {
        var grouped = self.groupData(data);

        $(self.content).empty();
        for (var groupKey in grouped) {
            var groupEl = document.createElement("ol");
            groupEl.textContent = groupKey;
            var groupArray = grouped[groupKey];

            var nodes = groupArray.map(function(file) {
                var el = document.createElement("li");
                el.textContent = file.path;
                return el;
            });
            var deleteButton = self.createDeleteButton(groupArray);
            $(groupEl).append(deleteButton);
            $(groupEl).append(nodes);
            self.content.appendChild(groupEl);
        }
        callback(grouped);
    });
}

MissingFilesPage.prototype.createDeleteButton = function(groupArray) {
    var button = document.createElement("button");
    button.textContent = "remove";
    $(button).click(function() {
        var paths = groupArray.map(function(file) {
            return file.path;
        });
        $.ajax({
            url: "/api/library/removeFiles/",
            type: "DELETE",
            data: JSON.stringify({paths: paths})
        }).done(function() {

        });;
    });
    return button;
}