2020-06-18 22:34:03 +03:00
|
|
|
import browser from 'browser';
|
|
|
|
import appHost from 'apphost';
|
|
|
|
import loading from 'loading';
|
|
|
|
import connectionManager from 'connectionManager';
|
|
|
|
import globalize from 'globalize';
|
|
|
|
import dom from 'dom';
|
|
|
|
import 'css!./multiSelect';
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-06-18 22:34:03 +03:00
|
|
|
/* eslint-disable indent */
|
|
|
|
|
|
|
|
let selectedItems = [];
|
|
|
|
let selectedElements = [];
|
|
|
|
let currentSelectionCommandsPanel;
|
2018-10-23 01:05:09 +03:00
|
|
|
|
|
|
|
function hideSelections() {
|
2020-06-18 22:34:03 +03:00
|
|
|
const selectionCommandsPanel = currentSelectionCommandsPanel;
|
2018-10-23 01:05:09 +03:00
|
|
|
if (selectionCommandsPanel) {
|
2019-01-10 15:39:37 +03:00
|
|
|
selectionCommandsPanel.parentNode.removeChild(selectionCommandsPanel);
|
|
|
|
currentSelectionCommandsPanel = null;
|
|
|
|
|
|
|
|
selectedItems = [];
|
|
|
|
selectedElements = [];
|
2020-06-18 22:34:03 +03:00
|
|
|
const elems = document.querySelectorAll('.itemSelectionPanel');
|
|
|
|
for (let i = 0, length = elems.length; i < length; i++) {
|
|
|
|
const parent = elems[i].parentNode;
|
2019-01-10 15:39:37 +03:00
|
|
|
parent.removeChild(elems[i]);
|
2020-05-04 12:44:12 +02:00
|
|
|
parent.classList.remove('withMultiSelect');
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onItemSelectionPanelClick(e, itemSelectionPanel) {
|
2019-01-10 15:39:37 +03:00
|
|
|
// toggle the checkbox, if it wasn't clicked on
|
2020-05-04 12:44:12 +02:00
|
|
|
if (!dom.parentWithClass(e.target, 'chkItemSelect')) {
|
2020-06-18 22:34:03 +03:00
|
|
|
const chkItemSelect = itemSelectionPanel.querySelector('.chkItemSelect');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (chkItemSelect) {
|
2020-05-04 12:44:12 +02:00
|
|
|
if (chkItemSelect.classList.contains('checkedInitial')) {
|
|
|
|
chkItemSelect.classList.remove('checkedInitial');
|
2019-01-10 15:39:37 +03:00
|
|
|
} else {
|
2020-06-18 22:34:03 +03:00
|
|
|
const newValue = !chkItemSelect.checked;
|
2019-01-10 15:39:37 +03:00
|
|
|
chkItemSelect.checked = newValue;
|
|
|
|
updateItemSelection(chkItemSelect, newValue);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
return false;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateItemSelection(chkItemSelect, selected) {
|
2020-06-18 22:34:03 +03:00
|
|
|
const id = dom.parentWithAttribute(chkItemSelect, 'data-id').getAttribute('data-id');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
if (selected) {
|
2020-06-18 22:34:03 +03:00
|
|
|
const current = selectedItems.filter(i => {
|
2019-01-10 15:39:37 +03:00
|
|
|
return i === id;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!current.length) {
|
|
|
|
selectedItems.push(id);
|
|
|
|
selectedElements.push(chkItemSelect);
|
|
|
|
}
|
|
|
|
} else {
|
2020-06-18 22:34:03 +03:00
|
|
|
selectedItems = selectedItems.filter(i => {
|
2019-01-10 15:39:37 +03:00
|
|
|
return i !== id;
|
|
|
|
});
|
2020-06-18 22:34:03 +03:00
|
|
|
selectedElements = selectedElements.filter(i => {
|
2019-01-10 15:39:37 +03:00
|
|
|
return i !== chkItemSelect;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
if (selectedItems.length) {
|
2020-06-18 22:34:03 +03:00
|
|
|
const itemSelectionCount = document.querySelector('.itemSelectionCount');
|
2019-01-10 15:39:37 +03:00
|
|
|
if (itemSelectionCount) {
|
|
|
|
itemSelectionCount.innerHTML = selectedItems.length;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hideSelections();
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onSelectionChange(e) {
|
2019-01-10 15:39:37 +03:00
|
|
|
updateItemSelection(this, this.checked);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function showSelection(item, isChecked) {
|
2020-06-18 22:34:03 +03:00
|
|
|
let itemSelectionPanel = item.querySelector('.itemSelectionPanel');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
if (!itemSelectionPanel) {
|
2020-05-04 12:44:12 +02:00
|
|
|
itemSelectionPanel = document.createElement('div');
|
|
|
|
itemSelectionPanel.classList.add('itemSelectionPanel');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-06-18 22:34:03 +03:00
|
|
|
const parent = item.querySelector('.cardBox') || item.querySelector('.cardContent');
|
2020-05-04 12:44:12 +02:00
|
|
|
parent.classList.add('withMultiSelect');
|
2019-01-10 15:39:37 +03:00
|
|
|
parent.appendChild(itemSelectionPanel);
|
|
|
|
|
2020-06-18 22:34:03 +03:00
|
|
|
let cssClass = 'chkItemSelect';
|
2019-01-10 15:39:37 +03:00
|
|
|
if (isChecked && !browser.firefox) {
|
|
|
|
// In firefox, the initial tap hold doesnt' get treated as a click
|
|
|
|
// In other browsers it does, so we need to make sure that initial click is ignored
|
2020-05-04 12:44:12 +02:00
|
|
|
cssClass += ' checkedInitial';
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
2020-06-18 22:34:03 +03:00
|
|
|
const checkedAttribute = isChecked ? ' checked' : '';
|
|
|
|
itemSelectionPanel.innerHTML = `<label class="checkboxContainer"><input type="checkbox" is="emby-checkbox" data-outlineclass="multiSelectCheckboxOutline" class="${cssClass}"${checkedAttribute}/><span></span></label>`;
|
|
|
|
const chkItemSelect = itemSelectionPanel.querySelector('.chkItemSelect');
|
2020-05-04 12:44:12 +02:00
|
|
|
chkItemSelect.addEventListener('change', onSelectionChange);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function showSelectionCommands() {
|
2020-06-18 22:34:03 +03:00
|
|
|
let selectionCommandsPanel = currentSelectionCommandsPanel;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
if (!selectionCommandsPanel) {
|
2020-05-04 12:44:12 +02:00
|
|
|
selectionCommandsPanel = document.createElement('div');
|
|
|
|
selectionCommandsPanel.classList.add('selectionCommandsPanel');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
document.body.appendChild(selectionCommandsPanel);
|
|
|
|
currentSelectionCommandsPanel = selectionCommandsPanel;
|
|
|
|
|
2020-06-18 22:34:03 +03:00
|
|
|
let html = '';
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-04-26 02:37:28 +03:00
|
|
|
html += '<button is="paper-icon-button-light" class="btnCloseSelectionPanel autoSize"><span class="material-icons close"></span></button>';
|
2019-01-10 15:39:37 +03:00
|
|
|
html += '<h1 class="itemSelectionCount"></h1>';
|
|
|
|
|
2020-05-16 18:07:32 +02:00
|
|
|
const moreIcon = 'more_vert';
|
2020-06-18 22:34:03 +03:00
|
|
|
html += `<button is="paper-icon-button-light" class="btnSelectionPanelOptions autoSize" style="margin-left:auto;"><span class="material-icons ${moreIcon}"></span></button>`;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
selectionCommandsPanel.innerHTML = html;
|
|
|
|
|
2020-05-04 12:44:12 +02:00
|
|
|
selectionCommandsPanel.querySelector('.btnCloseSelectionPanel').addEventListener('click', hideSelections);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-06-18 22:34:03 +03:00
|
|
|
const btnSelectionPanelOptions = selectionCommandsPanel.querySelector('.btnSelectionPanelOptions');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-05-04 12:44:12 +02:00
|
|
|
dom.addEventListener(btnSelectionPanelOptions, 'click', showMenuForSelectedItems, { passive: true });
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function alertText(options) {
|
2020-06-18 22:34:03 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
import('alert').then(({default: alert}) => {
|
2019-01-10 15:39:37 +03:00
|
|
|
alert(options).then(resolve, resolve);
|
|
|
|
});
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function deleteItems(apiClient, itemIds) {
|
2020-06-18 22:34:03 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let msg = globalize.translate('ConfirmDeleteItem');
|
|
|
|
let title = globalize.translate('HeaderDeleteItem');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (itemIds.length > 1) {
|
2020-05-04 12:44:12 +02:00
|
|
|
msg = globalize.translate('ConfirmDeleteItems');
|
|
|
|
title = globalize.translate('HeaderDeleteItems');
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
2020-06-18 22:34:03 +03:00
|
|
|
import('confirm').then(({default: confirm}) => {
|
|
|
|
confirm(msg, title).then(() => {
|
|
|
|
const promises = itemIds.map(itemId => {
|
2019-01-10 15:39:37 +03:00
|
|
|
apiClient.deleteItem(itemId);
|
2018-10-23 01:05:09 +03:00
|
|
|
});
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-06-18 22:34:03 +03:00
|
|
|
Promise.all(promises).then(resolve, () => {
|
2020-05-04 12:44:12 +02:00
|
|
|
alertText(globalize.translate('ErrorDeletingItem')).then(reject, reject);
|
2019-01-10 15:39:37 +03:00
|
|
|
});
|
|
|
|
}, reject);
|
|
|
|
});
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function showMenuForSelectedItems(e) {
|
2020-06-18 22:34:03 +03:00
|
|
|
const apiClient = connectionManager.currentApiClient();
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-06-18 22:34:03 +03:00
|
|
|
apiClient.getCurrentUser().then(user => {
|
|
|
|
const menuItems = [];
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
menuItems.push({
|
2020-05-04 12:44:12 +02:00
|
|
|
name: globalize.translate('AddToCollection'),
|
|
|
|
id: 'addtocollection',
|
|
|
|
icon: 'add'
|
2019-01-10 15:39:37 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
menuItems.push({
|
2020-05-04 12:44:12 +02:00
|
|
|
name: globalize.translate('AddToPlaylist'),
|
|
|
|
id: 'playlist',
|
|
|
|
icon: 'playlist_add'
|
2019-01-10 15:39:37 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: Be more dynamic based on what is selected
|
|
|
|
if (user.Policy.EnableContentDeletion) {
|
|
|
|
menuItems.push({
|
2020-05-04 12:44:12 +02:00
|
|
|
name: globalize.translate('Delete'),
|
|
|
|
id: 'delete',
|
|
|
|
icon: 'delete'
|
2019-01-10 15:39:37 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-16 22:19:09 +02:00
|
|
|
if (user.Policy.EnableContentDownloading && appHost.default.supports('filedownload')) {
|
2019-01-25 22:43:26 +01:00
|
|
|
menuItems.push({
|
2020-05-04 12:44:12 +02:00
|
|
|
name: globalize.translate('ButtonDownload'),
|
|
|
|
id: 'download',
|
|
|
|
icon: 'file_download'
|
2019-01-25 22:43:26 +01:00
|
|
|
});
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
2019-08-15 18:37:37 +03:00
|
|
|
if (user.Policy.IsAdministrator) {
|
|
|
|
menuItems.push({
|
2020-05-04 12:44:12 +02:00
|
|
|
name: globalize.translate('GroupVersions'),
|
|
|
|
id: 'groupvideos',
|
|
|
|
icon: 'call_merge'
|
2019-08-15 18:37:37 +03:00
|
|
|
});
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
menuItems.push({
|
2020-05-04 12:44:12 +02:00
|
|
|
name: globalize.translate('MarkPlayed'),
|
|
|
|
id: 'markplayed',
|
|
|
|
icon: 'check_box'
|
2019-01-10 15:39:37 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
menuItems.push({
|
2020-05-04 12:44:12 +02:00
|
|
|
name: globalize.translate('MarkUnplayed'),
|
|
|
|
id: 'markunplayed',
|
|
|
|
icon: 'check_box_outline_blank'
|
2019-01-10 15:39:37 +03:00
|
|
|
});
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
menuItems.push({
|
2020-05-04 12:44:12 +02:00
|
|
|
name: globalize.translate('RefreshMetadata'),
|
|
|
|
id: 'refresh',
|
|
|
|
icon: 'refresh'
|
2019-01-10 15:39:37 +03:00
|
|
|
});
|
|
|
|
|
2020-06-18 22:34:03 +03:00
|
|
|
import('actionsheet').then(({default: actionsheet}) => {
|
2018-10-23 01:05:09 +03:00
|
|
|
actionsheet.show({
|
|
|
|
items: menuItems,
|
|
|
|
positionTo: e.target,
|
2019-01-10 15:39:37 +03:00
|
|
|
callback: function (id) {
|
2020-06-18 22:34:03 +03:00
|
|
|
const items = selectedItems.slice(0);
|
|
|
|
const serverId = apiClient.serverInfo().Id;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
switch (id) {
|
2020-05-04 12:44:12 +02:00
|
|
|
case 'addtocollection':
|
2020-06-18 22:34:03 +03:00
|
|
|
import('collectionEditor').then(({default: collectionEditor}) => {
|
2020-07-25 13:42:03 +02:00
|
|
|
new collectionEditor({
|
2018-10-23 01:05:09 +03:00
|
|
|
items: items,
|
|
|
|
serverId: serverId
|
2019-01-10 15:39:37 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
hideSelections();
|
|
|
|
dispatchNeedsRefresh();
|
2018-10-23 01:05:09 +03:00
|
|
|
break;
|
2020-05-04 12:44:12 +02:00
|
|
|
case 'playlist':
|
2020-06-18 22:34:03 +03:00
|
|
|
import('playlistEditor').then(({default: playlistEditor}) => {
|
2020-07-25 13:42:03 +02:00
|
|
|
new playlistEditor({
|
2018-10-23 01:05:09 +03:00
|
|
|
items: items,
|
|
|
|
serverId: serverId
|
2019-01-10 15:39:37 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
hideSelections();
|
|
|
|
dispatchNeedsRefresh();
|
2018-10-23 01:05:09 +03:00
|
|
|
break;
|
2020-05-04 12:44:12 +02:00
|
|
|
case 'delete':
|
2019-01-10 15:39:37 +03:00
|
|
|
deleteItems(apiClient, items).then(dispatchNeedsRefresh);
|
|
|
|
hideSelections();
|
|
|
|
dispatchNeedsRefresh();
|
2018-10-23 01:05:09 +03:00
|
|
|
break;
|
2020-05-04 12:44:12 +02:00
|
|
|
case 'groupvideos':
|
2018-10-23 01:05:09 +03:00
|
|
|
combineVersions(apiClient, items);
|
|
|
|
break;
|
2020-05-04 12:44:12 +02:00
|
|
|
case 'markplayed':
|
2020-06-18 22:34:03 +03:00
|
|
|
items.forEach(itemId => {
|
2019-01-10 15:39:37 +03:00
|
|
|
apiClient.markPlayed(apiClient.getCurrentUserId(), itemId);
|
|
|
|
});
|
|
|
|
hideSelections();
|
|
|
|
dispatchNeedsRefresh();
|
2018-10-23 01:05:09 +03:00
|
|
|
break;
|
2020-05-04 12:44:12 +02:00
|
|
|
case 'markunplayed':
|
2020-06-18 22:34:03 +03:00
|
|
|
items.forEach(itemId => {
|
2019-01-10 15:39:37 +03:00
|
|
|
apiClient.markUnplayed(apiClient.getCurrentUserId(), itemId);
|
|
|
|
});
|
|
|
|
hideSelections();
|
|
|
|
dispatchNeedsRefresh();
|
2018-10-23 01:05:09 +03:00
|
|
|
break;
|
2020-05-04 12:44:12 +02:00
|
|
|
case 'refresh':
|
2020-06-18 22:34:03 +03:00
|
|
|
import('refreshDialog').then(({default: refreshDialog}) => {
|
2018-10-23 01:05:09 +03:00
|
|
|
new refreshDialog({
|
|
|
|
itemIds: items,
|
|
|
|
serverId: serverId
|
2019-01-10 15:39:37 +03:00
|
|
|
}).show();
|
|
|
|
});
|
|
|
|
hideSelections();
|
|
|
|
dispatchNeedsRefresh();
|
2018-10-23 01:05:09 +03:00
|
|
|
break;
|
2019-01-10 15:39:37 +03:00
|
|
|
default:
|
|
|
|
break;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function dispatchNeedsRefresh() {
|
2020-06-18 22:34:03 +03:00
|
|
|
const elems = [];
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-06-18 22:34:03 +03:00
|
|
|
[].forEach.call(selectedElements, i => {
|
|
|
|
const container = dom.parentWithAttribute(i, 'is', 'emby-itemscontainer');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-06-18 22:34:03 +03:00
|
|
|
if (container && !elems.includes(container)) {
|
2019-01-10 15:39:37 +03:00
|
|
|
elems.push(container);
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
});
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-06-18 22:34:03 +03:00
|
|
|
for (let i = 0, length = elems.length; i < length; i++) {
|
2019-01-10 15:39:37 +03:00
|
|
|
elems[i].notifyRefreshNeeded(true);
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function combineVersions(apiClient, selection) {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (selection.length < 2) {
|
2020-06-18 22:34:03 +03:00
|
|
|
import('alert').then(({default: alert}) => {
|
2019-01-10 15:39:37 +03:00
|
|
|
alert({
|
2020-07-19 20:11:53 +01:00
|
|
|
|
2020-05-04 12:44:12 +02:00
|
|
|
text: globalize.translate('PleaseSelectTwoItems')
|
2019-01-10 15:39:37 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
loading.show();
|
|
|
|
|
|
|
|
apiClient.ajax({
|
|
|
|
|
2020-05-04 12:44:12 +02:00
|
|
|
type: 'POST',
|
|
|
|
url: apiClient.getUrl('Videos/MergeVersions', { Ids: selection.join(',') })
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-06-18 22:34:03 +03:00
|
|
|
}).then(() => {
|
2019-01-10 15:39:37 +03:00
|
|
|
loading.hide();
|
|
|
|
hideSelections();
|
|
|
|
dispatchNeedsRefresh();
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function showSelections(initialCard) {
|
2020-06-18 22:34:03 +03:00
|
|
|
import('emby-checkbox').then(() => {
|
|
|
|
const cards = document.querySelectorAll('.card');
|
|
|
|
for (let i = 0, length = cards.length; i < length; i++) {
|
2019-01-10 15:39:37 +03:00
|
|
|
showSelection(cards[i], initialCard === cards[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
showSelectionCommands();
|
|
|
|
updateItemSelection(initialCard, true);
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onContainerClick(e) {
|
2020-06-18 22:34:03 +03:00
|
|
|
const target = e.target;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
if (selectedItems.length) {
|
2020-06-18 22:34:03 +03:00
|
|
|
const card = dom.parentWithClass(target, 'card');
|
2018-10-23 01:05:09 +03:00
|
|
|
if (card) {
|
2020-06-18 22:34:03 +03:00
|
|
|
const itemSelectionPanel = card.querySelector('.itemSelectionPanel');
|
2019-01-10 15:39:37 +03:00
|
|
|
if (itemSelectionPanel) {
|
|
|
|
return onItemSelectionPanelClick(e, itemSelectionPanel);
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
return false;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-05-04 12:44:12 +02:00
|
|
|
document.addEventListener('viewbeforehide', hideSelections);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-06-18 22:34:03 +03:00
|
|
|
export default function (options) {
|
|
|
|
const self = this;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-06-18 22:34:03 +03:00
|
|
|
const container = options.container;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
function onTapHold(e) {
|
2020-06-18 22:34:03 +03:00
|
|
|
const card = dom.parentWithClass(e.target, 'card');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (card) {
|
|
|
|
showSelections(card);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
e.preventDefault();
|
|
|
|
// It won't have this if it's a hammer event
|
|
|
|
if (e.stopPropagation) {
|
|
|
|
e.stopPropagation();
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
function getTouches(e) {
|
|
|
|
return e.changedTouches || e.targetTouches || e.touches;
|
|
|
|
}
|
|
|
|
|
2020-06-18 22:34:03 +03:00
|
|
|
let touchTarget;
|
|
|
|
let touchStartTimeout;
|
|
|
|
let touchStartX;
|
|
|
|
let touchStartY;
|
2019-01-10 15:39:37 +03:00
|
|
|
function onTouchStart(e) {
|
2020-06-18 22:34:03 +03:00
|
|
|
const touch = getTouches(e)[0];
|
2019-01-10 15:39:37 +03:00
|
|
|
touchTarget = null;
|
|
|
|
touchStartX = 0;
|
|
|
|
touchStartY = 0;
|
|
|
|
|
|
|
|
if (touch) {
|
|
|
|
touchStartX = touch.clientX;
|
|
|
|
touchStartY = touch.clientY;
|
2020-06-18 22:34:03 +03:00
|
|
|
const element = touch.target;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (element) {
|
2020-06-18 22:34:03 +03:00
|
|
|
const card = dom.parentWithClass(element, 'card');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (card) {
|
|
|
|
if (touchStartTimeout) {
|
|
|
|
clearTimeout(touchStartTimeout);
|
|
|
|
touchStartTimeout = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
touchTarget = card;
|
|
|
|
touchStartTimeout = setTimeout(onTouchStartTimerFired, 550);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onTouchMove(e) {
|
|
|
|
if (touchTarget) {
|
2020-06-18 22:34:03 +03:00
|
|
|
const touch = getTouches(e)[0];
|
|
|
|
let deltaX;
|
|
|
|
let deltaY;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (touch) {
|
2020-06-18 22:34:03 +03:00
|
|
|
const touchEndX = touch.clientX || 0;
|
|
|
|
const touchEndY = touch.clientY || 0;
|
2019-01-10 15:39:37 +03:00
|
|
|
deltaX = Math.abs(touchEndX - (touchStartX || 0));
|
|
|
|
deltaY = Math.abs(touchEndY - (touchStartY || 0));
|
|
|
|
} else {
|
|
|
|
deltaX = 100;
|
|
|
|
deltaY = 100;
|
|
|
|
}
|
|
|
|
if (deltaX >= 5 || deltaY >= 5) {
|
|
|
|
onMouseOut(e);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onTouchEnd(e) {
|
|
|
|
onMouseOut(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onMouseDown(e) {
|
|
|
|
if (touchStartTimeout) {
|
|
|
|
clearTimeout(touchStartTimeout);
|
|
|
|
touchStartTimeout = null;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
touchTarget = e.target;
|
|
|
|
touchStartTimeout = setTimeout(onTouchStartTimerFired, 550);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onMouseOut(e) {
|
|
|
|
if (touchStartTimeout) {
|
|
|
|
clearTimeout(touchStartTimeout);
|
|
|
|
touchStartTimeout = null;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
touchTarget = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onTouchStartTimerFired() {
|
|
|
|
if (!touchTarget) {
|
|
|
|
return;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-06-18 22:34:03 +03:00
|
|
|
const card = dom.parentWithClass(touchTarget, 'card');
|
2019-01-10 15:39:37 +03:00
|
|
|
touchTarget = null;
|
|
|
|
|
|
|
|
if (card) {
|
|
|
|
showSelections(card);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function initTapHold(element) {
|
|
|
|
// mobile safari doesn't allow contextmenu override
|
|
|
|
if (browser.touch && !browser.safari) {
|
2020-05-04 12:44:12 +02:00
|
|
|
element.addEventListener('contextmenu', onTapHold);
|
2019-01-10 15:39:37 +03:00
|
|
|
} else {
|
2020-05-04 12:44:12 +02:00
|
|
|
dom.addEventListener(element, 'touchstart', onTouchStart, {
|
2019-01-10 15:39:37 +03:00
|
|
|
passive: true
|
|
|
|
});
|
2020-05-04 12:44:12 +02:00
|
|
|
dom.addEventListener(element, 'touchmove', onTouchMove, {
|
2019-01-10 15:39:37 +03:00
|
|
|
passive: true
|
|
|
|
});
|
2020-05-04 12:44:12 +02:00
|
|
|
dom.addEventListener(element, 'touchend', onTouchEnd, {
|
2019-01-10 15:39:37 +03:00
|
|
|
passive: true
|
|
|
|
});
|
2020-05-04 12:44:12 +02:00
|
|
|
dom.addEventListener(element, 'touchcancel', onTouchEnd, {
|
2019-01-10 15:39:37 +03:00
|
|
|
passive: true
|
|
|
|
});
|
2020-05-04 12:44:12 +02:00
|
|
|
dom.addEventListener(element, 'mousedown', onMouseDown, {
|
2019-01-10 15:39:37 +03:00
|
|
|
passive: true
|
|
|
|
});
|
2020-05-04 12:44:12 +02:00
|
|
|
dom.addEventListener(element, 'mouseleave', onMouseOut, {
|
2019-01-10 15:39:37 +03:00
|
|
|
passive: true
|
|
|
|
});
|
2020-05-04 12:44:12 +02:00
|
|
|
dom.addEventListener(element, 'mouseup', onMouseOut, {
|
2019-01-10 15:39:37 +03:00
|
|
|
passive: true
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
initTapHold(container);
|
|
|
|
|
|
|
|
if (options.bindOnClick !== false) {
|
2020-05-04 12:44:12 +02:00
|
|
|
container.addEventListener('click', onContainerClick);
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
self.onContainerClick = onContainerClick;
|
|
|
|
|
2020-06-18 22:34:03 +03:00
|
|
|
self.destroy = () => {
|
2020-05-04 12:44:12 +02:00
|
|
|
container.removeEventListener('click', onContainerClick);
|
|
|
|
container.removeEventListener('contextmenu', onTapHold);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-06-18 22:34:03 +03:00
|
|
|
const element = container;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-05-04 12:44:12 +02:00
|
|
|
dom.removeEventListener(element, 'touchstart', onTouchStart, {
|
2019-01-10 15:39:37 +03:00
|
|
|
passive: true
|
|
|
|
});
|
2020-05-04 12:44:12 +02:00
|
|
|
dom.removeEventListener(element, 'touchmove', onTouchMove, {
|
2019-01-10 15:39:37 +03:00
|
|
|
passive: true
|
|
|
|
});
|
2020-05-04 12:44:12 +02:00
|
|
|
dom.removeEventListener(element, 'touchend', onTouchEnd, {
|
2019-01-10 15:39:37 +03:00
|
|
|
passive: true
|
|
|
|
});
|
2020-05-04 12:44:12 +02:00
|
|
|
dom.removeEventListener(element, 'mousedown', onMouseDown, {
|
2019-01-10 15:39:37 +03:00
|
|
|
passive: true
|
|
|
|
});
|
2020-05-04 12:44:12 +02:00
|
|
|
dom.removeEventListener(element, 'mouseleave', onMouseOut, {
|
2019-01-10 15:39:37 +03:00
|
|
|
passive: true
|
|
|
|
});
|
2020-05-04 12:44:12 +02:00
|
|
|
dom.removeEventListener(element, 'mouseup', onMouseOut, {
|
2019-01-10 15:39:37 +03:00
|
|
|
passive: true
|
|
|
|
});
|
|
|
|
};
|
2020-06-18 22:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* eslint-enable indent */
|