Test Setup Failed
Push — master ( fef5ab...0d3315 )
by
unknown
13:24 queued 05:06
created

loading-mask-view.js ➔ ... ➔ _.bind   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 0
1
define(function(require) {
2
    'use strict';
3
4
    var LoadingMaskView;
5
    var BaseView = require('./base/view');
6
    var template = require('tpl!oroui/templates/loading-mask-view.html');
7
    var $ = require('jquery');
8
    var _ = require('underscore');
9
10
    LoadingMaskView = BaseView.extend({
11
        autoRender: true,
12
13
        /** @property {string|Function} */
14
        template: template,
15
16
        /** @property {string} */
17
        containerMethod: 'append',
18
19
        /** @property {string} */
20
        className: 'loader-mask',
21
22
        /** @property {string} */
23
        loadingHint: 'Loading...',
24
25
        /** @property {jQuery} */
26
        $parent: null,
27
28
        /**
29
         * Delay of loading mask hide. Allows avoid blinking.
30
         * Set to negative number to disable
31
         *
32
         * @property {number}
33
         */
34
        hideDelay: -1,
35
36
        /**
37
         * Timeout id of current hide request
38
         * If defined means that hide is queued
39
         *
40
         * @property {number}
41
         */
42
        hideTimeoutId: undefined,
43
44
        /**
45
         * @inheritDoc
46
         */
47
        initialize: function(options) {
48
            _.extend(this, _.pick(options, ['loadingHint', 'hideDelay']));
49
            $(window).on(
50
                'pagehide' + this.eventNamespace(),
51
                _.bind(function() {
52
                    this.hide();
53
                }, this)
54
            );
55
            LoadingMaskView.__super__.initialize.apply(this, arguments);
56
        },
57
58
        /**
59
         * @inheritDoc
60
         */
61
        getTemplateData: function() {
62
            var data = {
63
                loadingHint: this.loadingHint
64
            };
65
            return data;
66
        },
67
68
        /**
69
         * Shows loading mask
70
         *
71
         * @param hint {string=}
72
         */
73
        show: function(hint) {
74
            if (hint && _.isString(hint)) {
75
                this.setLoadingHint(hint);
76
            }
77
78
            if (this.hideTimeoutId) {
79
                // clear deferred hide timeout
80
                clearTimeout(this.hideTimeoutId);
81
                delete this.hideTimeoutId;
82
            }
83
84
            if (!this.isShown()) {
85
                this.$parent = this.$el.parent();
86
                this.$parent.addClass('loading');
87
                this.$el.addClass('shown');
88
            }
89
        },
90
91
        /**
92
         * Hides loading mask with delay
93
         * @see this.hideDelay
94
         *
95
         * @param {boolean=} instant if true loading mask will disappear instantly
96
         */
97
        hide: function(instant) {
98
            if (instant || this.hideDelay < 0) {
99
                // instant hide
100
                this._hide();
101
            } else {
102
                // defer hiding if mask is visible and it is not deferred already
103
                if (this.isShown() && !this.hideTimeoutId) {
104
                    this.hideTimeoutId = setTimeout(_.bind(this._hide, this), this.hideDelay);
105
                }
106
            }
107
        },
108
109
        /**
110
         * Hides loading mask
111
         */
112
        _hide: function() {
113
            clearTimeout(this.hideTimeoutId);
114
            delete this.hideTimeoutId;
115
116
            if (!this.isShown()) {
117
                // nothing to do
118
                return;
119
            }
120
121
            this.$el.removeClass('shown');
122
            if (this.$parent && !this.$parent.find('>.loader-mask.shown').length) {
123
                // there are no more loaders for the element
124
                this.$parent.removeClass('loading');
125
            }
126
            this.$parent = null;
127
        },
128
129
        /**
130
         * Toggles loading mask
131
         *
132
         * @param {boolean=} visible
133
         */
134
        toggle: function(visible) {
135
            if (typeof visible === 'undefined') {
136
                visible = !this.isShown();
137
            }
138
            this[visible ? 'show' : 'hide']();
139
        },
140
141
        /**
142
         * Returns state of loading mask
143
         *
144
         * @returns {boolean}
145
         */
146
        isShown: function() {
147
            return !this.disposed && this.$el.hasClass('shown');
148
        },
149
150
        /**
151
         * Sets loading hint for this mask
152
         *
153
         * @param {string} newHint
154
         */
155
        setLoadingHint: function(newHint) {
156
            var oldHint = this.loadingHint;
157
            this.loadingHint = newHint;
158
            this.render();
159
            return oldHint;
160
        },
161
162
        /**
163
         * @inheritDoc
164
         */
165
        dispose: function() {
166
            if (this.disposed) {
167
                return;
168
            }
169
            $(window).off('pagehide' + this.eventNamespace());
170
            this.hide(true);
171
            LoadingMaskView.__super__.dispose.apply(this, arguments);
172
        }
173
    });
174
175
    return LoadingMaskView;
176
});
177