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

290 lines
7.7 KiB
JavaScript
Raw Normal View History

2016-08-29 21:33:24 -07:00
define(['itemShortcuts', 'connectionManager', 'layoutManager', 'browser', 'dom', 'loading', 'serverNotifications', 'events', 'registerElement'], function (itemShortcuts, connectionManager, layoutManager, browser, dom, loading, serverNotifications, events) {
2016-10-17 22:06:48 -07:00
'use strict';
2016-07-16 11:02:39 -07:00
var ItemsContainerProtoType = Object.create(HTMLDivElement.prototype);
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-18 20:57:55 -07:00
var multiSelect = itemsContainer.multiSelect;
if (multiSelect) {
if (multiSelect.onContainerClick.call(itemsContainer, e) === false) {
return;
}
}
itemShortcuts.onClick.call(itemsContainer, e);
2016-07-17 11:55:07 -07:00
}
function disableEvent(e) {
e.preventDefault();
e.stopPropagation();
return false;
}
function onContextMenu(e) {
var itemsContainer = this;
var target = e.target;
2016-07-18 11:50:00 -07:00
var card = dom.parentWithAttribute(target, 'data-id');
2016-07-17 23:45:29 -07:00
2016-07-17 11:55:07 -07:00
if (card) {
//var itemSelectionPanel = card.querySelector('.itemSelectionPanel');
//if (!itemSelectionPanel) {
// showContextMenu(card, {});
//}
2016-07-17 13:08:04 -07:00
itemShortcuts.showContextMenu(card, {
positionTo: target,
itemsContainer: itemsContainer
2016-07-17 11:55:07 -07:00
});
2016-07-17 23:45:29 -07:00
e.preventDefault();
e.stopPropagation();
return false;
}
2016-07-17 11:55:07 -07:00
}
function getShortcutOptions() {
return {
click: false
};
2016-07-16 14:28:15 -07:00
}
ItemsContainerProtoType.enableHoverMenu = function (enabled) {
var current = this.hoverMenu;
2016-07-19 12:51:22 -07:00
if (!enabled) {
if (current) {
current.destroy();
this.hoverMenu = null;
}
return;
}
if (current) {
return;
}
var self = this;
require(['itemHoverMenu'], function (ItemHoverMenu) {
self.hoverMenu = new ItemHoverMenu(self);
});
};
2016-07-17 23:45:29 -07:00
ItemsContainerProtoType.enableMultiSelect = function (enabled) {
var current = this.multiSelect;
2016-07-19 12:51:22 -07:00
if (!enabled) {
if (current) {
current.destroy();
this.multiSelect = null;
}
2016-07-17 23:45:29 -07:00
return;
}
if (current) {
return;
}
var self = this;
require(['multiSelect'], function (MultiSelect) {
2016-07-18 20:57:55 -07:00
self.multiSelect = new MultiSelect({
container: self,
bindOnClick: false
});
2016-07-17 23:45:29 -07:00
});
};
2016-07-19 13:23:28 -07:00
function onDrop(evt, itemsContainer) {
loading.show();
var el = evt.item;
var newIndex = evt.newIndex;
var itemId = el.getAttribute('data-playlistitemid');
var playlistId = el.getAttribute('data-playlistid');
2016-07-19 13:23:28 -07:00
var serverId = el.getAttribute('data-serverid');
var apiClient = connectionManager.getApiClient(serverId);
newIndex = Math.max(0, newIndex - 1);
2016-07-19 13:23:28 -07:00
apiClient.ajax({
url: apiClient.getUrl('Playlists/' + playlistId + '/Items/' + itemId + '/Move/' + newIndex),
type: 'POST'
}).then(function () {
el.setAttribute('data-index', newIndex);
loading.hide();
}, function () {
loading.hide();
itemsContainer.dispatchEvent(new CustomEvent('needsrefresh', {
detail: {},
cancelable: false,
bubbles: true
}));
});
}
ItemsContainerProtoType.enableDragReordering = function (enabled) {
var current = this.sortable;
if (!enabled) {
if (current) {
current.destroy();
this.sortable = null;
}
return;
}
if (current) {
return;
}
var self = this;
require(['sortable'], function (Sortable) {
self.sortable = new Sortable(self, {
draggable: ".listItem",
handle: '.listViewDragHandle',
// dragging ended
onEnd: function (/**Event*/evt) {
onDrop(evt, self);
}
});
});
};
2016-08-29 21:33:24 -07:00
function onUserDataChanged(e, apiClient, userData) {
var itemsContainer = this;
require(['cardBuilder'], function (cardBuilder) {
cardBuilder.onUserDataChanged(userData, itemsContainer);
});
}
2016-09-19 08:41:35 -07:00
function onTimerCreated(e, apiClient, data) {
var itemsContainer = this;
var programId = data.ProgramId;
// This could be null, not supported by all tv providers
var newTimerId = data.Id;
require(['cardBuilder'], function (cardBuilder) {
cardBuilder.onTimerCreated(programId, newTimerId, itemsContainer);
});
}
function onSeriesTimerCreated(e, apiClient, data) {
var itemsContainer = this;
}
function onTimerCancelled(e, apiClient, data) {
var itemsContainer = this;
var id = data.Id;
require(['cardBuilder'], function (cardBuilder) {
cardBuilder.onTimerCancelled(id, itemsContainer);
});
}
function onSeriesTimerCancelled(e, apiClient, data) {
var itemsContainer = this;
var id = data.Id;
require(['cardBuilder'], function (cardBuilder) {
cardBuilder.onSeriesTimerCancelled(id, itemsContainer);
});
}
function addNotificationEvent(instance, name, handler) {
var localHandler = handler.bind(instance);
events.on(serverNotifications, name, localHandler);
instance[name] = localHandler;
}
function removeNotificationEvent(instance, name) {
var handler = instance[name];
if (handler) {
events.off(serverNotifications, 'UserDataChanged', handler);
instance[name] = null;
}
}
2016-07-16 11:02:39 -07:00
ItemsContainerProtoType.attachedCallback = function () {
2016-07-17 23:45:29 -07:00
2016-07-16 14:28:15 -07:00
this.addEventListener('click', onClick);
2016-07-17 11:55:07 -07:00
2016-08-06 07:07:44 -07:00
if (browser.touch) {
2016-07-17 11:55:07 -07:00
this.addEventListener('contextmenu', disableEvent);
} else {
2016-10-03 22:15:39 -07:00
if (this.getAttribute('data-contextmenu') !== 'false') {
this.addEventListener('contextmenu', onContextMenu);
}
2016-07-17 11:55:07 -07:00
}
if (layoutManager.desktop) {
this.enableHoverMenu(true);
}
2016-07-17 23:45:29 -07:00
if (layoutManager.desktop || layoutManager.mobile) {
this.enableMultiSelect(true);
}
2016-07-17 11:55:07 -07:00
itemShortcuts.on(this, getShortcutOptions());
2016-08-29 21:33:24 -07:00
2016-09-19 08:41:35 -07:00
addNotificationEvent(this, 'UserDataChanged', onUserDataChanged);
addNotificationEvent(this, 'TimerCreated', onTimerCreated);
addNotificationEvent(this, 'SeriesTimerCreated', onSeriesTimerCreated);
addNotificationEvent(this, 'TimerCancelled', onTimerCancelled);
addNotificationEvent(this, 'SeriesTimerCancelled', onSeriesTimerCancelled);
2016-07-16 11:02:39 -07:00
};
ItemsContainerProtoType.detachedCallback = function () {
2016-07-17 23:45:29 -07:00
this.enableHoverMenu(false);
this.enableMultiSelect(false);
2016-07-19 13:23:28 -07:00
this.enableDragReordering(false);
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-08-29 21:33:24 -07:00
2016-09-19 08:41:35 -07:00
removeNotificationEvent(this, 'UserDataChanged');
removeNotificationEvent(this, 'TimerCreated');
removeNotificationEvent(this, 'SeriesTimerCreated');
removeNotificationEvent(this, 'TimerCancelled');
removeNotificationEvent(this, 'SeriesTimerCancelled');
2016-07-16 11:02:39 -07:00
};
document.registerElement('emby-itemscontainer', {
prototype: ItemsContainerProtoType,
extends: 'div'
});
});