jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/userdatabuttons/emby-playstatebutton.js
2017-06-19 14:09:05 -04:00

1 line
2.9 KiB
JavaScript

define(["connectionManager","serverNotifications","events","globalize","emby-button"],function(connectionManager,serverNotifications,events,globalize,EmbyButtonPrototype){"use strict";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];handler&&(events.off(serverNotifications,name,handler),instance[name]=null)}function onClick(e){var button=this,id=button.getAttribute("data-id"),serverId=button.getAttribute("data-serverid"),apiClient=connectionManager.getApiClient(serverId);button.classList.contains("playstatebutton-played")?(apiClient.markUnplayed(apiClient.getCurrentUserId(),id,new Date),setState(button,!1)):(apiClient.markPlayed(apiClient.getCurrentUserId(),id,new Date),setState(button,!0))}function onUserDataChanged(e,apiClient,userData){var button=this;userData.ItemId===button.getAttribute("data-id")&&setState(button,userData.Played)}function setState(button,played,updateAttribute){played?button.classList.add("playstatebutton-played"):button.classList.remove("playstatebutton-played"),updateAttribute!==!1&&button.setAttribute("data-played",played)}function setTitle(button,itemType){"AudioBook"!==itemType&&"AudioPodcast"!==itemType?button.title=globalize.translate("sharedcomponents#MarkWatched"):button.title=globalize.translate("sharedcomponents#MarkPlayed")}function clearEvents(button){button.removeEventListener("click",onClick),removeNotificationEvent(button,"UserDataChanged")}function bindEvents(button){clearEvents(button),button.addEventListener("click",onClick),addNotificationEvent(button,"UserDataChanged",onUserDataChanged)}var EmbyPlaystateButtonPrototype=Object.create(EmbyButtonPrototype);EmbyPlaystateButtonPrototype.createdCallback=function(){EmbyButtonPrototype.createdCallback&&EmbyButtonPrototype.createdCallback.call(this)},EmbyPlaystateButtonPrototype.attachedCallback=function(){EmbyButtonPrototype.attachedCallback&&EmbyButtonPrototype.attachedCallback.call(this);var itemId=this.getAttribute("data-id"),serverId=this.getAttribute("data-serverid");itemId&&serverId&&(setState(this,"true"===this.getAttribute("data-played"),!1),bindEvents(this),setTitle(this,this.getAttribute("data-type")))},EmbyPlaystateButtonPrototype.detachedCallback=function(){EmbyButtonPrototype.detachedCallback&&EmbyButtonPrototype.detachedCallback.call(this),clearEvents(this)},EmbyPlaystateButtonPrototype.setItem=function(item){if(item){this.setAttribute("data-id",item.Id),this.setAttribute("data-serverid",item.ServerId);var played=item.UserData&&item.UserData.Played;setState(this,played),bindEvents(this),setTitle(this,item.Type)}else this.removeAttribute("data-id"),this.removeAttribute("data-serverid"),this.removeAttribute("data-played"),clearEvents(this)},document.registerElement("emby-playstatebutton",{prototype:EmbyPlaystateButtonPrototype,extends:"button"})});