-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContainer.ts
242 lines (197 loc) · 8.45 KB
/
Container.ts
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
232
233
234
235
236
237
238
239
240
241
242
/// <reference path="../../build/linq.d.ts" />
namespace BioDeep.UI {
export class Container {
private containers: Dictionary<string[]>;
private storeKey: string;
/**
* 从这个属性获取得到拖拽的结果数据
*/
public get Data(): object {
var x = {};
var table = this.containers;
this.containers
.Keys
.ForEach(key => {
x[key] = table.Item(key);
});
return x;
}
/**
* @param items 所需要进行拖拽的数据列表 是一个键值对,其中键名是资源的唯一标记,而键值则是用户界面显示使用的
* @param containers 所接纳的容器对象的id编号列表
*/
public constructor(
items: MapTuple<string, string>[],
srcContainer: string,
containers: string[],
storeKey: string = "group_set") {
var union: IEnumerator<string> = $from(containers)
.ToList()
.Add(srcContainer);
var groupset: string = localStorage.getItem(storeKey);
if (!isNullOrUndefined(groupset)) {
this.containers = new Dictionary<string[]>(JSON.parse(groupset));
} else {
this.containers = new Dictionary<string[]>(Activator.EmptyObject(union, () => (<string[]>[])));
// 在初始化的时候src应该是有所有的数据的
this.containers.Delete(srcContainer).Add(
srcContainer,
$from(items).Select(map => map.key).ToArray()
);
}
var srcItems = this.containers.Item(srcContainer);
var filter = $from(items)
.Where(key => srcItems.indexOf(key.key) > -1)
.ToArray();
this.registerDatas(filter, srcContainer);
this.registerContainers(union.ToArray());
stylingContainers(containers);
stylingItems($from(filter).Select(id => id.key));
stylingContainers([srcContainer]);
this.storeKey = storeKey;
this.init(items, srcContainer);
}
/**
* @param items 使用这个参数主要是为了得到在用户界面上的显示title
* @param src 因为数据源已经在前面的registerDatas函数中初始化过了,所以在这个初始化函数中会跳过
*/
private init(items: MapTuple<string, string>[], src: string) {
var data = this.containers;
var maps = new Dictionary<string>(items);
var dev: Container = this;
this.containers
.Keys
.Where(id => id != src)
.ForEach(containerId => {
var keys: string[] = data.Item(containerId);
keys.forEach(keyId => {
var value = new MapTuple<string, string>(keyId, maps.Item(keyId));
var newItem = Container.createItem(value);
var ul: string = `#${containerId}-ul`;
(<HTMLElement>$ts(ul)).appendChild(newItem.key);
applyItemStyle(keyId);
dev.registerItemDragEvent(newItem.value);
});
});
}
private registerContainers(names: string[]) {
$from(names)
.Select(id => <HTMLElement>document.querySelector(`#${id}`))
.ForEach(container => {
container.appendChild($ts("<ul>", {
id: `${container.id}-ul`
}));
this.binEach(container, this.containers)
});
}
/**
* 为某一个指定的容器对象注册鼠标事件
*/
private binEach(bin: HTMLElement, container: Dictionary<string[]>) {
var dev = this;
DOM.Events.addEvent(bin, 'dragover', function (e) {
if (e.preventDefault) {
// allows us to drop
e.preventDefault();
}
this.className = 'over';
(<DragEvent>e).dataTransfer.dropEffect = 'copy';
return false;
});
// to get IE to work
DOM.Events.addEvent(bin, 'dragenter', function (e) {
this.className = 'over';
return false;
});
DOM.Events.addEvent(bin, 'dragleave', function () {
this.className = '';
});
DOM.Events.addEvent(bin, 'drop', function (e) {
if (e.stopPropagation) {
// stops the browser from redirecting...why???
e.stopPropagation();
}
dev.dropData(<DragEvent>e, bin, container);
return false;
});
}
private dropData(event: DragEvent, bin: HTMLElement, container: Dictionary<string[]>) {
var strval: string = event.dataTransfer.getData('Text');
var data: MapTuple<string, string>;
try {
data = JSON.parse(strval);
} catch (ex) {
console.error("JSON parser error: " + strval);
return false;
}
var keyId: string = data.key;
var el = document.getElementById(keyId);
var newItem = Container.createItem(data);
var key: string = bin.id;
var ul: string = `${bin.id}-ul`;
el.parentNode.parentNode.removeChild(el.parentNode);
// stupid nom text + fade effect
bin.className = '';
document.getElementById(ul).appendChild(newItem.key);
applyItemStyle(keyId);
this.registerItemDragEvent(newItem.value);
container.Keys.ForEach(key => {
if (container.Item(key).indexOf(data.key) > -1) {
var list = $from(container.Item(key))
.Where(id => id != data.key)
.ToArray();
container.Delete(key).Add(key, list);
}
});
// 在这里得到data数据之后,将数据添加进入对应的容器之中
container.Item(key).push(data.key);
localStorage.setItem(this.storeKey, JSON.stringify(this.Data));
}
private registerItemDragEvent(a: HTMLAnchorElement): void {
a.setAttribute('draggable', 'true');
DOM.Events.addEvent(a, 'dragstart', function (e) {
var event = <DragEvent>e;
var data: string = JSON.stringify({
key: a.id,
value: a.innerText
});
// only dropEffect='copy' will be dropable
event.dataTransfer.effectAllowed = 'copy';
// required otherwise doesn't work
event.dataTransfer.setData('Text', data);
});
}
/**
* 为数据对象在容器之中注册鼠标事件
*/
private registerDatas(items: MapTuple<string, string>[], srcContainer: string) {
var data = Container.createDocument(items);
var container = document.getElementById(srcContainer);
data.ForEach(a => {
container.appendChild(a.key);
})
data.Select(a => a.value)
.ForEach(el => {
this.registerItemDragEvent(el);
});
}
/**
* @param item 资源的键值对数据,key为数据库之中的唯一编号,value则是这个资源的用户界面上的显示标题
*/
private static createItem(item: MapTuple<string, string>): MapTuple<HTMLLIElement, HTMLAnchorElement> {
var li = document.createElement("li");
var a: HTMLTsElement = (<HTMLTsElement>$ts("<a>", {
id: item.key,
href: "#"
}).asExtends).text(item.value);
li.appendChild(a.HTMLElement);
return <MapTuple<HTMLLIElement, HTMLAnchorElement>>{
key: li,
value: a.HTMLElement
};
}
private static createDocument(items: MapTuple<string, string>[]): IEnumerator<MapTuple<HTMLLIElement, HTMLAnchorElement>> {
return $from(items).Select(Container.createItem);
}
}
}