uki 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,196 +0,0 @@
1
- include('../view.js');
2
- include('../utils.js');
3
-
4
- uki.view.declare('uki.more.view.MultiselectList', uki.view.List, function(Base) {
5
-
6
- this._setup = function() {
7
- Base._setup.call(this);
8
- uki.extend(this, {
9
- _selectedIndexes: [],
10
- _lastClickIndex: -1
11
- });
12
- };
13
-
14
- this.lastClickIndex = uki.newProp('_lastClickIndex');
15
-
16
- this.clearSelection = function(skipClickIndex) {
17
- for (var i=0; i < this._selectedIndexes.length; i++) {
18
- this._setSelected(this._selectedIndexes[i], false);
19
- };
20
- this._selectedIndexes = [];
21
- if (!skipClickIndex) this._lastClickIndex = -1;
22
- };
23
-
24
- this.selectedIndexes = function(indexes) {
25
- if (indexes === undefined) return this._selectedIndexes;
26
- this.clearSelection(true);
27
- this._selectedIndexes = indexes;
28
- for (var i=0; i < this._selectedIndexes.length; i++) {
29
- this._setSelected(this._selectedIndexes[i], true);
30
- };
31
- this.trigger('selection', {source: this});
32
- return this;
33
- };
34
-
35
- this.selectedRows = function() {
36
- return uki.map(this.selectedIndexes(), function(index) {
37
- return this._data[index];
38
- }, this)
39
- };
40
-
41
- this.selectedIndex = function(position) {
42
- if (position === undefined) return this._selectedIndexes.length ? this._selectedIndexes[0] : -1;
43
- this.selectedIndexes([position]);
44
- this._scrollToPosition(position);
45
- return this;
46
- };
47
-
48
- function removeRange (array, from, to) {
49
- var p = uki.more.utils.binarySearch(array, from),
50
- initialP = p;
51
- while (array[p] <= to) p++;
52
- if (p > initialP) array.splice(initialP, p - initialP);
53
- }
54
-
55
- this._toggleSelection = function(p) {
56
- var indexes = [].concat(this._selectedIndexes);
57
- var addTo = uki.more.utils.binarySearch(indexes, p);
58
- if (indexes[addTo] == p) {
59
- indexes.splice(addTo, 1);
60
- } else {
61
- indexes.splice(addTo, 0, p);
62
- }
63
- this.selectedIndexes(indexes);
64
- };
65
-
66
- this._bindSelectionEvents = function() {
67
- this.bind('mousedown', this._mousedown);
68
- this.bind('mouseup', this._mouseup);
69
- this.bind(this.keyPressEvent(), this._keypress);
70
- };
71
-
72
- this._mouseup = function(e) {
73
- var o = uki.dom.offset(this._dom),
74
- y = e.pageY - o.y,
75
- p = y / this._rowHeight << 0;
76
-
77
- if (this._selectionInProcess && this._lastClickIndex == p && this.isSelected(p)) this.selectedIndexes([p]);
78
- this._selectionInProcess = false;
79
- };
80
-
81
- this._mousedown = function(e) {
82
- var o = uki.dom.offset(this._dom),
83
- y = e.pageY - o.y,
84
- p = y / this._rowHeight << 0,
85
- indexes = this._selectedIndexes;
86
-
87
- this._selectionInProcess = false;
88
- if (e.shiftKey && indexes.length > 0) {
89
- if (this.isSelected(p)) {
90
- indexes = [].concat(indexes);
91
- removeRange(indexes, Math.min(p+1, this._lastClickIndex), Math.max(p-1, this._lastClickIndex));
92
- this.selectedIndexes(indexes);
93
- } else {
94
- this.selectedIndexes(uki.more.utils.range(
95
- Math.min(p, indexes[0]),
96
- Math.max(p, indexes[indexes.length - 1])
97
- ));
98
- }
99
- } else if (e.metaKey) {
100
- this._toggleSelection(p);
101
- } else {
102
- if (!this.isSelected(p)) {
103
- this.selectedIndexes([p]);
104
- } else {
105
- this._selectionInProcess = true;
106
- }
107
- }
108
- this._lastClickIndex = p;
109
- };
110
-
111
- this._keypress = function(e) {
112
- var indexes = this._selectedIndexes,
113
- nextIndex = -1;
114
- if (e.which == 38 || e.keyCode == 38) { // UP
115
- nextIndex = Math.max(0, this._lastClickIndex - 1);
116
- } else if (e.which == 40 || e.keyCode == 40) { // DOWN
117
- nextIndex = Math.min(this._data.length-1, this._lastClickIndex + 1);
118
- }
119
- if (nextIndex > -1 && nextIndex != this._lastClickIndex) {
120
- if (e.shiftKey) {
121
- if (this.isSelected(nextIndex)) {
122
- this._toggleSelection(this._lastClickIndex);
123
- } else {
124
- this._toggleSelection(nextIndex);
125
- }
126
- } else {
127
- this.selectedIndex(nextIndex);
128
- }
129
- this._lastClickIndex = nextIndex;
130
- e.preventDefault();
131
- }
132
- };
133
-
134
- this.isSelected = function(index) {
135
- var found = uki.more.utils.binarySearch(this._selectedIndexes, index);
136
- return this._selectedIndexes[found] == index;
137
- };
138
-
139
- // xxxxx | xxxxx | xxxxxxxx | xxx
140
- // yyyyy | yyyyy | yyyy | yyyyyyy
141
- this._restorePackSelection = function(pack) {
142
- var indexes = this._selectedIndexes;
143
-
144
- if (
145
- (indexes[0] <= pack.itemFrom && indexes[indexes.length - 1] >= pack.itemFrom) || // left index
146
- (indexes[0] <= pack.itemTo && indexes[indexes.length - 1] >= pack.itemTo) || // right index
147
- (indexes[0] >= pack.itemFrom && indexes[indexes.length - 1] <= pack.itemTo) // within
148
- ) {
149
- var currentSelection = uki.more.utils.binarySearch(indexes, pack.itemFrom);
150
- currentSelection = Math.max(currentSelection, 0);
151
- while(indexes[currentSelection] !== null && indexes[currentSelection] < pack.itemTo) {
152
- var position = indexes[currentSelection] - pack.itemFrom;
153
- this._render.setSelected(pack.dom.childNodes[position], this._data[position], true, this.hasFocus());
154
- currentSelection++;
155
- }
156
- }
157
- };
158
-
159
- this._setSelected = function(position, state) {
160
- var item = this._itemAt(position);
161
- if (item) this._render.setSelected(item, this._data[position], state, this.hasFocus());
162
- };
163
-
164
- this._focus = function() {
165
- if (this._selectedIndexes.length == 0 && this._data.length > 0) {
166
- this.selectedIndexes([0]);
167
- } else {
168
- this.selectedIndexes(this.selectedIndexes());
169
- }
170
- };
171
-
172
- this._blur = function() {
173
- this.selectedIndexes(this.selectedIndexes());
174
- };
175
- });
176
-
177
- uki.view.declare('uki.more.view.ScrollableMultiselectList', uki.view.ScrollPane, function(Base) {
178
-
179
- this._createDom = function() {
180
- Base._createDom.call(this);
181
- this._list = uki({ view: 'uki.more.view.MultiselectList', rect: this.rect().clone().normalize(), anchors: 'left top right bottom' })[0];
182
- this.appendChild(this._list);
183
- };
184
-
185
- uki.each('data rowHeight render packSize visibleRectExt throttle focusable selectedIndexes selectedIndex selectedRows contentDraggable draggable hasFocus'.split(' '),
186
- function(i, name) {
187
- uki.delegateProp(this, name, '_list');
188
- }, this);
189
-
190
- });
191
-
192
-
193
- // export properties
194
- uki.Collection.addAttrs(['lastClickIndex','selectedIndexes']);
195
- uki.delegateProp(uki.view.Table.prototype, 'selectedIndexes', '_list');
196
- uki.delegateProp(uki.view.Table.prototype, 'lastClickIndex', '_list');