Test Setup Failed
Push — master ( d60e73...a71a5d )
by
unknown
04:37 queued 10s
created

ghlight   C

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 1
Ratio 11.11 %

Importance

Changes 0
Metric Value
cc 2
nc 2
dl 1
loc 9
rs 6.2142
c 0
b 0
f 0
nop 1
1
define(function(require) {
2
    'use strict';
3
4
    var StepEditView;
5
    var _ = require('underscore');
6
    var $ = require('jquery');
7
    var __ = require('orotranslation/js/translator');
8
    var BaseView = require('oroui/js/app/views/base/view');
9
    var DialogWidget = require('oro/dialog-widget');
10
    var helper = require('oroworkflow/js/tools/workflow-helper');
11
    var TransitionsListView = require('../transition/transition-list-view');
12
13
    StepEditView = BaseView.extend({
14
        attributes: {
15
            'class': 'widget-content'
16
        },
17
18
        options: {
19
            template: null,
20
            transitionListContainerEl: '.transitions-list-container',
21
            workflow: null
22
        },
23
24
        listen: {
25
            'destroy model': 'remove'
26
        },
27
28
        /**
29
         * @inheritDoc
30
         */
31
        constructor: function StepEditView() {
32
            StepEditView.__super__.constructor.apply(this, arguments);
33
        },
34
35
        /**
36
         * @inheritDoc
37
         */
38
        initialize: function(options) {
39
            this.options = _.defaults(options || {}, this.options);
40
41
            var template = this.options.template || $('#step-form-template').html();
42
            this.template = _.template(template);
43
            this.widget = null;
44
        },
45
46
        onStepAdd: function() {
47
            var formData = helper.getFormData(this.widget.form);
48
            var order = parseInt(formData.order);
49
50
            if (!this.model.get('name')) {
51
                this.model.set('name', helper.getNameByString(formData.label, 'step_'));
52
            }
53
            this.model.set('order', order > 0 ? order : 0);
54
            this.model.set('is_final', formData.hasOwnProperty('is_final'));
55
            this.model.set('label', formData.label);
56
            this.model.set('_is_clone', false);
57
58
            this.trigger('stepAdd', this.model);
59
60
            this.widget.remove();
61
        },
62
63
        onCancel: function() {
64
            if (this.model.get('_is_clone')) {
65
                this.model.destroy();
66
            } else {
67
                this.remove();
68
            }
69
        },
70
71
        remove: function() {
72
            if (this.transitionsListView) {
73
                this.transitionsListView.remove();
74
            }
75
            StepEditView.__super__.remove.call(this);
76
        },
77
78 View Code Duplication
        renderTransitions: function() {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
79
            this.transitionsListView = new TransitionsListView({
80
                el: this.$el.find(this.options.transitionListContainerEl),
81
                workflow: this.options.workflow,
82
                collection: this.model.getAllowedTransitions(this.options.workflow),
83
                stepFrom: this.model
84
            });
85
            this.transitionsListView.render();
86
        },
87
88
        renderWidget: function() {
89
            if (!this.widget) {
90
                var title = this.model.get('name') ? __('Edit step') : __('Add new step');
91
                if (this.model.get('_is_clone')) {
92
                    title = __('Clone step');
93
                }
94
95
                this.widget = new DialogWidget({
96
                    title: title,
97
                    el: this.$el,
98
                    stateEnabled: false,
99
                    incrementalPosition: false,
100
                    dialogOptions: {
101
                        close: _.bind(this.onCancel, this),
102
                        width: 800,
103
                        modal: true
104
                    }
105
                });
106
                this.widget.render();
107
            } else {
108
                this.widget._adoptWidgetActions();
109
            }
110
111
            // Disable widget submit handler and set our own instead
112
            this.widget.form.off('submit');
113
            this.widget.form.validate({
114
                submitHandler: _.bind(this.onStepAdd, this),
115
                ignore: '[type="hidden"]',
116
                highlight: function(element) {
117
                    var tabContent = $(element).closest('.tab-pane');
118
                    if (tabContent.is(':hidden')) {
119
                        tabContent
120
                            .closest('.oro-tabs')
121
                            .find('[href="#' + tabContent.prop('id') + '"]')
122
                            .click();
123
                    }
124
                }
125
            });
126
        },
127
128
        render: function() {
129
            var data = this.model.toJSON();
130
            data.transitionsAllowed = (this.options.workflow.get('steps').length > 1 && this.model.get('name'));
131
            this.$el.html(this.template(data));
132
133
            if (data.transitionsAllowed) {
134
                this.renderTransitions();
135
            }
136
            this.renderWidget();
137
138
            return this;
139
        }
140
    });
141
142
    return StepEditView;
143
});
144