loader_old.js 6.4 KB

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