Completed
Branch 3.0 (18257b)
by Antonio
13:24
created

BaseForm.extend.getIllustrationClass   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 5
c 1
b 1
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
1
'use strict';
2
3
define(
4
    [
5
        'jquery',
6
        'underscore',
7
        'oro/translator',
8
        'backbone',
9
        'routing',
10
        'pim/form',
11
        'pim/form-builder',
12
        'pim/user-context',
13
        'oro/loading-mask',
14
        'pim/router',
15
        'oro/messenger',
16
        'flagbit/template/product/clone-modal'
17
    ],
18
    function (
19
        $,
20
        _,
21
        __,
22
        Backbone,
23
        Routing,
24
        BaseForm,
25
        FormBuilder,
26
        UserContext,
27
        LoadingMask,
28
        router,
29
        messenger,
30
        template
31
    ) {
32
        return BaseForm.extend({
33
            config: {},
34
            template: _.template(template),
35
            globalErrors: [],
36
            /**
37
             * {@inheritdoc}
38
             */
39
            initialize(meta) {
40
                this.config = meta.config;
41
                this.globalErrors = [];
42
                BaseForm.prototype.initialize.apply(this, arguments);
43
            },
44
            getFieldsFormName() {
45
                if (this.getRoot().model.has('parent')) {
46
                    return this.config.variantFormName;
47
                }
48
                else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
49
                    return this.config.productFormName;
50
                }
51
            },
52
53
            render() {
54
55
56
                this.$el.html(this.template({
57
                    modalTitle: __(this.config.labels.title),
58
                    subTitle: __(this.getSubtitle()),
59
                    content: __(this.config.labels.content),
60
                    picture: this.config.picture,
61
                    errors: this.globalErrors
62
                }));
63
64
                return FormBuilder.build(this.getFieldsFormName()).then(form => {
65
                    this.addExtension(
66
                        form.code,
67
                        form,
68
                        'fields-container',
69
                        10000
70
                    );
71
                    form.configure();
72
                    this.renderExtensions();
73
                    return this;
74
                });
75
            },
76
77
            getIllustrationClass() {
78
                if (this.getProductType() == 'model') {
79
                    return 'product-model';
80
                }
81
                else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
82
                    return 'products';
83
                }
84
            },
85
86
            getSubtitle() {
87
                if (this.getProductType() == 'model') {
88
                    return this.config.labels.subTitleModel;
89
                }
90
                else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
91
                    return this.config.labels.subTitle;
92
                }
93
            },
94
95
            getProductType() {
96
                return this.getRoot().model.get('type');
97
            },
98
99
            /**
100
             * Opens the modal then instantiates the creation form inside it.
101
             * This function returns a rejected promise when the popin
102
             * is canceled and a resolved one when it's validated.
103
             *
104
             * @return {Promise}
105
             */
106
            open() {
107
108
                const deferred = $.Deferred();
109
                const modal = new Backbone.BootstrapModal({
110
                    content: '',
111
                    cancelText: __('pim_common.cancel'),
112
                    okText: __('pim_common.save'),
113
                    okCloses: false,
114
                    illustrationClass: this.getIllustrationClass()
115
                });
116
117
                modal.open();
118
                modal.$el.addClass('modal--fullPage');
119
120
                const modalBody = modal.$('.modal-body');
121
                modalBody.addClass('creation');
122
123
                this.setElement(modalBody);
124
                this.render();
125
126
                modal.on('cancel', () => {
127
                    deferred.reject();
128
                    modal.remove();
129
                });
130
131
                modal.on('ok', this.confirmModal.bind(this, modal, deferred));
132
133
                return deferred.promise();
134
            },
135
136
            /**
137
             * Confirm the modal and redirect to route after save
138
             * @param  {Object} modal    The backbone view for the modal
139
             * @param  {Promise} deferred Promise to resolve
140
             */
141
            confirmModal(modal, deferred) {
142
                this.save().done(entity => {
0 ignored issues
show
Unused Code introduced by
The parameter entity is not used and could be removed.

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.

Loading history...
143
                    modal.close();
144
                    modal.remove();
145
                    deferred.resolve();
146
147
                    router.redirectToRoute(this.config.editRoute);
148
                    messenger.notify('success', __(this.config.successMessage));
149
                });
150
            },
151
152
            /**
153
             * Save the form content by posting it to backend
154
             *
155
             * @return {Promise}
156
             */
157
            save() {
158
                const loadingMask = new LoadingMask();
159
                this.$el.empty().append(loadingMask.render().$el.show());
160
161
                let data = $.extend(this.getFormData(),
162
                    this.config.defaultValues || {});
163
164
                if (this.config.excludedProperties) {
165
                    data = _.omit(data, this.config.excludedProperties)
166
                }
167
168
                var that = this;
169
170
                return $.ajax({
171
                    url: Routing.generate(this.getPostRoute()),
172
                    type: 'POST',
173
                    data: JSON.stringify(data)
174
                }).fail(function (response) {
175
                    if (response.responseJSON) {
176
                        that.globalErrors = response.responseJSON.values;
177
                        this.render();
178
                    }
179
                }.bind(this))
180
                    .always(() => loadingMask.remove());
181
            },
182
            getPostRoute() {
183
                if (this.getFormData().type === 'model') {
184
                    return this.config.postProductModelRoute;
185
                } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
186
                    return this.config.postProductRoute;
187
                }
188
            }
189
        });
190
    }
191
);
192