| 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298 |
1x
1x
1x
1x
1x
1x
1x
1x
22x
22x
22x
1x
22x
1x
13x
1x
22x
1x
39x
34x
34x
22x
1x
1x
22x
22x
22x
22x
22x
22x
22x
1x
1x
23x
23x
23x
23x
1x
18x
1x
17x
17x
6x
4x
4x
2x
2x
2x
2x
2x
2x
2x
6x
6x
11x
13x
7x
7x
7x
3x
3x
4x
4x
4x
3x
1x
4x
4x
4x
52x
13x
2x
11x
11x
11x
11x
8x
8x
8x
8x
11x
11x
2x
11x
8x
1x
1x
22x
2x
1x
1x
1x
1x
1x
3x
1x
1x
3x
2x
1x
3x
1x
3x
1x
1x
3x
2x
1x
3x
1x
| /**
* @module CollectionControls
*
* */ /** */
import { bindable, computedFrom, customElement, autoinject } from 'aurelia-framework';
import { Content, Query, Repository, IContent } from 'sn-client-js';
import { Subscription } from 'rxjs/Subscription';
import { BindingSignaler } from 'aurelia-templating-resources';
export enum CollectionView {
List = 'List',
Grid = 'Grid',
Details = 'Details',
}
/**
* Component that lets you display and work with a simple Content Collection
*
* Usage example:
* ```html
* <content-list scope.bind="Scope"
* get-items.call="GetSelectedChildren(scope, query)"
* on-activate.call="Select(content)"
* on-drop-files.call="FilesDropped(event, files)"
* on-drop-content.call="ContentDropped(content)"
* on-drop-content-list.call="ContentListDropped(contentList)"
* on-drop-content-on-item.call="ContentDroppedOnItem(content, item)"
* on-drop-content-list-on-item.call="ContentListDroppedOnItem(contentList, item)"
* view-type.bind="ViewType"
* query.bind='query'
* selection.two-way="SelectedContent"
* actions.bind="exploreActions"></content-list>
* ```
*/
@customElement('content-list')
@autoinject
export class ContentList {
protected readonly selectionChangedSignalKey = 'content-list-selection-changed';
constructor(
private readonly bindingSignaler: BindingSignaler
) {
}
@bindable
public IsLoading: boolean = false;
@bindable
public Scope: Content;
@bindable
public Selection: Content[] = [];
@computedFrom('Scope')
get hasScope(): boolean {
return this.Scope && this.Scope.SavedFields && Object.keys(this.Scope.SavedFields).length > 0 || false;
}
@bindable
public Query?: Query;
private Subscriptions: Subscription[] = [];
@computedFrom('Scope')
get Repository(): Repository.BaseRepository {
return this.Scope.GetRepository();
};
clearSubscriptions() {
this.Subscriptions.forEach(subscription => {
subscription.unsubscribe();
});
this.Subscriptions = [];
}
detached() {
this.clearSubscriptions();
}
@bindable
public GetItems: (params: { scope: Content, query?: Query }) => Promise<Content[]>;
@bindable
public OnActivate: (params: { content: Content }) => void;
@bindable
public Actions: { name: string; action: (content: Content) => void }[] = [];
@bindable
public OnDropFiles: (params: {event: DragEvent, files: FileList}) => void = (params) => {
};
@bindable
public OnDropContent: (params: {event: DragEvent, content: Content}) => void = (params: {content: Content}) => {
if (this.Scope.Path && this.Scope.Path !== params.content.ParentPath && this.Scope.Path !== params.content.Path && !params.content.IsAncestorOf(this.Scope)) {
params.content.MoveTo(this.Scope.Path);
}
}
@bindable
public OnDropContentOnItem: (params: {event: DragEvent, content: Content, item: Content}) => void = (params) => {
if (params.item.Path && params.item.Path !== params.content.ParentPath && params.item.Path !== params.content.Path && !params.content.IsAncestorOf(params.item)) {
params.content.MoveTo(params.item.Path);
}
}
@bindable
public OnDropContentList: (params: {event: DragEvent, contentList: Content[]}) => void = (params) => {
if (this.Scope.Path){
params.contentList.forEach(c => this.OnDropContent({content: c, event: params.event}));
}
};
public OnDropFilesOnItem: (params: {files: FileList, item: Content, event: DragEvent, }) => void = (params) => {
console.log('Dropped files on item', params);
};
@bindable
public OnDropContentListOnItem: (params: {contentList: Content[], item: Content, event: DragEvent, }) => void = (params) => {
if (this.Scope.Path){
params.contentList.forEach(c => this.OnDropContentOnItem({content: c, item: params.item, event: params.event}));
}
};
public triggerAction(event: MouseEvent, action: (item: Content) => void, item: Content) {
event.stopPropagation();
action(item);
}
@bindable
ViewType: CollectionView = CollectionView.List;
@bindable
ShowScope: boolean = true;
@bindable
MultiSelect: boolean = true;
@bindable
EnableSelection: boolean = true;
activateItem(content: Content) {
this.OnActivate && this.OnActivate({ content: content });
}
selectItem(event: MouseEvent, content: Content) {
if (!this.EnableSelection) {
return;
}
const selectionIndex = this.Selection.indexOf(content);
if (this.MultiSelect && (event.ctrlKey || event.shiftKey)) {
// CTRL+Click - Add/Remove toggle to Selection
if (event.ctrlKey) {
this.toggleSelection(event, content);
return;
}
// SHIFT+Click - Select Range
Eif (event.shiftKey){
const lastSelected = this.Selection[this.Selection.length - 1];
const selectedIndex1 = this.Items.indexOf(lastSelected) + 1;
const selectedIndex2 = this.Items.indexOf(content) + 1;
const minSelected = Math.min(selectedIndex1, selectedIndex2) - 1;
const maxSelected = Math.max(selectedIndex1, selectedIndex2);
this.Items.slice(minSelected, maxSelected).forEach(item => {
Eif (selectionIndex === -1){
this.Selection.push(item);
}
})
}
} else {
this.Selection = [content];
}
this.bindingSignaler.signal(this.selectionChangedSignalKey);
}
toggleSelection(event: MouseEvent, content: Content) {
event.preventDefault()
event.stopImmediatePropagation();
if (!this.MultiSelect) {
this.selectItem(event, content);
return;
}
const tempSelection = this.Selection.map(s => s);
const index = tempSelection.indexOf(content);
if (index === -1) {
tempSelection.push(content);
} else {
tempSelection.splice(index, 1);
}
this.Selection = tempSelection;
setTimeout(() => {
this.bindingSignaler.signal(this.selectionChangedSignalKey);
});
}
isSelected(content: Content): boolean {
return this.Selection && this.Selection.indexOf(content) !== -1;
}
public async Reinitialize() {
if (this.IsLoading) {
// already triggered
return;
}
this.Selection = [];
this.IsLoading = true;
this.clearSubscriptions();
if (this.hasScope) {
this.Subscriptions.push(this.Repository.Events.OnContentCreated.subscribe(created => this.handleContentChanges(created.Content)));
this.Subscriptions.push(this.Repository.Events.OnContentDeleted.subscribe(deleted => this.handleContentChanges(deleted.ContentData)));
this.Subscriptions.push(this.Repository.Events.OnContentModified.subscribe(mod => this.handleContentChanges(mod.Content)));
this.Subscriptions.push(this.Repository.Events.OnContentMoved.subscribe(move => this.handleContentChanges(move.Content, { Path: move.From }, { Path: move.To })));
}
try {
const newItems = await this.GetItems({ scope: this.Scope, query: this.Query });
this.Items = newItems;
} catch (error) {
}
this.IsLoading = false;
}
ScopeChanged() {
this.Reinitialize();
};
QueryChanged(){
this.Reinitialize()
};
@bindable
public Items: Content[];
@bindable
public ReloadOnContentChange: (change: Content | IContent) => boolean =
(content: Content | IContent) => {
return true;
// ToDo: check this based on scope and Query
// return (this.Query !== undefined) || this.hasScope && ( isContent(content) && this.Scope.IsAncestorOf(content as Content)) || (content.Path && this.Scope.IsAncestorOf(content as any)) || false;
}
handleContentChanges(...changes: (Content | IContent)[]) {
changes.forEach(change => {
Eif (this.ReloadOnContentChange(change)) {
this.Reinitialize();
return;
}
});
return;
}
dropContent(event: DragEvent, stringifiedContent: string, stringifiedContentList?: string[], files?: FileList) {
if (stringifiedContent){
const droppedContent = this.Repository.ParseContent(stringifiedContent);
this.OnDropContent({event, content: droppedContent});
}
if (stringifiedContentList){
const contentList = stringifiedContentList.map(c => this.Repository.ParseContent(c))
this.OnDropContentList({event, contentList});
}
if (files){
this.OnDropFiles({event, files});
}
}
dropContentOnItem(event: DragEvent, item: Content, stringifiedContent?: string, stringifiedContentList?: string[], files?: FileList){
if (stringifiedContent){
const droppedContent = this.Repository.ParseContent(stringifiedContent);
this.OnDropContentOnItem({event, content: droppedContent, item});
}
if (stringifiedContentList){
const contentList = stringifiedContentList.map(c => this.Repository.ParseContent(c))
this.OnDropContentListOnItem({event, contentList, item});
}
if (files){
this.OnDropFilesOnItem({event, files, item});
}
}
} |