lib/amd/src/loadingicon.js

  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * Contain the logic for the loading icon.
  17. *
  18. * @module core/loadingicon
  19. * @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. define(['jquery', 'core/templates'], function($, Templates) {
  23. var TEMPLATES = {
  24. LOADING: 'core/loading',
  25. };
  26. var getIcon = function() {
  27. return Templates.render(TEMPLATES.LOADING, {});
  28. };
  29. /**
  30. * Add a loading icon to the end of the specified container and return an unresolved promise.
  31. *
  32. * Resolution of the returned promise causes the icon to be faded out and removed.
  33. *
  34. * @method addIconToContainer
  35. * @param {jQuery|HTMLElement} container The element to add the spinner to
  36. * @return {Promise} The Promise used to create the icon.
  37. */
  38. var addIconToContainer = function(container) {
  39. return getIcon()
  40. .then(function(html) {
  41. var loadingIcon = $(html).hide();
  42. $(container).append(loadingIcon);
  43. loadingIcon.fadeIn(150);
  44. return loadingIcon;
  45. });
  46. };
  47. /**
  48. * Add a loading icon to the end of the specified container and return an unresolved promise.
  49. *
  50. * Resolution of the returned promise causes the icon to be faded out and removed.
  51. *
  52. * @method addIconToContainerWithPromise
  53. * @param {jQuery|HTMLElement} container The element to add the spinner to
  54. * @param {Promise} loadingIconPromise The jQuery Promise which determines the removal of the icon
  55. * @return {jQuery} The Promise used to create and then remove the icon.
  56. */
  57. var addIconToContainerRemoveOnCompletion = function(container, loadingIconPromise) {
  58. return getIcon()
  59. .then(function(html) {
  60. var loadingIcon = $(html).hide();
  61. $(container).append(loadingIcon);
  62. loadingIcon.fadeIn(150);
  63. return $.when(loadingIcon.promise(), loadingIconPromise);
  64. })
  65. .then(function(loadingIcon) {
  66. // Once the content has finished loading and
  67. // the loading icon has been shown then we can
  68. // fade the icon away to reveal the content.
  69. return loadingIcon.fadeOut(100).promise();
  70. })
  71. .then(function(loadingIcon) {
  72. loadingIcon.remove();
  73. return;
  74. });
  75. };
  76. /**
  77. * Add a loading icon to the end of the specified container and return an unresolved promise.
  78. *
  79. * Resolution of the returned promise causes the icon to be faded out and removed.
  80. *
  81. * @method addIconToContainerWithPromise
  82. * @param {jQuery|HTMLElement} container The element to add the spinner to
  83. * @return {Promise} A jQuery Promise to resolve when ready
  84. */
  85. var addIconToContainerWithPromise = function(container) {
  86. var loadingIconPromise = $.Deferred();
  87. addIconToContainerRemoveOnCompletion(container, loadingIconPromise);
  88. return loadingIconPromise;
  89. };
  90. return {
  91. getIcon: getIcon,
  92. addIconToContainer: addIconToContainer,
  93. addIconToContainerWithPromise: addIconToContainerWithPromise,
  94. addIconToContainerRemoveOnCompletion: addIconToContainerRemoveOnCompletion,
  95. };
  96. });