updates mediapicker to use onValueChanged in case the server value changes.

This commit is contained in:
Shannon
2013-10-01 14:10:11 +10:00
parent 2bb279d8f7
commit 6277bc379e

View File

@@ -1,56 +1,68 @@
//this controller simply tells the dialogs service to open a mediaPicker window
//with a specified callback, this callback will receive an object with a selection on it
angular.module('umbraco').controller("Umbraco.Editors.MediaPickerController",
function($rootScope, $scope, dialogService, mediaResource, imageHelper, $log){
$scope.images = [];
$scope.ids = [];
angular.module('umbraco').controller("Umbraco.Editors.MediaPickerController",
function($rootScope, $scope, dialogService, mediaResource, imageHelper, $log) {
if($scope.model.value){
$scope.ids = $scope.model.value.split(',');
mediaResource.getByIds($scope.ids).then(function(medias){
//img.media = media;
_.each(medias, function (media, i) {
//shortcuts
//TODO, do something better then this for searching
var img = {};
img.src = imageHelper.getImagePropertyValue({imageModel: media});
img.thumbnail = imageHelper.getThumbnailFromPath(img.src);
$scope.images.push(img);
});
});
}
function setupViewModel() {
$scope.images = [];
$scope.ids = [];
$scope.remove = function(index){
$scope.images.splice(index, 1);
$scope.ids.splice(index, 1);
$scope.sync();
};
if ($scope.model.value) {
$scope.ids = $scope.model.value.split(',');
$scope.add = function(){
dialogService.mediaPicker({multipicker:true, callback: function(data){
_.each(data.selection, function (media, i) {
//shortcuts
//TODO, do something better then this for searching
mediaResource.getByIds($scope.ids).then(function (medias) {
//img.media = media;
_.each(medias, function (media, i) {
//shortcuts
//TODO, do something better then this for searching
var img = {};
img.src = imageHelper.getImagePropertyValue({ imageModel: media });
img.thumbnail = imageHelper.getThumbnailFromPath(img.src);
$scope.images.push(img);
});
});
}
}
var img = {};
img.id = media.id;
img.src = imageHelper.getImagePropertyValue({imageModel: media});
img.thumbnail = imageHelper.getThumbnailFromPath(img.src);
$scope.images.push(img);
$scope.ids.push(img.id);
});
setupViewModel();
$scope.sync();
}});
};
$scope.remove = function(index) {
$scope.images.splice(index, 1);
$scope.ids.splice(index, 1);
$scope.sync();
};
$scope.sync = function(){
$scope.model.value = $scope.ids.join();
};
//TODO: Need to add the onValueChanged method to detect if the server has changed the model value and re-create the ids
$scope.add = function() {
dialogService.mediaPicker({
multipicker: true,
callback: function(data) {
_.each(data.selection, function(media, i) {
//shortcuts
//TODO, do something better then this for searching
});
var img = {};
img.id = media.id;
img.src = imageHelper.getImagePropertyValue({ imageModel: media });
img.thumbnail = imageHelper.getThumbnailFromPath(img.src);
$scope.images.push(img);
$scope.ids.push(img.id);
});
$scope.sync();
}
});
};
$scope.sync = function() {
$scope.model.value = $scope.ids.join();
};
//here we declare a special method which will be called whenever the value has changed from the server
//this is instead of doing a watch on the model.value = faster
$scope.model.onValueChanged = function (newVal, oldVal) {
//update the display val again if it has changed from the server
setupViewModel();
};
});