2017-02-14 14:11:06 -07:00
|
|
|
define(["apiclientcore","localassetmanager","events"],function(apiclientcorefactory,localassetmanager,events){"use strict";var localPrefix="local:",localViewPrefix="localview:";return function(serverAddress,clientName,applicationVersion,deviceName,deviceId,devicePixelRatio){function getCurrentUser(){if(apiclientcore.isLoggedIn())return apiclientcore.getCurrentUser();var id=apiclientcore.getCurrentUserId(),user=localassetmanager.loadOfflineUser(id);return Promise.resolve(user)}function getUserViews(userId){return apiclientcore.getUserViews(userId).then(function(result){var serverInfo=apiclientcore.serverInfo();return serverInfo?getLocalView(serverInfo.Id,userId).then(function(localView){return localView&&(result.Items.push(localView),result.TotalRecordCount++),Promise.resolve(result)}):Promise.resolve(result)})}function getLocalView(serverId,userId){return localassetmanager.getViews(serverId,userId).then(function(views){var localView=null;return views.length>0&&(localView={Name:"Offline Items",ServerId:serverId,Id:"localview",Type:"localview"}),Promise.resolve(localView)})}function getItems(userId,options){var i,serverInfo=apiclientcore.serverInfo();if(serverInfo&&"localview"===options.ParentId)return localassetmanager.getViews(serverInfo.Id,userId).then(function(items){var result={Items:items,TotalRecordCount:items.length};return Promise.resolve(result)});if(serverInfo&&options&&(isLocalId(options.ParentId)||isLocalViewId(options.ParentId)))return localassetmanager.getViewItems(serverInfo.Id,userId,options).then(function(items){items.forEach(function(item){adjustGuidProperties(item)}),items.sort(function(a,b){return a.SortName.toLowerCase().localeCompare(b.SortName.toLowerCase())});var result={Items:items,TotalRecordCount:items.length};return Promise.resolve(result)});if(options&&options.ExcludeItemIds&&options.ExcludeItemIds.length){var exItems=options.ExcludeItemIds.split(",");for(i=0;i<exItems.length;i++)if(isLocalId(exItems[i]))return Promise.resolve(createEmptyList())}else if(options&&options.Ids&&options.Ids.length){var ids=options.Ids.split(","),hasLocal=!1;for(i=0;i<ids.length;i++)isLocalId(ids[i])&&(hasLocal=!0);if(hasLocal)return localassetmanager.getItemsFromIds(serverInfo.Id,ids).then(function(items){items.forEach(function(item){adjustGuidProperties(item)});var result={Items:items,TotalRecordCount:items.length};return Promise.resolve(result)})}return apiclientcore.getItems(userId,options)}function getItem(userId,itemId){itemId&&(itemId=itemId.toString());var serverInfo;return isLocalViewId(itemId)&&(serverInfo=apiclientcore.serverInfo())?localassetmanager.getViews(serverInfo.Id,userId).then(function(items){var views=items.filter(function(item){return item.Id===itemId});return views.length>0?Promise.resolve(views[0]):Promise.reject()}):isLocalId(itemId)&&(serverInfo=apiclientcore.serverInfo())?localassetmanager.getLocalItem(serverInfo.Id,stripLocalPrefix(itemId)).then(function(item){return adjustGuidProperties(item.Item),Promise.resolve(item.Item)}):apiclientcore.getItem(userId,itemId)}function adjustGuidProperties(downloadedItem){downloadedItem.Id=convertGuidToLocal(downloadedItem.Id),downloadedItem.SeriesId=convertGuidToLocal(downloadedItem.SeriesId),downloadedItem.SeasonId=convertGuidToLocal(downloadedItem.SeasonId),downloadedItem.AlbumId=convertGuidToLocal(downloadedItem.AlbumId),downloadedItem.ParentId=convertGuidToLocal(downloadedItem.ParentId),downloadedItem.ParentThumbItemId=convertGuidToLocal(downloadedItem.ParentThumbItemId),downloadedItem.ParentPrimaryImageItemId=convertGuidToLocal(downloadedItem.ParentPrimaryImageItemId),downloadedItem.PrimaryImageItemId=convertGuidToLocal(downloadedItem.PrimaryImageItemId),downloadedItem.ParentLogoItemId=convertGuidToLocal(downloadedItem.ParentLogoItemId),downloadedItem.ParentBackdropItemID=convertGuidToLocal(downloadedItem.ParentBackdropItemID),downloadedItem.ParentBackdropImageTags=null}function convertGuidToLocal(guid){return guid?isLocalId(guid)?guid:"local:"+guid:null}function getNextUpEpisodes(options){return options.SeriesId&&isLocalId(options.SeriesId)?Pr
|