@@ -9,36 +9,53 @@ angular.module("umbraco").controller("Umbraco.PropertyEditors.DropdownController
9
9
10
10
//map the user config
11
11
angular . extend ( config , $scope . model . config ) ;
12
+
12
13
//map back to the model
13
14
$scope . model . config = config ;
14
15
15
- if ( angular . isArray ( $scope . model . config . items ) ) {
16
-
17
- //ensure the items are sorted by the provided sort order
18
- $scope . model . config . items . sort ( function ( a , b ) { return ( a . sortOrder > b . sortOrder ) ? 1 : ( ( b . sortOrder > a . sortOrder ) ? - 1 : 0 ) ; } ) ;
16
+ function convertArrayToDictionaryArray ( model ) {
17
+ //now we need to format the items in the dictionary because we always want to have an array
18
+ var newItems = [ ] ;
19
+ for ( var i = 0 ; i < model . length ; i ++ ) {
20
+ newItems . push ( { id : model [ i ] , sortOrder : 0 , value : model [ i ] } ) ;
21
+ }
19
22
23
+ return newItems ;
20
24
}
21
- else if ( angular . isObject ( $scope . model . config . items ) ) {
22
25
26
+
27
+ function convertObjectToDictionaryArray ( model ) {
23
28
//now we need to format the items in the dictionary because we always want to have an array
24
29
var newItems = [ ] ;
25
30
var vals = _ . values ( $scope . model . config . items ) ;
26
31
var keys = _ . keys ( $scope . model . config . items ) ;
32
+
27
33
for ( var i = 0 ; i < vals . length ; i ++ ) {
28
34
var label = vals [ i ] . value ? vals [ i ] . value : vals [ i ] ;
29
35
newItems . push ( { id : keys [ i ] , sortOrder : vals [ i ] . sortOrder , value : label } ) ;
30
36
}
31
37
32
- //ensure the items are sorted by the provided sort order
33
- newItems . sort ( function ( a , b ) { return ( a . sortOrder > b . sortOrder ) ? 1 : ( ( b . sortOrder > a . sortOrder ) ? - 1 : 0 ) ; } ) ;
38
+ return newItems ;
39
+ }
34
40
35
- //re-assign
36
- $scope . model . config . items = newItems ;
41
+ if ( angular . isArray ( $scope . model . config . items ) ) {
42
+ //PP: I dont think this will happen, but we have tests that expect it to happen..
43
+ //if array is simple values, convert to array of objects
44
+ if ( ! angular . isObject ( $scope . model . config . items [ 0 ] ) ) {
45
+ $scope . model . config . items = convertArrayToDictionaryArray ( $scope . model . config . items ) ;
46
+ }
47
+ }
48
+ else if ( angular . isObject ( $scope . model . config . items ) ) {
49
+ $scope . model . config . items = convertObjectToDictionaryArray ( $scope . model . config . items ) ;
37
50
}
38
51
else {
39
52
throw "The items property must be either an array or a dictionary" ;
40
53
}
41
54
55
+
56
+ //sort the values
57
+ $scope . model . config . items . sort ( function ( a , b ) { return ( a . sortOrder > b . sortOrder ) ? 1 : ( ( b . sortOrder > a . sortOrder ) ? - 1 : 0 ) ; } ) ;
58
+
42
59
//now we need to check if the value is null/undefined, if it is we need to set it to "" so that any value that is set
43
60
// to "" gets selected by default
44
61
if ( $scope . model . value === null || $scope . model . value === undefined ) {
0 commit comments