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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. --group.count;
  72. if (group.count == 0) {
  73. if (!luBusyBusiness.hasError(group)) {
  74. group.deferred.resolve(null);
  75. }
  76. else {
  77. group.deferred.resolve(null);
  78. }
  79. group.deferred = null;
  80. }
  81. }
  82. };
  83. luBusyBusiness.reject = function(groups, error)
  84. {
  85. if (typeof groups === 'string') {
  86. groups = [groups];
  87. }
  88. for (var i = 0; i < groups.length; ++i) {
  89. var group = luBusyBusiness.groups[groups[i]];
  90. --group.count;
  91. group.errors.push(error);
  92. if (group.count == 0) {
  93. group.deferred.resolve(null);
  94. group.deferred = null;
  95. }
  96. }
  97. };
  98. luBusyBusiness.reset = function()
  99. {
  100. luBusyBusiness.groups = {};
  101. return luBusyBusiness;
  102. };
  103. return luBusyBusiness;
  104. }]);
  105. })();