-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathOneToMany.js
executable file
·113 lines (104 loc) · 4.46 KB
/
OneToMany.js
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
(function(){
'use strict';
OneToMany.$inject = ['$interpolate','$uibModal','$populate'];
function OneToMany($interpolate,$uibModal,$populate){
var template = [
'<div class="col-md-12">',
' <button type="button" class="btn btn-default" ng-click="newModal()">{{name}}</button>',
' <ul class="list-group">',
' <li ng-repeat="child in children" class="list-group-item">',
' <div class="row">',
' <div ng-hide="property" class="col-md-8" ng-transclude></div>',
' <div ng-show="property" class="col-md-8">{{::child[property]}}</div>',
' <div class="col-md-4">',
' <button type="button" class="{{::removeButtonClass}}" title="{{::removeButtonTitle}}" ng-click="removeFromList(child)">',
' <i class="{{::removeButtonIcon}}"></i> {{::removeButton}}',
' </button>',
' <button type="button" class="{{::editButtonClass}}" title="{{::editButtonTitle}}" ng-click="newModal(child)">',
' <i class="{{::editButtonIcon}}"></i> {{::editButton}}',
' </button>',
' </div> ',
' </div> ',
' </li>',
' <ul>',
'</div>',
'<div class="clearfix"></div>'
];
return {
restrict: 'E',
template: template.join('\n'),
transclude: true,
scope: {
children: '=',
templateUrl: '@',
property: '@displayableProperty',
name: '@',
controller: '@',
onDelete: '&?',
onValueVisualizationOpened: '&?',
onValueVisualizationClosed: '&?',
modalTitle: '@'
},
link: function (scope,elm,attrs) {
var eventHandler = {
valueVisualizationOpened: (attrs.onValueVisualizationOpened ? scope.onValueVisualizationOpened :angular.noop),
valueVisualizationClosed: (attrs.onValueVisualizationClosed ? scope.onValueVisualizationClosed :angular.noop),
delete: (attrs.onDelete ? scope.onDelete : angular.noop)
};
scope.newModal = newModal;
scope.editButton = attrs.editButton ? $interpolate(attrs.editButton)(scope) : 'Editar'
scope.editButtonTitle = attrs.editButtonTitle ? $interpolate(attrs.editButtonTitle)(scope) : 'Editar'
scope.editButtonClass = attrs.editButtonClass ? $interpolate(attrs.editButtonClass)(scope) : 'btn btn-default pull-right btn-sm'
scope.editButtonIcon = attrs.editButtonIcon ? $interpolate(attrs.editButtonIcon)(scope) : 'glyphicon glyphicon-pencil'
scope.removeButton = attrs.removeButton ? $interpolate(attrs.removeButton)(scope) : 'Remover'
scope.removeButtonTitle = attrs.removeButtonTitle ? $interpolate(attrs.removeButtonTitle)(scope) : 'Remover'
scope.removeButtonClass = attrs.removeButtonClass ? $interpolate(attrs.removeButtonClass)(scope) : 'btn btn-default pull-right btn-sm'
scope.removeButtonIcon = attrs.removeButtonIcon ? $interpolate(attrs.removeButtonIcon)(scope) : 'glyphicon glyphicon-remove'
scope.removeFromList = removeFromList;
scope.getFromModal = getFromModal;
scope.name = scope.name || 'Novo';
if(!scope.children) console.error('You must provide a list to GumgaOneToMany');
if(!scope.templateUrl) console.error('You must provide a templateUrl for the modal');
if(!scope.property) console.error('You must provide a property to display in GumgaOneToMany');
if(!scope.controller) console.error('You must provide a controller to the modal');
function getFromModal(selected){
eventHandler.valueVisualizationClosed();
if(JSON.stringify(scope.etty) !== '{}'){
scope.children.splice(scope.children.indexOf(scope.etty),1,selected);
} else {
scope.children.push(selected);
}
}
function removeFromList(obj){
eventHandler.delete({$value: obj});
scope.children.splice(scope.children.indexOf(obj),1);
}
function newModal(obj){
scope.etty = {};
if(obj){
scope.etty= obj;
}
eventHandler.valueVisualizationOpened();
var modalInstance = $uibModal.open({
templateUrl: scope.templateUrl,
controller: scope.controller,
resolve: {
entity: function(){
return scope.etty;
},
title: function(){
return scope.modalTitle;
},
populateScope: function(){
return $populate.populateScope;
}
}
});
modalInstance.result.then(getFromModal);
}
}
};
}
angular.module('gumga.directives.onetomany',['gumga.services.populate'])
.directive('gumgaOneToMany',OneToMany)
})();