define(["datetime","jQuery","material-icons"],function(datetime,$){"use strict";function getNode(item,folderState,selected){var htmlName=getNodeInnerHtml(item),node={id:item.Id,text:htmlName,state:{opened:item.IsFolder&&"open"==folderState,selected:selected},li_attr:{serveritemtype:item.Type,collectiontype:item.CollectionType}};return item.IsFolder?(node.children=[{text:"Loading...",icon:!1}],node.icon=!1):node.icon=!1,node.state.opened&&(node.li_attr.loadedFromServer=!0),selected&&(selectedNodeId=item.Id),node}function getNodeInnerHtml(item){var name=item.Name;item.Number&&(name=item.Number+" - "+name),null!=item.IndexNumber&&"Season"!=item.Type&&(name=item.IndexNumber+" - "+name);var cssClass="editorNode";"Offline"==item.LocationType&&(cssClass+=" offlineEditorNode");var htmlName="
";if(item.LockData&&(htmlName+='lock'),htmlName+=name,item.ImageTags&&item.ImageTags.Primary||(htmlName+=''),item.BackdropImageTags&&item.BackdropImageTags.length||"Episode"!==item.Type&&"Season"!==item.Type&&"Audio"!==item.MediaType&&"TvChannel"!==item.Type&&"MusicAlbum"!==item.Type&&(htmlName+=''),item.ImageTags&&item.ImageTags.Logo||"Movie"!=item.Type&&"Trailer"!=item.Type&&"Series"!=item.Type&&"MusicArtist"!=item.Type&&"BoxSet"!=item.Type||(htmlName+=''),"Episode"==item.Type&&"Virtual"==item.LocationType)try{item.PremiereDate&&(new Date).getTime()>=datetime.parseISO8601Date(item.PremiereDate,!0).getTime()&&(htmlName+='')}catch(err){}return htmlName+="
"}function loadChildrenOfRootNode(page,scope,callback){ApiClient.getLiveTvChannels({limit:0}).then(function(result){var nodes=[];nodes.push({id:"MediaFolders",text:Globalize.translate("HeaderMediaFolders"),state:{opened:!0},li_attr:{itemtype:"mediafolders",loadedFromServer:!0},icon:!1}),result.TotalRecordCount&&nodes.push({id:"livetv",text:Globalize.translate("HeaderLiveTV"),state:{opened:!1},li_attr:{itemtype:"livetv"},children:[{text:"Loading...",icon:!1}],icon:!1}),callback.call(scope,nodes),nodesToLoad.push("MediaFolders")})}function loadLiveTvChannels(service,openItems,callback){ApiClient.getLiveTvChannels({ServiceName:service,AddCurrentProgram:!1}).then(function(result){var nodes=result.Items.map(function(i){var state=openItems.indexOf(i.Id)==-1?"closed":"open";return getNode(i,state,!1)});callback(nodes)})}function loadMediaFolders(page,scope,openItems,callback){ApiClient.getJSON(ApiClient.getUrl("Library/MediaFolders")).then(function(result){var nodes=result.Items.map(function(n){var state=openItems.indexOf(n.Id)==-1?"closed":"open";return getNode(n,state,!1)});callback.call(scope,nodes);for(var i=0,length=nodes.length;ia",page)[0];if(null!=elem&&($(".editorNode",elem).remove(),$(elem).append(getNodeInnerHtml(item)),item.IsFolder)){var tree=jQuery.jstree._reference(".libraryTree"),currentNode=tree._get_node(null,!1);tree.refresh(currentNode)}}function setCurrentItemId(id){itemId=id}function getCurrentItemId(){if(itemId)return itemId;var url=window.location.hash||window.location.href;return getParameterByName("id",url)}var selectedNodeId,nodesToLoad=[];$(document).on("itemsaved",".metadataEditorPage",function(e,item){updateEditorNode(this,item)}).on("pagebeforeshow",".metadataEditorPage",function(){require(["css!css/metadataeditor.css"])}).on("pagebeforeshow",".metadataEditorPage",function(){var page=this;Dashboard.getCurrentUser().then(function(user){var id=getCurrentItemId();id?ApiClient.getAncestorItems(id,user.Id).then(function(ancestors){var ids=ancestors.map(function(i){return i.Id});initializeTree(page,user,ids,id)}):initializeTree(page,user,[])})}).on("pagebeforehide",".metadataEditorPage",function(){var page=this;$(".libraryTree",page).off("select_node.jstree",onNodeSelect).off("open_node.jstree",onNodeOpen).off("load_node.jstree",onNodeLoad)});var itemId;window.MetadataEditor={getItemPromise:function(){var currentItemId=getCurrentItemId();return currentItemId?ApiClient.getItem(Dashboard.getCurrentUserId(),currentItemId):ApiClient.getRootFolder(Dashboard.getCurrentUserId())},getCurrentItemId:getCurrentItemId,setCurrentItemId:setCurrentItemId}});