forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader_old.js
207 lines (182 loc) · 5.88 KB
/
loader_old.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/*jshint browser:true */
/*global console:true*/
(function (root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
define([
'jquery',
'mage/template',
'jquery/ui',
'mage/translate'
], factory);
} else {
factory(root.jQuery, root.mageTemplate);
}
}(this, function ($, mageTemplate) {
'use strict';
$.widget('mage.loader', {
loaderStarted: 0,
spinner: $(undefined),
options: {
icon: '',
texts: {
loaderText: $.mage.__('Please wait...'),
imgAlt: $.mage.__('Loading...')
},
template: '<div class="loading-mask" data-role="loader">' +
'<div class="popup popup-loading">' +
'<div class="popup-inner">' +
'<% if (data.icon) { %><img <% if (data.texts.imgAlt) { %>alt="<%- data.texts.imgAlt %>"<% } %> src="<%- data.icon %>"><% } %>' +
'<% if (data.texts.loaderText) { %><%- data.texts.loaderText %><% } %>' +
'</div>' +
'</div>' +
'</div>'
},
/**
* Loader creation
* @protected
*/
_create: function () {
this._bind();
},
/**
* Bind on ajax events
* @protected
*/
_bind: function () {
this._on({
'processStop': 'hide',
'processStart': 'show',
'show.loader': 'show',
'hide.loader': 'hide',
'contentUpdated.loader': '_contentUpdated'
});
},
/**
* Verify loader present after content updated
*
* This will be cleaned up by the task MAGETWO-11070
*
* @param event
* @private
*/
_contentUpdated: function (e) {
this.show(e);
},
/**
* Show loader
*/
show: function (e, ctx) {
this._render();
this.loaderStarted++;
this.spinner.show();
if (ctx) {
this.spinner
.css({
width: ctx.outerWidth(),
height: ctx.outerHeight(),
position: 'absolute'
})
.position({
my: 'top left',
at: 'top left',
of: ctx
});
}
return false;
},
/**
* Hide loader
*/
hide: function () {
if (this.loaderStarted > 0) {
this.loaderStarted--;
if (this.loaderStarted === 0) {
this.spinner.hide();
}
}
return false;
},
/**
* Render loader
* @protected
*/
_render: function () {
var tmpl;
if (this.spinner.length === 0) {
tmpl = mageTemplate(this.options.template, {
data: this.options
});
this.spinner = $(tmpl);
}
this.element.prepend(this.spinner);
},
/**
* Destroy loader
*/
_destroy: function () {
this.spinner.remove();
}
});
/**
* This widget takes care of registering the needed loader listeners on the body
*/
$.widget('mage.loaderAjax', {
options: {
defaultContainer: '[data-container=body]'
},
_create: function () {
this._bind();
// There should only be one instance of this widget, and it should be attached
// to the body only. Having it on the page twice will trigger multiple processStarts.
if (window.console && !this.element.is(this.options.defaultContainer) && $.mage.isDevMode(undefined)) {
console.warn('This widget is intended to be attached to the body, not below.');
}
},
_bind: function () {
$(document).on({
'ajaxSend': this._onAjaxSend.bind(this),
'ajaxComplete': this._onAjaxComplete.bind(this)
});
},
_getJqueryObj: function (loaderContext) {
var ctx;
// Check to see if context is jQuery object or not.
if (loaderContext) {
if (loaderContext.jquery) {
ctx = loaderContext;
} else {
ctx = $(loaderContext);
}
} else {
ctx = $('[data-container="body"]');
}
return ctx;
},
_onAjaxSend: function (e, jqxhr, settings) {
if (settings && settings.showLoader) {
var ctx = this._getJqueryObj(settings.loaderContext);
ctx.trigger('processStart');
// Check to make sure the loader is there on the page if not report it on the console.
// NOTE that this check should be removed before going live. It is just an aid to help
// in finding the uses of the loader that maybe broken.
if (window.console && !ctx.parents('[data-role="loader"]').length) {
console.warn('Expected to start loader but did not find one in the dom');
}
}
},
_onAjaxComplete: function (e, jqxhr, settings) {
if (settings && settings.showLoader && !settings.dontHide) {
this._getJqueryObj(settings.loaderContext).trigger('processStop');
}
}
});
return {
loader: $.mage.loader,
loaderAjax: $.mage.loaderAjax
};
}));