|
1 | 1 | (function(angular, _) {
|
2 | 2 | 'use strict';
|
3 | 3 |
|
4 |
| - angular.module("umbraco").controller("RJP.MultiUrlPickerController", function($scope, dialogService, iconHelper, entityResource) { |
| 4 | + angular.module("umbraco").controller("RJP.MultiUrlPickerController", function($scope, $q, dialogService, iconHelper, entityResource) { |
5 | 5 | var documentIds = [];
|
6 | 6 | var mediaIds = [];
|
7 | 7 |
|
|
12 | 12 |
|
13 | 13 | if( $scope.model.value ) {
|
14 | 14 | _.each($scope.model.value, function( item, i ) {
|
15 |
| - $scope.renderModel.push(new Link(item)); |
16 | 15 | if( item.id ) {
|
17 | 16 | (item.isMedia ? mediaIds : documentIds).push( item.id );
|
18 | 17 | }
|
19 | 18 | });
|
20 | 19 | }
|
21 | 20 |
|
22 |
| - var setIcon = function( nodes ) { |
23 |
| - if( _.isArray( nodes ) ) { |
24 |
| - _.each( nodes, setIcon ); |
25 |
| - } else { |
26 |
| - var item = _.find( $scope.renderModel, function( item ) { |
27 |
| - return +item.id === nodes.id; |
28 |
| - }); |
29 |
| - item.icon = iconHelper.convertFromLegacyIcon( nodes.icon ); |
30 |
| - } |
| 21 | + var setIcon = function( node ) { |
| 22 | + var item = _.find( $scope.renderModel, function( item ) { |
| 23 | + return +item.id === node.id; |
| 24 | + }); |
| 25 | + item.icon = iconHelper.convertFromLegacyIcon( node.icon ); |
31 | 26 | };
|
32 | 27 |
|
33 |
| - entityResource.getByIds( documentIds, 'Document' ).then( setIcon ); |
34 |
| - entityResource.getByIds( mediaIds, 'Media' ).then( setIcon ); |
| 28 | + $q.all([ |
| 29 | + entityResource.getByIds(documentIds, 'Document'), |
| 30 | + entityResource.getByIds(mediaIds, 'Media') |
| 31 | + ]).then(function (result) { |
| 32 | + var entities = result[0].concat(result[1]); |
| 33 | + |
| 34 | + _.each($scope.model.value, function (item) { |
| 35 | + if (item.id) { |
| 36 | + var entity = _.find(entities, function (e) { |
| 37 | + return e.id == item.id; |
| 38 | + }); |
| 39 | + |
| 40 | + if (entity) { |
| 41 | + item.icon = iconHelper.convertFromLegacyIcon(entity.icon); |
| 42 | + $scope.renderModel.push(new Link(item)); |
| 43 | + } |
| 44 | + } else { |
| 45 | + $scope.renderModel.push(new Link(item)); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + $scope.$watch( |
| 50 | + function () { |
| 51 | + return $scope.renderModel.length; |
| 52 | + }, |
| 53 | + function (newVal) { |
| 54 | + if ($scope.renderModel.length) { |
| 55 | + $scope.model.value = $scope.renderModel; |
| 56 | + } else { |
| 57 | + $scope.model.value = null; |
| 58 | + } |
| 59 | + |
| 60 | + if ($scope.cfg.minNumberOfItems && +$scope.cfg.minNumberOfItems > $scope.renderModel.length) { |
| 61 | + $scope.multiUrlPickerForm.minCount.$setValidity('minCount', false); |
| 62 | + } else { |
| 63 | + $scope.multiUrlPickerForm.minCount.$setValidity('minCount', true); |
| 64 | + } |
| 65 | + if ($scope.cfg.maxNumberOfItems && +$scope.cfg.maxNumberOfItems < $scope.renderModel.length) { |
| 66 | + $scope.multiUrlPickerForm.maxCount.$setValidity('maxCount', false); |
| 67 | + } else { |
| 68 | + $scope.multiUrlPickerForm.maxCount.$setValidity('maxCount', true); |
| 69 | + } |
| 70 | + } |
| 71 | + ); |
| 72 | + }); |
35 | 73 |
|
36 | 74 | if ( $scope.model.config ) {
|
37 | 75 | $scope.cfg = angular.extend( $scope.cfg, $scope.model.config );
|
|
74 | 112 | $scope.model.value = $scope.renderModel;
|
75 | 113 | };
|
76 | 114 |
|
77 |
| - $scope.$watch( |
78 |
| - function() { |
79 |
| - return $scope.renderModel.length; |
80 |
| - }, |
81 |
| - function(newVal) { |
82 |
| - if( $scope.renderModel.length ) { |
83 |
| - $scope.model.value = $scope.renderModel; |
84 |
| - } else { |
85 |
| - $scope.model.value = null; |
86 |
| - } |
87 |
| - |
88 |
| - if( $scope.cfg.minNumberOfItems && +$scope.cfg.minNumberOfItems > $scope.renderModel.length ) { |
89 |
| - $scope.multiUrlPickerForm.minCount.$setValidity( 'minCount', false ); |
90 |
| - } else { |
91 |
| - $scope.multiUrlPickerForm.minCount.$setValidity( 'minCount', true ); |
92 |
| - } |
93 |
| - if( $scope.cfg.maxNumberOfItems && +$scope.cfg.maxNumberOfItems < $scope.renderModel.length ) { |
94 |
| - $scope.multiUrlPickerForm.maxCount.$setValidity( 'maxCount', false ); |
95 |
| - } else { |
96 |
| - $scope.multiUrlPickerForm.maxCount.$setValidity( 'maxCount', true ); |
97 |
| - } |
98 |
| - } |
99 |
| - ); |
100 |
| - |
101 | 115 | $scope.$on("formSubmitting", function(ev, args) {
|
102 | 116 | if( $scope.renderModel.length ) {
|
103 | 117 | $scope.model.value = $scope.renderModel;
|
|
0 commit comments