-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogEpisodeLibrary.js
225 lines (191 loc) · 8.93 KB
/
progEpisodeLibrary.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const RATIO_16BY9 = 9 / 16;
var currentPreviewPlayer;
var currentPreviewPlayerElement = null;
var selectedItem = null;
function showAddOptions() {
if (currentPreviewPlayerElement != null) {
videojs(currentPreviewPlayerElement).dispose();
}
selectedItem = null;
$(".previewTitle").text("Add item");
$(".previewContent").html("").append([
$("<label>").append(
$("<input id='addItemFromSourceUrl' placeholder='Enter a source URL here'>")
),
$("<div class='right'>").append(
$("<button id='addItemFromSourceButton'>")
.text("Add item from source")
.click(function() {
if (/^(http|https):\/\/.*\/(.*)$/.test($("#addItemFromSourceUrl").val().trim())) {
$(this).prop("disabled", true);
var newItemRef = firebase.database().ref(episodePath + "/content/library").push();
newItemRef.set({
url: $("#addItemFromSourceUrl").val().trim(),
slug: ($("#addItemFromSourceUrl").val().trim().match(/^(http|https):\/\/.*\/(.*)$/)[2].split(".")[0] || "Untitled").trim()
}).then(function() {
showItemPreview(newItemRef.key);
});
} else {
$("#addItemFromSourceError").text("It seems that the URL you entered is invalid. Please enter a valid URL.");
}
})
),
$("<p id='addItemFromSourceError'>")
]);
$(".libraryItem").removeClass("selected");
}
function showItemPreview(itemKey) {
if (currentPreviewPlayerElement != null) {
videojs(currentPreviewPlayerElement).dispose();
}
selectedItem = itemKey;
$(".previewTitle").html("").append([
$("<a>")
.html("<i aria-hidden='true' class='material-icons'>arrow_left</i> Back")
.attr("href", "javascript:showAddOptions();")
,
$(document.createTextNode("Item preview"))
]);
$(".previewContent").text("Loading...");
$(".libraryItem").removeClass("selected");
$(".libraryItem[data-key='" + itemKey + "']").addClass("selected");
firebase.database().ref(episodePath + "/content/library/" + itemKey).once("value", function(snapshot) {
var itemAttributes = [
{key: "slug", label: "Slug", mandatory: true},
{key: "url", label: "Source URL", mandatory: true},
{key: "id", label: "Identifier", placeholder: "(Unassigned)"},
{key: "notes", label: "Notes", type: "textarea"}
];
$(".previewContent").html("").append($("<div class='previewArea spacedBottom'>"));
for (var i = 0; i < itemAttributes.length; i++) {
(function(itemAttribute) {
$(".previewContent").append(
$("<label>").append([
$("<span>").text(itemAttribute.label),
$(itemAttribute.type == "textarea" ? "<textarea>" : "<input>")
.attr("placeholder", itemAttribute.placeholder)
.val(snapshot.val()[itemAttribute.key])
.change(function(event) {
if (!itemAttribute.mandatory || $(event.target).val().trim() != "") {
firebase.database().ref(episodePath + "/content/library/" + itemKey + "/" + itemAttribute.key).set(
$(event.target).val()
);
if (itemAttribute.key == "slug") {
$(".libraryItem[data-key='" + itemKey + "'] .libraryItemSlug").text($(event.target).val());
}
$(".libraryItem[data-key='" + itemKey + "']").addClass("selected");
}
})
])
);
})(itemAttributes[i]);
}
$(".previewContent").append([
$("<hr>"),
$("<button>")
.text("Open")
.click(function() {
window.open(snapshot.val().url);
})
,
" ",
$("<button>")
.text("Delete")
.click(function() {
dialog("Delete item", `
Do you really want to delete this item? The item's data
will be permanently lost, but the contents of the item
may still be available at the target URL.
`, [
{text: "No", onclick: "closeDialog();", type: "secondary"},
{text: "Yes", onclick: "deleteSelectedItem();", type: "primary"}
]);
})
]);
if (snapshot.val().url.endsWith(".mp4")) {
$(".previewArea").append(
$("<video controls width='300' height='200' class='video-js' id='previewPlayer'>")
.on("error", function() {
$(".previewArea").html("").append(
$("<p class='center'>")
.text("The contents of this item don't seem to exist. Check the URL to see if it is correct.")
);
})
);
currentPreviewPlayerElement = $("#previewPlayer")[0];
videojs("previewPlayer", {}, function() {
currentPreviewPlayer = this;
currentPreviewPlayer.src(snapshot.val().url);
currentPreviewPlayer.width($(".previewArea").width());
currentPreviewPlayer.height($(".previewArea").width() * RATIO_16BY9);
});
} else if (snapshot.val().url.endsWith(".png") || snapshot.val().url.endsWith(".jpg") || snapshot.val().url.endsWith(".jpeg") || snapshot.val().url.endsWith(".gif")) {
$(".previewArea").append(
$("<img class='preview'>")
.attr("src", snapshot.val().url)
.attr("alt", "Image preview")
.on("error", function() {
$(".previewArea").html("").append(
$("<p class='center'>")
.text("The contents of this item don't seem to exist. Check the URL to see if it is correct.")
);
})
);
}
});
}
function deleteSelectedItem() {
firebase.database().ref(episodePath + "/content/library/" + selectedItem).remove();
closeDialog();
showAddOptions();
}
events.userReady.push(function() {
firebase.database().ref(episodePath + "/content/library").on("value", function(snapshot) {
$(".library").html("");
snapshot.forEach(function(childSnapshot) {
var thumbnail;
if (childSnapshot.val().url.endsWith(".mp4")) {
thumbnail = $("<video class='libraryItemThumbnail'>")
.attr("aria-label", "Video")
.append(
$("<source>").attr("src", childSnapshot.val().url + "#t=5")
)
;
} else if (childSnapshot.val().url.endsWith(".png") || childSnapshot.val().url.endsWith(".jpg") || childSnapshot.val().url.endsWith(".jpeg") || childSnapshot.val().url.endsWith(".gif")) {
thumbnail = $("<img class='libraryItemThumbnail'>")
.attr("alt", "Photo")
.attr("src", childSnapshot.val().url)
.on("error", function() {
this.onerror = null;
this.src = "https://imcnetwork.cf/LiveCloud/media/Blank App.png";
})
;
} else {
thumbnail = $("<img class='libraryItemThumbnail'>")
.attr("alt", "Unknown item")
.attr("src", "https://imcnetwork.cf/LiveCloud/media/Blank%20App.png")
;
}
$(".library").append(
$("<button class='libraryItem'>")
.attr("data-key", childSnapshot.key)
.append([
thumbnail,
$("<span class='libraryItemSlug'>").text(childSnapshot.val().slug)
])
.click(function(event) {
showItemPreview(childSnapshot.key);
$(".rightPane a:first-of-type").focus();
event.stopPropagation();
})
);
});
});
showAddOptions();
});
$(window).resize(function() {
if (currentPreviewPlayer) {
currentPreviewPlayer.width($(".previewArea").width());
currentPreviewPlayer.height($(".previewArea").width() * RATIO_16BY9);
}
});