You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lu-busy-business.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * Created by robin on 10/24/15.
  3. */
  4. (function () {
  5. 'use strict';
  6. angular.module('luticate2Utils')
  7. .factory('luBusyBusiness', ['$q', function($q) {
  8. var luBusyBusiness = {};
  9. luBusyBusiness.groups = null;
  10. luBusyBusiness.initGroups = function(groups)
  11. {
  12. if (typeof groups === 'string') {
  13. groups = [groups];
  14. }
  15. for (var i = 0; i < groups.length; ++i) {
  16. var group = luBusyBusiness.groups[groups[i]];
  17. if (group == null ) {
  18. group = {
  19. count: 0,
  20. deferred: null,
  21. errors: []
  22. };
  23. luBusyBusiness.groups[groups[i]] = group;
  24. }
  25. if (group.count == 0) {
  26. group.deferred = $q.defer();
  27. group.errors = [];
  28. }
  29. ++group.count;
  30. }
  31. };
  32. luBusyBusiness.isRunning = function(group)
  33. {
  34. var data = luBusyBusiness.groups[group];
  35. if (data != null) {
  36. return data.count > 0;
  37. }
  38. return false;
  39. };
  40. luBusyBusiness.promise = function(group)
  41. {
  42. var data = luBusyBusiness.groups[group];
  43. if (data != null && data.deferred != null) {
  44. return data.deferred.promise;
  45. }
  46. return null;
  47. };
  48. luBusyBusiness.hasError = function(group)
  49. {
  50. var data = luBusyBusiness.groups[group];
  51. if (data != null) {
  52. return data.errors.length != 0;
  53. }
  54. return null;
  55. };
  56. luBusyBusiness.errors = function(group)
  57. {
  58. var data = luBusyBusiness.groups[group];
  59. if (data != null) {
  60. return data.errors;
  61. }
  62. return null;
  63. };
  64. luBusyBusiness.resolve = function(groups)
  65. {
  66. if (typeof groups === 'string') {
  67. groups = [groups];
  68. }
  69. for (var i = 0; i < groups.length; ++i) {
  70. var group = luBusyBusiness.groups[groups[i]];
  71. if (group != null) {
  72. --group.count;
  73. if (group.count == 0) {
  74. if (!luBusyBusiness.hasError(group)) {
  75. group.deferred.resolve(null);
  76. }
  77. else {
  78. group.deferred.resolve(null);
  79. }
  80. group.deferred = null;
  81. }
  82. }
  83. }
  84. };
  85. luBusyBusiness.reject = function(groups, error)
  86. {
  87. if (typeof groups === 'string') {
  88. groups = [groups];
  89. }
  90. for (var i = 0; i < groups.length; ++i) {
  91. var group = luBusyBusiness.groups[groups[i]];
  92. if (group != null) {
  93. --group.count;
  94. group.errors.push(error);
  95. if (group.count == 0) {
  96. group.deferred.resolve(null);
  97. group.deferred = null;
  98. }
  99. }
  100. }
  101. };
  102. luBusyBusiness.reset = function()
  103. {
  104. luBusyBusiness.groups = {};
  105. return luBusyBusiness;
  106. };
  107. return luBusyBusiness;
  108. }]);
  109. })();