|
| 1 | +var app = angular.module('application', ['ui.scroll', 'ui.scroll.jqlite']); |
| 2 | + |
| 3 | +app.factory('Server', [ |
| 4 | + '$timeout', '$q', function ($timeout, $q) { |
| 5 | + |
| 6 | + var ServerFactory = { |
| 7 | + |
| 8 | + max: 50, |
| 9 | + |
| 10 | + first: 1, |
| 11 | + |
| 12 | + delay: 100, |
| 13 | + |
| 14 | + data: [], |
| 15 | + |
| 16 | + prependedData: [], |
| 17 | + |
| 18 | + appendedData: [], |
| 19 | + |
| 20 | + generateItem: function (number) { |
| 21 | + return { |
| 22 | + number: number, |
| 23 | + content: 'Item #' + number |
| 24 | + } |
| 25 | + }, |
| 26 | + |
| 27 | + init: function () { |
| 28 | + for (var i = this.first - 1; i <= this.max; i++) { |
| 29 | + this.data.push(this.generateItem(i)); |
| 30 | + } |
| 31 | + }, |
| 32 | + |
| 33 | + getItem: function (index) { |
| 34 | + if (index < this.first) { |
| 35 | + return this.prependedData[(-1) * index]; |
| 36 | + } |
| 37 | + else if (index > this.max) { |
| 38 | + return this.appendedData[index - this.max - 1]; |
| 39 | + } |
| 40 | + else { |
| 41 | + return this.data[index]; |
| 42 | + } |
| 43 | + }, |
| 44 | + |
| 45 | + request: function (index, count) { |
| 46 | + var self = this; |
| 47 | + var deferred = $q.defer(); |
| 48 | + |
| 49 | + var start = index; |
| 50 | + var end = index + count - 1; |
| 51 | + |
| 52 | + $timeout(function () { |
| 53 | + var item, result = { |
| 54 | + items: [], |
| 55 | + bof: false, |
| 56 | + eof: false |
| 57 | + }; |
| 58 | + if (start <= end) { |
| 59 | + for (var i = start; i <= end; i++) { |
| 60 | + if (item = self.getItem(i)) { |
| 61 | + result.items.push(item); |
| 62 | + } |
| 63 | + else { // if no item then begin or end of file is reached |
| 64 | + if (start <= self.first - self.prependedData.length) { |
| 65 | + result.bof = true; |
| 66 | + } |
| 67 | + if (end > self.max + self.appendedData.length) { |
| 68 | + result.eof = true; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + deferred.resolve(result); |
| 74 | + }, self.delay); |
| 75 | + |
| 76 | + return deferred.promise; |
| 77 | + }, |
| 78 | + |
| 79 | + prependItem: function (params) { |
| 80 | + var prependedDataIndex = this.first - this.prependedData.length - 1; |
| 81 | + var newItem = this.generateItem(prependedDataIndex); |
| 82 | + newItem.content += params; |
| 83 | + this.prependedData.push(newItem); |
| 84 | + return newItem; |
| 85 | + }, |
| 86 | + |
| 87 | + appendItem: function (params) { |
| 88 | + var appendedDataIndex = this.max + this.appendedData.length + 1; |
| 89 | + var newItem = this.generateItem(appendedDataIndex); |
| 90 | + newItem.content += params; |
| 91 | + this.appendedData.push(newItem); |
| 92 | + return newItem; |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + ServerFactory.init(); |
| 97 | + |
| 98 | + return ServerFactory; |
| 99 | + |
| 100 | + } |
| 101 | +]); |
| 102 | + |
| 103 | + |
| 104 | +app.controller('mainController', [ |
| 105 | + '$scope', 'Server', function ($scope, Server) { |
| 106 | + |
| 107 | + var bof = false, eof = false; |
| 108 | + |
| 109 | + function mySuccess(result, success) { |
| 110 | + bof = eof = false; |
| 111 | + if (result.bof) { |
| 112 | + bof = true; |
| 113 | + console.log('begin of file is reached'); |
| 114 | + } |
| 115 | + if (result.eof) { |
| 116 | + eof = true; |
| 117 | + console.log('end of file is reached'); |
| 118 | + } |
| 119 | + if (result.items.length) { |
| 120 | + console.log('resolved ' + result.items.length + ' items'); |
| 121 | + } |
| 122 | + success(result.items); |
| 123 | + } |
| 124 | + |
| 125 | + $scope.datasource = { |
| 126 | + get: function (index, count, success) { |
| 127 | + console.log('request by index = ' + index + ', count = ' + count); |
| 128 | + Server.request(index, count).then(function (result) { |
| 129 | + mySuccess(result, success); |
| 130 | + }); |
| 131 | + } |
| 132 | + }; |
| 133 | + |
| 134 | + $scope.prepend = function () { |
| 135 | + var newItem = Server.prependItem(' (new)*'); |
| 136 | + if (bof) { |
| 137 | + $scope.adapter.prepend([newItem]); |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + $scope.append = function () { |
| 142 | + var newItem = Server.appendItem(' (new)*'); |
| 143 | + if (eof) { |
| 144 | + $scope.adapter.append([newItem]); |
| 145 | + } |
| 146 | + }; |
| 147 | + |
| 148 | + } |
| 149 | +]); |
0 commit comments