app factory ModalHelper modal function modal var baseInjectables scope

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
app.factory('ModalHelper', ['$modal', function($modal) {
var baseInjectables = ['$scope', '$modalInstance', '$saveForm', 'resourceAction', 'model'];
/*
* Helps with creation of modal windows.
*
* Dynamically constructs controller for the modal window. Uses `templateUrl` as controller's template. Allows
* to pass a dictionary of resolves for $modal service. These resolves will be copied to the modal's scope once they
* are resolved.
*
* Takes additional parameter `scopeFactories` which is a dictionary of factory functions. The result of calling
* this functions will be copied to the modal's scope. `this` inside of those functions points to the
* ModalController, so it's possible to use controller's functions (getModel, getResolve, ...).
*/
return function(templateUrl, additionalResolves, scopeFactories) {
return new function(){
var self = this;
function ModalController($scope, $modalInstance, $saveForm, resourceAction, model) {
$scope.model = model;
this.getModel = function() {
return $scope.model;
};
// copy resolves to the scope
var resolves = [];
for (var i = baseInjectables.length; i < arguments.length; i++) {
resolves.push(arguments[i]);
}
Object.keys(self.updatedAdditionalResolves).forEach(function(resolveName, index) {
$scope[resolveName] = resolves[index];
});
this.getResolve = function(name) {
if (name in self.updatedAdditionalResolves && name in $scope) {
return $scope[name];
} else {
throw new Error("The resolve '" + name + "' wasn't copied to the modal's scope.")
}
};
var modalController = this;
if (scopeFactories !== undefined) {
Object.keys(scopeFactories).forEach(function(factoryName) {
$scope[factoryName] = scopeFactories[factoryName].call(modalController);
});
}
$scope.cancel = function() {
return $modalInstance.dismiss('cancel');
};
$scope.save = function() {
$saveForm($scope, resourceAction.call(null, $scope.model).$promise, function (savedObject) {
$modalInstance.close(savedObject);
});
};
}
this.openModal = function(model, resourceAction, updatedResolves) {
var injectablesForController = baseInjectables.slice();
self.updatedAdditionalResolves = angular.extend(additionalResolves || {}, updatedResolves || {});
Object.keys(self.updatedAdditionalResolves).forEach(function(resolveName) {
injectablesForController.push(resolveName);
});
injectablesForController.push(ModalController);
var resolveMap = angular.extend({
resourceAction: function() { return resourceAction; },
model: function() { return model; }
}, self.updatedAdditionalResolves);
return $modal.open({
templateUrl: templateUrl,
resolve: resolveMap,
controller: injectablesForController
})
}
};
};
}]);
app.factory('ModalCrudHelper', ['$rootScope', function($rootScope) {
return function(resource, modalHelper, signalPrefix) {
function CrudHelper(resource, modalHelper, signalPrefix) {
function broadcast(eventName, object) {
if (signalPrefix !== undefined) {
$rootScope.$broadcast(signalPrefix + '-' + eventName, object.id);
}
}
this.addObject = function (model, objectsList, updatedResolves) {
var modalInstance = modalHelper.openModal(model, resource.save, updatedResolves);
return modalInstance.result.then(function (object) {
objectsList.push(object);
broadcast('created', object);
});
};
this.editObject = function (object, objectsList, updatedResolves) {
var objectIndex = objectsList.indexOf(object);
var objectCopy = angular.copy(object);
var modalInstance = modalHelper.openModal(objectCopy, resource.update, updatedResolves);
return modalInstance.result.then(function (updatedObject) {
if (objectIndex > -1) {
objectsList.splice(objectIndex, 1, updatedObject);
}
broadcast('updated', updatedObject);
});
};
this.removeObject = function (object, objectsList) {
var index = objectsList.indexOf(object);
return resource.delete({id: object.id}).$promise.then(function () {
if (index > -1) {
objectsList.splice(index, 1);
}
broadcast('deleted', object);
});
};
}
return new CrudHelper(resource, modalHelper, signalPrefix);
}
}]);