mirror of
https://github.com/pcottle/learnGitBranching.git
synced 2025-07-17 17:23:41 +02:00
Merge pull request #519 from Hongarc/underscore
Replace function of `underscore` with simple function
This commit is contained in:
commit
8b71c1271e
7 changed files with 52 additions and 18 deletions
17
src/js/util/debounce.js
Normal file
17
src/js/util/debounce.js
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
module.exports = function(func, time, immediate) {
|
||||||
|
var timeout;
|
||||||
|
return function() {
|
||||||
|
var later = function() {
|
||||||
|
timeout = null;
|
||||||
|
if (!immediate) {
|
||||||
|
func.apply(this, arguments);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var callNow = immediate && !timeout;
|
||||||
|
clearTimeout(timeout);
|
||||||
|
timeout = setTimeout(later, time);
|
||||||
|
if (callNow) {
|
||||||
|
func.apply(this, arguments);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
13
src/js/util/throttle.js
Normal file
13
src/js/util/throttle.js
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
module.exports = function(func, time) {
|
||||||
|
var wait = false;
|
||||||
|
return function() {
|
||||||
|
if (!wait) {
|
||||||
|
func.apply(this, arguments);
|
||||||
|
wait = true;
|
||||||
|
|
||||||
|
setTimeout(function() {
|
||||||
|
wait = false;
|
||||||
|
}, time);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
|
@ -2,6 +2,7 @@ var _ = require('underscore');
|
||||||
var Q = require('q');
|
var Q = require('q');
|
||||||
|
|
||||||
var Views = require('../views');
|
var Views = require('../views');
|
||||||
|
var throttle = require('../util/throttle');
|
||||||
var ModalTerminal = Views.ModalTerminal;
|
var ModalTerminal = Views.ModalTerminal;
|
||||||
var ContainedBase = Views.ContainedBase;
|
var ContainedBase = Views.ContainedBase;
|
||||||
|
|
||||||
|
@ -97,7 +98,7 @@ var MarkdownGrabber = ContainedBase.extend({
|
||||||
|
|
||||||
keyup: function() {
|
keyup: function() {
|
||||||
if (!this.throttledPreview) {
|
if (!this.throttledPreview) {
|
||||||
this.throttledPreview = _.throttle(
|
this.throttledPreview = throttle(
|
||||||
this.updatePreview.bind(this),
|
this.updatePreview.bind(this),
|
||||||
500
|
500
|
||||||
);
|
);
|
||||||
|
|
|
@ -7,6 +7,8 @@ var intl = require('../intl');
|
||||||
var log = require('../log');
|
var log = require('../log');
|
||||||
var Constants = require('../util/constants');
|
var Constants = require('../util/constants');
|
||||||
var KeyboardListener = require('../util/keyboard').KeyboardListener;
|
var KeyboardListener = require('../util/keyboard').KeyboardListener;
|
||||||
|
var debounce = require('../util/debounce');
|
||||||
|
var throttle = require('../util/throttle');
|
||||||
|
|
||||||
var BaseView = Backbone.View.extend({
|
var BaseView = Backbone.View.extend({
|
||||||
getDestination: function() {
|
getDestination: function() {
|
||||||
|
@ -106,7 +108,7 @@ var GeneralButton = ContainedBase.extend({
|
||||||
|
|
||||||
click: function() {
|
click: function() {
|
||||||
if (!this.clickFunc) {
|
if (!this.clickFunc) {
|
||||||
this.clickFunc = _.throttle(
|
this.clickFunc = throttle(
|
||||||
this.sendClick.bind(this),
|
this.sendClick.bind(this),
|
||||||
500
|
500
|
||||||
);
|
);
|
||||||
|
@ -580,7 +582,7 @@ var CanvasTerminalHolder = BaseView.extend({
|
||||||
|
|
||||||
// If the entire window gets resized such that the terminal is outside the view, then
|
// If the entire window gets resized such that the terminal is outside the view, then
|
||||||
// move it back into the view, and expand/shrink it vertically as necessary.
|
// move it back into the view, and expand/shrink it vertically as necessary.
|
||||||
$(window).on('resize', _.debounce(this.recalcLayout.bind(this), 300));
|
$(window).on('resize', debounce(this.recalcLayout.bind(this), 300));
|
||||||
|
|
||||||
if (options.additionalClass) {
|
if (options.additionalClass) {
|
||||||
this.$el.addClass(options.additionalClass);
|
this.$el.addClass(options.additionalClass);
|
||||||
|
|
|
@ -4,6 +4,7 @@ var Backbone = require('backbone');
|
||||||
var LocaleStore = require('../stores/LocaleStore');
|
var LocaleStore = require('../stores/LocaleStore');
|
||||||
|
|
||||||
var util = require('../util');
|
var util = require('../util');
|
||||||
|
var debounce = require('../util/debounce');
|
||||||
var intl = require('../intl');
|
var intl = require('../intl');
|
||||||
var log = require('../log');
|
var log = require('../log');
|
||||||
var KeyboardListener = require('../util/keyboard').KeyboardListener;
|
var KeyboardListener = require('../util/keyboard').KeyboardListener;
|
||||||
|
@ -41,7 +42,7 @@ var LevelDropdownView = ContainedBase.extend({
|
||||||
};
|
};
|
||||||
|
|
||||||
this.navEvents = Object.assign({}, Backbone.Events);
|
this.navEvents = Object.assign({}, Backbone.Events);
|
||||||
this.navEvents.on('clickedID', _.debounce(
|
this.navEvents.on('clickedID', debounce(
|
||||||
this.loadLevelID.bind(this),
|
this.loadLevelID.bind(this),
|
||||||
300,
|
300,
|
||||||
true
|
true
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
var _ = require('underscore');
|
|
||||||
var Q = require('q');
|
var Q = require('q');
|
||||||
var Backbone = require('backbone');
|
var Backbone = require('backbone');
|
||||||
|
|
||||||
|
@ -10,6 +9,7 @@ var BuilderViews = require('../views/builderViews');
|
||||||
var MarkdownPresenter = BuilderViews.MarkdownPresenter;
|
var MarkdownPresenter = BuilderViews.MarkdownPresenter;
|
||||||
|
|
||||||
var KeyboardListener = require('../util/keyboard').KeyboardListener;
|
var KeyboardListener = require('../util/keyboard').KeyboardListener;
|
||||||
|
var debounce = require('../util/debounce');
|
||||||
|
|
||||||
var MultiView = Backbone.View.extend({
|
var MultiView = Backbone.View.extend({
|
||||||
tagName: 'div',
|
tagName: 'div',
|
||||||
|
@ -84,13 +84,13 @@ var MultiView = Backbone.View.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
getPosFunc: function() {
|
getPosFunc: function() {
|
||||||
return _.debounce(function() {
|
return debounce(function() {
|
||||||
this.navForward();
|
this.navForward();
|
||||||
}.bind(this), this.navEventDebounce, true);
|
}.bind(this), this.navEventDebounce, true);
|
||||||
},
|
},
|
||||||
|
|
||||||
getNegFunc: function() {
|
getNegFunc: function() {
|
||||||
return _.debounce(function() {
|
return debounce(function() {
|
||||||
this.navBackward();
|
this.navBackward();
|
||||||
}.bind(this), this.navEventDebounce, true);
|
}.bind(this), this.navEventDebounce, true);
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
var _ = require('underscore');
|
|
||||||
var Q = require('q');
|
var Q = require('q');
|
||||||
|
|
||||||
var intl = require('../intl');
|
var intl = require('../intl');
|
||||||
var GRAPHICS = require('../util/constants').GRAPHICS;
|
var GRAPHICS = require('../util/constants').GRAPHICS;
|
||||||
|
var debounce = require('../util/debounce');
|
||||||
var GlobalStateStore = require('../stores/GlobalStateStore');
|
var GlobalStateStore = require('../stores/GlobalStateStore');
|
||||||
|
|
||||||
var VisNode = require('../visuals/visNode').VisNode;
|
var VisNode = require('../visuals/visNode').VisNode;
|
||||||
|
@ -82,7 +82,7 @@ GitVisuals.prototype.resetAll = function() {
|
||||||
visTag.remove();
|
visTag.remove();
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
_.each(this.visNodeMap, function(visNode) {
|
Object.values(this.visNodeMap).forEach(function(visNode) {
|
||||||
visNode.remove();
|
visNode.remove();
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
|
@ -206,7 +206,7 @@ GitVisuals.prototype.animateAllAttrKeys = function(keys, attr, speed, easing) {
|
||||||
this.visBranchCollection.each(animate);
|
this.visBranchCollection.each(animate);
|
||||||
this.visEdgeCollection.each(animate);
|
this.visEdgeCollection.each(animate);
|
||||||
this.visTagCollection.each(animate);
|
this.visTagCollection.each(animate);
|
||||||
_.each(this.visNodeMap, animate);
|
Object.values(this.visNodeMap).forEach(animate);
|
||||||
|
|
||||||
var time = (speed !== undefined) ? speed : GRAPHICS.defaultAnimationTime;
|
var time = (speed !== undefined) ? speed : GRAPHICS.defaultAnimationTime;
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
|
@ -321,7 +321,7 @@ GitVisuals.prototype.finishAnimation = function(speed) {
|
||||||
GitVisuals.prototype.explodeNodes = function(speed) {
|
GitVisuals.prototype.explodeNodes = function(speed) {
|
||||||
var deferred = Q.defer();
|
var deferred = Q.defer();
|
||||||
var funcs = [];
|
var funcs = [];
|
||||||
_.each(this.visNodeMap, function(visNode) {
|
Object.values(this.visNodeMap).forEach(function(visNode) {
|
||||||
funcs.push(visNode.getExplodeStepFunc(speed));
|
funcs.push(visNode.getExplodeStepFunc(speed));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -368,7 +368,7 @@ GitVisuals.prototype.animateAllFromAttrToAttr = function(fromSnapshot, toSnapsho
|
||||||
this.visBranchCollection.each(animate);
|
this.visBranchCollection.each(animate);
|
||||||
this.visEdgeCollection.each(animate);
|
this.visEdgeCollection.each(animate);
|
||||||
this.visTagCollection.each(animate);
|
this.visTagCollection.each(animate);
|
||||||
_.each(this.visNodeMap, animate);
|
Object.values(this.visNodeMap).forEach(animate);
|
||||||
};
|
};
|
||||||
|
|
||||||
/***************************************
|
/***************************************
|
||||||
|
@ -392,7 +392,7 @@ GitVisuals.prototype.genSnapshot = function() {
|
||||||
this.fullCalc();
|
this.fullCalc();
|
||||||
|
|
||||||
var snapshot = {};
|
var snapshot = {};
|
||||||
_.each(this.visNodeMap, function(visNode) {
|
Object.values(this.visNodeMap).forEach(function(visNode) {
|
||||||
snapshot[visNode.get('id')] = visNode.getAttributes();
|
snapshot[visNode.get('id')] = visNode.getAttributes();
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
|
@ -632,7 +632,7 @@ GitVisuals.prototype.calcDepth = function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
var depthIncrement = this.getDepthIncrement(maxDepth);
|
var depthIncrement = this.getDepthIncrement(maxDepth);
|
||||||
_.each(this.visNodeMap, function(visNode) {
|
Object.values(this.visNodeMap).forEach(function(visNode) {
|
||||||
visNode.setDepthBasedOn(depthIncrement, this.getHeaderOffset());
|
visNode.setDepthBasedOn(depthIncrement, this.getHeaderOffset());
|
||||||
}, this);
|
}, this);
|
||||||
};
|
};
|
||||||
|
@ -655,7 +655,7 @@ GitVisuals.prototype.calcDepth = function() {
|
||||||
**************************************/
|
**************************************/
|
||||||
|
|
||||||
GitVisuals.prototype.animateNodePositions = function(speed) {
|
GitVisuals.prototype.animateNodePositions = function(speed) {
|
||||||
_.each(this.visNodeMap, function(visNode) {
|
Object.values(this.visNodeMap).forEach(function(visNode) {
|
||||||
visNode.animateUpdatedPosition(speed);
|
visNode.animateUpdatedPosition(speed);
|
||||||
}, this);
|
}, this);
|
||||||
};
|
};
|
||||||
|
@ -795,7 +795,7 @@ GitVisuals.prototype.canvasResize = function(width, height) {
|
||||||
};
|
};
|
||||||
|
|
||||||
GitVisuals.prototype.genResizeFunc = function() {
|
GitVisuals.prototype.genResizeFunc = function() {
|
||||||
this.resizeFunc = _.debounce(
|
this.resizeFunc = debounce(
|
||||||
function(width, height) {
|
function(width, height) {
|
||||||
this.refreshTree();
|
this.refreshTree();
|
||||||
}.bind(this),
|
}.bind(this),
|
||||||
|
@ -853,7 +853,7 @@ GitVisuals.prototype.zIndexReflow = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
GitVisuals.prototype.visNodesFront = function() {
|
GitVisuals.prototype.visNodesFront = function() {
|
||||||
_.each(this.visNodeMap, function(visNode) {
|
Object.values(this.visNodeMap).forEach(function(visNode) {
|
||||||
visNode.toFront();
|
visNode.toFront();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -892,7 +892,7 @@ GitVisuals.prototype.drawTreeFirstTime = function() {
|
||||||
this.gitReady = true;
|
this.gitReady = true;
|
||||||
this.calcTreeCoords();
|
this.calcTreeCoords();
|
||||||
|
|
||||||
_.each(this.visNodeMap, function(visNode) {
|
Object.values(this.visNodeMap).forEach(function(visNode) {
|
||||||
visNode.genGraphics(this.paper);
|
visNode.genGraphics(this.paper);
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue