|
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 | | - removeFirst: function () { |
87 | | - var firstItem = this.data.find(i => i.index === this.firstIndex); |
88 | | - if(!firstItem) { |
89 | | - return $q.reject(); |
90 | | - } |
91 | | - return this.removeItemById(firstItem.id); |
92 | | - }, |
93 | | - |
94 | | - removeLast: function () { |
95 | | - var lastItem = this.data.find(i => i.index === this.lastIndex); |
96 | | - if(!lastItem) { |
97 | | - return $q.reject(); |
98 | | - } |
99 | | - return this.removeItemById(lastItem.id); |
100 | | - }, |
101 | | - |
102 | | - removeItemById: function (itemId) { |
103 | | - var length = this.data.length; |
104 | | - for (var i = 0; i < length; i++) { |
105 | | - if (this.data[i].id === itemId) { |
106 | | - var indexRemoved = this.data[i].index; |
107 | | - if(indexRemoved > this.firstIndex) { |
108 | | - for (var j = i; j < length; j++) { |
109 | | - this.data[j].index--; |
110 | | - } |
111 | | - } |
112 | | - this.data.splice(i, 1); |
113 | | - this.setIndicies(); |
114 | | - return this.returnDeferredResult(indexRemoved); |
115 | | - } |
116 | | - } |
117 | | - return this.returnDeferredResult(false); |
118 | | - }, |
119 | | - |
120 | | - setIndicies: function() { |
121 | | - if(!this.data.length) { |
122 | | - this.firstIndex = 1; |
123 | | - this.lastIndex = 1; |
124 | | - return; |
125 | | - } |
126 | | - this.firstIndex = this.data[0].index; |
127 | | - this.lastIndex = this.data[0].index; |
128 | | - for (var i = this.data.length - 1; i >= 0; i--) { |
129 | | - if(this.data[i].index > this.lastIndex) { |
130 | | - this.lastIndex = this.data[i].index; |
131 | | - } |
132 | | - if(this.data[i].index < this.firstIndex) { |
133 | | - this.firstIndex = this.data[i].index; |
134 | | - } |
135 | | - } |
136 | | - } |
137 | | - }; |
138 | | - |
139 | | - ServerFactory.init(); |
140 | | - |
141 | | - return ServerFactory; |
142 | | - |
143 | | - } |
144 | | -]); |
145 | | - |
| 1 | +var app = angular.module('application', ['ui.scroll', 'server']); |
146 | 2 |
|
147 | 3 | app.controller('mainController', [ |
148 | 4 | '$scope', 'Server', function ($scope, Server) { |
@@ -218,5 +74,46 @@ app.controller('mainController', [ |
218 | 74 | } |
219 | 75 | }); |
220 | 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; |
221 | 118 | } |
222 | 119 | ]); |
0 commit comments