From d7bb32b760f18d6e744464dbdaddb500615cbc7d Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 18 Jul 2016 14:50:00 -0400 Subject: [PATCH] add helpers --- .../emby-webcomponents/dom.js | 47 +++++++++++++++++++ .../emby-itemscontainer.js | 17 +------ .../emby-webcomponents/itemcontextmenu.js | 3 ++ .../itemhovermenu/itemhovermenu.js | 39 ++++----------- .../multiselect/multiselect.js | 38 +++------------ .../emby-webcomponents/shortcuts.js | 42 ++++------------- 6 files changed, 76 insertions(+), 110 deletions(-) create mode 100644 dashboard-ui/bower_components/emby-webcomponents/dom.js diff --git a/dashboard-ui/bower_components/emby-webcomponents/dom.js b/dashboard-ui/bower_components/emby-webcomponents/dom.js new file mode 100644 index 0000000000..d06e621555 --- /dev/null +++ b/dashboard-ui/bower_components/emby-webcomponents/dom.js @@ -0,0 +1,47 @@ +define([], function () { + + function parentWithAttribute(elem, name, value) { + + while ((value ? elem.getAttribute(name) != value : !elem.getAttribute(name))) { + elem = elem.parentNode; + + if (!elem || !elem.getAttribute) { + return null; + } + } + + return elem; + } + + function parentWithTag(elem, tagName) { + + while (elem.tagName != tagName) { + elem = elem.parentNode; + + if (!elem) { + return null; + } + } + + return elem; + } + + function parentWithClass(elem, className) { + + while (!elem.classList || !elem.classList.contains(className)) { + elem = elem.parentNode; + + if (!elem) { + return null; + } + } + + return elem; + } + + return { + parentWithAttribute: parentWithAttribute, + parentWithClass: parentWithClass, + parentWithTag: parentWithTag + }; +}); \ No newline at end of file diff --git a/dashboard-ui/bower_components/emby-webcomponents/emby-itemscontainer/emby-itemscontainer.js b/dashboard-ui/bower_components/emby-webcomponents/emby-itemscontainer/emby-itemscontainer.js index a1703653ae..d8319ed3b4 100644 --- a/dashboard-ui/bower_components/emby-webcomponents/emby-itemscontainer/emby-itemscontainer.js +++ b/dashboard-ui/bower_components/emby-webcomponents/emby-itemscontainer/emby-itemscontainer.js @@ -1,20 +1,7 @@ -define(['itemShortcuts', 'connectionManager', 'layoutManager', 'browser', 'registerElement'], function (itemShortcuts, connectionManager, layoutManager, browser) { +define(['itemShortcuts', 'connectionManager', 'layoutManager', 'browser', 'dom', 'registerElement'], function (itemShortcuts, connectionManager, layoutManager, browser, dom) { var ItemsContainerProtoType = Object.create(HTMLDivElement.prototype); - function parentWithAttribute(elem, name) { - - while (!elem.getAttribute(name)) { - elem = elem.parentNode; - - if (!elem || !elem.getAttribute) { - return null; - } - } - - return elem; - } - function onClick(e) { var itemsContainer = this; @@ -35,7 +22,7 @@ var itemsContainer = this; var target = e.target; - var card = parentWithAttribute(target, 'data-id'); + var card = dom.parentWithAttribute(target, 'data-id'); if (card) { diff --git a/dashboard-ui/bower_components/emby-webcomponents/itemcontextmenu.js b/dashboard-ui/bower_components/emby-webcomponents/itemcontextmenu.js index df3da7a0ad..686f00eb6e 100644 --- a/dashboard-ui/bower_components/emby-webcomponents/itemcontextmenu.js +++ b/dashboard-ui/bower_components/emby-webcomponents/itemcontextmenu.js @@ -443,6 +443,9 @@ define(['apphost', 'globalize', 'connectionManager', 'itemHelper', 'embyRouter', getResolveFunction(resolve, id, true)(); }); + break; + case 'canceltimer': + deleteTimer(itemId, parentWithClass(card, 'itemsContainer')); break; default: reject(); diff --git a/dashboard-ui/bower_components/emby-webcomponents/itemhovermenu/itemhovermenu.js b/dashboard-ui/bower_components/emby-webcomponents/itemhovermenu/itemhovermenu.js index e82e85bdce..1436d6f4da 100644 --- a/dashboard-ui/bower_components/emby-webcomponents/itemhovermenu/itemhovermenu.js +++ b/dashboard-ui/bower_components/emby-webcomponents/itemhovermenu/itemhovermenu.js @@ -1,21 +1,8 @@ -define(['connectionManager', 'itemHelper', 'mediaInfo', 'userdataButtons', 'playbackManager', 'globalize', 'css!./itemhovermenu', 'emby-button'], function (connectionManager, itemHelper, mediaInfo, userdataButtons, playbackManager, globalize) { +define(['connectionManager', 'itemHelper', 'mediaInfo', 'userdataButtons', 'playbackManager', 'globalize', 'dom', 'css!./itemhovermenu', 'emby-button'], function (connectionManager, itemHelper, mediaInfo, userdataButtons, playbackManager, globalize, dom) { var preventHover = false; var showOverlayTimeout; - function parentWithAttribute(elem, name) { - - while (!elem.getAttribute(name)) { - elem = elem.parentNode; - - if (!elem || !elem.getAttribute) { - return null; - } - } - - return elem; - } - function onHoverOut(e) { var elem = e.target; @@ -164,18 +151,6 @@ return html; } - function parentWithClass(elem, className) { - - while (!elem.classList || !elem.classList.contains(className)) { - elem = elem.parentNode; - - if (!elem) { - return null; - } - } - - return elem; - } function onShowTimerExpired(elem) { @@ -186,10 +161,16 @@ innerElem.classList.add('hide'); innerElem.classList.add('cardOverlayTarget'); - elem.parentNode.appendChild(innerElem); + var appendTo; + if (elem.classList.contains('cardImageContainer')) { + appendTo = dom.parentWithClass(elem, 'cardBox'); + } else { + appendTo = elem.parentNode; + } + appendTo.appendChild(innerElem); } - var dataElement = parentWithAttribute(elem, 'data-id'); + var dataElement = dom.parentWithAttribute(elem, 'data-id'); var id = dataElement.getAttribute('data-id'); var type = dataElement.getAttribute('data-type'); @@ -218,7 +199,7 @@ function onHoverIn(e) { var elem = e.target; - var card = parentWithClass(elem, 'cardImageContainer') || parentWithClass(elem, 'cardImage'); + var card = dom.parentWithClass(elem, 'cardImageContainer') || dom.parentWithClass(elem, 'cardImage'); if (!card) { return; diff --git a/dashboard-ui/bower_components/emby-webcomponents/multiselect/multiselect.js b/dashboard-ui/bower_components/emby-webcomponents/multiselect/multiselect.js index 7a932c186e..f60f9a3a7b 100644 --- a/dashboard-ui/bower_components/emby-webcomponents/multiselect/multiselect.js +++ b/dashboard-ui/bower_components/emby-webcomponents/multiselect/multiselect.js @@ -1,22 +1,9 @@ -define(['browser', 'appStorage', 'apphost', 'loading', 'connectionManager', 'globalize', 'embyRouter', 'css!./multiselect'], function (browser, appStorage, appHost, loading, connectionManager, globalize, embyRouter) { +define(['browser', 'appStorage', 'apphost', 'loading', 'connectionManager', 'globalize', 'embyRouter', 'dom', 'css!./multiselect'], function (browser, appStorage, appHost, loading, connectionManager, globalize, embyRouter, dom) { var selectedItems = []; var selectedElements = []; var currentSelectionCommandsPanel; - function parentWithClass(elem, className) { - - while (!elem.classList || !elem.classList.contains(className)) { - elem = elem.parentNode; - - if (!elem) { - return null; - } - } - - return elem; - } - function hideSelections() { var selectionCommandsPanel = currentSelectionCommandsPanel; @@ -66,7 +53,7 @@ function onItemSelectionPanelClick(e, itemSelectionPanel) { // toggle the checkbox, if it wasn't clicked on - if (!parentWithClass(e.target, 'chkItemSelect')) { + if (!dom.parentWithClass(e.target, 'chkItemSelect')) { var chkItemSelect = itemSelectionPanel.querySelector('.chkItemSelect'); if (chkItemSelect) { @@ -88,7 +75,7 @@ function updateItemSelection(chkItemSelect, selected) { - var id = parentWithClass(chkItemSelect, 'card').getAttribute('data-id'); + var id = dom.parentWithClass(chkItemSelect, 'card').getAttribute('data-id'); if (selected) { @@ -377,26 +364,13 @@ }); } - function parentWithAttribute(elem, name, value) { - - while ((value ? elem.getAttribute(name) != value : !elem.getAttribute(name))) { - elem = elem.parentNode; - - if (!elem || !elem.getAttribute) { - return null; - } - } - - return elem; - } - function dispatchNeedsRefresh() { var elems = []; [].forEach.call(selectedElements, function (i) { - var container = parentWithAttribute(i, 'is', 'emby-itemscontainer'); + var container = dom.parentWithAttribute(i, 'is', 'emby-itemscontainer'); if (container && elems.indexOf(container) == -1) { elems.push(container); @@ -466,7 +440,7 @@ if (selectedItems.length) { - var card = parentWithClass(target, 'card'); + var card = dom.parentWithClass(target, 'card'); if (card) { var itemSelectionPanel = card.querySelector('.itemSelectionPanel'); if (itemSelectionPanel) { @@ -488,7 +462,7 @@ function onTapHold(e) { - var card = parentWithClass(e.target, 'card'); + var card = dom.parentWithClass(e.target, 'card'); if (card) { diff --git a/dashboard-ui/bower_components/emby-webcomponents/shortcuts.js b/dashboard-ui/bower_components/emby-webcomponents/shortcuts.js index 5826915fae..3a269dae82 100644 --- a/dashboard-ui/bower_components/emby-webcomponents/shortcuts.js +++ b/dashboard-ui/bower_components/emby-webcomponents/shortcuts.js @@ -1,4 +1,4 @@ -define(['playbackManager', 'inputManager', 'connectionManager', 'embyRouter', 'globalize', 'loading'], function (playbackManager, inputManager, connectionManager, embyRouter, globalize, loading) { +define(['playbackManager', 'inputManager', 'connectionManager', 'embyRouter', 'globalize', 'loading', 'dom'], function (playbackManager, inputManager, connectionManager, embyRouter, globalize, loading, dom) { function playAllFromHere(card, serverId, queue) { @@ -92,7 +92,7 @@ define(['playbackManager', 'inputManager', 'connectionManager', 'embyRouter', 'g function getItem(button) { - button = parentWithAttribute(button, 'data-id'); + button = dom.parentWithAttribute(button, 'data-id'); var serverId = button.getAttribute('data-serverid'); var id = button.getAttribute('data-id'); var type = button.getAttribute('data-type'); @@ -109,13 +109,13 @@ define(['playbackManager', 'inputManager', 'connectionManager', 'embyRouter', 'g getItem(card).then(function (item) { - var itemsContainer = options.itemsContainer || parentWithAttribute(card, 'is', 'emby-itemscontainer'); + var itemsContainer = options.itemsContainer || dom.parentWithAttribute(card, 'is', 'emby-itemscontainer'); var playlistId = itemsContainer ? itemsContainer.getAttribute('data-playlistid') : null; var collectionId = itemsContainer ? itemsContainer.getAttribute('data-collectionid') : null; if (playlistId) { - var elem = parentWithAttribute(card, 'data-playlistitemid'); + var elem = dom.parentWithAttribute(card, 'data-playlistitemid'); item.PlaylistItemId = elem ? elem.getAttribute('data-playlistitemid') : null; } @@ -173,7 +173,7 @@ define(['playbackManager', 'inputManager', 'connectionManager', 'embyRouter', 'g var id = card.getAttribute('data-id'); if (!id) { - card = parentWithAttribute(card, 'data-id'); + card = dom.parentWithAttribute(card, 'data-id'); id = card.getAttribute('data-id'); } @@ -363,7 +363,7 @@ define(['playbackManager', 'inputManager', 'connectionManager', 'embyRouter', 'g function onClick(e) { - var card = parentWithClass(e.target, 'itemAction'); + var card = dom.parentWithClass(e.target, 'itemAction'); if (card) { @@ -371,7 +371,7 @@ define(['playbackManager', 'inputManager', 'connectionManager', 'embyRouter', 'g var action = actionElement.getAttribute('data-action'); if (!action) { - actionElement = parentWithAttribute(actionElement, 'data-action'); + actionElement = dom.parentWithAttribute(actionElement, 'data-action'); action = actionElement.getAttribute('data-action'); } @@ -385,37 +385,11 @@ define(['playbackManager', 'inputManager', 'connectionManager', 'embyRouter', 'g } } - function parentWithAttribute(elem, name, value) { - - while ((value ? elem.getAttribute(name) != value : !elem.getAttribute(name))) { - elem = elem.parentNode; - - if (!elem || !elem.getAttribute) { - return null; - } - } - - return elem; - } - - function parentWithClass(elem, className) { - - while (!elem.classList || !elem.classList.contains(className)) { - elem = elem.parentNode; - - if (!elem) { - return null; - } - } - - return elem; - } - function onCommand(e) { var cmd = e.detail.command; if (cmd == 'play' || cmd == 'record' || cmd == 'menu' || cmd == 'info') { - var card = parentWithClass(e.target, 'itemAction'); + var card = dom.parentWithClass(e.target, 'itemAction'); if (card) { executeAction(card, card, cmd);