Test Setup Failed
Push — master ( 89eab9...c258e2 )
by
unknown
04:57
created

email-folder-tree-view.js ➔ ... ➔ inputs.each   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 4
rs 10
nop 0
1
define(function(require) {
2
    'use strict';
3
4
    var EmailFolderTreeView;
5
    var $ = require('jquery');
6
    var _ = require('underscore');
7
    var mediator = require('oroui/js/mediator');
8
    var BaseView = require('oroui/js/app/views/base/view');
9
10
    EmailFolderTreeView = BaseView.extend({
11
        dataInputSelector: null,
12
13
        checkAllSelector: null,
14
15
        relatedCheckboxesSelector: null,
16
17
        requiredOptions: [
18
            'dataInputSelector',
19
            'checkAllSelector',
20
            'relatedCheckboxesSelector'
21
        ],
22
23
        /**
24
         * @inheritDoc
25
         */
26
        constructor: function EmailFolderTreeView() {
27
            EmailFolderTreeView.__super__.constructor.apply(this, arguments);
28
        },
29
30
        /**
31
         * @inheritDoc
32
         */
33
        initialize: function(options) {
34
            _.each(this.requiredOptions, function(optionName) {
35
                if (!_.has(options, optionName)) {
36
                    throw new Error('Required option "' + optionName + '" not found.');
37
                }
38
            });
39
40
            this.dataInputSelector = options.dataInputSelector;
41
            this.$el.closest('form').on('submit' + this.eventNamespace(), _.bind(this._onSubmit, this));
42
43
            this.checkAllSelector = options.checkAllSelector;
44
            this.relatedCheckboxesSelector = options.relatedCheckboxesSelector;
45
            this.$(this.checkAllSelector).on('change' + this.eventNamespace(), _.bind(this._onCheckAllChange, this));
46
            this.listenTo(mediator, 'serializeFolderCollection', this._serializeFolderCollection);
47
        },
48
49
        dispose: function() {
50
            if (this.disposed) {
51
                return;
52
            }
53
            this.$el.closest('form').off(this.eventNamespace());
54
            this.$(this.checkAllSelector).off(this.eventNamespace());
55
            EmailFolderTreeView.__super__.dispose.apply(this, arguments);
56
        },
57
58
        _inputData: function($root) {
59
            var data = {};
60
            var inputs = $root.find('> input[data-name]').add($root.find('> label > input[data-name]:checked'));
61
62
            inputs.each(function() {
63
                var $input = $(this);
64
                data[$input.attr('data-name')] = $input.val();
65
            });
66
            data.subFolders = this._inputCollectionData($root.find('> .folder-sub-folders').children());
67
68
            return data;
69
        },
70
71
        _inputCollectionData: function($inputCollection) {
72
            return _.map(
73
                $inputCollection,
74
                function(recordEl) {
75
                    return this._inputData($(recordEl));
76
                },
77
                this
78
            );
79
        },
80
81
        _serializeFolderCollection: function() {
82
            var folders = this._inputCollectionData(this.$('.folder-list').children());
83
            this.$(this.dataInputSelector).val(JSON.stringify(folders));
84
        },
85
86
        _onSubmit: function() {
87
            this._serializeFolderCollection();
88
        },
89
90
        _onCheckAllChange: function(e) {
91
            this.$(this.relatedCheckboxesSelector).prop('checked', e.currentTarget.checked);
92
        }
93
    });
94
95
    return EmailFolderTreeView;
96
});
97