/* This file is loaded when Organizr is loaded */
// Load once Organizr loads
$('body').arrive('#activeInfo', {onceOnly: true}, function() {
plexLibrariesPluginLaunch();
});
// FUNCTIONS
function plexLibrariesPluginLaunch(){
organizrAPI2('GET','api/v2/plugins/plexlibraries/launch').success(function(data) {
try {
var menuList = `
Plex Libraries `;
$('.append-menu').after(menuList);
}catch(e) {
organizrCatchError(e,data);
}
}).fail(function(xhr) {
OrganizrApiError(xhr);
});
}
function togglePlexLibrariesPlugin(){
let div = `
`;
swal({
content: createElementFromHTML(div),
button: false,
className: 'orgAlertTransparent',
});
plexLibrariesPluginLoadShares();
}
function plexLibrariesPluginLoadShares(){
organizrAPI2('GET','api/v2/plugins/plexlibraries/shares').success(function(data) {
$('.loadingPlexLibraries').remove();
try {
if (data.response.data.plexAdmin == false) {
$.each(data.response.data.libraryData, function(_, sharedServer) {
$.each(sharedServer.libraries, function(_, obj) {
var userId = sharedServer.id;
plexLibrariesPluginLoadSharesItem(obj,"",userId);
if($('.plexLibrariesTableList').hasClass('hidden')){
$('.plexLibrariesTableList').removeClass('hidden');
}
});
});
} else {
// Plex Admin response contains all users shares, mark all toggles as disabled whilst this is a work in progress.
$.each(data.response.data.libraryData, function(_, sharedServer) {
const thtml = $("#plexUsers ");
var dropdown = document.getElementById('plexUsers');
dropdown.style.display = "block";
var username = sharedServer.username;
var userId = sharedServer.id;
thtml.append(''+username+' ');
$.each(sharedServer.libraries, function(_, obj) {
plexLibrariesPluginLoadSharesItem(obj,username,userId);
});
});
}
const thtml = $("#plexLibraries ");
thtml.append('');
thtml.append('');
}catch(e) {
organizrCatchError(e,data);
}
}).fail(function(xhr) {
$('.loadingPlexLibraries').remove();
OrganizrApiError(xhr);
});
}
function plexLibrariesPluginLoadSharesItem(obj,username,userId){
const thtml = $("#plexLibraries ");
var mediaType = obj.type
var mediaShared = obj.shared
var mediaIcon = "0"
var checked = "";
switch(mediaType) {
case 'movie':
var mediaIcon = "video-clapper"
var mediaIconColour = "purple"
break;
case 'show':
var mediaIcon = "video-camera"
var mediaIconColour = "warning"
break;
case 'artist':
var mediaIcon = "music-alt"
var mediaIconColour = "info"
break;
case 'photo':
var mediaIcon = "camera"
var mediaIconColour = "danger"
break;
}
if (mediaShared == 1) {
var checked = "checked";
}
if (username === "") {
var username = "none"
}
let libItem = `
${obj.title}
`;
thtml.append(libItem);
}
function plexLibrariesPluginOnToggle() {
$('.plexLibraries').change(function () {
let userId = $(this).attr('data-user-id');
if (this.checked) {
plexLibrariesPluginUpdateShare(userId,"share", this.value);
} else {
plexLibrariesPluginUpdateShare(userId,"unshare", this.value);
}
});
}
function plexLibrariesPluginOnSelect() {
$('#plexUsers').change(function () {
Array.from(document.getElementsByClassName('plexUser')).forEach(
function(element, index, array) {
element.style.display = "none";
}
);
Array.from(document.getElementsByClassName(this.value)).forEach(
function(element, index, array) {
element.style.display = "table-row";
}
);
if($('.plexLibrariesTableList').hasClass('hidden')){
$('.plexLibrariesTableList').removeClass('hidden');
}
});
}
function plexLibrariesPluginUpdateShare(userId, action, shareId) {
$('#plexLibrariesTable').block({
message: ' Just a moment... ',
css: {
border: '1px solid #000',
color: '#fff',
background: '#1b1b1b'
}
});
organizrAPI2('POST','api/v2/plugins/plexlibraries/shares/' + userId + '/' + action + '/' + shareId, {}).success(function(data) {
try {
let response = data.response;
$('#plexLibrariesTable').unblock();
message('Plex Share',response.message,activeInfo.settings.notifications.position,"#FFF","success","5000");
}catch(e) {
organizrCatchError(e,data);
}
}).fail(function(xhr) {
message('Plex Share',xhr.responseJSON.response.message,activeInfo.settings.notifications.position,"#FFF","error","5000");
$('#plexLibrariesTable').unblock();
OrganizrApiError(xhr);
});
}
// EVENTS and LISTENERS