Test Setup Failed
Push — master ( cd1dab...2a689d )
by
unknown
03:51
created

notes-component.js ➔ ... ➔ NotesComponent   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
define(function(require) {
2
    'use strict';
3
4
    var NotesComponent;
5
    var $ = require('jquery');
6
    var _ = require('underscore');
7
    var BaseComponent = require('oroui/js/app/components/base/component');
8
9
    /**
10
     * @export oroorder/js/app/components/notes-component
11
     * @extends oroui.app.components.base.Component
12
     * @class oroorder.app.components.NotesComponent
13
     */
14
    NotesComponent = BaseComponent.extend({
15
        /**
16
         * @property {Object}
17
         */
18
        options: {
19
            selectors: {
20
                edit: '.notes-widget-edit',
21
                preview: '.notes-widget-preview',
22
                editBtn: '.notes-widget-edit-btn',
23
                addBtn: '.notes-widget-add-btn',
24
                removeBtn: '.notes-widget-remove-btn'
25
            },
26
            template: '#order-notes-widget'
27
        },
28
29
        /**
30
         * @property {Function}
31
         */
32
        template: null,
33
34
        /**
35
         * @property {jQuery}
36
         */
37
        $el: null,
38
39
        /**
40
         * @property {jQuery}
41
         */
42
        $notes: null,
43
44
        /**
45
         * @property {jQuery}
46
         */
47
        $edit: null,
48
49
        /**
50
         * @property {jQuery}
51
         */
52
        $preview: null,
53
54
        /**
55
         * @property {jQuery}
56
         */
57
        $addBtn: null,
58
59
        /**
60
         * @property {jQuery}
61
         */
62
        $editBtn: null,
63
64
        /**
65
         * @property {jQuery}
66
         */
67
        $removeBtn: null,
68
69
        /**
70
         * @inheritDoc
71
         */
72
        constructor: function NotesComponent() {
73
            NotesComponent.__super__.constructor.apply(this, arguments);
74
        },
75
76
        /**
77
         * @inheritDoc
78
         */
79
        initialize: function(options) {
80
            this.options = $.extend(true, {}, this.options, options || {});
81
            this.$el = options._sourceElement;
82
            this.template = _.template($(this.options.template).text());
83
84
            this.initUI();
85
        },
86
87
        initUI: function() {
88
            this.$notes = this.$el.find('textarea');
89
            this.$el.html(this.template());
90
91
            this.$edit = this.$el.find(this.options.selectors.edit);
92
            this.$preview = this.$el.find(this.options.selectors.preview);
93
            this.$addBtn = this.$el.find(this.options.selectors.addBtn);
94
            this.$editBtn = this.$el.find(this.options.selectors.editBtn);
95
            this.$removeBtn = this.$el.find(this.options.selectors.removeBtn);
96
97
            this.$edit.prepend(this.$notes);
98
99
            this.$notes.change(_.bind(this.change, this));
100
            this.$notes.blur(_.bind(this.change, this));
101
            this.$preview.click(_.bind(this.addNotes, this));
102
            this.$addBtn.click(_.bind(this.addNotes, this))
103
                .mousedown(_.bind(this.addNotes, this));
104
            this.$editBtn.click(_.bind(this.addNotes, this))
105
                .mousedown(_.bind(this.addNotes, this));
106
            this.$removeBtn.click(_.bind(this.removeNotes, this))
107
                .mousedown(_.bind(this.removeNotes, this));
108
109
            this.changed();
110
            this.$el.show();
111
        },
112
113
        hasVal: function() {
114
            return this.$notes.val().replace(/\s/g, '').length > 0;
115
        },
116
117
        change: function(e) {
118
            if (e.relatedTarget === this.$addBtn.get(0) || e.relatedTarget === this.$editBtn.get(0)) {
119
                this.addNotes(e);
120
            } else if (e.relatedTarget === this.$removeBtn.get(0)) {
121
                this.removeNotes(e);
122
            } else {
123
                this.changed();
124
            }
125
        },
126
127
        changed: function() {
128
            if (!this.hasVal()) {
129
                this.removeNotes();
130
            } else {
131
                this.showPreview();
132
            }
133
        },
134
135
        addNotes: function(e) {
136
            this.$notes.show().focus();
137
            this.$preview.hide();
138
            this.$removeBtn.show();
139
            this.$addBtn.hide();
140
            this.$editBtn.hide();
141
            if (e) {
142
                e.preventDefault();
143
            }
144
        },
145
146
        removeNotes: function(e) {
147
            this.$notes.val('');
148
            this.showPreview();
149
            if (e) {
150
                e.preventDefault();
151
            }
152
        },
153
154
        showPreview: function() {
155
            if (this.hasVal()) {
156
                this.$preview.text(this.$notes.val()).show();
157
                this.$addBtn.hide();
158
                this.$editBtn.show();
159
            } else {
160
                this.$notes.val('');
161
                this.$preview.text('').hide();
162
                this.$addBtn.show();
163
                this.$editBtn.hide();
164
            }
165
            this.$notes.hide();
166
            this.$removeBtn.hide();
167
        }
168
    });
169
170
    return NotesComponent;
171
});
172