1
|
|
|
define(function(require) { |
2
|
|
|
'use strict'; |
3
|
|
|
|
4
|
|
|
var AggregatedFieldConditionView; |
5
|
|
|
var $ = require('jquery'); |
6
|
|
|
var _ = require('underscore'); |
7
|
|
|
var FieldConditionView = require('oroquerydesigner/js/app/views/field-condition-view'); |
8
|
|
|
|
9
|
|
|
AggregatedFieldConditionView = FieldConditionView.extend({ |
10
|
|
|
/** |
11
|
|
|
* @inheritDoc |
12
|
|
|
*/ |
13
|
|
|
constructor: function AggregatedFieldConditionView() { |
14
|
|
|
AggregatedFieldConditionView.__super__.constructor.apply(this, arguments); |
15
|
|
|
}, |
16
|
|
|
|
17
|
|
|
getDefaultOptions: function() { |
18
|
|
|
var defaultOptions = AggregatedFieldConditionView.__super__.getDefaultOptions.call(this); |
19
|
|
|
return _.extend({}, defaultOptions, { |
20
|
|
|
columnsCollection: null |
21
|
|
|
}); |
22
|
|
|
}, |
23
|
|
|
|
24
|
|
|
render: function() { |
25
|
|
|
var data = this.getValue(); |
26
|
|
|
if (data && data.columnName && data.func) { |
27
|
|
|
var column = this._getColumnByNameAndFunc(data.columnName, data.func); |
28
|
|
|
if (column) { |
29
|
|
|
this.$('.' + this.options.choiceInputClass).data('data', { |
30
|
|
|
id: column.get('name'), |
31
|
|
|
text: column.get('label') |
32
|
|
|
}); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
_.extend(this.options.fieldChoice, { |
36
|
|
|
select2ResultsCallback: this.fieldChoiceResultsCallback.bind(this), |
37
|
|
|
applicableConditionsCallback: this.applicableConditionsCallback.bind(this) |
38
|
|
|
}); |
39
|
|
|
var select2Opts = this.options.fieldChoice.select2; |
40
|
|
|
|
41
|
|
|
if (select2Opts) { |
42
|
|
|
var templateString = select2Opts.formatSelectionTemplate || |
43
|
|
|
$(select2Opts.formatSelectionTemplateSelector).text(); |
44
|
|
|
if (templateString) { |
45
|
|
|
this.options.fieldChoice = _.extend({}, this.options.fieldChoice, { |
46
|
|
|
select2: _.extend({}, select2Opts, { |
47
|
|
|
formatSelectionTemplate: this._compileSelectionTpl(templateString) |
48
|
|
|
}) |
49
|
|
|
}); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
AggregatedFieldConditionView.__super__.render.call(this); |
54
|
|
|
|
55
|
|
|
return this; |
56
|
|
|
}, |
57
|
|
|
|
58
|
|
|
onChoiceInputReady: function(choiceInputView) { |
59
|
|
|
AggregatedFieldConditionView.__super__.onChoiceInputReady.call(this, choiceInputView); |
60
|
|
|
|
61
|
|
|
this.listenTo(this.options.columnsCollection, { |
62
|
|
|
'remove': function(model) { |
63
|
|
|
if (model.get('label') === this._getColumnLabel()) { |
64
|
|
|
this._onRelatedColumnRemoved(); |
65
|
|
|
} |
66
|
|
|
}, |
67
|
|
|
'change:func change:label': function(model) { |
68
|
|
|
if (model.previous('label') === this._getColumnLabel()) { |
69
|
|
|
this._onRelatedColumnRemoved(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
}); |
73
|
|
|
}, |
74
|
|
|
|
75
|
|
|
_onRelatedColumnRemoved: function() { |
76
|
|
|
this.setChoiceInputValue('').then(function() { |
77
|
|
|
this.trigger('close'); |
78
|
|
|
}.bind(this)); |
79
|
|
|
}, |
80
|
|
|
|
81
|
|
|
_compileSelectionTpl: function(template) { |
82
|
|
|
var compiledTemplate = _.template(template); |
83
|
|
|
return _.bind(function(data) { |
84
|
|
|
var result = compiledTemplate(data); |
85
|
|
|
var func = this._getCurrentFunc(); |
86
|
|
|
if (func && func.name) { |
87
|
|
|
result += ' (' + func.name + ')'; |
88
|
|
|
} |
89
|
|
|
return result; |
90
|
|
|
}, this); |
91
|
|
|
}, |
92
|
|
|
|
93
|
|
|
applicableConditionsCallback: function(result, fieldId) { |
|
|
|
|
94
|
|
|
var returnType = _.result(this._getCurrentFunc(), 'return_type'); |
95
|
|
|
if (returnType) { |
96
|
|
|
result.type = returnType; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return result; |
100
|
|
|
}, |
101
|
|
|
|
102
|
|
|
fieldChoiceResultsCallback: function() { |
103
|
|
|
return _.map( |
104
|
|
|
this._getAggregatedColumns(), |
105
|
|
|
function(model) { |
106
|
|
|
return { |
107
|
|
|
id: model.get('name'), |
108
|
|
|
text: model.get('label') |
109
|
|
|
}; |
110
|
|
|
} |
111
|
|
|
); |
112
|
|
|
}, |
113
|
|
|
|
114
|
|
|
_getAggregatedColumns: function() { |
115
|
|
|
return _.filter( |
116
|
|
|
this.options.columnsCollection.models, |
117
|
|
|
_.compose(_.negate(_.isEmpty), _.property('func'), _.property('attributes')) |
118
|
|
|
); |
119
|
|
|
}, |
120
|
|
|
|
121
|
|
|
_collectValue: function() { |
122
|
|
|
var value = AggregatedFieldConditionView.__super__._collectValue.call(this); |
123
|
|
|
if (!_.isEmpty(value)) { |
124
|
|
|
value.func = this._getCurrentFunc(); |
125
|
|
|
} |
126
|
|
|
return value; |
127
|
|
|
}, |
128
|
|
|
|
129
|
|
|
_getColumnLabel: function() { |
130
|
|
|
return _.result(this.subview('choice-input').getData(), 'text'); |
131
|
|
|
}, |
132
|
|
|
|
133
|
|
|
_getCurrentFunc: function() { |
134
|
|
|
var column = this.options.columnsCollection.findWhere({label: this._getColumnLabel()}); |
135
|
|
|
if (_.isEmpty(column)) { |
136
|
|
|
return; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return column.get('func'); |
140
|
|
|
}, |
141
|
|
|
|
142
|
|
|
_getColumnByNameAndFunc: function(name, func) { |
143
|
|
|
if (!func) { |
144
|
|
|
return; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return _.find(this.options.columnsCollection.where({name: name}), function(column) { |
148
|
|
|
return column.get('func') && column.get('func').name === func.name; |
149
|
|
|
}); |
150
|
|
|
}, |
151
|
|
|
|
152
|
|
|
_getFilterCriterion: function() { |
153
|
|
|
var criterion = AggregatedFieldConditionView.__super__._getFilterCriterion.call(this); |
154
|
|
|
$.extend(true, criterion, { |
155
|
|
|
data: { |
156
|
|
|
params: { |
157
|
|
|
filter_by_having: true |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
}); |
161
|
|
|
|
162
|
|
|
return criterion; |
163
|
|
|
}, |
164
|
|
|
|
165
|
|
|
_hasEmptyFilter: function() { |
166
|
|
|
var isEmptyFilter = AggregatedFieldConditionView.__super__._hasEmptyFilter.call(this); |
167
|
|
|
return isEmptyFilter || _.isEmpty(this._getCurrentFunc()); |
168
|
|
|
} |
169
|
|
|
}); |
170
|
|
|
|
171
|
|
|
return AggregatedFieldConditionView; |
172
|
|
|
}); |
173
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.