選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

228 行
6.2 KiB

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