2017-03-27 12:31:49 -07:00
|
|
|
define(["events","apiclient","appStorage"],function(events,apiClientFactory,appStorage){"use strict";var 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 mergeServers(list1,list2){for(var i=0,length=list2.length;i<length;i++)credentialProvider.addOrUpdateServer(list1,list2[i]);return list1}function resolveFailure(resolve){resolve({State:ConnectionState.Unavailable,ConnectUser:self.connectUser()})}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 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 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 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),apiClients.push(apiClient),apiClient.serverInfo(server),apiClient.onAuthenticated=function(instance,result){onAuthenticated(instance,result,{},!0)},events.trigger(self,"apiclientcreated",[apiClient])}return console.log("returnin
|