Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
11
dojo.provide("dojo.collections.SortedList");
12
dojo.require("dojo.collections.Collections");
13
dojo.collections.SortedList = function (dictionary) {
14
	var _this = this;
15
	var items = {};
16
	var q = [];
17
	var sorter = function (a, b) {
18
		if (a.key > b.key) {
19
			return 1;
20
		}
21
		if (a.key < b.key) {
22
			return -1;
23
		}
24
		return 0;
25
	};
26
	var build = function () {
27
		q = [];
28
		var e = _this.getIterator();
29
		while (!e.atEnd()) {
30
			q.push(e.get());
31
		}
32
		q.sort(sorter);
33
	};
34
	var testObject = {};
35
	this.count = q.length;
36
	this.add = function (k, v) {
37
		if (!items[k]) {
38
			items[k] = new dojo.collections.DictionaryEntry(k, v);
39
			this.count = q.push(items[k]);
40
			q.sort(sorter);
41
		}
42
	};
43
	this.clear = function () {
44
		items = {};
45
		q = [];
46
		this.count = q.length;
47
	};
48
	this.clone = function () {
49
		return new dojo.collections.SortedList(this);
50
	};
51
	this.contains = this.containsKey = function (k) {
52
		if (testObject[k]) {
53
			return false;
54
		}
55
		return (items[k] != null);
56
	};
57
	this.containsValue = function (o) {
58
		var e = this.getIterator();
59
		while (!e.atEnd()) {
60
			var item = e.get();
61
			if (item.value == o) {
62
				return true;
63
			}
64
		}
65
		return false;
66
	};
67
	this.copyTo = function (arr, i) {
68
		var e = this.getIterator();
69
		var idx = i;
70
		while (!e.atEnd()) {
71
			arr.splice(idx, 0, e.get());
72
			idx++;
73
		}
74
	};
75
	this.entry = function (k) {
76
		return items[k];
77
	};
78
	this.forEach = function (fn, scope) {
79
		var s = scope || dj_global;
80
		if (Array.forEach) {
81
			Array.forEach(q, fn, s);
82
		} else {
83
			for (var i = 0; i < q.length; i++) {
84
				fn.call(s, q[i], i, q);
85
			}
86
		}
87
	};
88
	this.getByIndex = function (i) {
89
		return q[i].valueOf();
90
	};
91
	this.getIterator = function () {
92
		return new dojo.collections.DictionaryIterator(items);
93
	};
94
	this.getKey = function (i) {
95
		return q[i].key;
96
	};
97
	this.getKeyList = function () {
98
		var arr = [];
99
		var e = this.getIterator();
100
		while (!e.atEnd()) {
101
			arr.push(e.get().key);
102
		}
103
		return arr;
104
	};
105
	this.getValueList = function () {
106
		var arr = [];
107
		var e = this.getIterator();
108
		while (!e.atEnd()) {
109
			arr.push(e.get().value);
110
		}
111
		return arr;
112
	};
113
	this.indexOfKey = function (k) {
114
		for (var i = 0; i < q.length; i++) {
115
			if (q[i].key == k) {
116
				return i;
117
			}
118
		}
119
		return -1;
120
	};
121
	this.indexOfValue = function (o) {
122
		for (var i = 0; i < q.length; i++) {
123
			if (q[i].value == o) {
124
				return i;
125
			}
126
		}
127
		return -1;
128
	};
129
	this.item = function (k) {
130
		if (k in items && !testObject[k]) {
131
			return items[k].valueOf();
132
		}
133
		return undefined;
134
	};
135
	this.remove = function (k) {
136
		delete items[k];
137
		build();
138
		this.count = q.length;
139
	};
140
	this.removeAt = function (i) {
141
		delete items[q[i].key];
142
		build();
143
		this.count = q.length;
144
	};
145
	this.replace = function (k, v) {
146
		if (!items[k]) {
147
			this.add(k, v);
148
			return false;
149
		} else {
150
			items[k] = new dojo.collections.DictionaryEntry(k, v);
151
			q.sort(sorter);
152
			return true;
153
		}
154
	};
155
	this.setByIndex = function (i, o) {
156
		items[q[i].key].value = o;
157
		build();
158
		this.count = q.length;
159
	};
160
	if (dictionary) {
161
		var e = dictionary.getIterator();
162
		while (!e.atEnd()) {
163
			var item = e.get();
164
			q[q.length] = items[item.key] = new dojo.collections.DictionaryEntry(item.key, item.value);
165
		}
166
		q.sort(sorter);
167
	}
168
};
169