forked from mattsawyer77/atom-perforce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
204 lines (177 loc) · 6.79 KB
/
main.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
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
'use strict';
var settings = require('./settings/settings'),
atomPerforce = require('./lib/atom-perforce'),
CompositeDisposable = require('atom').CompositeDisposable,
commandsSetup = false,
reactivateCommands = ['autoAdd', 'autoEdit', 'autoRevert'],
observers;
// TODO: if Atom ever publishes an event for monitoring DOM changes, use that instead
// OR refactor this into its own service
function setupObservers() {
var mutationObserverOptions = {
subtree: true,
childList: true,
attributes: false
},
leftPanelObserver,
treeObserver,
destroyWatch;
function getToolPanel() {
return document.querySelector('.tool-panel');
}
function watchToolPanel() {
// monitor the tree for changes (collapsing/expanding)
treeObserver.observe(getToolPanel(), mutationObserverOptions);
}
// cleanup any observers before (re)observing
deactivate();
observers = new CompositeDisposable();
treeObserver = new MutationObserver(function treeChanged(mutations/*, observer*/) {
// we only care if some nodes were added, not removed
if(mutations.some(function(mutation) {
return mutation.addedNodes && mutation.addedNodes.length > 0;
})) {
atomPerforce.markOpenFiles();
}
});
// make this work like an Atom observer
treeObserver.dispose = treeObserver.disconnect;
observers.add(treeObserver);
// wait for the tool-panel to exist
if(getToolPanel()) {
watchToolPanel();
}
else {
// setup observer for the left panel
leftPanelObserver = new MutationObserver(function leftPanelChanged(/*mutations, observer*/) {
if(getToolPanel()) {
// stop waiting for the left panel to exist
leftPanelObserver.disconnect();
// watch for mutations within the panel
watchToolPanel();
}
});
leftPanelObserver.observe(document.querySelector('atom-panel-container.left'), mutationObserverOptions);
}
observers.add(atom.workspace.observeTextEditors(function(editor) {
// mark changes on save
var saveObserver = editor.buffer.onDidSave(function(file) {
if(atom.config.get('atom-perforce').autoAdd) {
atomPerforce.fileIsTracked(editor.getPath())
.then(function(fileinfo) {
if(fileinfo === false) {
atomPerforce.add(file.path)
.then(function() {
return atomPerforce.markOpenFiles();
});
}
});
}
atomPerforce.getChanges()
.then(function(changes) {
atomPerforce.showDiffMarks(file.path, changes);
});
});
observers.add(editor.onDidDestroy(function() {
saveObserver.dispose();
}));
// mark changes on initial load
if(editor.getPath()) {
atomPerforce.getChanges(editor)
.then(function(changes) {
atomPerforce.showDiffMarks(editor.getPath(), changes);
});
if(atom.config.get('atom-perforce').autoEdit) {
atomPerforce.fileIsTracked(editor.getPath())
.then(function(fileinfo) {
var watchBufferChanges;
if(fileinfo && !fileinfo.action) {
watchBufferChanges = editor.buffer.onDidChange(function onDidChange() {
watchBufferChanges.dispose();
atomPerforce.edit(editor.getPath(), false)
.then(function() {
atomPerforce.markOpenFiles();
});
});
}
});
}
}
// handle closing the buffer
destroyWatch = editor.onDidDestroy(function editorDestroyed() {
destroyWatch.dispose();
if(atom.config.get('atom-perforce').autoRevert) {
atomPerforce.fileIsTracked(editor.getPath())
.then(function(fileinfo) {
if(fileinfo && fileinfo.action === 'edit') {
atomPerforce.getChanges(editor)
.then(function(changes) {
if(!(changes && changes.length)) {
// revert the file without confirmation
atomPerforce.revert(editor.getPath(), false)
.then(function() {
atomPerforce.markOpenFiles();
});
}
});
}
});
}
});
observers.add(destroyWatch);
}));
observers.add(atom.workspace.observeActivePaneItem(function() {
atomPerforce.showClientName();
}));
observers.add(atom.config.onDidChange('atom-perforce.defaultP4Location', atomPerforce.setupEnvironment));
reactivateCommands.forEach(function(command) {
observers.add(atom.config.onDidChange('atom-perforce.' + command, reactivate));
});
atomPerforce.markOpenFiles();
return observers;
}
function stateChangeWrapper(fn) {
return function() {
var args = [].slice.call(arguments);
// execute a promise-returning function
return fn.apply(this, args)
// then unconditionally mark the open files
.finally(atomPerforce.showDiffMarks);
};
}
function revertReset() {
deactivate();
return atomPerforce.revert().finally(function() {
return setupObservers();
});
}
function setupCommands() {
if(!commandsSetup) {
['perforce', 'p4'].forEach(function(prefix) {
atom.commands.add('atom-workspace', prefix + ':edit', stateChangeWrapper(atomPerforce.edit));
atom.commands.add('atom-workspace', prefix + ':add', stateChangeWrapper(atomPerforce.add));
atom.commands.add('atom-workspace', prefix + ':sync', stateChangeWrapper(atomPerforce.sync));
atom.commands.add('atom-workspace', prefix + ':revert', stateChangeWrapper(revertReset));
atom.commands.add('atom-workspace', prefix + ':load-opened-files', stateChangeWrapper(atomPerforce.loadAllOpenFiles));
});
commandsSetup = true;
}
}
function activate(/*state*/) {
observers = setupObservers();
return setupCommands();
}
function deactivate() {
if(observers && observers.dispose) {
observers.dispose();
}
}
function reactivate() {
deactivate();
return activate();
}
module.exports = {
config: settings,
activate: activate,
deactivate: deactivate
};