|
1 | 1 | /* ========================================================== |
2 | | - * GitGraph v0.3.0 |
| 2 | + * GitGraph v1.0.0 |
3 | 3 | * https://github.com/nicoespeon/gitgraph.js |
4 | 4 | * ========================================================== |
5 | 5 | * Copyright (c) 2013 Nicolas CARLO (@nicoespeon) ٩(^‿^)۶ |
|
9 | 9 | * ========================================================== */ |
10 | 10 |
|
11 | 11 | (function () { |
| 12 | + "use strict"; |
12 | 13 | /** |
13 | 14 | * GitGraph |
14 | 15 | * |
|
20 | 21 | * @param {String} [options.author = "Sergio Flores <[email protected]>"] - Default author for commits |
21 | 22 | * @param {String} [options.mode = (null|"compact")] - Display mode |
22 | 23 | * @param {DOM} [options.canvas] - DOM canvas (ex: document.getElementById("id")) |
23 | | - * @param {Boolean} [options.testMode] - Active test mode for Jasmine |
24 | 24 | * @param {String} [options.orientation = ("vertical-reverse"|"horizontal"|"horizontal-reverse")] - Graph orientation |
25 | 25 | * |
26 | 26 | * @this GitGraph |
|
39 | 39 | options.template = this.newTemplate(options.template); |
40 | 40 | } |
41 | 41 | this.template = (options.template instanceof Template) ? |
42 | | - options.template : this.newTemplate(); |
| 42 | + options.template : this.newTemplate("metro"); |
43 | 43 | this.mode = options.mode || null; |
44 | 44 | if (this.mode === "compact") { |
45 | 45 | this.template.commit.message.display = false; |
|
53 | 53 | switch (options.orientation) { |
54 | 54 | case "vertical-reverse" : |
55 | 55 | this.template.commit.spacingY *= -1; |
| 56 | + this.orientation = "verticale-reverse"; |
56 | 57 | break; |
57 | 58 | case "horizontal" : |
58 | 59 | this.template.commit.message.display = false; |
59 | 60 | this.template.commit.spacingX = this.template.commit.spacingY; |
60 | 61 | this.template.branch.spacingY = this.template.branch.spacingX; |
61 | 62 | this.template.commit.spacingY = 0; |
62 | 63 | this.template.branch.spacingX = 0; |
| 64 | + this.orientation = "horizontal"; |
63 | 65 | break; |
64 | 66 | case "horizontal-reverse" : |
65 | 67 | this.template.commit.message.display = false; |
66 | 68 | this.template.commit.spacingX = - this.template.commit.spacingY; |
67 | 69 | this.template.branch.spacingY = this.template.branch.spacingX; |
68 | 70 | this.template.commit.spacingY = 0; |
69 | 71 | this.template.branch.spacingX = 0; |
| 72 | + this.orientation = "horizontale-reverse"; |
| 73 | + break; |
| 74 | + default: |
| 75 | + this.orientation = "vertical"; |
70 | 76 | break; |
71 | 77 | } |
72 | 78 |
|
|
80 | 86 | this.tooltip.style.position = "fixed"; |
81 | 87 | this.tooltip.style.display = "none"; |
82 | 88 |
|
83 | | - if (!options.testMode) { |
84 | | - // Add tooltip div into body |
85 | | - document.body.appendChild(this.tooltip); |
86 | | - } |
| 89 | + // Add tooltip div into body |
| 90 | + document.body.appendChild(this.tooltip); |
87 | 91 |
|
88 | 92 | // Navigation vars |
89 | 93 | this.HEAD = null; |
|
103 | 107 | gitgraph: this |
104 | 108 | }, false); |
105 | 109 | } |
| 110 | + |
| 111 | + // Render on window resize |
| 112 | + var self = this; |
| 113 | + window.onresize = function (event) { |
| 114 | + self.render(); |
| 115 | + }; |
| 116 | + |
106 | 117 | } |
107 | 118 |
|
108 | 119 | /** |
109 | 120 | * Create new branch |
110 | 121 | * |
111 | 122 | * @param {(String | Object)} options - Branch name | Options of Branch |
112 | 123 | * @see Branch |
| 124 | + * @return {Branch} New branch |
113 | 125 | * @this GitGraph |
114 | 126 | **/ |
115 | 127 | GitGraph.prototype.branch = function (options) { |
|
132 | 144 | return branch; |
133 | 145 | }; |
134 | 146 |
|
| 147 | + /** |
| 148 | + * Create new orphan branch |
| 149 | + * |
| 150 | + * @param {(String | Object)} options - Branch name | Options of Branch |
| 151 | + * @return {Branch} New branch |
| 152 | + * @see Branch |
| 153 | + * @this GitGraph |
| 154 | + **/ |
135 | 155 | GitGraph.prototype.orphanBranch = function (options) { |
136 | 156 | // Options |
137 | 157 | if (typeof options === "string") { |
|
232 | 252 | var test = 0; |
233 | 253 | var out = true; // Flag for hide tooltip |
234 | 254 |
|
| 255 | + // Fix firefox MouseEvent |
| 256 | + event.offsetX = event.offsetX ? event.offsetX : event.layerX; |
| 257 | + event.offsetY = event.offsetY ? event.offsetY : event.layerY; |
| 258 | + event.x = event.x ? event.x : event.clientX; |
| 259 | + event.y = event.y ? event.y : event.clientY; |
| 260 | + |
235 | 261 | for (var i = 0, commit; !! (commit = this.gitgraph.commits[i]); i++) { |
236 | 262 | test = Math.sqrt((commit.x + self.offsetX + self.marginX - event.offsetX) * (commit.x + self.offsetX + self.marginX - event.offsetX) + (commit.y + self.offsetY +self.marginY - event.offsetY) * (commit.y + self.offsetY + self.marginY - event.offsetY)); // Distance between commit and mouse (Pythagore) |
237 | 263 | if (test < self.template.commit.dot.size) { |
|
302 | 328 | this.checkout(); |
303 | 329 | } |
304 | 330 |
|
| 331 | + /** |
| 332 | + * Create new branch |
| 333 | + * |
| 334 | + * @param {(String | Object)} options - Branch name | Options of Branch |
| 335 | + * @see Branch |
| 336 | + * @return {Branch} New Branch |
| 337 | + * @this Branch |
| 338 | + **/ |
| 339 | + Branch.prototype.branch = function (options) { |
| 340 | + // Options |
| 341 | + if (typeof options === "string") { |
| 342 | + var name = options; |
| 343 | + options = {}; |
| 344 | + options.name = name; |
| 345 | + } |
| 346 | + |
| 347 | + options = (typeof options === "object") ? options : {}; |
| 348 | + options.parent = this.parent; |
| 349 | + options.parentBranch = options.parentBranch || this; |
| 350 | + |
| 351 | + // Add branch |
| 352 | + var branch = new Branch(options); |
| 353 | + this.parent.branchs.push(branch); |
| 354 | + |
| 355 | + // Return |
| 356 | + return branch; |
| 357 | + }; |
| 358 | + |
305 | 359 | /** |
306 | 360 | * Render the branch |
307 | 361 | * |
|
335 | 389 | * Add a commit |
336 | 390 | * |
337 | 391 | * @param {(String | Object)} [options] - Message | Options of commit |
| 392 | + * @param {String} [options.detailId] - Id of detail DOM Element |
338 | 393 | * @see Commit |
339 | 394 | * @this Branch |
340 | 395 | **/ |
|
369 | 424 | options.x = this.offsetX - this.parent.commitOffsetX; |
370 | 425 | options.y = this.offsetY - this.parent.commitOffsetY; |
371 | 426 |
|
| 427 | + // Detail |
| 428 | + if (typeof options.detailId === "string" |
| 429 | + && this.parent.orientation === "vertical" |
| 430 | + && this.parent.mode !== "compact") { |
| 431 | + options.detail = document.getElementById(options.detailId); |
| 432 | + } else { |
| 433 | + options.detail = null; |
| 434 | + } |
| 435 | + |
372 | 436 | // Check collision (Cause of special compact mode) |
373 | 437 | if (options.branch.commits.slice(-1)[0] && options.x + options.y === options.branch.commits.slice(-1)[0].x + options.branch.commits.slice(-1)[0].y) { |
374 | 438 | this.parent.commitOffsetX += this.template.commit.spacingX; |
|
411 | 475 | // Increment commitOffset for next commit position |
412 | 476 | this.parent.commitOffsetX += this.template.commit.spacingX; |
413 | 477 | this.parent.commitOffsetY += this.template.commit.spacingY; |
414 | | - |
| 478 | + |
| 479 | + // Add height of detail div (normal vertical mode only) |
| 480 | + if (commit.detail !== null) { |
| 481 | + commit.detail.style.display = "block"; |
| 482 | + this.parent.commitOffsetY -= commit.detail.clientHeight - 40; |
| 483 | + } |
| 484 | + |
415 | 485 | // Auto-render |
416 | 486 | this.parent.render(); |
417 | 487 |
|
|
441 | 511 | * Merge branch |
442 | 512 | * |
443 | 513 | * @param {Branch} [target = this.parent.HEAD] |
444 | | - * @param {string} [message] |
| 514 | + * @param {(String | Object)} [commitOptions] - Message | Options of commit |
| 515 | + * @return {Branch} this |
445 | 516 | * @this Branch |
446 | 517 | **/ |
447 | | - Branch.prototype.merge = function (target, message) { |
| 518 | + Branch.prototype.merge = function (target, commitOptions) { |
448 | 519 | // Merge target |
449 | 520 | var targetBranch = target || this.parent.HEAD; |
450 | 521 |
|
|
454 | 525 | } |
455 | 526 |
|
456 | 527 | // Merge commit |
457 | | - message = (typeof message === "string") ? |
458 | | - message : "Merge branch `" + this.name + "` into `" + targetBranch.name + "`"; |
459 | | - targetBranch.commit({ |
460 | | - message: message, |
461 | | - type: "mergeCommit", |
462 | | - parentCommit: this.commits.slice(-1)[0] |
463 | | - }); |
| 528 | + var message = commitOptions; |
| 529 | + commitOptions = commitOptions || {}; |
| 530 | + commitOptions.message = commitOptions.message |
| 531 | + || ((typeof message === "string") ? message : "Merge branch `" + this.name + "` into `" + targetBranch.name + "`"); |
| 532 | + commitOptions.type = "mergeCommit"; |
| 533 | + commitOptions.parentCommit = this.commits.slice(-1)[0]; |
| 534 | + |
| 535 | + targetBranch.commit(commitOptions); |
464 | 536 |
|
465 | 537 | // Add points to path |
466 | 538 | var endOfBranch = { |
|
525 | 597 | * @param {Boolean} options.arrowDisplay - Add a arrow under commit dot |
526 | 598 | * @param {String} [options.author = this.parent.author] - Author name & email |
527 | 599 | * @param {String} [options.date] - Date of commit, default is now |
| 600 | + * @param {String} [options.detail] - DOM Element of detail part |
528 | 601 | * @param {String} [options.sha1] - Sha1, default is a random short sha1 |
529 | 602 | * @param {String} [options.dotColor = options.color] - Specific dot color |
530 | 603 | * @param {Number} [options.dotSize = this.template.commit.dot.size] - Dot size |
|
552 | 625 | this.branch = options.branch; |
553 | 626 | this.author = options.author || this.parent.author; |
554 | 627 | this.date = options.date || new Date().toUTCString(); |
| 628 | + this.detail = options.detail || null; |
555 | 629 | this.sha1 = options.sha1 || (Math.random(100)).toString(16).substring(3, 10); |
556 | 630 | this.message = options.message || "He doesn't like George Michael! Boooo!"; |
557 | 631 | this.arrowDisplay = options.arrowDisplay; |
|
594 | 668 | this.arrow(); |
595 | 669 | } |
596 | 670 |
|
| 671 | + // Detail |
| 672 | + if (this.detail !== null) { |
| 673 | + this.detail.style.left = this.parent.canvas.offsetLeft + (this.parent.columnMax + 1) * this.template.branch.spacingX + 30 + "px"; |
| 674 | + this.detail.style.top = this.parent.canvas.offsetTop + this.y + 40 + "px"; |
| 675 | + this.detail.width = 30; |
| 676 | + } |
| 677 | + |
597 | 678 | // Message |
598 | 679 | if (this.messageDisplay) { |
599 | | - var message = this.sha1 + " " + this.message + " - " + this.author; |
| 680 | + var message = this.sha1 + " " + this.message + (this.author ? " - " + this.author : ""); |
600 | 681 | this.context.font = this.template.commit.message.font; |
601 | 682 | this.context.fillStyle = this.messageColor; |
602 | | - this.context.fillText(message, (this.parent.columnMax + 2) * this.template.branch.spacingX, this.y + 3); |
| 683 | + this.context.fillText(message, (this.parent.columnMax + 1) * this.template.branch.spacingX, this.y + 3); |
603 | 684 | } |
604 | 685 | }; |
605 | 686 |
|
|
0 commit comments