Test Setup Failed
Push — master ( 11cd9d...8453e3 )
by
unknown
03:59
created

  B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 25
rs 8.8571
nop 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A attribute-form-option-edit-view.js ➔ ... ➔ this.form.submit 0 3 1
1
define(function(require) {
2
    'use strict';
3
4
    var AttributeFormOptionEditView;
5
    var _ = require('underscore');
6
    var __ = require('orotranslation/js/translator');
7
    var $ = require('jquery');
8
    var BaseView = require('oroui/js/app/views/base/view');
9
    var FieldChoiceView = require('oroentity/js/app/views/field-choice-view');
10
    var helper = require('oroworkflow/js/tools/workflow-helper');
11
    require('jquery.validate');
12
13
    AttributeFormOptionEditView = BaseView.extend({
14
        attributes: {
15
            'class': 'widget-content'
16
        },
17
18
        options: {
19
            template: null,
20
            data: {
21
                label: '',
22
                property_path: '',
23
                property_path_text: '',
24
                required: false
25
            },
26
            entity_field_template: null,
27
            entity: null,
28
            filterPreset: null,
29
            workflow: null,
30
            entityFieldsProvider: null
31
        },
32
33
        requiredOptions: ['workflow', 'entityFieldsProvider'],
34
35
        initialize: function(options) {
36
            options = options || {};
37
            var requiredMissed = this.requiredOptions.filter(function(option) {
38
                return _.isUndefined(options[option]);
39
            });
40
            if (requiredMissed.length) {
41
                throw new TypeError('Missing required option(s): ' + requiredMissed.join(', '));
42
            }
43
            this.options = _.defaults(options, this.options);
44
            var template = this.options.template || $('#attribute-form-option-edit-template').html();
45
            this.template = _.template(template);
46
47
            this.entity_field_template = this.options.entity_field_template ||
48
                $('#entity-column-chain-template').html();
49
        },
50
51
        getFieldChoiceView: function() {
52
            return this.subview('field-choice');
53
        },
54
55
        onAdd: function() {
56
            var formData = helper.getFormData(this.form);
57
58
            formData.property_path = this.options.entityFieldsProvider.getPropertyPathByPath(formData.property_path);
59
            formData.required = formData.hasOwnProperty('required');
60
61
            this.resetForm();
62
            this.trigger('formOptionAdd', formData);
63
        },
64
65
        resetForm: function() {
66
            this.subview('field-choice').setValue('');
67
            this.form.find('[name=itemId]').val('');
68
            this.form.get(0).reset();
69
            this.submitBtn.html('<i class="fa-plus"></i> ' + __('Add'));
70
            this.resetBtn.addClass('hide');
71
        },
72
73
        initFieldChoiceView: function(container) {
74
            this.subview('field-choice', new FieldChoiceView({
75
                autoRender: true,
76
                el: container.find('[name="property_path"]'),
77
                entity: this.options.entity,
78
                filterPreset: this.options.filterPreset,
79
                allowSelectRelation: true,
80
                select2: {
81
                    placeholder: __('Choose field...')
82
                }
83
            }));
84
        },
85
86
        editRow: function(data) {
87
            this.fieldSelectorEl.inputWidget(
88
                'val',
89
                this.options.entityFieldsProvider.getPathByPropertyPathSafely(data.property_path)
90
            );
91
            this.form.find('[name=itemId]').val(data.itemId || '');
92
            this.labelEl.val(data.isSystemLabel ? '' : data.label);
93
            this.requiredEl.get(0).checked = data.required;
94
            this.submitBtn.html('<i class="fa-pencil-square-o"></i> ' + __('Update'));
95
            this.resetBtn.removeClass('hide');
96
        },
97
98
        render: function() {
99
            this._deferredRender();
100
            this.form = $(this.template(this.options.data)).filter('form');
101
            this.form.validate({
102
                submitHandler: _.bind(this.onAdd, this)
103
            });
104
            this.form.on('submit', function(e) {
105
                e.preventDefault();
106
            });
107
            this.initFieldChoiceView(this.form);
108
109
            this.submitBtn = this.form.find('[type=submit]');
110
            this.resetBtn = this.form.find('[type=reset]');
111
            this.fieldSelectorEl = this.form.find('[name=property_path]');
112
            this.labelEl = this.form.find('[name=label]');
113
            this.requiredEl = this.form.find('[name=required]');
114
115
            this.resetBtn.click(_.bind(this.resetForm, this));
116
117
            this.$el.append(this.form);
118
            // since we have no async operation right here but there is one in subview `deferredRender` promise
119
            // will be resolved with promise of subview
120
            this._resolveDeferredRender();
121
            return this;
122
        }
123
    });
124
125
    return AttributeFormOptionEditView;
126
});
127