Code Duplication    Length = 63-74 lines in 2 locations

src/Oro/Bundle/ChannelBundle/Resources/public/js/app/components/select2-grid-channel-aware-component.js 1 location

@@ 1-74 (lines=74) @@
1
define(function(require) {
2
    'use strict';
3
4
    var Select2GridChannelAwareComponent;
5
    var _ = require('underscore');
6
    var Select2GridComponent = require('oro/select2-grid-component');
7
    var BaseSelect2View = require('oroform/js/app/views/select2-view');
8
    var viewFactory = require('orochannel/js/app/factory/select2-channel-aware-view-factory');
9
    var Select2View = viewFactory(BaseSelect2View);
10
11
    Select2GridChannelAwareComponent = Select2GridComponent.extend({
12
        $sourceElement: null,
13
        channelId: '',
14
        channelFieldName: '',
15
        gridName: '',
16
        ViewType: Select2View,
17
        /**
18
         * @inheritDoc
19
         */
20
        constructor: function Select2GridChannelAwareComponent() {
21
            Select2GridChannelAwareComponent.__super__.constructor.apply(this, arguments);
22
        },
23
24
        /**
25
         * @inheritDoc
26
         */
27
        initialize: function(options) {
28
            this.$sourceElement = options._sourceElement;
29
            this.channelId = _.result(options, 'channel_id') || this.channelId;
30
            this.channelFieldName = _.result(options, 'channel_field_name') || this.channelFieldName;
31
            this.gridName = options.configs.grid.name;
32
            Select2GridChannelAwareComponent.__super__.initialize.call(this, options);
33
        },
34
        prepareViewOptions: function(options, config) {
35
            var opts = Select2GridChannelAwareComponent.__super__.prepareViewOptions.apply(this, arguments);
36
            opts.$channelSelector = this.findChannelSelectorElement();
37
            opts.additionalParamsCb = _.bind(this._getAdditionalParams, this);
38
39
            return opts;
40
        },
41
        preConfig: function(config) {
42
            Select2GridChannelAwareComponent.__super__.preConfig.call(this, config);
43
44
            var that = this;
45
            config.ajax.data = _.wrap(config.ajax.data, function(parentDataFunction) {
46
                var result = parentDataFunction.apply(this, _.rest(arguments));
47
48
                return _.extend(result, that._getAdditionalParams());
49
            });
50
51
            return config;
52
        },
53
        findChannelSelectorElement: function() {
54
            return this.$sourceElement.closest('form').find('select[name="' + this.channelFieldName + '"]');
55
        },
56
        _getAdditionalParams: function() {
57
            var result = {};
58
            var $channel = this.findChannelSelectorElement();
59
            var channelIds = [$channel.val()];
60
61
            if (this.channelId) {
62
                channelIds.push(this.channelId);
63
            }
64
65
            result.channelIds = channelIds.join(',');
66
67
            result[this.gridName + '[channelIds]'] = channelIds.join(',');
68
69
            return result;
70
        }
71
    });
72
73
    return Select2GridChannelAwareComponent;
74
});
75
76

src/Oro/Bundle/ChannelBundle/Resources/public/js/app/components/select2-autocomplete-channel-aware-component.js 1 location

@@ 1-63 (lines=63) @@
1
define(function(require) {
2
    'use strict';
3
4
    var Select2AutocompleteChannelAwareComponent;
5
    var $ = require('jquery');
6
    var _ = require('underscore');
7
    var Select2AutocompleteComponent = require('oro/select2-autocomplete-component');
8
    var BaseSelect2View = require('oroform/js/app/views/select2-autocomplete-view');
9
    var viewFactory = require('orochannel/js/app/factory/select2-channel-aware-view-factory');
10
    var Select2View = viewFactory(BaseSelect2View);
11
12
    Select2AutocompleteChannelAwareComponent = Select2AutocompleteComponent.extend({
13
        $sourceElement: null,
14
        channelId: '',
15
        channelFieldName: '',
16
        ViewType: Select2View,
17
        /**
18
         * @inheritDoc
19
         */
20
        constructor: function Select2AutocompleteChannelAwareComponent() {
21
            Select2AutocompleteChannelAwareComponent.__super__.constructor.apply(this, arguments);
22
        },
23
        /**
24
         * @inheritDoc
25
         */
26
        initialize: function(options) {
27
            this.$sourceElement = options._sourceElement;
28
            this.channelId = _.result(options, 'channel_id') || this.channelId;
29
            this.channelFieldName = _.result(options, 'channel_field_name') || this.channelFieldName;
30
            Select2AutocompleteChannelAwareComponent.__super__.initialize.call(this, options);
31
        },
32
        prepareViewOptions: function(options, config) {
33
            var opts = Select2AutocompleteChannelAwareComponent.__super__.prepareViewOptions.apply(this, arguments);
34
            opts.$channelSelector = this.findChannelSelectorElement();
35
            opts.additionalParamsCb = _.bind(this._getAdditionalParams, this);
36
37
            return opts;
38
        },
39
        makeQuery: function(query) {
40
            var $channel = $('select[name="' + this.channelFieldName + '"]');
41
            return query + ';' + (this.channelId || $channel.val());
42
        },
43
        findChannelSelectorElement: function() {
44
            return this.$sourceElement.closest('form').find('select[name="' + this.channelFieldName + '"]');
45
        },
46
        _getAdditionalParams: function() {
47
            var params = {};
48
49
            var $channel = this.findChannelSelectorElement();
50
            var channelIds = [$channel.val()];
51
            if (this.channelId) {
52
                channelIds.push(this.channelId);
53
            }
54
55
            params.channelIds = channelIds.join(',');
56
57
            return {
58
                params: params
59
            };
60
        }
61
    });
62
    return Select2AutocompleteChannelAwareComponent;
63
});
64
65