|
| 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 | + |
| 124 | + |
| 125 | +app.controller('mainController', [ |
| 126 | + '$scope', 'Server', function ($scope, Server) { |
| 127 | + |
| 128 | + var ctrl = this; |
| 129 | + |
| 130 | + ctrl.datasource = { |
| 131 | + get: function (index, count, success) { |
| 132 | + console.log('request by index = ' + index + ', count = ' + count); |
| 133 | + Server.request(index, count).then(function (result) { |
| 134 | + if (result.items) { |
| 135 | + console.log('resolved ' + result.items.length + ' items'); |
| 136 | + } |
| 137 | + success(result.items); |
| 138 | + }); |
| 139 | + } |
| 140 | + }; |
| 141 | + |
| 142 | + $scope.$watch('adapter', (prev, next) => { |
| 143 | + console.log('The adapter has been initialized'); |
| 144 | + }); |
| 145 | + |
| 146 | + ctrl.prepend = function () { |
| 147 | + Server.prependItem(' ***').then(function (newItem) { |
| 148 | + if (ctrl.adapter.isBOF()) { |
| 149 | + ctrl.adapter.prepend([newItem]); |
| 150 | + } |
| 151 | + }); |
| 152 | + }; |
| 153 | + |
| 154 | + ctrl.append = function () { |
| 155 | + Server.appendItem(' ***').then(function (newItem) { |
| 156 | + if (ctrl.adapter.isEOF()) { |
| 157 | + ctrl.adapter.append([newItem]); |
| 158 | + } |
| 159 | + }); |
| 160 | + }; |
| 161 | + |
| 162 | + // todo dhilt : need to implement it properly |
| 163 | + ctrl.removeAll = function () { |
| 164 | + ctrl.adapter.applyUpdates(function (item) { |
| 165 | + if (item.id) { |
| 166 | + Server.removeItemById(item.id); |
| 167 | + return []; |
| 168 | + } |
| 169 | + }); |
| 170 | + }; |
| 171 | + |
| 172 | + ctrl.remove = function (itemRemove) { |
| 173 | + Server.removeItemById(itemRemove.id).then(function (result) { |
| 174 | + if (result) { |
| 175 | + ctrl.adapter.applyUpdates(function (item) { |
| 176 | + if (item.id === itemRemove.id) { |
| 177 | + return []; |
| 178 | + } |
| 179 | + }); |
| 180 | + } |
| 181 | + }); |
| 182 | + }; |
| 183 | + |
| 184 | + ctrl.removeLast = function () { |
| 185 | + const lastIndex = Server.lastIndex; |
| 186 | + const lastItem = Server.data.find(i => i.index === Server.lastIndex); |
| 187 | + if(!lastItem) { |
| 188 | + retirn; |
| 189 | + } |
| 190 | + Server.removeItemById(lastItem.id).then(function (result) { |
| 191 | + if (result) { |
| 192 | + ctrl.adapter.applyUpdates(lastIndex, []); |
| 193 | + } |
| 194 | + }); |
| 195 | + }; |
| 196 | + |
| 197 | + } |
| 198 | +]); |
0 commit comments