-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrafity.List.js
executable file
·95 lines (79 loc) · 2.21 KB
/
crafity.List.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
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
/*jslint bitwise: true, unparam: true, maxerr: 50, white: true */
/*globals window */
/*!
* crafity.List
* Copyright(c) 2011 Crafity
* Copyright(c) 2011 Bart Riemens
* Copyright(c) 2011 Galina Slavova
* MIT Licensed
*/
(function (crafity) {
"use strict";
if (!crafity.common) {
throw new Error("Missing dependency 'crafity.common'");
}
if (!crafity.Event) {
throw new Error("Missing dependency 'crafity.Event'");
}
if (!crafity.arrays) {
throw new Error("Missing dependency 'crafity.arrays'");
}
var common = crafity.common
, Event = crafity.Event
, arrays = crafity.arrays;
crafity.List = function List(items) {
common.arg({ name: 'items', value: items, type:Array, required: false });
var self = this
, innerList = [];
this.onItemAdded = new Event('sync');
this.onItemChanged = new Event('sync');
this.onItemRemoved = new Event('sync');
this.any = false;
this.get = function (index) {
common.arg({ name: 'index', value: index, type: Number, required: true });
return innerList.slice();
};
this.toArray = function () {
return innerList.slice();
};
this.clear = function () {
innerList.forEach(function (item) {
self.remove(item);
});
return self;
};
this.remove = function (item) {
common.arg({ name: 'item', value: item, required: true });
var index = innerList.indexOf(item);
if (~index) {
arrays.remove(innerList, item);
self.onItemRemoved.raise(item, index);
}
return self;
};
this.set = function (index, item) {
common.arg({ name: 'index', value: index, type: Number, required: true });
common.arg({ name: 'item', value: item, required: true });
innerList[index] = item;
self.onItemChanged.raise(item, index);
return self;
};
this.add = function (item) {
common.arg({ name: 'item', value: item, required: true });
var index = innerList.push(item);
self.onItemAdded.raise(item, index);
return self;
};
this.addMany = function (items) {
common.arg({ name: 'items', value: items, type:Array, required: true });
self.clear();
items.forEach(function (item) {
self.add(item);
});
return self;
};
if (items) {
self.addMany(items);
}
};
}(window.crafity = window.crafity || {}));