|
|
var Backbone = require('backbone'); |
|
|
|
|
|
var Collections = require('../models/collections'); |
|
|
var CommitCollection = Collections.CommitCollection; |
|
|
var BranchCollection = Collections.BranchCollection; |
|
|
var TagCollection = Collections.TagCollection; |
|
|
var EventBaton = require('../util/eventBaton').EventBaton; |
|
|
|
|
|
var GitVisuals = require('../visuals').GitVisuals; |
|
|
|
|
|
var Visualization = Backbone.View.extend({ |
|
|
initialize: function(options) { |
|
|
options = options || {}; |
|
|
this.options = options; |
|
|
this.customEvents = Object.assign({}, Backbone.Events); |
|
|
this.containerElement = options.containerElement; |
|
|
|
|
|
var _this = this; |
|
|
|
|
|
var container = options.containerElement || $('#canvasHolder')[0]; |
|
|
new Raphael(container, 200, 200, function() { |
|
|
|
|
|
var paper = this; |
|
|
|
|
|
process.nextTick(function() { |
|
|
_this.paperInitialize(paper, options); |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
|
|
|
paperInitialize: function(paper, options) { |
|
|
this.treeString = options.treeString; |
|
|
this.paper = paper; |
|
|
|
|
|
var Main = require('../app'); |
|
|
|
|
|
|
|
|
|
|
|
this.eventBaton = (options.noKeyboardInput) ? |
|
|
new EventBaton({noInput: true}) : |
|
|
Main.getEventBaton(); |
|
|
|
|
|
this.commitCollection = new CommitCollection(); |
|
|
this.branchCollection = new BranchCollection(); |
|
|
this.tagCollection = new TagCollection(); |
|
|
|
|
|
this.gitVisuals = new GitVisuals({ |
|
|
commitCollection: this.commitCollection, |
|
|
branchCollection: this.branchCollection, |
|
|
tagCollection: this.tagCollection, |
|
|
paper: this.paper, |
|
|
noClick: this.options.noClick, |
|
|
isGoalVis: this.options.isGoalVis, |
|
|
smallCanvas: this.options.smallCanvas, |
|
|
visualization: this |
|
|
}); |
|
|
|
|
|
var GitEngine = require('../git').GitEngine; |
|
|
this.gitEngine = new GitEngine({ |
|
|
collection: this.commitCollection, |
|
|
branches: this.branchCollection, |
|
|
tags: this.tagCollection, |
|
|
gitVisuals: this.gitVisuals, |
|
|
eventBaton: this.eventBaton |
|
|
}); |
|
|
this.gitEngine.init(); |
|
|
this.gitVisuals.assignGitEngine(this.gitEngine); |
|
|
|
|
|
this.myResize(); |
|
|
|
|
|
$(window).on('resize', () => this.myResize()); |
|
|
|
|
|
|
|
|
|
|
|
this.$el.parents('.ui-draggable').on('drag', function(event, ui) { |
|
|
this.customEvents.trigger('drag', event, ui); |
|
|
this.myResize(); |
|
|
}.bind(this)); |
|
|
|
|
|
this.gitVisuals.drawTreeFirstTime(); |
|
|
if (this.treeString) { |
|
|
this.gitEngine.loadTreeFromString(this.treeString); |
|
|
} |
|
|
if (this.options.zIndex) { |
|
|
this.setTreeIndex(this.options.zIndex); |
|
|
} |
|
|
|
|
|
this.shown = false; |
|
|
this.setTreeOpacity(0); |
|
|
|
|
|
process.nextTick(this.fadeTreeIn.bind(this)); |
|
|
|
|
|
this.customEvents.trigger('gitEngineReady'); |
|
|
this.customEvents.trigger('paperReady'); |
|
|
}, |
|
|
|
|
|
clearOrigin: function() { |
|
|
delete this.originVis; |
|
|
}, |
|
|
|
|
|
makeOrigin: function(options) { |
|
|
|
|
|
|
|
|
this.originVis = new Visualization(Object.assign( |
|
|
{}, |
|
|
|
|
|
this.options, |
|
|
{ |
|
|
|
|
|
noKeyboardInput: true, |
|
|
noClick: true, |
|
|
treeString: options.treeString |
|
|
} |
|
|
)); |
|
|
|
|
|
this.originVis.customEvents.on('paperReady', function() { |
|
|
var value = $(this.paper.canvas).css('z-index'); |
|
|
this.originVis.setTreeIndex(value); |
|
|
}.bind(this)); |
|
|
|
|
|
|
|
|
return this.originVis; |
|
|
}, |
|
|
|
|
|
originToo: function(methodToCall, args) { |
|
|
if (!this.originVis) { |
|
|
return; |
|
|
} |
|
|
var callMethod = function() { |
|
|
this.originVis[methodToCall].apply(this.originVis, args); |
|
|
}.bind(this); |
|
|
|
|
|
if (this.originVis.paper) { |
|
|
callMethod(); |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.originVis.customEvents.on('paperReady', callMethod); |
|
|
}, |
|
|
|
|
|
setTreeIndex: function(level) { |
|
|
$(this.paper.canvas).css('z-index', level); |
|
|
this.originToo('setTreeIndex', arguments); |
|
|
}, |
|
|
|
|
|
setTreeOpacity: function(level) { |
|
|
if (level === 0) { |
|
|
this.shown = false; |
|
|
} |
|
|
|
|
|
$(this.paper.canvas).css('opacity', level); |
|
|
this.originToo('setTreeOpacity', arguments); |
|
|
}, |
|
|
|
|
|
getAnimationTime: function() { return 300; }, |
|
|
|
|
|
fadeTreeIn: function() { |
|
|
this.shown = true; |
|
|
if (!this.paper) { |
|
|
return; |
|
|
} |
|
|
$(this.paper.canvas).animate({opacity: 1}, this.getAnimationTime()); |
|
|
|
|
|
this.originToo('fadeTreeIn', arguments); |
|
|
}, |
|
|
|
|
|
fadeTreeOut: function() { |
|
|
this.shown = false; |
|
|
if (this.paper && this.paper.canvas) { |
|
|
$(this.paper.canvas).animate({opacity: 0}, this.getAnimationTime()); |
|
|
} |
|
|
this.originToo('fadeTreeOut', arguments); |
|
|
}, |
|
|
|
|
|
hide: function() { |
|
|
this.fadeTreeOut(); |
|
|
|
|
|
setTimeout(function() { |
|
|
$(this.paper.canvas).css('visibility', 'hidden'); |
|
|
}.bind(this), this.getAnimationTime()); |
|
|
this.originToo('hide', arguments); |
|
|
}, |
|
|
|
|
|
show: function() { |
|
|
$(this.paper.canvas).css('visibility', 'visible'); |
|
|
setTimeout(this.fadeTreeIn.bind(this), 10); |
|
|
this.originToo('show', arguments); |
|
|
this.myResize(); |
|
|
}, |
|
|
|
|
|
showHarsh: function() { |
|
|
$(this.paper.canvas).css('visibility', 'visible'); |
|
|
this.setTreeOpacity(1); |
|
|
this.originToo('showHarsh', arguments); |
|
|
this.myResize(); |
|
|
}, |
|
|
|
|
|
resetFromThisTreeNow: function(treeString) { |
|
|
this.treeString = treeString; |
|
|
|
|
|
var oTree = this.getOriginInTreeString(treeString); |
|
|
if (oTree) { |
|
|
var oTreeString = this.gitEngine.printTree(oTree); |
|
|
this.originToo('resetFromThisTreeNow', [oTreeString]); |
|
|
} |
|
|
}, |
|
|
|
|
|
getOriginInTreeString: function(treeString) { |
|
|
var tree = JSON.parse(unescape(treeString)); |
|
|
return tree.originTree; |
|
|
}, |
|
|
|
|
|
reset: function(tree) { |
|
|
var treeString = tree || this.treeString; |
|
|
this.setTreeOpacity(0); |
|
|
if (treeString) { |
|
|
this.gitEngine.loadTreeFromString(treeString); |
|
|
} else { |
|
|
this.gitEngine.defaultInit(); |
|
|
} |
|
|
this.fadeTreeIn(); |
|
|
|
|
|
if (this.originVis) { |
|
|
if (treeString) { |
|
|
var oTree = this.getOriginInTreeString(treeString); |
|
|
this.originToo('reset', [JSON.stringify(oTree)]); |
|
|
} else { |
|
|
|
|
|
this.originToo('reset', arguments); |
|
|
} |
|
|
} |
|
|
}, |
|
|
|
|
|
tearDown: function(options) { |
|
|
options = options || {}; |
|
|
|
|
|
this.gitEngine.tearDown(); |
|
|
this.gitVisuals.tearDown(); |
|
|
delete this.paper; |
|
|
this.originToo('tearDown', arguments); |
|
|
}, |
|
|
|
|
|
die: function() { |
|
|
this.fadeTreeOut(); |
|
|
setTimeout(function() { |
|
|
if (!this.shown) { |
|
|
this.tearDown({fromDie: true}); |
|
|
} |
|
|
}.bind(this), this.getAnimationTime()); |
|
|
this.originToo('die', arguments); |
|
|
}, |
|
|
|
|
|
myResize: function() { |
|
|
if (!this.paper) { return; } |
|
|
|
|
|
var el = this.el; |
|
|
|
|
|
var elSize = el.getBoundingClientRect(); |
|
|
var width = elSize.width; |
|
|
var height = elSize.height; |
|
|
|
|
|
|
|
|
|
|
|
if (!this.containerElement) { |
|
|
var left = this.$el.offset().left; |
|
|
var top = this.$el.offset().top; |
|
|
|
|
|
$(this.paper.canvas).css({ |
|
|
position: 'absolute', |
|
|
left: left + 'px', |
|
|
top: top + 'px' |
|
|
}); |
|
|
} else { |
|
|
|
|
|
$(this.paper.canvas).css({ |
|
|
position: 'absolute' |
|
|
}); |
|
|
} |
|
|
|
|
|
this.paper.setSize(width, height); |
|
|
this.gitVisuals.canvasResize(width, height); |
|
|
this.originToo('myResize', arguments); |
|
|
} |
|
|
}); |
|
|
|
|
|
exports.Visualization = Visualization; |
|
|
|