Files
fnx_web/res/static/script/ListNavigator.js

230 lines
6.2 KiB
JavaScript
Raw Normal View History

2017-11-10 12:39:55 +01:00
/* global Viewer */
var ListNavigator = {
length: 0,
position: 0,
data: [],
history: [],
shuffle: false,
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
nextItem: function(){
if(!Viewer.isList){
return;
}
if(this.shuffle){
this.randItem();
return;
}
if(this.position >= this.length){
this.position = 0;
}else{
this.position++;
}
this.setItem(this.position);
},
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
previousItem: function(){
if(!Viewer.isList){
return;
}
if(this.position === 0){
this.position = this.length - 1;
}else{
this.position--;
}
this.setItem(this.position);
},
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
randItem: function(){
if(!Viewer.isList){
return;
}
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
// Avoid viewing the same file multiple times
var rand;
do {
rand = Math.round(Math.random() * this.length);
console.log("rand is " + rand);
} while(this.inHistory(rand));
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
this.setItem(rand);
},
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
setItem: function(index){
if(index >= this.length){
this.position = 0;
}else{
this.position = index;
}
2018-09-11 21:35:44 +02:00
// Set the URL hash
2017-11-10 12:39:55 +01:00
location.hash = "item=" + this.position;
Viewer.setFile(this.data[this.position]);
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
this.addToHistory(index);
2019-07-18 19:53:09 +02:00
$("#list_navigator").find("*").removeClass("file_button_selected");
var selectedItem = $("#list_navigator div").eq(this.position);
selectedItem.addClass("file_button_selected");
var itemWidth = selectedItem.outerWidth(true);
2017-11-10 12:39:55 +01:00
// This centers the scroll bar exactly on the selected item
2019-07-18 19:53:09 +02:00
$("#list_navigator").animate(
{scrollLeft: ((this.position * itemWidth) + (itemWidth / 2)) - ($("#list_navigator").width() / 2)},
{duration: 1000, queue: false}
2017-11-10 12:39:55 +01:00
);
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
this.loadThumbnails(index);
},
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
addToHistory: function(index){
if(this.history.length >= (this.length - 6)){
this.history.shift();
}
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
this.history.push(index);
},
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
inHistory: function(index){
var i = $.inArray(index, this.history); // Returns -1 when the item is not found
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
return (i !== -1); // Return false when it's not in the array
},
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
toggleShuffle: function(){
this.shuffle = !this.shuffle; // :P
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
if(this.shuffle){
$("#btnShuffle > span").html(" Shuffle ☑"); // Check icon
2018-01-07 21:42:19 +01:00
$("#btnShuffle").addClass("button_highlight");
2017-11-10 12:39:55 +01:00
}else{
$("#btnShuffle > span").html(" Shuffle ☐"); // Empty checkbox
2018-01-07 21:42:19 +01:00
$("#btnShuffle").removeClass("button_highlight");
2017-11-10 12:39:55 +01:00
}
},
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
loadThumbnails: function(index){
2019-07-17 23:19:57 +02:00
var startPos = +index - 50;
var endPos = +index + 50;
2017-11-10 12:39:55 +01:00
// fyi, the + is to let javascript know it's actually a number instead of a string
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
if(startPos < 0){
startPos = 0;
}
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
if(endPos >= this.length){
endPos = this.length - 1;
}
console.log(endPos);
2018-09-11 21:35:44 +02:00
2019-07-18 19:53:09 +02:00
var navigatorItems = document.getElementById("list_navigator").children
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
for (i = startPos; i <= endPos; i++){
2019-07-18 19:53:09 +02:00
if (navigatorItems[i].innerHTML.includes("list_item_thumbnail")) {
continue; // Thumbnail already loaded
2019-07-17 23:19:57 +02:00
}
2019-12-10 14:47:11 +01:00
var thumb = "/api/file/" + this.data[i].id + "/thumbnail?width=48&height=48";
2018-10-04 23:36:34 +02:00
var name = this.data[i].name;
2018-09-11 21:35:44 +02:00
2019-07-18 19:53:09 +02:00
var itemHtml = "<img src=\"" + thumb + "\" "
+ "class=\"list_item_thumbnail\" alt=\"" + escapeHTML(name) + "\"/>"
+ escapeHTML(name);
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
navigatorItems[i].innerHTML = itemHtml;
}
},
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
init: function(data){
this.data = data;
this.length = data.length;
2018-09-11 21:35:44 +02:00
2019-02-20 22:46:35 +01:00
var listHTML = "";
data.forEach(function(item, i){
2017-11-10 12:39:55 +01:00
var filename;
2018-10-04 23:36:34 +02:00
if(item.name !== "null"){
filename = item.name;
2017-11-10 12:39:55 +01:00
}else{
filename = "Removed File";
}
2019-07-18 19:53:09 +02:00
listHTML += "<div class=\"file_button list_item\" "
2017-11-10 12:39:55 +01:00
+ "onClick=\"ListNavigator.setItem('" + i + "')\">"
+ escapeHTML(filename) + "<br>"
2017-11-10 12:39:55 +01:00
+ "</div>";
});
2019-07-18 19:53:09 +02:00
document.getElementById("list_navigator").innerHTML = listHTML;
2018-09-11 21:35:44 +02:00
2019-07-17 23:19:57 +02:00
var btnLastItem = document.createElement("button");
btnLastItem.innerText = "◀";
btnLastItem.setAttribute("id", "button_last_item");
btnLastItem.setAttribute("class", "button_highlight");
btnLastItem.setAttribute("onClick", "ListNavigator.previousItem();");
var btnNextItem = document.createElement("button");
btnNextItem.innerText = "▶";
btnNextItem.setAttribute("id", "button_next_item");
btnNextItem.setAttribute("class", "button_highlight");
btnNextItem.setAttribute("onClick", "ListNavigator.nextItem();");
var headerbar = document.getElementById("list_navigator_buttons");
headerbar.appendChild(btnLastItem);
headerbar.appendChild(btnNextItem);
2017-11-10 12:39:55 +01:00
// Add the list download button to the toolbar
2018-01-11 23:12:35 +01:00
var btnDownloadList = document.createElement("button");
btnDownloadList.setAttribute("id", "btnDownloadList");
btnDownloadList.setAttribute("class", "toolbar_button button_full_width");
btnDownloadList.setAttribute("onClick", "Toolbar.downloadList();");
2018-09-11 21:35:44 +02:00
2018-01-11 23:12:35 +01:00
var btnDownloadListImg = document.createElement("img");
btnDownloadListImg.setAttribute("src", "/res/img/floppy_small.png");
btnDownloadListImg.setAttribute("alt", "Download List");
2018-09-11 21:35:44 +02:00
2018-01-11 23:12:35 +01:00
var btnDownloadListText = document.createElement("span");
btnDownloadListText.innerHTML = "&nbsp;All Files";
2018-09-11 21:35:44 +02:00
2018-01-11 23:12:35 +01:00
btnDownloadList.appendChild(btnDownloadListImg);
btnDownloadList.appendChild(btnDownloadListText);
2019-02-20 22:46:35 +01:00
document.getElementById("btnDownload").after(btnDownloadList);
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
// Add the shuffle button to the toolbar
var btnShuffle = document.createElement("button");
btnShuffle.setAttribute("id", "btnShuffle");
2018-01-07 21:42:19 +01:00
btnShuffle.setAttribute("class", "toolbar_button button_full_width");
2017-11-10 12:39:55 +01:00
btnShuffle.setAttribute("onClick", "ListNavigator.toggleShuffle();");
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
var btnShuffleImg = document.createElement("img");
btnShuffleImg.setAttribute("src", "/res/img/shuffle_small.png");
btnShuffleImg.setAttribute("alt", "Shuffle playback order");
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
var btnShuffleText = document.createElement("span");
btnShuffleText.innerHTML = "&nbsp;Shuffle&nbsp;&#x2610;";
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
btnShuffle.appendChild(btnShuffleImg);
btnShuffle.appendChild(btnShuffleText);
2019-02-20 22:46:35 +01:00
document.getElementById("btnShare").after(btnShuffle);
2018-09-11 21:35:44 +02:00
2019-07-17 23:19:57 +02:00
// Make the navigator visible
2019-07-18 19:53:09 +02:00
document.getElementById("list_navigator").style.display = "inline-block";
// Skip to the file defined in the link hash
if(Number.isInteger(parseInt(getHashValue("item")))){
this.setItem(parseInt(getHashValue("item")));
}else{
this.setItem(0);
}
2017-11-10 12:39:55 +01:00
}
};
// Misc function, don't really know where else to put it
function getHashValue(key) {
var matches = location.hash.match(new RegExp(key + '=([^&]*)'));
return matches ? matches[1] : null;
2018-09-11 21:35:44 +02:00
}