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

187 lines
5.5 KiB
JavaScript
Raw Normal View History

2016-07-17 11:55:07 -07:00
define(['itemShortcuts', 'connectionManager', 'layoutManager', 'browser', 'registerElement'], function (itemShortcuts, connectionManager, layoutManager, browser) {
2016-07-16 11:02:39 -07:00
var ItemsContainerProtoType = Object.create(HTMLDivElement.prototype);
2016-07-16 14:28:15 -07:00
function parentWithClass(elem, className) {
while (!elem.classList || !elem.classList.contains(className)) {
elem = elem.parentNode;
if (!elem) {
return null;
}
}
return elem;
}
function parentWithAttribute(elem, name) {
while (!elem.getAttribute(name)) {
elem = elem.parentNode;
if (!elem) {
return null;
}
}
return elem;
}
function getItem(button) {
button = parentWithAttribute(button, 'data-id');
var serverId = button.getAttribute('data-serverid');
var id = button.getAttribute('data-id');
2016-07-16 21:20:36 -07:00
var type = button.getAttribute('data-type');
2016-07-16 14:28:15 -07:00
var apiClient = connectionManager.getApiClient(serverId);
return apiClient.getItem(apiClient.getCurrentUserId(), id);
}
2016-07-17 09:59:14 -07:00
function showContextMenu(button, itemsContainer) {
2016-07-16 14:28:15 -07:00
getItem(button).then(function (item) {
2016-07-17 09:59:14 -07:00
var playlistId = itemsContainer.getAttribute('data-playlistid');
var collectionId = itemsContainer.getAttribute('data-collectionid');
if (playlistId) {
var elem = parentWithAttribute(button, 'data-playlistitemid');
item.PlaylistItemId = elem ? elem.getAttribute('data-playlistitemid') : null;
}
2016-07-16 14:28:15 -07:00
require(['itemContextMenu'], function (itemContextMenu) {
itemContextMenu.show({
positionTo: button,
2016-07-16 16:39:10 -07:00
item: item,
play: true,
queue: true,
playAllFromHere: !item.IsFolder,
2016-07-16 21:20:36 -07:00
queueAllFromHere: !item.IsFolder,
2016-07-17 09:59:14 -07:00
identify: false,
playlistId: playlistId,
collectionId: collectionId
2016-07-16 16:39:10 -07:00
2016-07-17 09:59:14 -07:00
}).then(function (result) {
2016-07-16 16:08:21 -07:00
2016-07-16 16:39:10 -07:00
if (result.command == 'playallfromhere' || result.command == 'queueallfromhere') {
itemShortcuts.execute(button, result.command);
}
2016-07-17 09:59:14 -07:00
else if (result.command == 'removefromplaylist' || result.command == 'removefromcollection') {
itemsContainer.dispatchEvent(new CustomEvent('needsrefresh', {
detail: {},
cancelable: false,
bubbles: true
}));
}
2016-07-16 16:39:10 -07:00
});
2016-07-16 14:28:15 -07:00
});
});
}
function onClick(e) {
2016-07-17 09:59:14 -07:00
var itemsContainer = this;
2016-07-17 11:55:07 -07:00
var target = e.target;
2016-07-17 09:59:14 -07:00
2016-07-17 11:55:07 -07:00
var menuButton = parentWithClass(target, 'menuButton');
2016-07-16 14:28:15 -07:00
if (menuButton) {
2016-07-17 11:55:07 -07:00
var card = parentWithAttribute(target, 'data-id');
if (card) {
showContextMenu(card, target);
e.stopPropagation();
return false;
}
}
itemShortcuts.onClick.call(this, e);
}
function disableEvent(e) {
e.preventDefault();
e.stopPropagation();
return false;
}
function showContextMenu(card, target, options) {
var itemId = card.getAttribute('data-id');
var serverId = card.getAttribute('data-serverid');
var type = card.getAttribute('data-type');
var apiClient = connectionManager.getApiClient(serverId);
var promise = type == 'Timer' ? apiClient.getLiveTvTimer(itemId) : apiClient.getItem(apiClient.getCurrentUserId(), itemId);
promise.then(function (item) {
require(['itemContextMenu'], function (itemContextMenu) {
itemContextMenu.show(Object.assign(options || {}, {
item: item,
positionTo: target
}));
});
});
}
function onContextMenu(e) {
var itemsContainer = this;
var target = e.target;
var card = parentWithAttribute(target, 'data-id');
if (card) {
//var itemSelectionPanel = card.querySelector('.itemSelectionPanel');
//if (!itemSelectionPanel) {
// showContextMenu(card, {});
//}
showContextMenu(card, target, {
identify: false
});
2016-07-16 14:28:15 -07:00
}
2016-07-17 11:55:07 -07:00
e.preventDefault();
e.stopPropagation();
return false;
}
function getShortcutOptions() {
return {
click: false
};
2016-07-16 14:28:15 -07:00
}
2016-07-16 11:02:39 -07:00
ItemsContainerProtoType.attachedCallback = function () {
2016-07-16 14:28:15 -07:00
this.addEventListener('click', onClick);
2016-07-17 11:55:07 -07:00
// mobile safari doesn't allow contextmenu override
if (browser.safari && browser.mobile) {
this.addEventListener('contextmenu', disableEvent);
// todo: use tap hold
} else {
this.addEventListener('contextmenu', onContextMenu);
}
itemShortcuts.on(this, getShortcutOptions());
2016-07-16 11:02:39 -07:00
};
ItemsContainerProtoType.detachedCallback = function () {
2016-07-16 14:28:15 -07:00
this.removeEventListener('click', onClick);
2016-07-17 11:55:07 -07:00
this.removeEventListener('contextmenu', onContextMenu);
this.removeEventListener('contextmenu', disableEvent);
itemShortcuts.off(this, getShortcutOptions());
2016-07-16 11:02:39 -07:00
};
document.registerElement('emby-itemscontainer', {
prototype: ItemsContainerProtoType,
extends: 'div'
});
});