jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/userdatabuttons/userdatabuttons.js

243 lines
7.4 KiB
JavaScript
Raw Normal View History

2016-09-26 22:13:56 -07:00
define(['connectionManager', 'globalize', 'dom', 'paper-icon-button-light', 'material-icons', 'emby-button', 'css!./userdatabuttons'], function (connectionManager, globalize, dom) {
2016-10-03 22:15:39 -07:00
'use strict';
2016-09-26 22:13:56 -07:00
var userDataMethods = {
markPlayed: markPlayed,
markDislike: markDislike,
markLike: markLike,
markFavorite: markFavorite
};
2016-07-15 14:16:18 -07:00
2016-08-01 22:55:52 -07:00
function getUserDataButtonHtml(method, itemId, buttonCssClass, iconCssClass, icon, tooltip, style) {
2016-07-15 14:16:18 -07:00
2016-10-03 22:15:39 -07:00
if (style === 'fab-mini') {
style = 'fab';
2016-08-01 22:55:52 -07:00
buttonCssClass = buttonCssClass ? (buttonCssClass + ' mini') : 'mini';
}
2016-10-03 22:15:39 -07:00
var is = style === 'fab' ? 'emby-button' : 'paper-icon-button-light';
var className = style === 'fab' ? 'autoSize fab' : 'autoSize';
2016-07-15 14:16:18 -07:00
2016-08-01 22:55:52 -07:00
if (buttonCssClass) {
className += ' ' + buttonCssClass;
}
if (iconCssClass) {
2016-08-01 22:55:52 -07:00
iconCssClass += ' ';
} else {
iconCssClass = '';
}
2016-07-15 14:16:18 -07:00
2016-08-01 22:55:52 -07:00
iconCssClass += 'md-icon';
2016-10-03 22:15:39 -07:00
return '<button title="' + tooltip + '" data-itemid="' + itemId + '" is="' + is + '" data-method="' + method + '" class="' + className + '"><i class="'+ iconCssClass + '">' + icon + '</i></button>';
2016-07-15 14:16:18 -07:00
}
2016-09-26 22:13:56 -07:00
function onContainerClick(e) {
var btnUserData = dom.parentWithClass(e.target, 'btnUserData');
if (!btnUserData) {
return;
}
var method = btnUserData.getAttribute('data-method');
userDataMethods[method](btnUserData);
}
2016-07-15 14:16:18 -07:00
function fill(options) {
var html = getIconsHtml(options);
2016-07-15 14:16:18 -07:00
2016-10-03 22:15:39 -07:00
if (options.fillMode === 'insertAdjacent') {
2016-09-26 22:13:56 -07:00
options.element.insertAdjacentHTML(options.insertLocation || 'beforeend', html);
} else {
options.element.innerHTML = html;
}
dom.removeEventListener(options.element, 'click', onContainerClick, {
passive: true
});
dom.addEventListener(options.element, 'click', onContainerClick, {
passive: true
});
}
function destroy(options) {
options.element.innerHTML = '';
dom.removeEventListener(options.element, 'click', onContainerClick, {
passive: true
});
2016-07-15 14:16:18 -07:00
}
function getIconsHtml(options) {
var item = options.item;
var includePlayed = options.includePlayed;
var cssClass = options.cssClass;
var style = options.style;
2016-07-15 14:16:18 -07:00
var html = '';
var userData = item.UserData || {};
var itemId = item.Id;
var btnCssClass = "btnUserData";
if (cssClass) {
btnCssClass += " " + cssClass;
}
2016-08-01 22:55:52 -07:00
var iconCssClass = options.iconCssClass;
2016-07-15 14:16:18 -07:00
if (includePlayed !== false) {
2016-07-18 20:57:55 -07:00
var tooltipPlayed = globalize.translate('sharedcomponents#MarkPlayed');
2016-07-15 14:16:18 -07:00
2016-10-03 22:15:39 -07:00
if (item.MediaType === 'Video' || item.Type === 'Series' || item.Type === 'Season' || item.Type === 'BoxSet' || item.Type === 'Playlist') {
if (item.Type !== 'TvChannel') {
2016-07-15 14:16:18 -07:00
if (userData.Played) {
2016-08-01 22:55:52 -07:00
html += getUserDataButtonHtml('markPlayed', itemId, btnCssClass + ' btnUserDataOn', iconCssClass, 'check', tooltipPlayed, style);
2016-07-15 14:16:18 -07:00
} else {
2016-08-01 22:55:52 -07:00
html += getUserDataButtonHtml('markPlayed', itemId, btnCssClass, iconCssClass, 'check', tooltipPlayed, style);
2016-07-15 14:16:18 -07:00
}
}
}
}
//var tooltipLike = globalize.translate('sharedcomponents#Like');
//var tooltipDislike = globalize.translate('sharedcomponents#Dislike');
2016-07-15 14:16:18 -07:00
//if (typeof userData.Likes == "undefined") {
// html += getUserDataButtonHtml('markDislike', itemId, btnCssClass + ' btnUserData btnDislike', 'thumb-down', tooltipDislike);
// html += getUserDataButtonHtml('markLike', itemId, btnCssClass + ' btnUserData btnLike', 'thumb-up', tooltipLike);
//}
//else if (userData.Likes) {
// html += getUserDataButtonHtml('markDislike', itemId, btnCssClass + ' btnUserData btnDislike', 'thumb-down', tooltipDislike);
// html += getUserDataButtonHtml('markLike', itemId, btnCssClass + ' btnUserData btnLike btnUserDataOn', 'thumb-up', tooltipLike);
//}
//else {
// html += getUserDataButtonHtml('markDislike', itemId, btnCssClass + ' btnUserData btnDislike btnUserDataOn', 'thumb-down', tooltipDislike);
// html += getUserDataButtonHtml('markLike', itemId, btnCssClass + ' btnUserData btnLike', 'thumb-up', tooltipLike);
//}
var tooltipFavorite = globalize.translate('sharedcomponents#Favorite');
if (userData.IsFavorite) {
2016-08-01 22:55:52 -07:00
html += getUserDataButtonHtml('markFavorite', itemId, btnCssClass + ' btnUserData btnUserDataOn', iconCssClass, 'favorite', tooltipFavorite, style);
2016-07-15 14:16:18 -07:00
} else {
2016-08-01 22:55:52 -07:00
html += getUserDataButtonHtml('markFavorite', itemId, btnCssClass + ' btnUserData', iconCssClass, 'favorite', tooltipFavorite, style);
2016-07-15 14:16:18 -07:00
}
return html;
}
function markFavorite(link) {
var id = link.getAttribute('data-itemid');
var markAsFavorite = !link.classList.contains('btnUserDataOn');
favorite(id, markAsFavorite);
if (markAsFavorite) {
link.classList.add('btnUserDataOn');
} else {
link.classList.remove('btnUserDataOn');
}
}
function markLike(link) {
var id = link.getAttribute('data-itemid');
if (!link.classList.contains('btnUserDataOn')) {
likes(id, true);
link.classList.add('btnUserDataOn');
} else {
clearLike(id);
link.classList.remove('btnUserDataOn');
}
link.parentNode.querySelector('.btnDislike').classList.remove('btnUserDataOn');
}
function markDislike(link) {
var id = link.getAttribute('data-itemid');
if (!link.classList.contains('btnUserDataOn')) {
likes(id, false);
link.classList.add('btnUserDataOn');
} else {
clearLike(id);
link.classList.remove('btnUserDataOn');
}
link.parentNode.querySelector('.btnLike').classList.remove('btnUserDataOn');
}
function markPlayed(link) {
var id = link.getAttribute('data-itemid');
if (!link.classList.contains('btnUserDataOn')) {
played(id, true);
link.classList.add('btnUserDataOn');
} else {
played(id, false);
link.classList.remove('btnUserDataOn');
}
}
function likes(id, isLiked) {
var apiClient = connectionManager.currentApiClient();
return apiClient.updateUserItemRating(apiClient.getCurrentUserId(), id, isLiked);
}
function played(id, isPlayed) {
var apiClient = connectionManager.currentApiClient();
var method = isPlayed ? 'markPlayed' : 'markUnplayed';
return apiClient[method](apiClient.getCurrentUserId(), id, new Date());
}
function favorite(id, isFavorite) {
var apiClient = connectionManager.currentApiClient();
return apiClient.updateFavoriteStatus(apiClient.getCurrentUserId(), id, isFavorite);
}
function clearLike(id) {
var apiClient = connectionManager.currentApiClient();
return apiClient.clearUserItemRating(apiClient.getCurrentUserId(), id);
}
return {
fill: fill,
2016-09-26 22:13:56 -07:00
destroy: destroy,
2016-07-15 14:16:18 -07:00
getIconsHtml: getIconsHtml
};
});