-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathCollectionModel.js
50 lines (39 loc) · 1.21 KB
/
CollectionModel.js
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
import { documentHelpers } from 'substance'
import ValueModel from './ValueModel'
import { isCollectionEmpty } from './modelHelpers'
export default class CollectionModel extends ValueModel {
constructor (api, path, targetTypes) {
super(api, path)
this._targetTypes = targetTypes
}
get type () { return 'collection' }
get isCollection () {
return true
}
getItems () {
const doc = this._api.getDocument()
return documentHelpers.getNodesForIds(doc, this.getValue())
}
addItem (item) {
// TODO: instead of requiring a bunch of low-level API
// methods we should instead introduce a Collection API
// where these low-level things are implemented
// TODO: things brings me then to the point, questioning
// the benefit of a general CollectionModel. Probably this
// should be moved into Article API land.
this._api._appendChild(this._path, item)
}
removeItem (item) {
this._api._removeChild(this._path, item.id)
}
get length () { return this.getValue().length }
getValue () {
return super.getValue() || []
}
isEmpty () {
return isCollectionEmpty(this._api, this._path)
}
hasTargetType (type) {
return this._targetTypes.has(type)
}
}