Skip to content

Commit 79594a8

Browse files
committed
Refactor Buffer constructor
1 parent 66ff0fb commit 79594a8

File tree

2 files changed

+83
-90
lines changed

2 files changed

+83
-90
lines changed

src/.jshintrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2+
"esversion": 6,
3+
"expr": true,
24
"strict": false
35
}

src/ui-scroll.js

Lines changed: 81 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ angular.module('ui.scroll', [])
3333
function (console, $injector, $rootScope, $timeout, $q, $parse) {
3434
const $animate = ($injector.has && $injector.has('$animate')) ? $injector.get('$animate') : null;
3535
const isAngularVersionLessThen1_3 = angular.version.major === 1 && angular.version.minor < 3;
36-
const log = console.debug || console.log;
36+
//const log = console.debug || console.log;
3737

3838
return {
3939
require: ['?^uiScrollViewport'],
@@ -91,117 +91,108 @@ angular.module('ui.scroll', [])
9191
}
9292

9393
function Buffer(itemName, $scope, linker, bufferSize) {
94-
var buffer, reset;
94+
const buffer = Object.create(Array.prototype);
9595

96-
buffer = Object.create(Array.prototype);
97-
98-
reset = function (origin) {
96+
function reset(origin) {
9997
buffer.eof = false;
10098
buffer.bof = false;
10199
buffer.first = origin;
102100
buffer.next = origin;
103101
buffer.minIndex = Number.MAX_VALUE;
104102
return buffer.maxIndex = Number.MIN_VALUE;
105-
};
103+
}
106104

107-
buffer.size = bufferSize;
105+
angular.extend(buffer, {
106+
size: bufferSize,
108107

109-
buffer.append = function (items) {
110-
var item, j, len, results;
111-
results = [];
112-
for (j = 0, len = items.length; j < len; j++) {
113-
item = items[j];
114-
++buffer.next;
115-
results.push(buffer.insert('append', item));
116-
}
117-
return results;
118-
};
108+
append(items) {
109+
items.forEach((item) => {
110+
++buffer.next;
111+
buffer.insert('append', item);
112+
});
113+
},
119114

120-
buffer.prepend = function (items) {
121-
var item, j, len, ref, results;
122-
ref = items.reverse();
123-
results = [];
124-
for (j = 0, len = ref.length; j < len; j++) {
125-
item = ref[j];
126-
--buffer.first;
127-
results.push(buffer.insert('prepend', item));
128-
}
129-
return results;
130-
};
115+
prepend(items) {
116+
items.reverse().forEach((item) => {
117+
--buffer.first;
118+
buffer.insert('prepend', item);
119+
});
120+
},
131121

132-
/**
133-
* inserts wrapped element in the buffer
134-
* the first argument is either operation keyword (see below) or a number for operation 'insert'
135-
* for insert the number is the index for the buffer element the new one have to be inserted after
136-
* operations: 'append', 'prepend', 'insert', 'remove', 'update', 'none'
137-
*/
138-
buffer.insert = function (operation, item) {
139-
var itemScope, wrapper;
140-
itemScope = $scope.$new();
141-
itemScope[itemName] = item;
142-
wrapper = {
143-
scope: itemScope,
144-
item: item
145-
};
146-
linker(itemScope, function (clone) {
147-
return wrapper.element = clone;
148-
});
149-
if (operation % 1 === 0) {// it is an insert
150-
wrapper.op = 'insert';
151-
return buffer.splice(operation, 0, wrapper);
152-
} else {
153-
wrapper.op = operation;
154-
switch (operation) {
155-
case 'append':
156-
return buffer.push(wrapper);
157-
case 'prepend':
158-
return buffer.unshift(wrapper);
159-
}
160-
}
161-
};
122+
/**
123+
* inserts wrapped element in the buffer
124+
* the first argument is either operation keyword (see below) or a number for operation 'insert'
125+
* for insert the number is the index for the buffer element the new one have to be inserted after
126+
* operations: 'append', 'prepend', 'insert', 'remove', 'update', 'none'
127+
*/
128+
insert(operation, item) {
129+
const itemScope = $scope.$new();
130+
const wrapper = {
131+
item,
132+
scope: itemScope
133+
};
134+
135+
itemScope[itemName] = item;
162136

163-
// removes elements from buffer
164-
buffer.remove = function (arg1, arg2) {
165-
var i, j, ref, ref1;
166-
if (angular.isNumber(arg1)) {
167-
// removes items from arg1 (including) through arg2 (excluding)
168-
for (i = j = ref = arg1, ref1 = arg2; ref <= ref1 ? j < ref1 : j > ref1; i = ref <= ref1 ? ++j : --j) {
169-
removeElement(buffer[i]);
137+
linker(itemScope, (clone) => wrapper.element = clone);
138+
139+
if (operation % 1 === 0) {// it is an insert
140+
wrapper.op = 'insert';
141+
buffer.splice(operation, 0, wrapper);
142+
} else {
143+
wrapper.op = operation;
144+
switch (operation) {
145+
case 'append':
146+
buffer.push(wrapper);
147+
break;
148+
case 'prepend':
149+
buffer.unshift(wrapper);
150+
break;
151+
}
152+
}
153+
},
154+
155+
// removes elements from buffer
156+
remove(arg1, arg2) {
157+
if (angular.isNumber(arg1)) {
158+
// removes items from arg1 (including) through arg2 (excluding)
159+
for (var i = arg1; i < arg2; i++) {
160+
removeElement(buffer[i]);
161+
}
162+
return buffer.splice(arg1, arg2 - arg1);
170163
}
171-
return buffer.splice(arg1, arg2 - arg1);
172-
} else {
173164
// removes single item(wrapper) from the buffer
174165
buffer.splice(buffer.indexOf(arg1), 1);
166+
175167
return removeElementAnimated(arg1);
176-
}
177-
};
168+
},
178169

179-
buffer.setUpper = function () {
180-
return buffer.maxIndex = buffer.eof ? buffer.next - 1 : Math.max(buffer.next - 1, buffer.maxIndex);
181-
};
170+
setUpper() {
171+
buffer.maxIndex = buffer.eof ? buffer.next - 1 : Math.max(buffer.next - 1, buffer.maxIndex);
172+
},
182173

183-
buffer.setLower = function () {
184-
return buffer.minIndex = buffer.bof ? buffer.minIndex = buffer.first : Math.min(buffer.first, buffer.minIndex);
185-
};
174+
setLower() {
175+
buffer.minIndex = buffer.bof ? buffer.minIndex = buffer.first : Math.min(buffer.first, buffer.minIndex);
176+
},
186177

187-
buffer.syncDatasource = function (datasource) {
188-
var offset;
189-
offset = buffer.minIndex - (Math.min(buffer.minIndex, datasource.minIndex || Number.MAX_VALUE));
190-
datasource.minIndex = (buffer.minIndex -= offset);
191-
datasource.maxIndex = buffer.maxIndex = Math.max(buffer.maxIndex, datasource.maxIndex || Number.MIN_VALUE);
192-
return offset;
193-
};
178+
syncDatasource(datasource) {
179+
const offset = buffer.minIndex - (Math.min(buffer.minIndex, datasource.minIndex || Number.MAX_VALUE));
194180

195-
// clears the buffer
196-
buffer.clear = function () {
197-
buffer.remove(0, buffer.length);
198-
if (arguments.length) {
199-
return reset(arguments[0]);
200-
} else {
201-
return reset(1);
181+
datasource.minIndex = (buffer.minIndex -= offset);
182+
datasource.maxIndex = buffer.maxIndex = Math.max(buffer.maxIndex, datasource.maxIndex || Number.MIN_VALUE);
183+
184+
return offset;
185+
},
186+
187+
// clears the buffer
188+
clear() {
189+
buffer.remove(0, buffer.length);
190+
arguments.length ? reset(arguments[0]) : reset(1);
202191
}
203-
};
192+
});
193+
204194
reset(1);
195+
205196
return buffer;
206197
}
207198

0 commit comments

Comments
 (0)