really hacky job of refs

This commit is contained in:
Peter Cottle 2012-09-19 20:10:58 -07:00
parent 8d6e835538
commit 18256dad4b
5 changed files with 103 additions and 9 deletions

View file

@ -6,6 +6,10 @@ var CommandCollection = Backbone.Collection.extend({
model: Command,
});
var BranchCollection = Backbone.Collection.extend({
model: Branch
});
var CommandBuffer = Backbone.Model.extend({
defaults: {
collection: null,

View file

@ -11,7 +11,7 @@ function GitEngine(options) {
this.refs = {};
this.HEAD = null;
this.id_gen = 0;
this.branches = [];
this.branches = options.branches;
this.collection = options.collection;
// global variable to keep track of the options given
@ -82,7 +82,7 @@ GitEngine.prototype.makeBranch = function(id, target) {
target: target,
id: id
});
this.branches.push(branch);
this.branches.add(branch);
this.refs[branch.get('id')] = branch;
return branch;
};
@ -93,7 +93,7 @@ GitEngine.prototype.getHead = function() {
GitEngine.prototype.getBranches = function() {
var toReturn = [];
_.each(this.branches, function(branch) {
this.branches.each(function(branch) {
toReturn.push({
id: branch.get('id'),
selected: this.HEAD.get('target') === branch,
@ -669,6 +669,7 @@ GitEngine.prototype.deleteBranch = function(name) {
target.delete();
delete this.refs[id];
// delete from array
// TODO
var toDelete = -1;
_.each(this.branches, function(branch, index) {
console.log(branch);

View file

@ -15,6 +15,7 @@ $(document).ready(function(){
// the two major collections that affect everything
var commitCollection = new CommitCollection();
commandCollection = new CommandCollection();
var branchCollection = new BranchCollection();
commandBuffer = new CommandBuffer({
collection: commandCollection
@ -30,11 +31,13 @@ $(document).ready(function(){
});
gitVisuals = new GitVisuals({
collection: commitCollection
commitCollection: commitCollection,
branchCollection: branchCollection
});
gitEngine = new GitEngine({
collection: commitCollection
collection: commitCollection,
branches: branchCollection
});
$('#commandTextField').focus();

View file

@ -1,3 +1,60 @@
var VisBranch = Backbone.Model.extend({
defaults: {
pos: null
},
validateAtInit: function() {
if (!this.get('branch')) {
throw new Error('need a branch!');
}
},
initialize: function() {
this.validateAtInit();
},
getPosition: function() {
var commit = this.get('branch').get('target');
var visNode = commit.get('visNode');
var pos = visNode.getScreenCoords();
return {
x: pos.x + 30,
y: pos.y
};
},
genGraphics: function(paper) {
var pos = this.getPosition();
if (!paper) {
console.log('no paper');
return;
}
var name = this.get('branch').get('id');
var circle = paper.text(pos.x, pos.y, String(name));
this.set('text', circle);
},
animateUpdatedPos: function(paper) {
var pos = this.getPosition();
var t = this.get('text');
if (!t) {
this.genGraphics(paper);
t = this.get('text');
// TODO HACKY
}
this.get('text').toFront().stop().animate({
x: pos.x,
y: pos.y
},
300,
'easeInOut'
);
}
});
var VisNode = Backbone.Model.extend({
defaults: {
depth: undefined,
@ -163,3 +220,7 @@ var VisEdge = Backbone.Model.extend({
var VisEdgeCollection = Backbone.Collection.extend({
model: VisEdge
});
var VisBranchCollection = Backbone.Collection.extend({
model: VisBranch
});

View file

@ -1,7 +1,10 @@
function GitVisuals(options) {
this.commitCollection = options.collection;
this.commitCollection = options.commitCollection;
this.branchCollection = options.branchCollection;
this.visNodeMap = {};
this.edgeCollection = new VisEdgeCollection();
this.visBranchCollection = new VisBranchCollection();
this.commitMap = {};
this.rootCommit = null;
@ -12,6 +15,9 @@ function GitVisuals(options) {
this.commitCollection.on('change', this.collectionChanged, this);
this.branchCollection.on('add', this.addBranch, this);
this.branchCollection.on('remove', this.removeBranch, this);
events.on('canvasResize', _.bind(
this.canvasResize, this
));
@ -70,6 +76,7 @@ GitVisuals.prototype.refreshTree = function() {
this.calculateTreeCoords();
this.animateNodePositions();
this.animateEdges();
this.animateRefs();
};
GitVisuals.prototype.calculateTreeCoords = function() {
@ -104,8 +111,6 @@ GitVisuals.prototype.assignBoundsRecursive = function(commit, min, max) {
// I always center myself within my bounds
var myWidthPos = (min + max) / 2.0;
commit.get('visNode').get('pos').x = myWidthPos;
// TODO get rid of
// commit.get('visNode').get('pos').x = Math.random();
if (commit.get('children').length == 0) {
return;
@ -167,6 +172,22 @@ GitVisuals.prototype.animateNodePositions = function() {
}, this);
};
GitVisuals.prototype.addBranch = function(branch) {
var visBranch = new VisBranch({
branch: branch
});
this.visBranchCollection.add(visBranch);
if (this.paperReady) {
visBranch.genGraphics();
}
};
GitVisuals.prototype.animateRefs = function() {
this.visBranchCollection.each(function(visBranch) {
visBranch.animateUpdatedPos(paper);
}, this);
};
GitVisuals.prototype.animateEdges = function() {
this.edgeCollection.each(function(edge) {
edge.animateUpdatedPath();
@ -254,6 +275,10 @@ GitVisuals.prototype.drawTreeFirstTime = function() {
this.edgeCollection.each(function(edge) {
edge.genGraphics(paper);
}, this);
this.visBranchCollection.each(function(visBranch) {
visBranch.genGraphics(paper);
}, this);
};