Skip to content

Commit

Permalink
Refactor TreeView consumer to inherit base class
Browse files Browse the repository at this point in the history
  • Loading branch information
Alhadis committed Jan 10, 2017
1 parent 1eaaf92 commit a9276b1
Showing 1 changed file with 27 additions and 93 deletions.
120 changes: 27 additions & 93 deletions lib/consumers/tree-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,113 +4,47 @@ const {isAbsolute, join, sep} = require("path");
const {CompositeDisposable, Disposable, Emitter} = require("atom");
const FileSystem = require("../filesystem/filesystem.js");
const TreeEntry = require("./tree-entry.js");
const Consumer = require("./consumer.js");


class TreeView{
class TreeView extends Consumer {

init(){
this.disposables = new CompositeDisposable();
this.entries = new Map();
this.emitter = new Emitter();
this.element = null;

this.checkPanes();
this.disposables.add(
atom.packages.onDidActivatePackage(_=> this.checkPanes()),
atom.packages.onDidDeactivatePackage(_=> this.checkPanes()),
atom.packages.onDidActivateInitialPackages(() => {
const workspace = atom.views.getView(atom.workspace);
atom.commands.dispatch(workspace, "tree-view:show");
this.checkPanes();
}),

this.onDidAttach(() => {
if(this.metadata.consumedServices["file-icons.element-icons"]){
this.isRedundant = true;
return;
}

this.entryElements = this.element[0].getElementsByClassName("entry");

// TODO: Remove check when atom/tree-view#966 is merged/shipped
if("function" === typeof this.element.onEntryMoved){
const onMove = this.element.onEntryMoved(paths => {
FileSystem.fixPath(paths.oldPath, paths.newPath);
});
this.disposables.add(onMove);
}
this.disposables.add(
atom.project.onDidChangePaths(_=> this.rebuild()),
atom.config.onDidChange("tree-view.hideIgnoredNames", _=> this.rebuild()),
atom.config.onDidChange("tree-view.hideVcsIgnoredFiles", _=> this.rebuild()),
atom.config.onDidChange("tree-view.squashDirectoryNames", _=> this.rebuild()),
atom.config.onDidChange("tree-view.sortFoldersBeforeFiles", _=> this.rebuild())
);
this.rebuild();
})
);
}


reset(){
this.disposables.dispose();
this.emitter.dispose();
this.entries.clear();

constructor(){
super("tree-view");
this.entryElements = null;
this.disposables = null;
this.entries = null;
this.emitter = null;
this.entries = new Map();
this.element = null;
}


onDidAttach(fn){
return this.emitter.on("did-attach", fn);
}


onDidRemove(fn){
return this.emitter.on("did-remove", fn);
init(){
super.init();
this.updateStatus();
}


/**
* Query the activation status of the tree-view package.
*
* @private
*/
checkPanes(){
const treePackage = atom.packages.activePackages["tree-view"];
activate(){
const workspace = atom.views.getView(atom.workspace);
atom.commands.dispatch(workspace, "tree-view:show");

if(treePackage && !this.element){
const {treeView} = treePackage.mainModule;
this.metadata = treePackage.metadata;

if(treeView){
this.element = treeView;
this.emitter.emit("did-attach");
}

else if(!this.pending){
this.pending = atom.commands.onDidDispatch(cmd => {
if("tree-view:toggle" === cmd.type || "tree-view:show" === cmd.type){
this.pending.dispose();
this.disposables.remove(this.pending);
delete this.pending;

this.element = treePackage.mainModule.treeView;
this.emitter.emit("did-attach");
}
});
this.disposables.add(this.pending);
}
}
this.element = this.packageModule.treeView;
this.entryElements = this.element[0].getElementsByClassName("entry");

else if(!treePackage && this.element){
this.element = null;
this.emitter.emit("did-remove");
// TODO: Remove check when atom/tree-view#966 is merged/shipped
if("function" === typeof this.element.onEntryMoved){
const onMove = this.element.onEntryMoved(paths => {
FileSystem.fixPath(paths.oldPath, paths.newPath);
});
this.disposables.add(onMove);
}
this.disposables.add(
atom.project.onDidChangePaths(() => this.rebuild()),
atom.config.onDidChange("tree-view.hideIgnoredNames", () => this.rebuild()),
atom.config.onDidChange("tree-view.hideVcsIgnoredFiles", () => this.rebuild()),
atom.config.onDidChange("tree-view.squashDirectoryNames", () => this.rebuild()),
atom.config.onDidChange("tree-view.sortFoldersBeforeFiles", () => this.rebuild())
);
this.rebuild();
}


Expand Down

0 comments on commit a9276b1

Please sign in to comment.