1
- var app = angular . module ( 'application' , [ 'ui.scroll' ] ) ;
2
-
3
- app . factory ( 'Server' , [
4
- '$timeout' , '$q' , function ( $timeout , $q ) {
5
-
6
- var ServerFactory = {
7
-
8
- firstIndex : 1 ,
9
-
10
- lastIndex : 40 ,
11
-
12
- delay : 100 ,
13
-
14
- data : [ ] ,
15
-
16
- absIndex : 1 ,
17
-
18
- generateId : function ( ) {
19
- var d = '-' ;
20
- function S4 ( ) {
21
- return ( ( ( 1 + Math . random ( ) ) * 0x10000 ) | 0 ) . toString ( 16 ) . substring ( 1 ) ;
22
- }
23
- return ( S4 ( ) + S4 ( ) + d + S4 ( ) + d + S4 ( ) + d + S4 ( ) + d + S4 ( ) + S4 ( ) + S4 ( ) ) ;
24
- } ,
25
-
26
- generateItem : function ( index ) {
27
- return {
28
- index : index ,
29
- id : this . generateId ( ) ,
30
- content : 'Item #' + this . absIndex ++
31
- }
32
- } ,
33
-
34
- init : function ( ) {
35
- for ( var i = this . firstIndex ; i <= this . lastIndex ; i ++ ) {
36
- this . data . push ( this . generateItem ( i ) ) ;
37
- }
38
- } ,
39
-
40
- getItem : function ( index ) {
41
- for ( var i = this . data . length - 1 ; i >= 0 ; i -- ) {
42
- if ( this . data [ i ] . index === index ) {
43
- return this . data [ i ] ;
44
- }
45
- }
46
- } ,
47
-
48
- returnDeferredResult : function ( result ) {
49
- var deferred = $q . defer ( ) ;
50
- $timeout ( function ( ) {
51
- deferred . resolve ( result ) ;
52
- } , this . delay ) ;
53
- return deferred . promise ;
54
- } ,
55
-
56
- request : function ( index , count ) {
57
- var start = index ;
58
- var end = index + count - 1 ;
59
- var item , result = {
60
- items : [ ]
61
- } ;
62
- if ( start <= end ) {
63
- for ( var i = start ; i <= end ; i ++ ) {
64
- if ( item = this . getItem ( i ) ) {
65
- result . items . push ( item ) ;
66
- }
67
- }
68
- }
69
- return this . returnDeferredResult ( result ) ;
70
- } ,
71
-
72
- prependItem : function ( params ) {
73
- var newItem = this . generateItem ( -- this . firstIndex ) ;
74
- newItem . content += params ;
75
- this . data . unshift ( newItem ) ;
76
- return this . returnDeferredResult ( newItem ) ;
77
- } ,
78
-
79
- appendItem : function ( params ) {
80
- var newItem = this . generateItem ( ++ this . lastIndex ) ;
81
- newItem . content += params ;
82
- this . data . push ( newItem ) ;
83
- return this . returnDeferredResult ( newItem ) ;
84
- } ,
85
-
86
- removeItemById : function ( itemId ) {
87
- var length = this . data . length ;
88
- for ( var i = 0 ; i < length ; i ++ ) {
89
- if ( this . data [ i ] . id === itemId ) {
90
- this . data . splice ( i , 1 ) ;
91
- this . setIndicies ( ) ;
92
- return this . returnDeferredResult ( true ) ;
93
- }
94
- }
95
- return this . returnDeferredResult ( false ) ;
96
- } ,
97
-
98
- setIndicies : function ( ) {
99
- if ( ! this . data . length ) {
100
- this . firstIndex = 1 ;
101
- this . lastIndex = 1 ;
102
- return ;
103
- }
104
- this . firstIndex = this . data [ 0 ] . index ;
105
- this . lastIndex = this . data [ 0 ] . index ;
106
- for ( var i = this . data . length - 1 ; i >= 0 ; i -- ) {
107
- if ( this . data [ i ] . index > this . lastIndex ) {
108
- this . lastIndex = this . data [ i ] . index ;
109
- }
110
- if ( this . data [ i ] . index < this . firstIndex ) {
111
- this . firstIndex = this . data [ i ] . index ;
112
- }
113
- }
114
- }
115
- } ;
116
-
117
- ServerFactory . init ( ) ;
118
-
119
- return ServerFactory ;
120
-
121
- }
122
- ] ) ;
123
-
1
+ var app = angular . module ( 'application' , [ 'ui.scroll' , 'server' ] ) ;
124
2
125
3
app . controller ( 'mainController' , [
126
4
'$scope' , 'Server' , function ( $scope , Server ) {
@@ -171,7 +49,7 @@ app.controller('mainController', [
171
49
172
50
ctrl . remove = function ( itemRemove ) {
173
51
Server . removeItemById ( itemRemove . id ) . then ( function ( result ) {
174
- if ( result ) {
52
+ if ( result !== false ) {
175
53
ctrl . adapter . applyUpdates ( function ( item ) {
176
54
if ( item . id === itemRemove . id ) {
177
55
return [ ] ;
@@ -181,5 +59,61 @@ app.controller('mainController', [
181
59
} ) ;
182
60
} ;
183
61
62
+ ctrl . removeFirst = function ( ) {
63
+ Server . removeFirst ( ) . then ( function ( indexRemoved ) {
64
+ if ( indexRemoved !== false ) {
65
+ ctrl . adapter . applyUpdates ( indexRemoved , [ ] ) ;
66
+ }
67
+ } ) ;
68
+ } ;
69
+
70
+ ctrl . removeLast = function ( ) {
71
+ Server . removeLast ( ) . then ( function ( indexRemoved ) {
72
+ if ( indexRemoved !== false ) {
73
+ ctrl . adapter . applyUpdates ( indexRemoved , [ ] ) ;
74
+ }
75
+ } ) ;
76
+ } ;
77
+
78
+ ctrl . insertSome = function ( indexToInsert ) {
79
+ indexToInsert = parseInt ( indexToInsert , 10 ) ;
80
+ var promises = [
81
+ Server . insertAfterIndex ( indexToInsert , ' *** (1)' ) ,
82
+ Server . insertAfterIndex ( indexToInsert + 1 , ' *** (2)' ) ,
83
+ Server . insertAfterIndex ( indexToInsert + 2 , ' *** (3)' )
84
+ ] ;
85
+ Promise . all ( promises ) . then ( function ( result ) {
86
+ if ( result && result . length ) {
87
+ // need to protect from null
88
+ var _result = [ ] ;
89
+ for ( var i = 0 ; i < result . length ; i ++ ) {
90
+ if ( result [ i ] ) {
91
+ _result . push ( result [ i ] ) ;
92
+ }
93
+ }
94
+ if ( _result . length ) {
95
+ var item = getItemByIndex ( indexToInsert ) ;
96
+ if ( item ) {
97
+ _result . unshift ( item ) ;
98
+ }
99
+ ctrl . adapter . applyUpdates ( indexToInsert , _result ) ;
100
+ }
101
+ }
102
+ } ) ;
103
+ } ;
104
+
105
+ function getItemByIndex ( index ) {
106
+ var foundItem = null ;
107
+ // use Adapter.applyUpdates to get indexed item from Buffer
108
+ ctrl . adapter . applyUpdates ( function ( item ) {
109
+ if ( item . index === index ) {
110
+ foundItem = item ;
111
+ }
112
+ } ) ;
113
+ return foundItem ;
114
+ }
115
+
116
+ ctrl . datasource . minIndex = Server . firstIndex ;
117
+ ctrl . datasource . maxIndex = Server . lastIndex ;
184
118
}
185
119
] ) ;
0 commit comments