1 angular.module('egFmRecordEditorMod',
2 ['egCoreMod', 'egUiMod', 'ui.bootstrap'])
4 .directive('egEditFmRecord', function() {
9 // IDL class hint (e.g. "aou")
12 // mode: 'create' for creating a new record,
13 // 'update' for editing an existing record
16 // record ID to update
19 // comma-separated list of fields that should not be
23 // comma-separated list of fields that should always
27 // comma-separated list of required fields; this
28 // supplements what the IDL considers required
31 // hash, keyed by field name, of functions to invoke
32 // to check whether a field is required. Each
33 // callback is passed the field name and the record
34 // and should return a boolean value. This supports
35 // cases where whether a field is required or not
36 // depends on the current value of another field.
37 isRequiredOverride : '@',
39 // reference to handler to run upon saving
40 // record. The handler will be passed the
41 // record ID and a parameter indicating whether
42 // the save did a create or an update. Note that
43 // even if the mode of the egEditFmRecord is
44 // 'create', the onSave handler may still get
45 // 'update' if the user is permitted to create a
46 // record, then update it
49 // reference to handler to run if the user
55 templateUrl : '/eg/staff/share/t_fm_record_editor',
59 function($scope , egCore) {
61 function list_to_hash(str) {
63 if (angular.isString(str)) {
64 str.split(/,/).map(function(s) {
65 hash[s.trim()] = true;
71 $scope.required = list_to_hash($scope.requiredFields);
72 $scope.readonly = list_to_hash($scope.readonlyFields);
73 $scope.hidden = list_to_hash($scope.hiddenFields);
75 $scope.record_label = egCore.idl.classes[$scope.idlClass].label;
77 $scope.rec_org_values = {};
78 $scope.id_is_editable = false;
80 if ($scope.mode == 'update') {
81 egCore.pcrud.retrieve($scope.idlClass, $scope.recordId).then(function(r) {
83 convert_datatypes_to_js($scope.rec);
84 $scope.fields = get_field_list();
87 if (!('pkey_sequence' in egCore.idl.classes[$scope.idlClass])) {
88 $scope.id_is_editable = true;
90 $scope.rec = new egCore.idl[$scope.idlClass]();
91 $scope.fields = get_field_list();
94 function convert_datatypes_to_js(rec) {
95 var fields = egCore.idl.classes[$scope.idlClass].fields;
96 angular.forEach(fields, function(field) {
97 if (field.datatype == 'bool') {
98 if (rec[field.name]() == 't') {
99 rec[field.name](true);
100 } else if (rec[field.name]() == 'f') {
101 rec[field.name](false);
107 function convert_datatypes_to_idl(rec) {
108 var fields = egCore.idl.classes[$scope.idlClass].fields;
109 angular.forEach(fields, function(field) {
110 if (field.datatype == 'bool') {
111 if (rec[field.name]() == true) {
112 rec[field.name]('t');
113 } else if (rec[field.name]() == false) {
114 rec[field.name]('f');
120 function flatten_linked_values(cls, list) {
122 var fields = egCore.idl.classes[cls].fields;
125 angular.forEach(fields, function(fld) {
126 if (fld.datatype == 'id') {
128 selector = fld.selector ? fld.selector : id_field;
132 angular.forEach(list, function(item) {
133 var rec = egCore.idl.toHash(item);
142 function get_field_list() {
143 var fields = egCore.idl.classes[$scope.idlClass].fields;
145 angular.forEach(fields, function(field) {
146 field.readonly = (field.name in $scope.readonly);
147 if (angular.isObject($scope.isRequiredOverride) &&
148 field.name in $scope.isRequiredOverride) {
149 field.is_required = function() {
150 return $scope.isRequiredOverride[field.name](field.name, $scope.rec);
153 field.is_required = function() {
154 return field.required || (field.name in $scope.required);
157 if (field.datatype == 'link') {
158 egCore.pcrud.retrieveAll(
159 field.class, {}, {atomic : true}
160 ).then(function(list) {
161 field.linked_values = flatten_linked_values(field.class, list);
164 if (field.datatype == 'org_unit') {
165 $scope.rec_orgs[field.name] = function(org) {
166 if (arguments.length == 1) $scope.rec[field.name](org.id());
167 return egCore.org.get($scope.rec[field.name]());
169 if ($scope.rec[field.name]()) {
170 $scope.rec_org_values[field.name] = $scope.rec_orgs[field.name]();
174 return fields.filter(function(field) { return !(field.name in $scope.hidden) });
177 $scope.ok = function($event) {
178 var recToSave = egCore.idl.Clone($scope.rec)
179 convert_datatypes_to_idl(recToSave);
180 if ($scope.mode == 'update') {
181 egCore.pcrud.update(recToSave).then(function() {
182 $scope.onSave($event);
185 egCore.pcrud.create(recToSave).then(function() {
186 $scope.onSave($event);
190 $scope.cancel = function($event) {
191 $scope.onCancel($event);