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

228 lines
6.3 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);
$("#listNavigatorItems").find("*").css("border-color", "#333");
$("#listNavigatorItems div").eq(this.position).css("border-color", "#fff");
// This centers the scroll bar exactly on the selected item
$("#listNavigatorItems").animate(
{
scrollLeft: (this.position * 109) - (($("#listNavigatorItems").width() / 2) - 55)
}, {
duration: 1000,
queue: false
}
);
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){
2018-09-11 21:35:44 +02:00
var startPos = +index - 30;
2017-11-10 12:39:55 +01:00
var endPos = +index + 30;
// 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-02-20 22:46:35 +01:00
var navigatorItems = document.getElementById("listNavigatorItems").children
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
for (i = startPos; i <= endPos; i++){
2017-12-12 23:33:41 +01:00
var thumb = "/api/file/" + this.data[i].id + "/thumbnail";
2018-10-04 23:36:34 +02:00
var name = this.data[i].name;
2018-09-11 21:35:44 +02:00
var itemHtml = escapeHTML(name) + "<br>"
2017-11-10 12:39:55 +01:00
+ "<img src=\"" + thumb + "\" "
+ "class=\"listItemThumbnail lazy\" alt=\"" + 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){
var hashPos = getHashValue("item");
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-02-20 22:46:35 +01:00
listHTML += "<div class=\"listItem\" "
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-02-20 22:46:35 +01:00
document.getElementById("listNavigatorItems").innerHTML = listHTML;
2018-09-11 21:35:44 +02:00
2017-11-10 12:39:55 +01:00
// Skip to this file if the parameter is set
if(Number.isInteger(parseInt(hashPos))){
this.setItem(hashPos);
}else{
this.setItem(0);
}
2018-09-11 21:35:44 +02:00
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
2017-11-10 12:39:55 +01:00
// We need to adjust the height of some elements to make the navigation bar fit
var navHeight = $("#listNavigator").height() + 2;
2019-02-20 22:46:35 +01:00
2017-11-10 12:39:55 +01:00
window.setTimeout(function(){
2019-02-20 22:46:35 +01:00
document.getElementById("listNavigator").style.top = "0px";
document.getElementById("filepreview").style.top = navHeight+"px";
document.getElementById("toolbar").style.top = navHeight+"px";
document.getElementById("button-expand-toolbar").style.top = navHeight+"px";
document.getElementById("sharebar").style.top = navHeight+"px";
document.getElementById("info_popup").style.top = (navHeight+20)+"px";
// $("#listNavigator").animate( {top: 0}, {"duration": 1500, "queue": false});
// $("#filepreview").animate( {top: navHeight},{"duration": 1500, "queue": false});
// $("#toolbar").animate( {top: navHeight},{"duration": 1500, "queue": false});
// $("#button-expand-toolbar").animate({top: navHeight},{"duration": 1500, "queue": false});
// $("#sharebar").animate( {top: navHeight},{"duration": 1500, "queue": false});
// $("#info_popup").css("top", "120px");
}, 200);
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
}