abp.spin.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. var abp = abp || {};
  2. (function () {
  3. if (!$.fn.spin) {
  4. return;
  5. }
  6. abp.libs = abp.libs || {};
  7. abp.libs.spinjs = {
  8. spinner_config: {
  9. lines: 11,
  10. length: 0,
  11. width: 10,
  12. radius: 20,
  13. corners: 1.0,
  14. trail: 60,
  15. speed: 1.2
  16. },
  17. //Config for busy indicator in element's inner element that has '.abp-busy-indicator' class.
  18. spinner_config_inner_busy_indicator: {
  19. lines: 11,
  20. length: 0,
  21. width: 4,
  22. radius: 7,
  23. corners: 1.0,
  24. trail: 60,
  25. speed: 1.2
  26. }
  27. };
  28. abp.ui.setBusy = function (elm, optionsOrPromise) {
  29. optionsOrPromise = optionsOrPromise || {};
  30. if (optionsOrPromise.always || optionsOrPromise['finally']) { //Check if it's promise
  31. optionsOrPromise = {
  32. promise: optionsOrPromise
  33. };
  34. }
  35. var options = $.extend({}, optionsOrPromise);
  36. if (!elm) {
  37. if (options.blockUI != false) {
  38. abp.ui.block();
  39. }
  40. $('body').spin(abp.libs.spinjs.spinner_config);
  41. } else {
  42. var $elm = $(elm);
  43. var $busyIndicator = $elm.find('.abp-busy-indicator'); //TODO@Halil: What if more than one element. What if there are nested elements?
  44. if ($busyIndicator.length) {
  45. $busyIndicator.spin(abp.libs.spinjs.spinner_config_inner_busy_indicator);
  46. } else {
  47. if (options.blockUI != false) {
  48. abp.ui.block(elm);
  49. }
  50. $elm.spin(abp.libs.spinjs.spinner_config);
  51. }
  52. }
  53. if (options.promise) { //Supports Q and jQuery.Deferred
  54. if (options.promise.always) {
  55. options.promise.always(function () {
  56. abp.ui.clearBusy(elm);
  57. });
  58. } else if (options.promise['finally']) {
  59. options.promise['finally'](function () {
  60. abp.ui.clearBusy(elm);
  61. });
  62. }
  63. }
  64. };
  65. abp.ui.clearBusy = function (elm) {
  66. //TODO@Halil: Maybe better to do not call unblock if it's not blocked by setBusy
  67. if (!elm) {
  68. abp.ui.unblock();
  69. $('body').spin(false);
  70. } else {
  71. var $elm = $(elm);
  72. var $busyIndicator = $elm.find('.abp-busy-indicator');
  73. if ($busyIndicator.length) {
  74. $busyIndicator.spin(false);
  75. } else {
  76. abp.ui.unblock(elm);
  77. $elm.spin(false);
  78. }
  79. }
  80. };
  81. })();