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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Created by robin on 10/24/15.
  3. */
  4. (function () {
  5. 'use strict';
  6. angular.module('luticate2Utils')
  7. .factory('luBusyBusiness', [function() {
  8. var luBusyBusiness = {};
  9. luBusyBusiness.promises = {};
  10. luBusyBusiness.loaders = [];
  11. luBusyBusiness.errors = [];
  12. luBusyBusiness.STATUS_RUNNING = 0;
  13. luBusyBusiness.STATUS_RESOLVED = 1;
  14. luBusyBusiness.STATUS_REJECTED = 2;
  15. luBusyBusiness.build = function(array)
  16. {
  17. luBusyBusiness[array] = {};
  18. for (var id in luBusyBusiness.promises) {
  19. if (luBusyBusiness.promises.hasOwnProperty(id)) {
  20. var promise = luBusyBusiness.promises[id];
  21. promise[array].forEach(function (group) {
  22. if (luBusyBusiness[array][group] == null) {
  23. luBusyBusiness[array][group] = [];
  24. }
  25. luBusyBusiness[array][group].push(promise);
  26. });
  27. }
  28. }
  29. };
  30. luBusyBusiness.newPromise = function(id, groups, loaderGroups, errorGroups) {
  31. return {
  32. id: id,
  33. groups: groups,
  34. loaderGroups: loaderGroups,
  35. errorGroups: errorGroups
  36. };
  37. };
  38. luBusyBusiness.initLuBusy = function(data) {
  39. var p = {
  40. loaders : [],
  41. errors: [],
  42. status: luBusyBusiness.STATUS_RUNNING,
  43. id: data.id
  44. };
  45. if (data.groups instanceof Array) {
  46. data.groups.forEach(function (group) {
  47. p.loaders.push(group);
  48. p.errors.push(group);
  49. });
  50. }
  51. else if (typeof data.groups === 'string') {
  52. p.loaders.push(data.groups);
  53. p.errors.push(data.groups);
  54. }
  55. if (data.loaderGroups instanceof Array) {
  56. data.loaderGroups.forEach(function (group) {
  57. p.loaders.push(group);
  58. });
  59. }
  60. else if (typeof data.loaderGroups === 'string') {
  61. p.loaders.push(data.loaderGroups);
  62. }
  63. if (data.errorGroups instanceof Array) {
  64. data.errorGroups.forEach(function (group) {
  65. p.errors.push(group);
  66. });
  67. }
  68. else if (typeof data.errorGroups === 'string') {
  69. p.errors.push(data.errorGroups);
  70. }
  71. luBusyBusiness.promises[data.id] = p;
  72. luBusyBusiness.build("loaders");
  73. luBusyBusiness.build("errors");
  74. };
  75. luBusyBusiness.getLoadersGroup = function(group) {
  76. if (luBusyBusiness.loaders[group] == null) {
  77. return null;
  78. }
  79. return luBusyBusiness.loaders[group];
  80. };
  81. luBusyBusiness.getErrorsGroup = function(group) {
  82. if (luBusyBusiness.errors[group] == null) {
  83. return null;
  84. }
  85. return luBusyBusiness.errors[group];
  86. };
  87. luBusyBusiness.reject = function(promise, error) {
  88. luBusyBusiness.promises[promise.id].status = luBusyBusiness.STATUS_REJECTED;
  89. luBusyBusiness.promises[promise.id].value = error;
  90. };
  91. luBusyBusiness.resolve = function(promise) {
  92. luBusyBusiness.promises[promise.id].status = luBusyBusiness.STATUS_RESOLVED;
  93. luBusyBusiness.promises[promise.id].value = null;
  94. };
  95. return luBusyBusiness;
  96. }]);
  97. })();