|
1
|
|
|
define(function(require) { |
|
2
|
|
|
'use strict'; |
|
3
|
|
|
|
|
4
|
|
|
var SegmentChoiceView; |
|
5
|
|
|
var $ = require('jquery'); |
|
6
|
|
|
var _ = require('underscore'); |
|
7
|
|
|
var routing = require('routing'); |
|
8
|
|
|
var Select2View = require('oroform/js/app/views/select2-view'); |
|
9
|
|
|
|
|
10
|
|
|
SegmentChoiceView = Select2View.extend({ |
|
11
|
|
|
defaultOptions: { |
|
12
|
|
|
entity: void 0, |
|
|
|
|
|
|
13
|
|
|
currentSegment: void 0, |
|
|
|
|
|
|
14
|
|
|
select2: { |
|
15
|
|
|
allowClear: false |
|
16
|
|
|
} |
|
17
|
|
|
}, |
|
18
|
|
|
|
|
19
|
|
|
events: { |
|
20
|
|
|
change: 'onChange' |
|
21
|
|
|
}, |
|
22
|
|
|
|
|
23
|
|
|
initialize: function(options) { |
|
24
|
|
|
options = $.extend(true, {}, this.defaultOptions, options); |
|
25
|
|
|
_.extend(this, _.pick(options, _.without(_.keys(this.defaultOptions), 'select2'))); |
|
26
|
|
|
this.select2Config = this._processSelect2Options(options); |
|
27
|
|
|
SegmentChoiceView.__super__.initialize.call(this, options); |
|
28
|
|
|
}, |
|
29
|
|
|
|
|
30
|
|
|
_processSelect2Options: function(options) { |
|
31
|
|
|
var opts = _.clone(options.select2) || {}; |
|
32
|
|
|
|
|
33
|
|
|
if (!opts.formatSelectionTemplate && opts.formatSelectionTemplateSelector) { |
|
34
|
|
|
opts.formatSelectionTemplate = $(opts.formatSelectionTemplateSelector).text(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if (opts.formatSelectionTemplate) { |
|
38
|
|
|
var template = _.template(opts.formatSelectionTemplate); |
|
39
|
|
|
opts.formatSelection = function(item) { |
|
40
|
|
|
return item && item.id ? template(item) : ''; |
|
41
|
|
|
}; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if (opts.ajax) { |
|
45
|
|
|
var currentSegment = this.currentSegment; |
|
46
|
|
|
opts.ajax = _.extend({}, opts.ajax, { |
|
47
|
|
|
url: routing.generate(opts.ajax.url, {entityName: this.entity.replace(/\\/g, '_')}), |
|
48
|
|
|
data: function(term, page) { |
|
49
|
|
|
return { |
|
50
|
|
|
page: page, |
|
51
|
|
|
pageLimit: opts.pageLimit, |
|
52
|
|
|
term: term, |
|
53
|
|
|
currentSegment: currentSegment |
|
54
|
|
|
}; |
|
55
|
|
|
}, |
|
56
|
|
|
results: function(data) { |
|
57
|
|
|
return data; |
|
58
|
|
|
} |
|
59
|
|
|
}); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
opts.initSelection = function(element, callback) { |
|
63
|
|
|
var data = element.data('data'); |
|
64
|
|
|
if (!_.isEmpty(data)) { |
|
65
|
|
|
callback(data); |
|
66
|
|
|
} |
|
67
|
|
|
}; |
|
68
|
|
|
|
|
69
|
|
|
return opts; |
|
70
|
|
|
}, |
|
71
|
|
|
|
|
72
|
|
|
onChange: function(e) { |
|
73
|
|
|
var selectedItem = e.added || this.getData(); |
|
74
|
|
|
this.trigger('change', selectedItem); |
|
75
|
|
|
} |
|
76
|
|
|
}); |
|
77
|
|
|
|
|
78
|
|
|
return SegmentChoiceView; |
|
79
|
|
|
}); |
|
80
|
|
|
|