@@ -5,18 +5,16 @@ require('app')
55 */
66function ControllerInstance (
77 async ,
8- determineActiveAccount ,
8+ $filter ,
99 errs ,
1010 keypather ,
1111 OpenItems ,
1212 QueryAssist ,
13- fetchUser ,
13+ $rootScope ,
1414 $scope ,
1515 $state ,
16- $log ,
1716 $stateParams ,
18- exists ,
19- user
17+ fetchUser
2018) {
2119 var dataInstance = $scope . dataInstance = {
2220 data : { } ,
@@ -38,7 +36,37 @@ function ControllerInstance(
3836 // in shows/hides file-menu
3937 in : false
4038 } ;
41-
39+ if ( ! $stateParams . instanceName ) {
40+ var unwatch = $rootScope . $watch ( 'dataApp.data.instances' , function ( n , p ) {
41+ if ( n !== p && n ) {
42+ unwatch ( ) ;
43+ if ( n . models . length ) {
44+ var models = $filter ( 'orderBy' ) ( n . models , 'attrs.name' ) ;
45+ $state . go ( 'instance.instance' , {
46+ instanceName : models [ 0 ] . attrs . name ,
47+ userName : $stateParams . userName
48+ } , { location : 'replace' } ) ;
49+ } else {
50+ $state . go ( 'instance.new' , {
51+ userName : $stateParams . userName
52+ } , { location : 'replace' } ) ;
53+ }
54+ }
55+ } ) ;
56+ } else if ( $stateParams . instanceName && $stateParams . userName ) {
57+ async . waterfall ( [
58+ fetchUser ,
59+ fetchInstance
60+ ] , function ( err ) {
61+ if ( err ) {
62+ $state . go ( 'instance.instance' , {
63+ instanceName : '' ,
64+ userName : $stateParams . userName
65+ } , { reload : true } ) ;
66+ }
67+ errs . handler ( err ) ;
68+ } ) ;
69+ }
4270 // watch showExplorer (toggle when user clicks file menu)
4371 // if no running container, return early (user shouldn't be able to even click
4472 // button in this situation)
@@ -88,68 +116,29 @@ function ControllerInstance(
88116 }
89117 }
90118
91- async . waterfall ( [
92- determineActiveAccount ,
93- function ( activeAccount , cb ) {
94- data . activeAccount = activeAccount ;
95- $scope . safeApply ( ) ;
96- cb ( ) ;
97- } ,
98- function ( cb ) {
99- fetchUser ( function ( err , user ) {
100- if ( err ) { return cb ( err ) ; }
101- data . user = user ;
102- $scope . safeApply ( ) ;
103- cb ( ) ;
104- } ) ;
105- } ,
106- fetchInstance ,
107- fetchInstances
108- ] , errs . handler ) ;
109-
110- // This is to fetch the list of instances. This is separate so the page can load quickly
111- // since it will have its instance. Only the modals use this list
112- function fetchInstances ( cb ) {
113- new QueryAssist ( data . user , cb )
114- . wrapFunc ( 'fetchInstances' , cb )
115- . query ( {
116- githubUsername : $stateParams . userName
117- } )
118- . cacheFetch ( function ( instances , cached , cb ) {
119- if ( ! cached && instances . models . length === 0 ) {
120- return cb ( new Error ( 'instance not found' ) ) ;
121- }
122- data . instances = instances ;
123- $scope . safeApply ( ) ;
124- cb ( ) ;
125- } )
126- . resolve ( function ( err ) {
127- cb ( err ) ;
128- } )
129- . go ( ) ;
130- }
131-
132- function fetchInstance ( cb ) {
133- new QueryAssist ( data . user , cb )
134- . wrapFunc ( 'fetchInstances' )
135- . query ( {
136- githubUsername : $stateParams . userName ,
137- name : $stateParams . instanceName
138- } )
139- . cacheFetch ( function ( instances , cached , cb ) {
140- if ( ! cached && instances . models . length === 0 ) {
141- return cb ( new Error ( 'Instance not found' ) ) ;
142- }
143- var instance = instances . models [ 0 ] ;
144- data . instance = instance ;
145- data . instance . state = { } ;
146- $scope . safeApply ( ) ;
147- cb ( ) ;
148- } )
149- . resolve ( function ( err ) {
150- cb ( err ) ;
151- } )
152- . go ( ) ;
119+ function fetchInstance ( user , cb ) {
120+ if ( $stateParams . instanceName && $stateParams . userName ) {
121+ new QueryAssist ( user , cb )
122+ . wrapFunc ( 'fetchInstances' )
123+ . query ( {
124+ githubUsername : $stateParams . userName ,
125+ name : $stateParams . instanceName
126+ } )
127+ . cacheFetch ( function ( instances , cached , cb ) {
128+ data . instance = keypather . get ( instances , 'models[0]' ) ;
129+ if ( ! data . instance ) {
130+ cb ( new Error ( 'Could not find instance on server' ) ) ;
131+ } else {
132+ data . instance . state = { } ;
133+ $scope . safeApply ( ) ;
134+ cb ( ) ;
135+ }
136+ } )
137+ . resolve ( function ( err ) {
138+ cb ( err ) ;
139+ } )
140+ . go ( ) ;
141+ }
153142 }
154143
155144 function watchForContainerBeforeDisplayTabs ( container ) {
0 commit comments