@@ -34,12 +34,46 @@ function BindService($filter,
34
34
35
35
ctrl . $onInit = function ( ) {
36
36
// TODO: handle not having any service instances, or handle path where coming into binding from svc instance
37
- // TODO: need serviceInstances as an array first
38
- // ctrl.serviceToBind = _.get(_.head(ctrl.serviceInstances), 'metadata.name');
39
-
40
37
ctrl . gotoStep ( ctrl . steps [ 0 ] ) ;
41
38
} ;
42
39
40
+ var statusCondition = $filter ( 'statusCondition' ) ;
41
+ ctrl . $onChanges = function ( changes ) {
42
+ if ( changes . serviceInstances && ! ctrl . serviceToBind ) {
43
+ var newestReady ;
44
+ var newestNotReady ;
45
+ _ . each ( ctrl . serviceInstances , function ( instance ) {
46
+ var ready = _ . get ( statusCondition ( instance , 'Ready' ) , 'status' , 'False' ) === 'True' ;
47
+ if ( ready && ( ! newestReady || instance . metadata . creationTimestamp > newestReady . metadata . creationTimestamp ) ) {
48
+ newestReady = instance ;
49
+ }
50
+ if ( ! ready && ( ! newestNotReady || instance . metadata . creationTimestamp > newestNotReady . metadata . creationTimestamp ) ) {
51
+ newestNotReady = instance ;
52
+ }
53
+ } ) ;
54
+ ctrl . serviceToBind = _ . get ( newestReady , 'metadata.name' ) || _ . get ( newestNotReady , 'metadata.name' ) ;
55
+ }
56
+
57
+ // wait till both service instances and service classes are available so that the sort is stable and items dont jump around
58
+ if ( changes . serviceInstances && ctrl . serviceClasses || changes . serviceClasses && ctrl . serviceInstances ) {
59
+ var instances = _ . toArray ( ctrl . serviceInstances ) ;
60
+ instances . sort ( function ( left , right ) {
61
+ var leftName = _ . get ( ctrl . serviceClasses , [ left . spec . serviceClassName , 'osbMetadata' , 'displayName' ] ) || left . spec . serviceClassName ;
62
+ var rightName = _ . get ( ctrl . serviceClasses , [ left . spec . serviceClassName , 'osbMetadata' , 'displayName' ] ) || right . spec . serviceClassName ;
63
+
64
+ // Fall back to sorting by `metadata.name` if the display names are the
65
+ // same so that the sort is stable.
66
+ if ( leftName === rightName ) {
67
+ leftName = _ . get ( left , 'metadata.name' , '' ) ;
68
+ rightName = _ . get ( right , 'metadata.name' , '' ) ;
69
+ }
70
+
71
+ return leftName . localeCompare ( rightName ) ;
72
+ } ) ;
73
+ ctrl . orderedServiceInstances = instances ;
74
+ }
75
+ } ;
76
+
43
77
var gotoStepID = function ( id ) {
44
78
var step = _ . find ( ctrl . steps , { id : id } ) ;
45
79
ctrl . gotoStep ( step ) ;
0 commit comments