123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- /**
- * Created by robin on 10/24/15.
- */
-
- (function () {
- 'use strict';
- angular.module('luticate2Utils')
- .factory('luBusyBusiness', [function() {
-
- var luBusyBusiness = {};
- luBusyBusiness.promises = {};
- luBusyBusiness.loaders = [];
- luBusyBusiness.errors = [];
-
- luBusyBusiness.STATUS_RUNNING = 0;
- luBusyBusiness.STATUS_RESOLVED = 1;
- luBusyBusiness.STATUS_REJECTED = 2;
-
- luBusyBusiness.build = function(array)
- {
- luBusyBusiness[array] = {};
- for (var id in luBusyBusiness.promises) {
- if (luBusyBusiness.promises.hasOwnProperty(id)) {
- var promise = luBusyBusiness.promises[id];
- promise[array].forEach(function (group) {
- if (luBusyBusiness[array][group] == null) {
- luBusyBusiness[array][group] = [];
- }
- luBusyBusiness[array][group].push(promise);
- });
- }
- }
- };
-
- luBusyBusiness.newPromise = function(id, groups, loaderGroups, errorGroups) {
- return {
- id: id,
- groups: groups,
- loaderGroups: loaderGroups,
- errorGroups: errorGroups
- };
- };
-
- luBusyBusiness.initLuBusy = function(data) {
- var p = {
- loaders : [],
- errors: [],
- status: luBusyBusiness.STATUS_RUNNING,
- id: data.id
- };
-
- if (data.groups instanceof Array) {
- data.groups.forEach(function (group) {
- p.loaders.push(group);
- p.errors.push(group);
- });
- }
- else if (typeof data.groups === 'string') {
- p.loaders.push(data.groups);
- p.errors.push(data.groups);
- }
-
- if (data.loaderGroups instanceof Array) {
- data.loaderGroups.forEach(function (group) {
- p.loaders.push(group);
- });
- }
- else if (typeof data.loaderGroups === 'string') {
- p.loaders.push(data.loaderGroups);
- }
-
- if (data.errorGroups instanceof Array) {
- data.errorGroups.forEach(function (group) {
- p.errors.push(group);
- });
- }
- else if (typeof data.errorGroups === 'string') {
- p.errors.push(data.errorGroups);
- }
-
- luBusyBusiness.promises[data.id] = p;
-
- luBusyBusiness.build("loaders");
- luBusyBusiness.build("errors");
- };
-
- luBusyBusiness.getLoadersGroup = function(group) {
- if (luBusyBusiness.loaders[group] == null) {
- return null;
- }
- return luBusyBusiness.loaders[group];
- };
-
- luBusyBusiness.getErrorsGroup = function(group) {
- if (luBusyBusiness.errors[group] == null) {
- return null;
- }
- return luBusyBusiness.errors[group];
- };
-
- luBusyBusiness.reject = function(promise, error) {
- luBusyBusiness.promises[promise.id].status = luBusyBusiness.STATUS_REJECTED;
- luBusyBusiness.promises[promise.id].value = error;
- };
-
- luBusyBusiness.resolve = function(promise) {
- luBusyBusiness.promises[promise.id].status = luBusyBusiness.STATUS_RESOLVED;
- luBusyBusiness.promises[promise.id].value = null;
- };
-
- return luBusyBusiness;
- }]);
- })();
|