/
fields-loader.js
105 lines (91 loc) · 3.4 KB
/
fields-loader.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
define(function(require) {
'use strict';
var $ = require('jquery');
var _ = require('underscore');
var routing = require('routing');
var EntityFieldsUtil = require('oroentity/js/entity-fields-util');
require('jquery-ui');
/**
* Widget responsible for loading fields of selected entity
*/
$.widget('oroentity.fieldsLoader', {
options: {
router: null,
routingParams: {},
afterRevertCallback: null,
// supports 'oroui/js/modal' confirmation dialog
confirm: null,
requireConfirm: function() { return true; }
},
_create: function() {
this.setFieldsData(this.element.data('fields') || []);
this._on({
change: this._onChange
});
},
_onChange: function(e, extraArgs) {
_.extend(e, extraArgs);
var oldVal;
var confirm = this.options.confirm;
if (confirm && this.options.requireConfirm()) {
// @todo support also other kind of inputs than select2
oldVal = (e.removed && e.removed.id) || null;
this._confirm(confirm, e.val, oldVal);
} else {
this.loadFields();
}
},
loadFields: function() {
var routeName = this.options.router;
var routeParams = this.options.routingParams;
var additionalRequestParams = this.element.data('select2_query_additional_params');
if (additionalRequestParams) {
routeParams = $.extend({}, routeParams, additionalRequestParams);
}
$.ajax({
url: routing.generate(routeName, routeParams),
success: $.proxy(this._onLoaded, this),
beforeSend: $.proxy(this._trigger, this, 'start'),
complete: $.proxy(this._trigger, this, 'complete')
});
},
getEntityName: function() {
return this.element.val();
},
setFieldsData: function(data) {
var fields = EntityFieldsUtil.convertData(data);
this.element.data('fields', fields);
this._trigger('update', null, [fields]);
},
getFieldsData: function() {
return this.element.data('fields');
},
_confirm: function(confirm, newVal, oldVal) {
if (!oldVal) {
return;
}
var $el = this.element;
var load = $.proxy(this.loadFields, this);
var revert = $.proxy(function() {
var $entityChoice = $el.data('relatedChoice');
if ($entityChoice && $entityChoice.val() !== oldVal) {
$entityChoice.val(oldVal).change();
}
$el.val(oldVal).change();
if ($.isFunction(this.options.afterRevertCallback)) {
this.options.afterRevertCallback.call(this, $el);
}
}, this);
confirm.on('ok', load);
confirm.on('cancel', revert);
confirm.once('hidden', function() {
confirm.off('ok', load);
confirm.off('cancel', revert);
});
confirm.open();
},
_onLoaded: function(data) {
this.setFieldsData(data);
}
});
});