forked from googlearchive/code-editor-app
/
files_list_view_controller.js
141 lines (118 loc) · 3.86 KB
/
files_list_view_controller.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
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
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Callbacks for the controller
var FilesListViewControllerDelegate = function() {
}
FilesListViewControllerDelegate.prototype.filesListViewControllerSelectionChanged = function(entries) {
// Do nothing.
}
FilesListViewControllerDelegate.prototype.filesListViewControllerDoubleClicked = function(entries) {
// Do nothing.
}
FilesListViewControllerDelegate.prototype.filesListViewControllerShowContextMenuForElement = function(element) {
// Do nothing.
}
var FilesListViewController = function(element, delegate) {
this.entries = null;
this.listView = new ListView(element, this);
this.listView.reloadData();
this.delegate = delegate;
element.keydown(this.keyDown.bind(this));
}
FilesListViewController.prototype.keyDown = function(e) {
if (e.keyCode == 8) {
e.preventDefault();
$('#RemoveFilesModal').modal('show');
}
}
FilesListViewController.prototype.updateEntries = function(entries) {
this.entries = entries;
this.listView.reloadData();
}
FilesListViewController.prototype.setSelection = function(selectedEntries) {
var indexes = new Object();
this.entries.forEach(function(entry, i) {
indexes[entry.name] = i;
});
var rowIndexes = new Array();
selectedEntries.forEach(function(entry, i) {
var idx = indexes[entry.name]
if (idx != null) {
rowIndexes.push(idx);
}
});
this.listView.setSelectedRows(rowIndexes);
}
FilesListViewController.prototype.setSelectionByNames = function(names) {
var indexes = new Object();
this.entries.forEach(function(entry, i) {
indexes[entry.name] = i;
});
var rowIndexes = new Array();
names.forEach(function(name, i) {
var idx = indexes[name];
if (idx != null) {
rowIndexes.push(idx);
}
});
this.listView.setSelectedRows(rowIndexes);
}
FilesListViewController.prototype.selection = function() {
var result = new Array();
if (this.entries == null) {
return result;
}
var controller = this;
this.listView.selectedRows().forEach(function(rowIndex, i) {
result.push(controller.entries[rowIndex]);
});
return result;
}
// Callbacks for ListView.
FilesListViewController.prototype.listViewNumberOfRows = function() {
if (this.entries == null) {
return 0;
}
return this.entries.length;
}
FilesListViewController.prototype.listViewElementForRow = function(rowIndex) {
var fileicon = $('<img src="img/file-regular.png"/>');
var text = $('<span class="file-item-text">' + htmlEncode(this.entries[rowIndex].name) + '</span>');
var caret = $('<span class="caret"></span>');
var dropdown = $('<div></div>');
var link = $('<a href="#"></a>');
link.append(caret);
dropdown.append(link);
var listitem = $("<div class=\"file-item\"></div>");
listitem.append(fileicon);
listitem.append(text);
listitem.append(dropdown);
var controller = this;
link.click(function(e) {
if ($('#files-menu').css('display') == 'block') {
return;
}
// Select item if not selected.
var isSelected = false;
controller.listView.selectedRows().forEach(function(currentRowIndex, i) {
if (currentRowIndex == rowIndex) {
isSelected = true;
}
});
if (!isSelected) {
controller.listView.setSelectedRow(rowIndex);
}
controller.delegate.filesListViewControllerShowContextMenuForElement(link, e);
});
return listitem;
}
FilesListViewController.prototype.listViewHeightForRow = function(rowIndex) {
return 25.;
}
FilesListViewController.prototype.listViewSelectionChanged = function(rowIndexes) {
this.delegate.filesListViewControllerSelectionChanged(this.selection());
}
FilesListViewController.prototype.listViewDoubleClicked = function(rowIndexes) {
this.delegate.filesListViewControllerDoubleClicked(this.selection());
}