forked from libreosteo/LibreOsteo
/
editformmanager.js
216 lines (202 loc) · 6.52 KB
/
editformmanager.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
This file is part of Libreosteo.
Libreosteo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Libreosteo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Libreosteo. If not, see <http://www.gnu.org/licenses/>.
*/
var editFormManager = angular.module('loEditFormManager', []);
function Callback(name, callback) {
this.name = name;
this.callback = callback;
}
editFormManager.factory('loEditFormManager', function() {
var forms = [];
var actions = ['edit', 'cancel', 'save', 'delete'];
var callback_form = [];
var triggers_form = [];
return {
add: function(form, action_list, trigger) {
if (forms.indexOf(form) === -1) {
forms.push(form);
}
angular.forEach(action_list, function(action, key) {
if (action && actions.indexOf(action.name) != -1) {
var idx_form = forms.indexOf(form);
var should_add = true;
var callbacks = null;
angular.forEach(callback_form, function(value, key) {
if (value.id == idx_form) {
should_add = false;
callbacks = value.callbacks;
}
});
if (should_add) {
callback_form.push({
id: forms.indexOf(form),
callbacks: [action]
});
} else {
callbacks.push(action);
}
}
});
var idx_form = forms.indexOf(form);
if (trigger)
triggers_form[idx_form] = trigger;
},
isavailable: function() {
this.available = this._visibleCtrl() != null;
return this.available;
},
available: false,
action_available: function(name_action) {
return this.action_callback(name_action) != null && this.action_trigger(name_action);
},
action_callback: function(name_action) {
var idx_form = forms.indexOf(this._visibleCtrl());
var callbacks = null;
var callback = null;
angular.forEach(callback_form, function(value, key) {
if (value.id == idx_form) {
callbacks = value.callbacks;
}
});
if (callbacks != null) {
angular.forEach(callbacks, function(value, key) {
if (value.name == name_action) {
callback = value;
}
});
}
return callback;
},
action_trigger: function(name_action) {
var idx_form = forms.indexOf(this._visibleCtrl());
var triggers = null;
var trigger = true;
angular.forEach(triggers_form, function(value, key) {
if (key == idx_form) {
triggers = value;
}
});
if (triggers != null) {
angular.forEach(triggers, function(value, key) {
if (key == name_action) {
if (value != null)
trigger = value;
}
});
}
return trigger;
},
call_action: function(name) {
this.action_callback(name).callback();
},
_visibleCtrl: function() {
var visibleCtrl = null;
angular.forEach(forms, function(value, key) {
if (visibleCtrl != null && $(value).is(':visible')) {
console.log("Cannot manage many ctrl....");
}
if (visibleCtrl == null && $(value).is(':visible')) {
visibleCtrl = value;
}
});
return visibleCtrl;
}
};
});
editFormManager.directive('editFormControl', ['$timeout', function($timeout) {
return {
restrict: 'A',
scope: {
save: "=save",
trigger: "=trigger",
edit: "=edit",
cancel: "=cancel",
delete: "=delete",
saveOnLostFocus: "=saveOnLostFocus"
},
compile: function(element, attr) {
if (!attr.save) {
attr.save = null;
}
if (!attr.edit) {
attr.edit = null;
}
if (!attr.cancel) {
attr.cancel = null;
}
if (!attr.delete) {
attr.delete = null;
}
if(!attr.saveOnLostFocus){
attr.saveOnLostFocus = false;
}
},
controller: ['$scope', '$routeParams', 'loEditFormManager', '$element', function($scope, $routeParams, loEditFormManager, $element) {
var actions = [];
if ($scope.save != null) {
actions.push(new Callback('save', $scope.save));
}
if ($scope.edit != null) {
actions.push(new Callback('edit', $scope.edit));
}
if ($scope.cancel != null) {
actions.push(new Callback('cancel', $scope.cancel));
}
if ($scope.delete != null) {
actions.push(new Callback('delete', $scope.delete));
}
$timeout(function() {
loEditFormManager.add($element, actions, $scope.trigger);
});
/**
* Handle a user attempt to leave a view containing unsaved data
*
* May autosave, or prompt user, or let leave.
*
* @param {event} event - The event of leaving the view (cancel-able)
* @param{boolean} askUser - Wether we ask if he really wants to leave
*/
function handleUnsavedForm(event, askUser) {
if ($element.hasClass('ng-dirty')) {
if ($scope.saveOnLostFocus) {
$scope.save();
$scope.trigger.save = false;
} else if (askUser) {
var quitAnyway = confirm(
gettext('There are unsaved changes. Do you really want to leave this page ?')
);
if (!quitAnyway) {
event.preventDefault(); // prevent view change
}
}
}
}
// tab change
$scope.$on('uiTabChange', function(event) {
// :visible is a hack to figure out if we are current tab.
if ($element.is(':visible')) {
handleUnsavedForm(event, false);
}
});
// router view change
$scope.$on('$locationChangeStart', function(event) {
handleUnsavedForm(event, true);
});
}],
}
}]);
var getStackTrace = function() {
var obj = {};
Error.captureStackTrace(obj, getStackTrace);
return obj.stack;
};