瀏覽代碼

begin promises management

tags/0.1.0
Robin Thoni 9 年之前
父節點
當前提交
fa26dd22e9
共有 5 個文件被更改,包括 763 次插入25 次删除
  1. 285
    0
      src/lubusy.css
  2. 344
    0
      src/lubusy.js
  3. 7
    0
      src/luticateutils.js
  4. 89
    0
      src/promises.js
  5. 38
    25
      src/request.js

+ 285
- 0
src/lubusy.css 查看文件

@@ -0,0 +1,285 @@
1
+.lu-busy{
2
+    position:absolute;
3
+    top:0px;
4
+    left:0px;
5
+    right:0px;
6
+    bottom:0px;
7
+    z-index:1001;
8
+}
9
+
10
+.lu-busy-animation.ng-hide-add,
11
+.lu-busy-animation.ng-hide-remove {
12
+    -webkit-transition:all .3s ease;
13
+    -moz-transition:all .3s ease;
14
+    -o-transition:all .3s ease;
15
+    transition:all .3s ease;
16
+    display:block !important;
17
+}
18
+.lu-busy-animation.ng-hide-remove {
19
+    opacity:0;
20
+    -webkit-transform:translate(0px,-40px);
21
+    -moz-transform:translate(0px,-40px);
22
+    -ms-transform:translate(0px,-40px);
23
+    -o-transform:translate(0px,-40px);
24
+    transform:translate(0px,-40px);
25
+}
26
+.lu-busy-animation.ng-hide-remove.ng-hide-remove-active {
27
+    opacity:1;
28
+    -webkit-transform:translate(0px,0px);
29
+    -moz-transform:translate(0px,0px);
30
+    -ms-transform:translate(0px,0px);
31
+    -o-transform:translate(0px,0px);
32
+    transform:translate(0px,0px);
33
+}
34
+.lu-busy-animation.ng-hide-add {
35
+    opacity:1;
36
+    -webkit-transform:translate(0px,0px);
37
+    -moz-transform:translate(0px,0px);
38
+    -ms-transform:translate(0px,0px);
39
+    -o-transform:translate(0px,0px);
40
+    transform:translate(0px,0px);
41
+}
42
+.lu-busy-animation.ng-hide-add.ng-hide-add-active {
43
+    opacity:0;
44
+    -webkit-transform:translate(0px,-40px);
45
+    -moz-transform:translate(0px,-40px);
46
+    -ms-transform:translate(0px,-40px);
47
+    -o-transform:translate(0px,-40px);
48
+    transform:translate(0px,-40px);
49
+}
50
+
51
+.lu-busy-backdrop {
52
+    background-color:white;
53
+    opacity:.7;
54
+}
55
+
56
+.lu-busy-backdrop-animation.ng-hide-add,
57
+.lu-busy-backdrop-animation.ng-hide-remove {
58
+    -webkit-transition:opacity .3s ease;
59
+    -moz-transition:opacity .3s ease;
60
+    -o-transition:opacity .3s ease;
61
+    transition:opacity .3s ease;
62
+    display:block !important;
63
+}
64
+
65
+.lu-busy-backdrop-animation.ng-hide {
66
+    opacity:0;
67
+}
68
+
69
+/* All styles below are for the default template. */
70
+
71
+.lu-busy-default-wrapper {
72
+    text-align:center;
73
+}
74
+
75
+.lu-busy-default-sign{
76
+    display: inline-block;
77
+    position:relative;
78
+    z-index:1002;
79
+    padding-bottom: 6px;
80
+    color:#333333;
81
+    text-shadow:0 1px 1px rgba(255, 255, 255, 0.75);
82
+    background-color:#e9eeee;
83
+    border:1px solid #dddddd;
84
+    border-top-width:0;
85
+    -webkit-border-radius:7px;
86
+    -moz-border-radius:7px;
87
+    border-radius:7px;
88
+    border-top-left-radius:0;
89
+    border-top-right-radius:0;
90
+    -webkit-box-shadow:inset 0 1px 0 rgba(255,255,255,.2), 0 1px 2px rgba(0,0,0,.05);
91
+    -moz-box-shadow:inset 0 1px 0 rgba(255,255,255,.2), 0 1px 2px rgba(0,0,0,.05);
92
+    box-shadow:inset 0 1px 0 rgba(255,255,255,.2), 0 1px 2px rgba(0,0,0,.05);
93
+}
94
+.lu-busy-default-text{
95
+    margin:13px 12px 6px 49px;
96
+    font-size:16px;
97
+    color:#555;
98
+    text-align: left;
99
+    max-width: 400px;
100
+}
101
+.lu-busy-default-spinner{
102
+    position:absolute;
103
+    width:25px;
104
+    height:25px;
105
+    display:inline-block;
106
+    top:12px;
107
+    left:14px;
108
+}
109
+.lu-busy-default-spinner div{
110
+    width:12%;
111
+    height:26%;
112
+    background:#000;
113
+    position:absolute;
114
+    left:44.5%;
115
+    top:37%;
116
+    opacity:0;
117
+    -webkit-animation:lu-busy-spinner-anim 1s linear infinite;
118
+    -moz-animation:lu-busy-spinner-anim 1s linear infinite;
119
+    -ms-animation:lu-busy-spinner-anim 1s linear infinite;
120
+    -o-animation:lu-busy-spinner-anim 1s linear infinite;
121
+    animation:lu-busy-spinner-anim 1s linear infinite;
122
+    -webkit-border-radius:50px;
123
+    -moz-border-radius:50px;
124
+    border-radius:50px;
125
+    -webkit-box-shadow:0 0 3px rgba(0,0,0,0.2);
126
+    -moz-box-shadow:0 0 3px rgba(0,0,0,0.2);
127
+    box-shadow:0 0 3px rgba(0,0,0,0.2);
128
+}
129
+.lu-busy-default-spinner div.bar1{
130
+    -webkit-transform:rotate(0deg) translate(0, -142%);
131
+    -moz-transform:rotate(0deg) translate(0, -142%);
132
+    -ms-transform:rotate(0deg) translate(0, -142%);
133
+    -o-transform:rotate(0deg) translate(0, -142%);
134
+    transform:rotate(0deg) translate(0, -142%);
135
+    -webkit-animation-delay:0s;
136
+    -moz-animation-delay:0s;
137
+    -ms-animation-delay:0s;
138
+    -o-animation-delay:0s;
139
+    animation-delay:0s;
140
+}
141
+.lu-busy-default-spinner div.bar2{
142
+    -webkit-transform:rotate(30deg) translate(0, -142%);
143
+    -moz-transform:rotate(30deg) translate(0, -142%);
144
+    -ms-transform:rotate(30deg) translate(0, -142%);
145
+    -o-transform:rotate(30deg) translate(0, -142%);
146
+    transform:rotate(30deg) translate(0, -142%);
147
+    -webkit-animation-delay:-0.9167s;
148
+    -moz-animation-delay:-0.9167s;
149
+    -ms-animation-delay:-0.9167s;
150
+    -o-animation-delay:-0.9167s;
151
+    animation-delay:-0.9167s;
152
+}
153
+.lu-busy-default-spinner div.bar3{
154
+    -webkit-transform:rotate(60deg) translate(0, -142%);
155
+    -moz-transform:rotate(60deg) translate(0, -142%);
156
+    -ms-transform:rotate(60deg) translate(0, -142%);
157
+    -o-transform:rotate(60deg) translate(0, -142%);
158
+    transform:rotate(60deg) translate(0, -142%);
159
+    -webkit-animation-delay:-0.833s;
160
+    -moz-animation-delay:-0.833s;
161
+    -ms-animation-delay:-0.833s;
162
+    -o-animation-delay:-0.833s;
163
+    animation-delay:-0.833s;
164
+}
165
+.lu-busy-default-spinner div.bar4{
166
+    -webkit-transform:rotate(90deg) translate(0, -142%);
167
+    -moz-transform:rotate(90deg) translate(0, -142%);
168
+    -ms-transform:rotate(90deg) translate(0, -142%);
169
+    -o-transform:rotate(90deg) translate(0, -142%);
170
+    transform:rotate(90deg) translate(0, -142%);
171
+    -webkit-animation-delay:-0.75s;
172
+    -moz-animation-delay:-0.75s;
173
+    -ms-animation-delay:-0.75s;
174
+    -o-animation-delay:-0.75s;
175
+    animation-delay:-0.75s;
176
+}
177
+.lu-busy-default-spinner div.bar5{
178
+    -webkit-transform:rotate(120deg) translate(0, -142%);
179
+    -moz-transform:rotate(120deg) translate(0, -142%);
180
+    -ms-transform:rotate(120deg) translate(0, -142%);
181
+    -o-transform:rotate(120deg) translate(0, -142%);
182
+    transform:rotate(120deg) translate(0, -142%);
183
+    -webkit-animation-delay:-0.667s;
184
+    -moz-animation-delay:-0.667s;
185
+    -ms-animation-delay:-0.667s;
186
+    -o-animation-delay:-0.667s;
187
+    animation-delay:-0.667s;
188
+}
189
+.lu-busy-default-spinner div.bar6{
190
+    -webkit-transform:rotate(150deg) translate(0, -142%);
191
+    -moz-transform:rotate(150deg) translate(0, -142%);
192
+    -ms-transform:rotate(150deg) translate(0, -142%);
193
+    -o-transform:rotate(150deg) translate(0, -142%);
194
+    transform:rotate(150deg) translate(0, -142%);
195
+    -webkit-animation-delay:-0.5833s;
196
+    -moz-animation-delay:-0.5833s;
197
+    -ms-animation-delay:-0.5833s;
198
+    -o-animation-delay:-0.5833s;
199
+    animation-delay:-0.5833s;
200
+}
201
+.lu-busy-default-spinner div.bar7{
202
+    -webkit-transform:rotate(180deg) translate(0, -142%);
203
+    -moz-transform:rotate(180deg) translate(0, -142%);
204
+    -ms-transform:rotate(180deg) translate(0, -142%);
205
+    -o-transform:rotate(180deg) translate(0, -142%);
206
+    transform:rotate(180deg) translate(0, -142%);
207
+    -webkit-animation-delay:-0.5s;
208
+    -moz-animation-delay:-0.5s;
209
+    -ms-animation-delay:-0.5s;
210
+    -o-animation-delay:-0.5s;
211
+    animation-delay:-0.5s;
212
+}
213
+.lu-busy-default-spinner div.bar8{
214
+    -webkit-transform:rotate(210deg) translate(0, -142%);
215
+    -moz-transform:rotate(210deg) translate(0, -142%);
216
+    -ms-transform:rotate(210deg) translate(0, -142%);
217
+    -o-transform:rotate(210deg) translate(0, -142%);
218
+    transform:rotate(210deg) translate(0, -142%);
219
+    -webkit-animation-delay:-0.41667s;
220
+    -moz-animation-delay:-0.41667s;
221
+    -ms-animation-delay:-0.41667s;
222
+    -o-animation-delay:-0.41667s;
223
+    animation-delay:-0.41667s;
224
+}
225
+.lu-busy-default-spinner div.bar9{
226
+    -webkit-transform:rotate(240deg) translate(0, -142%);
227
+    -moz-transform:rotate(240deg) translate(0, -142%);
228
+    -ms-transform:rotate(240deg) translate(0, -142%);
229
+    -o-transform:rotate(240deg) translate(0, -142%);
230
+    transform:rotate(240deg) translate(0, -142%);
231
+    -webkit-animation-delay:-0.333s;
232
+    -moz-animation-delay:-0.333s;
233
+    -ms-animation-delay:-0.333s;
234
+    -o-animation-delay:-0.333s;
235
+    animation-delay:-0.333s;
236
+}
237
+.lu-busy-default-spinner div.bar10{
238
+    -webkit-transform:rotate(270deg) translate(0, -142%);
239
+    -moz-transform:rotate(270deg) translate(0, -142%);
240
+    -ms-transform:rotate(270deg) translate(0, -142%);
241
+    -o-transform:rotate(270deg) translate(0, -142%);
242
+    transform:rotate(270deg) translate(0, -142%);
243
+    -webkit-animation-delay:-0.25s;
244
+    -moz-animation-delay:-0.25s;
245
+    -ms-animation-delay:-0.25s;
246
+    -o-animation-delay:-0.25s;
247
+    animation-delay:-0.25s;
248
+}
249
+.lu-busy-default-spinner div.bar11{
250
+    -webkit-transform:rotate(300deg) translate(0, -142%);
251
+    -moz-transform:rotate(300deg) translate(0, -142%);
252
+    -ms-transform:rotate(300deg) translate(0, -142%);
253
+    -o-transform:rotate(300deg) translate(0, -142%);
254
+    transform:rotate(300deg) translate(0, -142%);
255
+    -webkit-animation-delay:-0.1667s;
256
+    -moz-animation-delay:-0.1667s;
257
+    -ms-animation-delay:-0.1667s;
258
+    -o-animation-delay:-0.1667s;
259
+    animation-delay:-0.1667s;
260
+}
261
+.lu-busy-default-spinner div.bar12{
262
+    -webkit-transform:rotate(330deg) translate(0, -142%);
263
+    -moz-transform:rotate(330deg) translate(0, -142%);
264
+    -ms-transform:rotate(330deg) translate(0, -142%);
265
+    -o-transform:rotate(330deg) translate(0, -142%);
266
+    transform:rotate(330deg) translate(0, -142%);
267
+    -webkit-animation-delay:-0.0833s;
268
+    -moz-animation-delay:-0.0833s;
269
+    -ms-animation-delay:-0.0833s;
270
+    -o-animation-delay:-0.0833s;
271
+    animation-delay:-0.0833s;
272
+}
273
+
274
+@-webkit-keyframes lu-busy-spinner-anim{
275
+    from {opacity: 1;}
276
+    to {opacity: 0.25;}
277
+}
278
+@-moz-keyframes lu-busy-spinner-anim{
279
+    from {opacity: 1;}
280
+    to {opacity: 0.25;}
281
+}
282
+@keyframes lu-busy-spinner-anim{
283
+    from {opacity: 1;}
284
+    to {opacity: 0.25;}
285
+}

+ 344
- 0
src/lubusy.js 查看文件

@@ -0,0 +1,344 @@
1
+/**
2
+ * Created by robin on 10/24/15.
3
+ */
4
+
5
+
6
+angular.module('luticateUtils')
7
+    .directive('luBusy', ['$compile', 'luticatePromises', function($compile, luticatePromises){
8
+        return {
9
+            restrict: 'A',
10
+            scope: {},
11
+            link: function($scope, element, attrs) {
12
+
13
+                var position = element.css('position');
14
+                if (position === 'static' || position === '' || typeof position === 'undefined'){
15
+                    element.css('position','relative');
16
+                }
17
+                var group = attrs.luBusy;
18
+
19
+                $scope.isLoading = false;
20
+                $scope.hasError = false;
21
+
22
+                $scope.update = function()
23
+                {
24
+                    var loaders = luticatePromises.getLoadersGroup(group);
25
+                    $scope.isLoading = false;
26
+                    if (loaders != null) {
27
+                        $scope.isLoading = loaders.some(function (promise) {
28
+                            return promise.status == 0;
29
+                        });
30
+                    }
31
+                    var errors = luticatePromises.getLoadersGroup(group);
32
+                    $scope.hasError = false;
33
+                    if (loaders != null) {
34
+                        $scope.hasError = errors.some(function (promise) {
35
+                            return promise.status == 2;
36
+                        });
37
+                    }
38
+                };
39
+
40
+                $scope.loaderSplashIsActive = function() {
41
+                    $scope.update();
42
+                    return $scope.isLoading && !$scope.hasError;
43
+                };
44
+                $scope.errorSplashIsActive = function() {
45
+                    $scope.update();
46
+                    return $scope.hasError;
47
+                };
48
+
49
+                var backdrop = '<div class="lu-busy lu-busy-backdrop lu-busy-backdrop-animation ng-hide" ng-show="loaderSplashIsActive()"></div>';
50
+                var backdropElement = $compile(backdrop)($scope);
51
+                element.append(backdropElement);
52
+
53
+                var template = '<div class="lu-busy lu-busy-animation ng-hide" ng-show="loaderSplashIsActive()"><div class="lu-busy-default-wrapper">' +
54
+                    '<div class="lu-busy-default-sign">' +
55
+                    '<div class="lu-busy-default-text">Please wait...</div>' +
56
+                    '</div>' +
57
+                    '</div></div>';
58
+                var templateElement = $compile(template)($scope);
59
+
60
+                angular.element(templateElement.children()[0])
61
+                    .css('position','absolute')
62
+                    .css('top',0)
63
+                    .css('left',0)
64
+                    .css('right',0)
65
+                    .css('bottom',0);
66
+                element.append(templateElement);
67
+            }
68
+        };
69
+    }
70
+    ]);
71
+
72
+/*
73
+angular.module('luticateUtils').factory('_luBusyTrackerFactory',['$timeout','$q',function($timeout,$q){
74
+
75
+    return function(){
76
+
77
+        var tracker = {};
78
+        tracker.promises = [];
79
+        tracker.delayPromise = null;
80
+        tracker.durationPromise = null;
81
+        tracker.delayJustFinished = false;
82
+
83
+        tracker.reset = function(options){
84
+            tracker.minDuration = options.minDuration;
85
+
86
+            tracker.promises = [];
87
+            angular.forEach(options.promises,function(p){
88
+                if (!p || p.$luBusyFulfilled) {
89
+                    return;
90
+                }
91
+                addPromiseLikeThing(p);
92
+            });
93
+
94
+            if (tracker.promises.length === 0) {
95
+                //if we have no promises then dont do the delay or duration stuff
96
+                return;
97
+            }
98
+
99
+            tracker.delayJustFinished = false;
100
+            if (options.delay) {
101
+                tracker.delayPromise = $timeout(function(){
102
+                    tracker.delayPromise = null;
103
+                    tracker.delayJustFinished = true;
104
+                },parseInt(options.delay,10));
105
+            }
106
+            if (options.minDuration) {
107
+                tracker.durationPromise = $timeout(function(){
108
+                    tracker.durationPromise = null;
109
+                },parseInt(options.minDuration,10) + (options.delay ? parseInt(options.delay,10) : 0));
110
+            }
111
+        };
112
+
113
+        tracker.isPromise = function(promiseThing){
114
+            var then = promiseThing && (promiseThing.then || promiseThing.$then ||
115
+                (promiseThing.$promise && promiseThing.$promise.then));
116
+
117
+            return typeof then !== 'undefined';
118
+        };
119
+
120
+        tracker.callThen = function(promiseThing,success,error){
121
+            var promise;
122
+            if (promiseThing.then || promiseThing.$then){
123
+                promise = promiseThing;
124
+            } else if (promiseThing.$promise){
125
+                promise = promiseThing.$promise;
126
+            } else if (promiseThing.denodeify){
127
+                promise = $q.when(promiseThing);
128
+            }
129
+
130
+            var then = (promise.then || promise.$then);
131
+
132
+            then.call(promise,success,error);
133
+        };
134
+
135
+        var addPromiseLikeThing = function(promise){
136
+
137
+            if (!tracker.isPromise(promise)) {
138
+                throw new Error('luBusy expects a promise (or something that has a .promise or .$promise');
139
+            }
140
+
141
+            if (tracker.promises.indexOf(promise) !== -1){
142
+                return;
143
+            }
144
+            tracker.promises.push(promise);
145
+
146
+            tracker.callThen(promise, function(){
147
+                promise.$luBusyFulfilled = true;
148
+                if (tracker.promises.indexOf(promise) === -1) {
149
+                    return;
150
+                }
151
+                tracker.promises.splice(tracker.promises.indexOf(promise),1);
152
+            },function(){
153
+                promise.$luBusyFulfilled = true;
154
+                if (tracker.promises.indexOf(promise) === -1) {
155
+                    return;
156
+                }
157
+                tracker.promises.splice(tracker.promises.indexOf(promise),1);
158
+            });
159
+        };
160
+
161
+        tracker.active = function(){
162
+            if (tracker.delayPromise){
163
+                return false;
164
+            }
165
+
166
+            if (!tracker.delayJustFinished){
167
+                if (tracker.durationPromise){
168
+                    return true;
169
+                }
170
+                return tracker.promises.length > 0;
171
+            } else {
172
+                //if both delay and min duration are set,
173
+                //we don't want to initiate the min duration if the
174
+                //promise finished before the delay was complete
175
+                tracker.delayJustFinished = false;
176
+                if (tracker.promises.length === 0) {
177
+                    tracker.durationPromise = null;
178
+                }
179
+                return tracker.promises.length > 0;
180
+            }
181
+        };
182
+
183
+        return tracker;
184
+
185
+    };
186
+}]);
187
+
188
+angular.module('luticateUtils').value('luBusyDefaults',{});
189
+
190
+angular.module('luticateUtils').directive('luBusy',['$compile','$templateCache','luBusyDefaults','$http','_luBusyTrackerFactory',
191
+    function($compile,$templateCache,luBusyDefaults,$http,_luBusyTrackerFactory){
192
+        return {
193
+            restrict: 'A',
194
+            link: function(scope, element, attrs, fn) {
195
+
196
+                //Apply position:relative to parent element if necessary
197
+                var position = element.css('position');
198
+                if (position === 'static' || position === '' || typeof position === 'undefined'){
199
+                    element.css('position','relative');
200
+                }
201
+
202
+                var templateElement;
203
+                var backdropElement;
204
+                var currentTemplate;
205
+                var templateScope;
206
+                var backdrop;
207
+                var tracker = _luBusyTrackerFactory();
208
+
209
+                var defaults = {
210
+                    templateUrl: 'luticate-busy.html',
211
+                    delay:0,
212
+                    minDuration:0,
213
+                    backdrop: true,
214
+                    message:'Please Wait...',
215
+                    wrapperClass: 'lu-busy lu-busy-animation'
216
+                };
217
+
218
+                angular.extend(defaults,luBusyDefaults);
219
+
220
+                scope.$watchCollection(attrs.luBusy,function(options){
221
+
222
+                    if (!options) {
223
+                        options = {promise:null};
224
+                    }
225
+
226
+                    if (angular.isString(options)) {
227
+                        throw new Error('Invalid value for lu-busy. luBusy no longer accepts string ids to represent promises/trackers.');
228
+                    }
229
+
230
+                    //is it an array (of promises) or one promise
231
+                    if (angular.isArray(options) || tracker.isPromise(options)) {
232
+                        options = {promise:options};
233
+                    }
234
+
235
+                    options = angular.extend(angular.copy(defaults),options);
236
+
237
+                    if (!options.templateUrl){
238
+                        options.templateUrl = defaults.templateUrl;
239
+                    }
240
+
241
+                    if (!angular.isArray(options.promise)){
242
+                        options.promise = [options.promise];
243
+                    }
244
+
245
+                    // options.promise = angular.isArray(options.promise) ? options.promise : [options.promise];
246
+                    // options.message = options.message ? options.message : 'Please Wait...';
247
+                    // options.template = options.template ? options.template : luBusyTemplateName;
248
+                    // options.minDuration = options.minDuration ? options.minDuration : 0;
249
+                    // options.delay = options.delay ? options.delay : 0;
250
+
251
+                    if (!templateScope) {
252
+                        templateScope = scope.$new();
253
+                    }
254
+
255
+                    templateScope.$message = options.message;
256
+
257
+                    if (!angular.equals(tracker.promises,options.promise)) {
258
+                        tracker.reset({
259
+                            promises:options.promise,
260
+                            delay:options.delay,
261
+                            minDuration: options.minDuration
262
+                        });
263
+                    }
264
+
265
+                    templateScope.$luBusyIsActive = function() {
266
+                        return tracker.active();
267
+                    };
268
+
269
+
270
+                    if (!templateElement || currentTemplate !== options.templateUrl || backdrop !== options.backdrop) {
271
+
272
+                        if (templateElement) {
273
+                            templateElement.remove();
274
+                        }
275
+                        if (backdropElement){
276
+                            backdropElement.remove();
277
+                        }
278
+
279
+                        currentTemplate = options.templateUrl;
280
+                        backdrop = options.backdrop;
281
+
282
+                        $http.get(currentTemplate,{cache: $templateCache}).success(function(indicatorTemplate){
283
+
284
+                            options.backdrop = typeof options.backdrop === 'undefined' ? true : options.backdrop;
285
+
286
+                            if (options.backdrop){
287
+                                var backdrop = '<div class="lu-busy lu-busy-backdrop lu-busy-backdrop-animation ng-hide" ng-show="$luBusyIsActive()"></div>';
288
+                                backdropElement = $compile(backdrop)(templateScope);
289
+                                element.append(backdropElement);
290
+                            }
291
+
292
+                            var template = '<div class="'+options.wrapperClass+' ng-hide" ng-show="$luBusyIsActive()">' + indicatorTemplate + '</div>';
293
+                            templateElement = $compile(template)(templateScope);
294
+
295
+                            angular.element(templateElement.children()[0])
296
+                                .css('position','absolute')
297
+                                .css('top',0)
298
+                                .css('left',0)
299
+                                .css('right',0)
300
+                                .css('bottom',0);
301
+                            element.append(templateElement);
302
+
303
+                        }).error(function(data){
304
+                            throw new Error('Template specified for luBusy ('+options.templateUrl+') could not be loaded. ' + data);
305
+                        });
306
+                    }
307
+
308
+                },true);
309
+            }
310
+        };
311
+    }
312
+]);
313
+
314
+angular.module('luticateUtils').run(['$templateCache', function($templateCache) {
315
+    'use strict';
316
+
317
+    $templateCache.put('luticate-busy.html',
318
+        "<div class=\"lu-busy-default-wrapper\">\n" +
319
+        "\n" +
320
+        "   <div class=\"lu-busy-default-sign\">\n" +
321
+        "\n" +
322
+        "      <div class=\"lu-busy-default-spinner\">\n" +
323
+        "         <div class=\"bar1\"></div>\n" +
324
+        "         <div class=\"bar2\"></div>\n" +
325
+        "         <div class=\"bar3\"></div>\n" +
326
+        "         <div class=\"bar4\"></div>\n" +
327
+        "         <div class=\"bar5\"></div>\n" +
328
+        "         <div class=\"bar6\"></div>\n" +
329
+        "         <div class=\"bar7\"></div>\n" +
330
+        "         <div class=\"bar8\"></div>\n" +
331
+        "         <div class=\"bar9\"></div>\n" +
332
+        "         <div class=\"bar10\"></div>\n" +
333
+        "         <div class=\"bar11\"></div>\n" +
334
+        "         <div class=\"bar12\"></div>\n" +
335
+        "      </div>\n" +
336
+        "\n" +
337
+        "      <div class=\"lu-busy-default-text\">{{$message}}</div>\n" +
338
+        "\n" +
339
+        "   </div>\n" +
340
+        "\n" +
341
+        "</div>"
342
+    );
343
+
344
+}]);*/

+ 7
- 0
src/luticateutils.js 查看文件

@@ -0,0 +1,7 @@
1
+/**
2
+ * Created by robin on 10/25/15.
3
+ */
4
+
5
+(function() {
6
+    angular.module("luticateUtils", []);
7
+})();

+ 89
- 0
src/promises.js 查看文件

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

+ 38
- 25
src/request.js 查看文件

@@ -2,36 +2,58 @@
2 2
  * Created by robin on 10/24/15.
3 3
  */
4 4
 
5
-angular.module('luticate', []);
6 5
 (function () {
7 6
     'use strict';
8
-    angular.module('luticate')
9
-        .factory('luticateUtils', ['$q', '$http', function ($q, $http) {
7
+    angular.module('luticateUtils')
8
+        .factory('luticateRequest', ['$q', '$http', 'luticatePromises', function ($q, $http, luticatePromises) {
10 9
 
11 10
             var coPlanningServices = {};
12 11
 
13
-            coPlanningServices.get = function(url, dataGet)
12
+            coPlanningServices.get = function(url, dataGet, promise)
14 13
             {
15
-                return coPlanningServices.request(url, 'GET', dataGet, null);
14
+                return coPlanningServices.request(url, 'GET', dataGet, null, promise);
16 15
             };
17 16
 
18
-            coPlanningServices.post = function(url, dataPost, dataGet)
17
+            coPlanningServices.post = function(url, dataPost, dataGet, promise)
19 18
             {
20
-                return coPlanningServices.request(url, 'POST', dataGet, dataPost);
19
+                return coPlanningServices.request(url, 'POST', dataGet, dataPost, promise);
21 20
             };
22 21
 
23
-            coPlanningServices.put = function(url, dataPost, dataGet)
22
+            coPlanningServices.put = function(url, dataPost, dataGet, promise)
24 23
             {
25
-                return coPlanningServices.request(url, 'PUT', dataGet, dataPost);
24
+                return coPlanningServices.request(url, 'PUT', dataGet, dataPost, promise);
26 25
             };
27 26
 
28
-            coPlanningServices.delete = function(url, dataPost, dataGet)
27
+            coPlanningServices.delete = function(url, dataPost, dataGet, promise)
29 28
             {
30
-                return coPlanningServices.request(url, 'DELETE', dataGet, dataPost);
29
+                return coPlanningServices.request(url, 'DELETE', dataGet, dataPost, promise);
31 30
             };
32 31
 
33
-            coPlanningServices.request = function(url, method, dataGet, dataPost)
32
+            function reject(defered, data, status, promise)
34 33
             {
34
+                var obj = {
35
+                    data: data,
36
+                    status: status
37
+                };
38
+                defered.reject(obj);
39
+                if (promise != null) {
40
+                    luticatePromises.reject(promise, obj);
41
+                }
42
+            }
43
+
44
+            function resolve(defered, data, promise)
45
+            {
46
+                defered.resolve(data);
47
+                if (promise != null) {
48
+                    luticatePromises.resolve(promise, data);
49
+                }
50
+            }
51
+
52
+            coPlanningServices.request = function(url, method, dataGet, dataPost, promise)
53
+            {
54
+                if (promise != null) {
55
+                    luticatePromises.initPromise(promise);
56
+                }
35 57
                 var defered = $q.defer();
36 58
 
37 59
                 var params = {
@@ -44,27 +66,18 @@ angular.module('luticate', []);
44 66
                 $http(params)
45 67
                     .success(function (result, status) {
46 68
                         if (result.Message != null) {
47
-                            defered.reject({
48
-                                data: result.Message,
49
-                                status: status
50
-                            });
69
+                            reject(defered, result.Message, status, promise);
51 70
                         }
52 71
                         else {
53
-                            defered.resolve(result.Data);
72
+                            resolve(defered, result.Data, promise);
54 73
                         }
55 74
                     })
56 75
                     .error(function (result, status) {
57 76
                         if (result.Message != null) {
58
-                            defered.reject({
59
-                                data: result.Message,
60
-                                status: status
61
-                            });
77
+                            reject(defered, result.Message, status, promise);
62 78
                         }
63 79
                         else {
64
-                            defered.reject({
65
-                                data: result,
66
-                                status: status
67
-                            });
80
+                            reject(defered, result, status, promise);
68 81
                         }
69 82
                     });
70 83
                 return defered.promise;

Loading…
取消
儲存