|
1 | 1 | /* ==========================================================
|
2 |
| - * GitGraph v1.2.2 |
| 2 | + * GitGraph v1.2.3 |
3 | 3 | * https://github.com/nicoespeon/gitgraph.js
|
4 | 4 | * ==========================================================
|
5 | 5 | * Copyright (c) 2016 Nicolas CARLO (@nicoespeon) ٩(^‿^)۶
|
|
381 | 381 | * @this GitGraph
|
382 | 382 | **/
|
383 | 383 | GitGraph.prototype.applyCommits = function ( event, callbackFn ) {
|
| 384 | + // Fallback onto layerX/layerY for older versions of Firefox. |
| 385 | + var offsetX = event.offsetX || event.layerX; |
| 386 | + var offsetY = event.offsetY || event.layerY; |
| 387 | + |
384 | 388 | for ( var i = 0, commit; !!(commit = this.commits[ i ]); i++ ) {
|
385 |
| - var distanceX = (commit.x + (this.offsetX + this.marginX) / this.scalingFactor - event.offsetX); |
386 |
| - var distanceY = (commit.y + (this.offsetY + this.marginY) / this.scalingFactor - event.offsetY); |
| 389 | + var distanceX = (commit.x + (this.offsetX + this.marginX) / this.scalingFactor - offsetX); |
| 390 | + var distanceY = (commit.y + (this.offsetY + this.marginY) / this.scalingFactor - offsetY); |
387 | 391 | var distanceBetweenCommitCenterAndMouse = Math.sqrt( Math.pow( distanceX, 2 ) + Math.pow( distanceY, 2 ) );
|
388 | 392 | var isOverCommit = distanceBetweenCommitCenterAndMouse < this.template.commit.dot.size;
|
389 | 393 |
|
|
487 | 491 | *
|
488 | 492 | * @param {Object} options - Options of branch
|
489 | 493 | * @param {GitGraph} options.parent - GitGraph constructor
|
490 |
| - * @param {Branch} [options.parentBranch] - Parent branch |
| 494 | + * @param {Branch} [options.parentBranch = options.parentCommit.branch] - Parent branch |
| 495 | + * @param {Commit} [options.parentCommit = options.parentBranch.commits.slice(-1)[0]] - Parent commit |
491 | 496 | * @param {String} [options.name = "no-name"] - Branch name
|
492 | 497 | *
|
493 | 498 | * @this Branch
|
|
501 | 506 | // Options
|
502 | 507 | options = (typeof options === "object") ? options : {};
|
503 | 508 | this.parent = options.parent;
|
504 |
| - this.parentBranch = options.parentBranch; |
| 509 | + if ( options.parentCommit && options.parentBranch ) { |
| 510 | + if ( options.parentCommit.branch !== options.parentBranch ) { |
| 511 | + return; |
| 512 | + } |
| 513 | + this.parentCommit = options.parentCommit; |
| 514 | + this.parentBranch = options.parentBranch; |
| 515 | + } else if ( options.parentCommit ) { |
| 516 | + this.parentCommit = options.parentCommit; |
| 517 | + this.parentBranch = options.parentCommit.branch; |
| 518 | + } else if ( options.parentBranch ) { |
| 519 | + this.parentCommit = options.parentBranch.commits.slice( -1 )[ 0 ]; |
| 520 | + this.parentBranch = options.parentBranch; |
| 521 | + } else { |
| 522 | + this.parentCommit = null; |
| 523 | + this.parentBranch = null; |
| 524 | + } |
505 | 525 | this.name = (typeof options.name === "string") ? options.name : "no-name";
|
506 | 526 | this.context = this.parent.context;
|
507 | 527 | this.template = this.parent.template;
|
|
530 | 550 | this.offsetX = this.column * this.spacingX;
|
531 | 551 | this.offsetY = this.column * this.spacingY;
|
532 | 552 |
|
| 553 | + // Add start point |
| 554 | + if (this.parentBranch) { |
| 555 | + if ( this.parentCommit === this.parentBranch.commits.slice( -1 )[ 0 ] ) { |
| 556 | + this.startPoint = { |
| 557 | + x: this.parentBranch.offsetX - this.parent.commitOffsetX + this.template.commit.spacingX, |
| 558 | + y: this.parentBranch.offsetY - this.parent.commitOffsetY + this.template.commit.spacingY, |
| 559 | + type: "start" |
| 560 | + }; |
| 561 | + } else { |
| 562 | + this.startPoint = { |
| 563 | + x: this.parentCommit.x, |
| 564 | + y: this.parentCommit.y, |
| 565 | + type: "start" |
| 566 | + }; |
| 567 | + } |
| 568 | + } else { |
| 569 | + this.startPoint = null; |
| 570 | + } |
| 571 | + |
533 | 572 | var columnIndex = (this.column % this.template.colors.length);
|
534 | 573 | this.color = options.color || this.template.branch.color || this.template.colors[ columnIndex ];
|
535 | 574 |
|
|
676 | 715 |
|
677 | 716 | // Fork case: Parent commit from parent branch
|
678 | 717 | if ( options.parentCommit instanceof Commit === false && this.parentBranch instanceof Branch ) {
|
679 |
| - options.parentCommit = this.parentBranch.commits.slice( -1 )[ 0 ]; |
| 718 | + options.parentCommit = this.parentCommit; |
680 | 719 | }
|
681 | 720 |
|
682 | 721 | // First commit
|
683 |
| - var isFirstBranch = options.parentCommit instanceof Commit; |
| 722 | + var isFirstBranch = !( options.parentCommit instanceof Commit ); |
684 | 723 | var isPathBeginning = this.path.length === 0;
|
685 | 724 |
|
686 | 725 | options.showLabel = (isPathBeginning && this.showLabel) ? true : false;
|
|
700 | 739 | type: "join"
|
701 | 740 | };
|
702 | 741 |
|
703 |
| - if ( isFirstBranch && isPathBeginning ) { |
704 |
| - var parent = { |
705 |
| - x: commit.parentCommit.branch.offsetX - this.parent.commitOffsetX + this.template.commit.spacingX, |
706 |
| - y: commit.parentCommit.branch.offsetY - this.parent.commitOffsetY + this.template.commit.spacingY, |
707 |
| - type: "start" |
708 |
| - }; |
709 |
| - this.path.push( JSON.parse( JSON.stringify( parent ) ) ); // Elegant way for cloning an object |
| 742 | + if ( !isFirstBranch && isPathBeginning ) { |
| 743 | + // Start point on parent branch |
| 744 | + this.pushPath( this.startPoint ); |
| 745 | + // Move to this branch |
| 746 | + this.pushPath( { |
| 747 | + x: this.startPoint.x - this.parentBranch.offsetX + this.offsetX - this.template.commit.spacingX, |
| 748 | + y: this.startPoint.y - this.parentBranch.offsetY + this.offsetY - this.template.commit.spacingY, |
| 749 | + type: "join" |
| 750 | + } ); |
| 751 | + |
| 752 | + // Extend parent branch |
| 753 | + var parent = JSON.parse( JSON.stringify( this.startPoint ) ); // Elegant way for cloning an object |
710 | 754 | parent.type = "join";
|
711 |
| - this.parentBranch.path.push( parent ); |
| 755 | + this.parentBranch.pushPath( parent ); |
712 | 756 | } else if ( isPathBeginning ) {
|
713 | 757 | point.type = "start";
|
714 | 758 | }
|
715 | 759 |
|
716 | 760 | // Increment commitOffset for next commit position
|
717 |
| - this.path.push( point ); |
| 761 | + this.pushPath( point ); |
718 | 762 |
|
719 | 763 | this.parent.commitOffsetX += this.template.commit.spacingX * (options.showLabel ? 2 : 1);
|
720 | 764 | this.parent.commitOffsetY += this.template.commit.spacingY * (options.showLabel ? 2 : 1);
|
|
790 | 834 | y: this.offsetY + this.template.commit.spacingY * (targetCommit.showLabel ? 3 : 2) - this.parent.commitOffsetY,
|
791 | 835 | type: "join"
|
792 | 836 | };
|
793 |
| - this.path.push( JSON.parse( JSON.stringify( endOfBranch ) ) ); // Elegant way for cloning an object |
| 837 | + this.pushPath( JSON.parse( JSON.stringify( endOfBranch ) ) ); // Elegant way for cloning an object |
794 | 838 |
|
795 | 839 | var mergeCommit = {
|
796 | 840 | x: targetCommit.x,
|
797 | 841 | y: targetCommit.y,
|
798 | 842 | type: "end"
|
799 | 843 | };
|
800 |
| - this.path.push( mergeCommit ); |
| 844 | + this.pushPath( mergeCommit ); |
801 | 845 |
|
802 | 846 | endOfBranch.type = "start";
|
803 |
| - this.path.push( endOfBranch ); // End of branch for future commits |
| 847 | + this.pushPath( endOfBranch ); // End of branch for future commits |
804 | 848 |
|
805 | 849 | // Auto-render
|
806 | 850 | this.parent.render();
|
|
836 | 880 | }
|
837 | 881 | };
|
838 | 882 |
|
| 883 | + /** |
| 884 | + * Push a new point to path. |
| 885 | + * This method will combine duplicate points and reject reversed points. |
| 886 | + * |
| 887 | + * @this Branch |
| 888 | + */ |
| 889 | + Branch.prototype.pushPath = function (point) { |
| 890 | + var lastPoint = this.path.slice( -1 )[ 0 ]; |
| 891 | + if ( !lastPoint ) { |
| 892 | + this.path.push( point ); |
| 893 | + } else if ( lastPoint.x === point.x && lastPoint.y === point.y ) { |
| 894 | + if ( lastPoint.type !== "start" && point.type === "end" ) { |
| 895 | + lastPoint.type = "end"; |
| 896 | + } else if ( point.type === "join" ) { |
| 897 | + |
| 898 | + } else { |
| 899 | + this.path.push( point ); |
| 900 | + } |
| 901 | + } else { |
| 902 | + if ( point.type === "join" ) { |
| 903 | + if ( ( point.x - lastPoint.x ) * this.template.commit.spacingX < 0 ) { |
| 904 | + this.path.push( point ); |
| 905 | + } else if ( ( point.y - lastPoint.y ) * this.template.commit.spacingY < 0 ) { |
| 906 | + this.path.push( point ); |
| 907 | + } |
| 908 | + } else { |
| 909 | + this.path.push( point ); |
| 910 | + } |
| 911 | + } |
| 912 | + }; |
| 913 | + |
839 | 914 | // --------------------------------------------------------------------
|
840 | 915 | // ----------------------- Commit ------------------------
|
841 | 916 | // --------------------------------------------------------------------
|
|
1036 | 1111 | isReversed
|
1037 | 1112 | && _isVertical( this.parent )
|
1038 | 1113 | && Math.abs( this.y - this.parentCommit.y ) > Math.abs( this.template.commit.spacingY )
|
| 1114 | + ) || ( |
| 1115 | + _isVertical( this.parent ) |
| 1116 | + && commitSpaceDelta > 1 |
1039 | 1117 | );
|
1040 | 1118 | var alphaX = (isArrowVertical)
|
1041 | 1119 | ? 0
|
|
1045 | 1123 | isReversed
|
1046 | 1124 | && _isHorizontal( this.parent )
|
1047 | 1125 | && Math.abs( this.x - this.parentCommit.x ) > Math.abs( this.template.commit.spacingX )
|
| 1126 | + ) || ( |
| 1127 | + _isHorizontal( this.parent ) |
| 1128 | + && commitSpaceDelta > 1 |
1048 | 1129 | );
|
1049 | 1130 | var alphaY = (isArrowHorizontal)
|
1050 | 1131 | ? 0
|
|
0 commit comments