-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
223 lines (190 loc) · 6.73 KB
/
index.js
File metadata and controls
223 lines (190 loc) · 6.73 KB
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
/* global ace cre chrome uuid listings dirEntries */
var teListingEntry = cre('.listing', {wall: true}, [
cre('.section', {part: 'config-section'}, [
cre('div', {part: 'config-editor'}),
]),
cre('.section', {part: 'op-section'}, [
cre('.section', {part: 'no-dir', hidden: true}, [
cre('button', {part: 'dir-chooser', type: 'button'}, 'Choose directory')
]),
cre('.section', {part: 'modified-config', hidden: true}, [
cre('button', {part: 'config-save', type: 'button'},
'Save config'),
cre('button', {part: 'config-revert', type: 'button'},
'Discard changes'),
]),
cre('.section', {part: 'pull-ready', hidden: true}, [
cre('.tightline', {part: 'dir-line'}, [
cre('span', {part: 'dir-location'}),
cre('button', {part: 'dir-changer', type: 'button'}, 'Change'),
]),
cre('button', {part: 'pull-button'}, 'Pull')
]),
cre('.section', {part: 'pull-ongoing', hidden: true}, [
cre('output', {part: 'pull-status'})
// TODO: cancel button
]),
cre('button', {part: 'delete-button', type: 'button'}, 'Delete')
])
]);
function createListingObject() {
return {id: uuid()};
}
function createListingEntry(listing) {
if (!listing.config) {
listing.config = '';
}
var listingEntry = cre(teListingEntry);
var noDirOps = listingEntry.getPart('no-dir');
var modifiedConfigOps = listingEntry.getPart('modified-config');
var pullReadyOps = listingEntry.getPart('pull-ready');
var ongoingOps = listingEntry.getPart('pull-ongoing');
var modes = [noDirOps, modifiedConfigOps, pullReadyOps, ongoingOps];
function showOps(element) {
for (var i = 0; i < modes.length; i++) {
var mode = modes[i];
mode.hidden = mode != element;
}
}
var deleteButton = listingEntry.getPart('delete-button');
function deleteThisListing() {
return listings.remove(listing.id).then(function(){
listingEntry.remove();
});
}
deleteButton.addEventListener('click', deleteThisListing);
// TODO: determine if operation is in progress externally,
// everywhere this is currently used
var pullInProgress = false;
function showApplicableOps() {
var configInEditor = editor.getValue();
deleteButton.hidden = configInEditor != '';
if (editor.getValue() == listing.config) {
if (listing.retainedDirId) {
if (pullInProgress) {
return showOps(ongoingOps);
} else {
return showOps(pullReadyOps);
}
} else {
return showOps(noDirOps);
}
} else {
return showOps(modifiedConfigOps);
}
}
var dirLocationSpan = listingEntry.getPart('dir-location');
function displayPath(path) {
dirLocationSpan.textContent = path;
}
if (listing.retainedDirId) {
dirEntries.getDisplayPath(listing.retainedDirId).then(function (path) {
if (path) {
displayPath(path);
return showApplicableOps();
// If the path is null (non restorable)
} else {
// TODO: have something responsible for removing this from the listing
delete listing.retainedDirId;
return showOps(noDirOps);
}
});
} else showOps(noDirOps);
function updateConfig(configJson) {
// update our local copy for config-has-been-modified checking
listing.config = configJson;
return listings.updateById(listing.id, 'config', configJson);
}
function selectDir () {
dirEntries.chooseDir().then(function(dir){
dirEntries.getEntryDisplayPath(dir.entry).then(displayPath);
listing.retainedDirId = dir.retainedId;
return listings.updateById(
listing.id, 'retainedDirId', dir.retainedId)
.then(showApplicableOps);
// TODO: read directory for config, if config is blank
});
}
listingEntry.getPart('dir-chooser')
.addEventListener('click', selectDir);
listingEntry.getPart('dir-changer')
.addEventListener('click', selectDir);
var editorElement = listingEntry.getPart('config-editor');
var editor = ace.edit(editorElement);
var session = editor.getSession();
editor.setTheme("ace/theme/chrome");
session.setMode("ace/mode/yaml");
session.setTabSize(2);
session.setUseSoftTabs(true);
editor.renderer.setOption('showLineNumbers', false);
editor.setValue(listing.config, 1);
// update the visible operations every time the config is modified
session.on('change', showApplicableOps);
function saveConfig() {
var configJson = editor.getValue();
return updateConfig(configJson)
.then(showApplicableOps);
}
listingEntry.getPart('config-save')
.addEventListener('click', saveConfig);
editor.commands.addCommand({
name: "save",
bindKey: {win: "Ctrl-S", mac: "Command-S"},
exec: saveConfig
});
function revertConfig() {
// TODO: retrieve value to revert to directly from store
// TODO: don't modify cursor position?
editor.setValue(listing.config, 1);
return showApplicableOps();
}
listingEntry.getPart('config-revert')
.addEventListener('click', revertConfig);
var statusOutput = listingEntry.getPart('pull-status');
var pullButton = listingEntry.getPart('pull-button');
function performPull() {
var port = chrome.runtime.connect();
port.postMessage({type: 'startPull', listingId: listing.id});
pullInProgress = true;
showApplicableOps();
function updateStatus(message) {
if (message.type == 'status') {
statusOutput.textContent = message.message;
} else if (message.type == 'finish') {
// Clear status for next time
statusOutput.textContent = '';
pullInProgress = false;
showApplicableOps();
} else if (message.type == 'error') {
// TODO: make it possible to escape this
statusOutput.textContent = 'Error: ' + message.error.message;
console.error(message.error);
}
}
port.onMessage.addListener(updateStatus);
port.onDisconnect.addListener(function(){
pullInProgress = false;
showApplicableOps();
});
}
pullButton.addEventListener('click', performPull);
return listingEntry;
}
var lastListingItem = document.getElementById('pseudo-listing');
var newListingButton = document.getElementById('new-listing');
newListingButton.addEventListener('click', function() {
var newListing = createListingObject();
listings.addNew(newListing);
listingsDiv.insertBefore(
createListingEntry(newListing), lastListingItem);
});
var listingsDiv = document.getElementById('pull-listings');
var loadingMessage = document.getElementById('loading-message');
listings.getAll().then(function(listingList) {
loadingMessage.remove();
newListingButton.hidden = false;
for (var i = 0; i < listingList.length; i++) {
listingsDiv.insertBefore(
createListingEntry(listingList[i]), lastListingItem);
}
});