2017-05-23 09:44:11 -07:00
|
|
|
define(["events","apiclient","appStorage"],function(events,apiClientFactory,appStorage){"use strict";function paramsToString(params){var values=[];for(var key in params){var value=params[key];null!==value&&void 0!==value&&""!==value&&values.push(encodeURIComponent(key)+"="+encodeURIComponent(value))}return values.join("&")}function resolveFailure(instance,resolve){resolve({State:ConnectionState.Unavailable,ConnectUser:instance.connectUser()})}function mergeServers(credentialProvider,list1,list2){for(var i=0,length=list2.length;i<length;i++)credentialProvider.addOrUpdateServer(list1,list2[i]);return list1}function updateServerInfo(server,systemInfo){server.Name=systemInfo.ServerName,server.Id=systemInfo.Id,systemInfo.LocalAddress&&(server.LocalAddress=systemInfo.LocalAddress),systemInfo.WanAddress&&(server.RemoteAddress=systemInfo.WanAddress),systemInfo.MacAddress&&(server.WakeOnLanInfos=[{MacAddress:systemInfo.MacAddress}])}function getEmbyServerUrl(baseUrl,handler){return baseUrl+"/emby/"+handler}function getFetchPromise(request){var headers=request.headers||{};"json"===request.dataType&&(headers.accept="application/json");var fetchRequest={headers:headers,method:request.type,credentials:"same-origin"},contentType=request.contentType;return request.data&&("string"==typeof request.data?fetchRequest.body=request.data:(fetchRequest.body=paramsToString(request.data),contentType=contentType||"application/x-www-form-urlencoded; charset=UTF-8")),contentType&&(headers["Content-Type"]=contentType),request.timeout?fetchWithTimeout(request.url,fetchRequest,request.timeout):fetch(request.url,fetchRequest)}function fetchWithTimeout(url,options,timeoutMs){return console.log("fetchWithTimeout: timeoutMs: "+timeoutMs+", url: "+url),new Promise(function(resolve,reject){var timeout=setTimeout(reject,timeoutMs);options=options||{},options.credentials="same-origin",fetch(url,options).then(function(response){clearTimeout(timeout),console.log("fetchWithTimeout: succeeded connecting to url: "+url),resolve(response)},function(error){clearTimeout(timeout),console.log("fetchWithTimeout: timed out connecting to url: "+url),reject()})})}function ajax(request){if(!request)throw new Error("Request cannot be null");return request.headers=request.headers||{},console.log("ConnectionManager requesting url: "+request.url),getFetchPromise(request).then(function(response){return console.log("ConnectionManager response status: "+response.status+", url: "+request.url),response.status<400?"json"===request.dataType||"application/json"===request.headers.accept?response.json():response:Promise.reject(response)},function(err){throw console.log("ConnectionManager request failed to url: "+request.url),err})}function tryConnect(url,timeout){return url=getEmbyServerUrl(url,"system/info/public"),console.log("tryConnect url: "+url),ajax({type:"GET",url:url,dataType:"json",timeout:timeout||defaultTimeout})}function getConnectUrl(handler){return"https://connect.emby.media/service/"+handler}var defaultTimeout=2e4,ConnectionState={Unavailable:0,ServerSelection:1,ServerSignIn:2,SignedIn:3,ConnectSignIn:4,ServerUpdateNeeded:5},ConnectionMode={Local:0,Remote:1,Manual:2},ServerInfo={getServerAddress:function(server,mode){switch(mode){case ConnectionMode.Local:return server.LocalAddress;case ConnectionMode.Manual:return server.ManualAddress;case ConnectionMode.Remote:return server.RemoteAddress;default:return server.ManualAddress||server.LocalAddress||server.RemoteAddress}}},ConnectionManager=function(credentialProvider,appName,appVersion,deviceName,deviceId,capabilities,devicePixelRatio){function onConnectUserSignIn(user){appStorage.removeItem("lastLocalServerId"),connectUser=user,events.trigger(self,"connectusersignedin",[user])}function getOrAddApiClient(server,connectionMode){var apiClient=self.getApiClient(server.Id);if(!apiClient){var url=ServerInfo.getServerAddress(server,connectionMode);apiClient=new apiClientFactory(url,appName,appVersion,deviceName,deviceId,devicePixelRatio),self._apiClients.push(apiClient),apiClient.serverInfo(server),apiClient.onAuthenticated=funct
|