@@ -15,14 +15,22 @@ define(function (require) {
15
15
HistoryViewer = require ( "src/HistoryViewer" ) ,
16
16
Preferences = require ( "src/Preferences" ) ;
17
17
18
+ if ( Preferences . get ( "useGravatar" ) ) {
19
+ var md5 ;
20
+ require ( [ "md5" ] , function ( _md5 ) {
21
+ md5 = _md5 ;
22
+ } ) ;
23
+ }
24
+
18
25
// Templates
19
26
var gitPanelHistoryTemplate = require ( "text!templates/git-panel-history.html" ) ,
20
- panelHistoryTemplate = require ( "text!templates/git-panel-history-commits.html" ) ;
27
+ gitPanelHistoryCommitsTemplate = require ( "text!templates/git-panel-history-commits.html" ) ;
21
28
22
29
// Module variables
23
30
var $gitPanel = $ ( null ) ,
24
31
$tableContainer = $ ( null ) ,
25
- $historyList = $ ( null ) ;
32
+ $historyList = $ ( null ) ,
33
+ commitCache = [ ] ;
26
34
27
35
// Implementation
28
36
@@ -39,26 +47,34 @@ define(function (require) {
39
47
loadMoreHistory ( ) ;
40
48
} )
41
49
. on ( "click.history" , ".history-commit" , function ( ) {
42
- HistoryViewer . show ( $ ( this ) . data ( "history-commit" ) ) ;
50
+ var hash = $ ( this ) . attr ( "x-hash" ) ;
51
+ var commit = _ . find ( commitCache , function ( commit ) { return commit . hash === hash ; } ) ;
52
+ HistoryViewer . show ( commit ) ;
43
53
} ) ;
44
54
}
45
55
46
56
// Render history list the first time
47
57
function renderHistory ( file ) {
58
+ // clear cache
59
+ commitCache = [ ] ;
60
+
48
61
return Git . getCurrentBranchName ( ) . then ( function ( branchName ) {
49
62
// Get the history commits of the current branch
50
63
var p = file ? Git . getFileHistory ( file . relative , branchName ) : Git . getHistory ( branchName ) ;
51
64
return p . then ( function ( commits ) {
52
- commits = convertCommitDates ( commits ) ;
53
65
54
- var template = "<table class='git-history-list bottom-panel-table table table-striped table-condensed row-highlight'>" ;
55
- template += "<tbody>" ;
56
- template += gitPanelHistoryTemplate ;
57
- template += "</tbody>" ;
58
- template += "</table>" ;
66
+ // calculate some missing stuff like gravatars
67
+ commits = addAdditionalCommitInfo ( commits ) ;
68
+ commitCache = commitCache . concat ( commits ) ;
59
69
60
- $tableContainer . append ( Mustache . render ( template , {
61
- commits : commits
70
+ var templateData = {
71
+ commits : commits ,
72
+ useGravatar : Preferences . get ( "useGravatar" ) ,
73
+ Strings : Strings
74
+ } ;
75
+
76
+ $tableContainer . append ( Mustache . render ( gitPanelHistoryTemplate , templateData , {
77
+ commits : gitPanelHistoryCommitsTemplate
62
78
} ) ) ;
63
79
64
80
$historyList = $tableContainer . find ( ".git-history-list" )
@@ -91,9 +107,17 @@ define(function (require) {
91
107
$historyList . attr ( "x-finished" , "true" ) ;
92
108
return ;
93
109
}
94
- commits = convertCommitDates ( commits ) ;
95
- $tableContainer . find ( ".git-history-list > tbody" )
96
- . append ( Mustache . render ( gitPanelHistoryTemplate , { commits : commits } ) ) ;
110
+
111
+ commits = addAdditionalCommitInfo ( commits ) ;
112
+ commitCache = commitCache . concat ( commits ) ;
113
+
114
+ var templateData = {
115
+ commits : commits ,
116
+ useGravatar : Preferences . get ( "useGravatar" ) ,
117
+ Strings : Strings
118
+ } ;
119
+ var commitsHtml = Mustache . render ( gitPanelHistoryCommitsTemplate , templateData ) ;
120
+ $historyList . children ( "tbody" ) . append ( commitsHtml ) ;
97
121
} )
98
122
. catch ( function ( err ) {
99
123
ErrorHandler . showError ( err , "Failed to load more history rows" ) ;
@@ -106,14 +130,25 @@ define(function (require) {
106
130
}
107
131
}
108
132
109
- function convertCommitDates ( commits ) {
133
+ function addAdditionalCommitInfo ( commits ) {
110
134
var mode = Preferences . get ( "dateMode" ) ,
111
135
format = Strings . DATE_FORMAT ,
112
136
now = moment ( ) ,
113
137
yesterday = moment ( ) . subtract ( "d" , 1 ) . startOf ( "d" ) ,
114
138
ownFormat = Preferences . get ( "dateFormat" ) || Strings . DATE_FORMAT ;
115
139
116
140
_ . forEach ( commits , function ( commit ) {
141
+
142
+ if ( Preferences . get ( "useGravatar" ) ) {
143
+ // email hash for gravatars
144
+ commit . emailHash = md5 ( commit . email ) ;
145
+ } else {
146
+ commit . avatarLetter = commit . author . substring ( 0 , 1 ) ;
147
+ }
148
+
149
+ // shorten the commit subject
150
+ commit . subject = commit . subject . substring ( 0 , 49 ) + ( ( commit . subject . length > 50 ) ? "\u2026" : "" ) ;
151
+
117
152
if ( mode === 4 ) {
118
153
// mode 4: Original Git date
119
154
commit . date = {
@@ -154,6 +189,7 @@ define(function (require) {
154
189
/* mode 4 (Original Git date) is handled above */
155
190
}
156
191
} ) ;
192
+
157
193
return commits ;
158
194
}
159
195
@@ -195,7 +231,9 @@ define(function (require) {
195
231
}
196
232
197
233
// Render .git-history-list if is not already generated or if the viewed file for file history has changed
198
- if ( historyEnabled && ( $historyList . length === 0 || currentFile !== ( file ? file . absolute : null ) ) ) {
234
+ var isEmpty = $historyList . find ( "tr" ) . length === 0 ,
235
+ fileChanged = currentFile !== ( file ? file . absolute : null ) ;
236
+ if ( historyEnabled && ( isEmpty || fileChanged ) ) {
199
237
if ( $historyList . length > 0 ) {
200
238
$historyList . remove ( ) ;
201
239
}
@@ -237,129 +275,3 @@ define(function (require) {
237
275
} ) ;
238
276
239
277
} ) ;
240
-
241
- /*
242
- // Render history list the first time
243
- function renderHistory() {
244
- return Main.gitControl.getBranchName().then(function (branchName) {
245
- // Get the history commit of the current branch
246
- return Main.gitControl.getHistory(branchName).then(function (commits) {
247
- commits = convertCommitDates(commits);
248
-
249
- var partials = {gitPanelHistoryCommits: gitPanelHistoryCommitsTemplate, gitDiffDetails: gitDiffDetailsTemplate},
250
- variables = {commits: commits, useGravatar: Preferences.get("useGravatar")};
251
-
252
- $tableContainer
253
- .append(Mustache.render(gitPanelHistoryTemplate, variables, partials))
254
- .find(".history-commits-list")
255
- .off("scroll.history")
256
- .on("scroll.history", function () {
257
- loadMoreHistory();
258
- });
259
-
260
- renderHistoryCommit(commits[0], $tableContainer.find(".history-commits-list .history-commit").first());
261
-
262
- });
263
- }).catch(function (err) {
264
- ErrorHandler.showError(err, "Failed to get history");
265
- });
266
- }
267
-
268
- // Load more rows in the history list on scroll
269
- function loadMoreHistory() {
270
- if ($tableContainer.find(".git-history-list").is(":visible")) {
271
- var $commitsList = $tableContainer.find(".history-commits-list");
272
- if (($commitsList.prop("scrollHeight") - $commitsList.scrollTop()) === $commitsList.height()) {
273
- return Main.gitControl.getBranchName().then(function (branchName) {
274
- return Main.gitControl.getHistory(branchName, $commitsList.find(".history-commit").length).then(function (commits) {
275
- if (commits.length === 0) {
276
- return;
277
- }
278
- commits = convertCommitDates(commits);
279
- var variables = {commits: commits, useGravatar: Preferences.get("useGravatar")};
280
-
281
- $commitsList.append(Mustache.render(gitPanelHistoryCommitsTemplate, variables));
282
- })
283
- .catch(function (err) {
284
- ErrorHandler.showError(err, "Failed to load more history rows");
285
- });
286
- })
287
- .catch(function (err) {
288
- ErrorHandler.showError(err, "Failed to get branch name");
289
- });
290
- }
291
- }
292
- }
293
-
294
- function convertCommitDates(commits) {
295
- var mode = Preferences.get("dateMode"),
296
- format = Strings.DATE_FORMAT,
297
- now = moment(),
298
- yesterday = moment().subtract("d", 1).startOf("d"),
299
- ownFormat = Preferences.get("dateFormat") || Strings.DATE_FORMAT;
300
-
301
- _.forEach(commits, function (commit) {
302
- if (mode === 4) {
303
- // mode 4: Original Git date
304
- commit.date = {
305
- shown: commit.date
306
- };
307
- return;
308
- }
309
-
310
- var date = moment(commit.date);
311
- commit.date = {
312
- title: ""
313
- };
314
- switch (mode) {
315
- // mode 0 (default): formatted with Strings.DATE_FORMAT
316
- default:
317
- case 0:
318
- commit.date.shown = date.format(format);
319
- break;
320
- // mode 1: always relative
321
- case 1:
322
- commit.date.shown = date.fromNow();
323
- commit.date.title = date.format(format);
324
- break;
325
- // mode 2: intelligent relative/formatted
326
- case 2:
327
- if (date.diff(yesterday) > 0) {
328
- commit.date.shown = moment.duration(Math.max(date.diff(now), -24 * 60 * 60 * 1000), "ms").humanize(true);
329
- commit.date.title = date.format(format);
330
- } else {
331
- commit.date.shown = date.format(format);
332
- }
333
- break;
334
- // mode 3: formatted with own format (as pref)
335
- case 3:
336
- commit.date.shown = date.format(ownFormat);
337
- commit.date.title = date.format(format);
338
- break;
339
- // mode 4 (Original Git date) is handled above
340
- }
341
- });
342
- return commits;
343
- }
344
-
345
- // Show or hide the history list on click of .history button
346
- function handleToggleHistory() {
347
-
348
- var $panel = gitPanel.$panel,
349
- historyEnabled = !$panel.find(".git-history-list").is(":visible");
350
-
351
- // Render .git-history-list if is not already generated
352
- if ($tableContainer.find(".git-history-list").length === 0) { renderHistory(); }
353
-
354
- // Toggle commit button and check-all checkbox
355
- $panel.find(".git-commit, .check-all").prop("disabled", historyEnabled);
356
-
357
- // Toggle visibility of .git-edited-list and .git-history-list
358
- $tableContainer.find(".git-edited-list, .git-history-list").toggle();
359
-
360
- // Toggle history button
361
- $panel.find(".git-history").toggleClass("active")
362
- .attr("title", historyEnabled ? Strings.TOOLTIP_HIDE_HISTORY : Strings.TOOLTIP_SHOW_HISTORY);
363
-
364
- }
365
- */
0 commit comments