1
|
|
|
define(function(require) { |
2
|
|
|
'use strict'; |
3
|
|
|
|
4
|
|
|
var AttributeSelectComponent; |
5
|
|
|
var $ = require('jquery'); |
6
|
|
|
var mediator = require('oroui/js/mediator'); |
7
|
|
|
var _ = require('underscore'); |
8
|
|
|
|
9
|
|
|
var Select2AutocompleteComponent = require('oro/select2-autocomplete-component'); |
10
|
|
|
|
11
|
|
|
AttributeSelectComponent = Select2AutocompleteComponent.extend({ |
12
|
|
|
/** |
13
|
|
|
* @property {Object} |
14
|
|
|
*/ |
15
|
|
|
options: { |
16
|
|
|
delimiter: ';' |
17
|
|
|
}, |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @property {Object} |
21
|
|
|
*/ |
22
|
|
|
attributeSelect: null, |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @property {Array} |
26
|
|
|
*/ |
27
|
|
|
oldOptionLabels: [], |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @property {String} |
31
|
|
|
*/ |
32
|
|
|
ftid: null, |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritDoc |
36
|
|
|
*/ |
37
|
|
|
constructor: function AttributeSelectComponent() { |
38
|
|
|
AttributeSelectComponent.__super__.constructor.apply(this, arguments); |
39
|
|
|
}, |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritDoc |
43
|
|
|
*/ |
44
|
|
|
initialize: function(options) { |
45
|
|
|
this.options = _.defaults(options || {}, this.options); |
46
|
|
|
this.attributeSelect = options._sourceElement; |
47
|
|
|
this.ftid = $(this.attributeSelect).data('ftid'); |
48
|
|
|
var self = this; |
49
|
|
|
|
50
|
|
|
AttributeSelectComponent.__super__.initialize.call(this, options); |
51
|
|
|
|
52
|
|
|
// Modify dropdown - show already selected attributes (from other groups) in braces |
53
|
|
|
$(this.attributeSelect).on('select2-opening', function() { |
54
|
|
|
self.addLabelsToAttributesFromOtherGroups(); |
55
|
|
|
}); |
56
|
|
|
|
57
|
|
|
// Restore dropdown with initial option labels |
58
|
|
|
$(this.attributeSelect).on('select2-close', function() { |
59
|
|
|
self.restoreInitialDropdown(); |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
$(this.attributeSelect).on('selected', function(e) { |
63
|
|
|
// Modify selected tag to show initial value (without braces) |
64
|
|
|
if (e.val in self.oldOptionLabels) { |
65
|
|
|
var tag = $(self.attributeSelect).parent().find('.select2-choices li div').last(); |
66
|
|
|
$(tag).html(self.oldOptionLabels[e.val]); |
67
|
|
|
} |
68
|
|
|
var eventData = { |
69
|
|
|
ftid: self.ftid, |
70
|
|
|
selectedValue: e.val |
71
|
|
|
}; |
72
|
|
|
mediator.trigger('attribute-select:selected', eventData); |
73
|
|
|
}); |
74
|
|
|
|
75
|
|
|
mediator.on('attribute-select:selected', this.onAttributeSelect, this); |
76
|
|
|
mediator.on('attribute-group:remove', this.onGroupRemove, this); |
77
|
|
|
}, |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param {Array} selectedOptions |
81
|
|
|
*/ |
82
|
|
|
prepareDropdown: function(selectedOptions) { |
83
|
|
|
if (selectedOptions.length) { |
84
|
|
|
for (var id in selectedOptions) { |
85
|
|
|
if (selectedOptions.hasOwnProperty(id)) { |
86
|
|
|
var option = $(this.attributeSelect).find('option[value="' + id + '"]'); |
87
|
|
|
var oldText = $(option).text(); |
88
|
|
|
var moveFrom = _.__('oro.attribute.move_from'); |
89
|
|
|
var groupName = selectedOptions[id] ? selectedOptions[id] : _.__('oro.attribute.noname'); |
90
|
|
|
var newText = oldText + '(' + moveFrom + ' ' + groupName + ')'; |
91
|
|
|
$(option).text(newText); |
92
|
|
|
this.oldOptionLabels[id] = oldText; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
}, |
97
|
|
|
|
98
|
|
|
restoreInitialDropdown: function() { |
99
|
|
|
if (this.oldOptionLabels.length) { |
100
|
|
|
for (var id in this.oldOptionLabels) { |
101
|
|
|
if (this.oldOptionLabels.hasOwnProperty(id)) { |
102
|
|
|
var option = $(this.attributeSelect).find('option[value="' + id + '"]'); |
103
|
|
|
$(option).text(this.oldOptionLabels[id]); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
}, |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param {Object} eventData |
111
|
|
|
*/ |
112
|
|
|
onAttributeSelect: function(eventData) { |
113
|
|
|
if (eventData.ftid !== this.ftid) { |
114
|
|
|
this.applyOption(eventData.selectedValue, false, this.attributeSelect); |
115
|
|
|
} |
116
|
|
|
}, |
117
|
|
|
|
118
|
|
|
onGroupRemove: function(eventData) { |
119
|
|
|
if (eventData.attributeSelectFtid !== this.ftid) { |
120
|
|
|
return; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// Move system attributes to the first group |
124
|
|
|
var selectedSystemOptions = $(this.attributeSelect).find('option[locked="locked"]:selected'); |
125
|
|
|
var optionsArray = $(selectedSystemOptions).map(function() { |
126
|
|
|
return this.value; |
127
|
|
|
}).get(); |
128
|
|
|
|
129
|
|
|
if (optionsArray.length) { |
130
|
|
|
var select = eventData.firstGroup.find('[data-bound-input-widget="select2"]'); |
131
|
|
|
for (var i = 0; i < optionsArray.length; i++) { |
132
|
|
|
var value = optionsArray[i]; |
133
|
|
|
this.applyOption(value, true, select); |
134
|
|
|
} |
135
|
|
|
var message = _.__('oro.attribute.attributes_moved_to_default_group'); |
136
|
|
|
mediator.execute('showFlashMessage', 'info', message); |
137
|
|
|
} |
138
|
|
|
}, |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param {Integer} value |
142
|
|
|
* @param {Boolean} isSelected |
143
|
|
|
* @param {Object} select |
144
|
|
|
*/ |
145
|
|
|
applyOption: function(value, isSelected, select) { |
146
|
|
|
var option; |
147
|
|
|
if (isSelected) { |
148
|
|
|
option = $(select).find('option[value="' + value + '"]').not(':selected'); |
149
|
|
|
} else { |
150
|
|
|
option = $(select).find('option[value="' + value + '"]:selected'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (option.length) { |
154
|
|
|
// Need this timeout to deffer Change call because it causes some delay and it may be visible on UI |
155
|
|
|
setTimeout(function() { |
156
|
|
|
$(option).prop('selected', isSelected).change(); |
157
|
|
|
}, 1); |
158
|
|
|
} |
159
|
|
|
}, |
160
|
|
|
|
161
|
|
|
addLabelsToAttributesFromOtherGroups: function() { |
162
|
|
|
var eventData = {attributeSelects: []}; |
163
|
|
|
mediator.trigger('attribute-select:find-selected-attributes', eventData); |
164
|
|
|
|
165
|
|
|
var selectedOptions = []; |
166
|
|
|
var self = this; |
167
|
|
|
$(eventData.attributeSelects).each(function(key, value) { |
168
|
|
|
var groupLabel = value.groupLabel; |
169
|
|
|
var attributesSelect = value.attributesSelect; |
170
|
|
|
if ($(attributesSelect).data('ftid') === self.ftid) { |
171
|
|
|
return; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$(attributesSelect).find('option:selected').each(function() { |
175
|
|
|
var val = $(this).val(); |
176
|
|
|
selectedOptions[val] = groupLabel; |
177
|
|
|
}); |
178
|
|
|
}); |
179
|
|
|
|
180
|
|
|
this.prepareDropdown(selectedOptions); |
181
|
|
|
}, |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param {Object} e |
185
|
|
|
*/ |
186
|
|
|
dispose: function(e) { |
|
|
|
|
187
|
|
|
mediator.off('attribute-select:selected', this.onAttributeSelect, this); |
188
|
|
|
mediator.off('attribute-group:remove', this.onGroupRemove, this); |
189
|
|
|
|
190
|
|
|
AttributeSelectComponent.__super__.dispose.call(this); |
191
|
|
|
} |
192
|
|
|
}); |
193
|
|
|
|
194
|
|
|
return AttributeSelectComponent; |
195
|
|
|
}); |
196
|
|
|
|
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.