-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathangular-collection.js
More file actions
231 lines (189 loc) · 6.09 KB
/
angular-collection.js
File metadata and controls
231 lines (189 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* Angular Collection - The Collection module for AngularJS
* @version v0.5.3 - 2018-06-18
* @author Tomasz Kuklis
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
(function (window, angular) {
'use strict'
angular.module('ngCollection', [])
.factory('$collection', ['$filter', '$parse', function ($filter, $parse) {
function s4 () {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16).substring(1)
}
function guid () {
return s4() + s4() + '-' + s4() + '-' + s4() +
'-' + s4() + '-' + s4() + s4() + s4()
}
function checkValue (item, compareFn) {
return compareFn(item)
}
function Collection (options) {
options || (options = {})
if (options.comparator !== void 0) this.comparator = options.comparator
this.idAttribute = options.idAttribute || this.idAttribute
this.current = null
this._reset()
this.initialize.apply(this, arguments)
}
Collection.prototype = {
idAttribute: 'id',
initialize: function () { },
add: function (obj, options) {
options || (options = {})
var id, sort, existing, index
sort = options.sort !== false
if (options.index !== void 0) {
index = options.index
} else {
index = this.array.length
}
if (!obj[this.idAttribute]) {
obj[this.idAttribute] = guid()
}
existing = this.get(obj)
if (existing) {
angular.extend(existing, obj)
} else {
id = obj[this.idAttribute]
this.hash[id] = obj
this.array.splice(index, 0, obj)
this.length += 1
}
if (sort) this.sort()
return this
},
addAll: function (objArr, options) {
options || (options = {})
var sort = options.sort !== false
for (var i = 0; i < objArr.length; i++) {
var obj = objArr[i]
this.add(obj, angular.extend(options, { sort: false }))
}
if (sort) this.sort()
return this
},
sort: function (comparator) {
comparator = comparator || this.comparator
if (comparator) {
this.array = $filter('orderBy')(this.array, comparator)
}
return this
},
get: function (obj) {
if (obj == null) return void 0
return this.hash[obj[this.idAttribute] || obj]
},
find: function (expr, value, deepCompare) {
var compareFn = expr
if (typeof expr === 'string') {
var parse = $parse(expr)
compareFn = function (item) {
if (deepCompare) {
return parse(item) == value // eslint-disable-line eqeqeq
} else {
return parse(item) === value
}
}
compareFn.prototype.value = value
compareFn.prototype.deepCompare = deepCompare
}
// loop over all the items in the array
for (var i = 0; i < this.array.length; i++) {
if (checkValue(this.array[i], compareFn)) {
return this.array[i]
}
}
// if nothing matches return void
return void 0
},
where: function (expr, value, deepCompare) {
var results = []
var compareFn = expr
if (typeof expr === 'string') {
var parse = $parse(expr)
compareFn = function (item) {
if (deepCompare) {
return parse(item) === value
} else {
return parse(item) === value
}
}
compareFn.prototype.value = value
compareFn.prototype.deepCompare = deepCompare
}
// loop over all the items in the array
for (var i = 0; i < this.array.length; i++) {
if (checkValue(this.array[i], compareFn)) {
results.push(this.array[i])
}
}
// if nothing matches return void
return results
},
update: function (obj) {
var existing
existing = this.get(obj)
if (existing) angular.extend(existing, obj)
if (!existing) this.add(obj)
return this
},
remove: function (obj) {
var index
index = this.array.indexOf(obj)
if (index === -1) {
return this
}
delete this.hash[obj[this.idAttribute]]
this.array.splice(index, 1)
this.length--
return this
},
removeAll: function () {
for (var i = this.array.length - 1; i >= 0; i--) {
this.remove(this.at(i))
}
return this
},
last: function () {
return this.array[this.length - 1]
},
at: function (index) {
return this.array[index]
},
size: function () {
return this.array.length
},
all: function () {
return this.array
},
_reset: function () {
this.length = 0
this.hash = {}
this.array = []
}
}
Collection.extend = function (protoProps) {
var parent = this
var child
if (protoProps && protoProps.hasOwnProperty('constructor')) {
child = protoProps.constructor
} else {
child = function () { return parent.apply(this, arguments) }
}
var Surrogate = function () { this.constructor = child }
Surrogate.prototype = parent.prototype
child.prototype = new Surrogate()
if (protoProps) angular.extend(child.prototype, protoProps)
child.extend = parent.extend
child.getInstance = Collection.getInstance
child._super = parent.prototype
return child
}
Collection.getInstance = function (options) {
return new this(options)
}
return Collection
}])
})(window, window.angular)