1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
define [
'backbone.marionette'
'templates/component-viewer'
'component-viewer/coverage-popup'
'component-viewer/duplication-popup'
'component-viewer/time-changes-popup'
'component-viewer/line-actions-popup'
'issue/issue-view'
'issue/models/issue'
'common/handlebars-extensions'
], (
Marionette
Templates
CoveragePopupView
DuplicationPopupView
TimeChangesPopupView
LineActionsPopupView
IssueView
Issue
) ->
$ = jQuery
API_COVERAGE_TESTS = "#{baseUrl}/api/tests/test_cases"
ISSUES_LIMIT = 100
class SourceView extends Marionette.ItemView
template: Templates['cw-source']
expandTemplate: Templates['cw-code-expand']
LINES_AROUND_ISSUE = 4
LINES_AROUND_COVERED_LINE = 1
EXPAND_LINES = 20
HIGHLIGHTED_ROW_CLASS = 'row-highlighted'
events:
'click .sym': 'highlightUsages'
'click .js-line-actions': 'highlightLine'
'click .coverage-tests': 'showCoveragePopup'
'click .duplication-exists': 'showDuplicationPopup'
'mouseenter .duplication-exists': 'duplicationMouseEnter'
'mouseleave .duplication-exists': 'duplicationMouseLeave'
'click .js-expand': 'expandBlock'
'click .js-expand-all': 'expandAll'
'click .js-time-changes': 'toggleTimeChangePopup'
initialize: ->
super
@showBlocks = []
resetShowBlocks: ->
@showBlocks = []
@options.main.trigger 'resetShowBlocks'
addShowBlock: (from, to, forceIncludeZero = false) ->
if from <= 0 && !forceIncludeZero
from = 1
@showBlocks.push from: from, to: to
onRender: ->
@delegateEvents()
@showSettings = false
@renderExpandButtons()
@renderIssues() if @options.main.settings.get('issues') && @model.has('activeIssues')
@highlightCurrentLine()
renderExpandButtons: ->
rows = @$('.row[data-line-number]')
rows.get().forEach (row) =>
line = $(row).data 'line-number'
linePrev = $(row).prev('[data-line-number]').data 'line-number'
if line? && linePrev? && (linePrev + 1) < line
expand = @expandTemplate from: linePrev, to: line, settings: @options.main.settings.toJSON()
$(expand).insertBefore $(row)
firstShown = rows.first().data('line-number')
if firstShown > 1
expand = @expandTemplate from: firstShown - EXPAND_LINES, to: firstShown, settings: @options.main.settings.toJSON()
$(expand).insertBefore rows.first()
lines = _.size @model.get 'source'
lastShown = rows.last().data('line-number')
if lastShown < lines
expand = @expandTemplate from: lastShown, to: lines, settings: @options.main.settings.toJSON()
$(expand).insertAfter rows.last()
@delegateEvents()
renderIssues: ->
issues = @model.get 'activeIssues'
issues = _.sortBy issues, 'line'
rendered = 0
issues.forEach (issue) =>
line = issue.line || 0
line = 0 if issue.status == 'CLOSED'
row = @$("##{@cid}-#{line}")
unless row.length > 0
line = 0
row = @$("##{@cid}-#{line}")
if row.length > 0
rendered += 1
row.removeClass 'row-hidden'
container = row.children('.line')
container.addClass 'issue' if line > 0
if rendered < ISSUES_LIMIT
issueModel = new Issue issue
issueView = new IssueView model: issueModel
issueView.render().$el.appendTo container
issueView.on 'reset', =>
@updateIssue issueModel
@options.main.requestComponent(@options.main.key, false, false).done =>
@options.main.headerView.silentUpdate = true
@options.main.headerView.render()
else
row.prop 'title', tp('component_viewer.issues_limit_reached_tooltip', issue.message)
updateIssue: (issueModel) ->
issues = @model.get 'issues'
issues = _.reject issues, (issue) -> issue.key == issueModel.get('key')
issues.push issueModel.toJSON()
@model.set 'issues', issues
issues = @model.get 'activeIssues'
issues = _.reject issues, (issue) -> issue.key == issueModel.get('key')
issues.push issueModel.toJSON()
@model.set 'activeIssues', issues
showSpinner: ->
@$el.html '<div style="padding: 10px;"><i class="spinner"></i></div>'
showLineActionsPopup: (e) ->
e.stopPropagation()
$('body').click()
popup = new LineActionsPopupView
triggerEl: $(e.currentTarget)
main: @options.main
row: $(e.currentTarget).closest '.row'
popup.render()
highlightLine: (e) ->
@$(".#{HIGHLIGHTED_ROW_CLASS}").removeClass HIGHLIGHTED_ROW_CLASS
row = $(e.currentTarget).closest('.row')
row.addClass HIGHLIGHTED_ROW_CLASS
@highlightedLine = row.data 'line-number'
@showLineActionsPopup(e) if @options.main.state.get 'canCreateManualIssue'
highlightCurrentLine: ->
if @highlightedLine?
@$("[data-line-number=#{@highlightedLine}]").addClass HIGHLIGHTED_ROW_CLASS
highlightUsages: (e) ->
key = e.currentTarget.className.split(/\s+/)[0]
@$('.sym.highlighted').removeClass 'highlighted'
@$(".sym.#{key}").addClass 'highlighted'
toggleSettings: ->
@$('.settings-toggle button').toggleClass 'open'
@$('.component-viewer-source-settings').toggleClass 'open'
toggleMeasures: (e) ->
row = $(e.currentTarget).closest '.component-viewer-header'
row.toggleClass 'component-viewer-header-full'
showCoveragePopup: (e) ->
e.stopPropagation()
$('body').click()
line = $(e.currentTarget).closest('.row').data 'line-number'
$.get API_COVERAGE_TESTS, key: @options.main.component.get('key'), line: line, (data) =>
popup = new CoveragePopupView
model: new Backbone.Model data
triggerEl: $(e.currentTarget)
main: @options.main
popup.render()
showDuplicationPopup: (e) ->
e.stopPropagation()
$('body').click()
index = $(e.currentTarget).data 'index'
line = $(e.currentTarget).closest('[data-line-number]').data 'line-number'
blocks = @model.get('duplications')[index - 1].blocks
blocks = _.filter blocks, (b) ->
(b._ref != '1') || (b._ref == '1' && b.from > line) || (b._ref == '1' && b.from + b.size < line)
popup = new DuplicationPopupView
triggerEl: $(e.currentTarget)
main: @options.main
collection: new Backbone.Collection blocks
popup.render()
duplicationMouseEnter: (e) ->
@toggleDuplicationHover e, true
duplicationMouseLeave: (e) ->
@toggleDuplicationHover e, false
toggleDuplicationHover: (e, add) ->
bar = $(e.currentTarget)
index = bar.parent().children('.duplication').index bar
@$('.duplications').each ->
$(".duplication", @).eq(index).filter('.duplication-exists').toggleClass 'duplication-hover', add
expandBlock: (e) ->
linesFrom = $(e.currentTarget).data 'from'
linesTo = $(e.currentTarget).data 'to'
if linesTo == _.size @model.get 'source'
if linesTo - linesFrom > EXPAND_LINES
linesTo = linesFrom + EXPAND_LINES
if linesFrom == 0 && linesTo > EXPAND_LINES
linesFrom = linesTo - EXPAND_LINES
@showBlocks.push from: linesFrom, to: linesTo
@render()
expandAll: ->
@options.main.showAllLines()
getSCMForLine: (lineNumber) ->
scm = @model.get('scm') || []
closest = -1
closestIndex = -1
scm.forEach (s, i) ->
line = s[0]
if line <= lineNumber && line > closest
closest = line
closestIndex = i
if closestIndex != -1 then scm[closestIndex] else null
augmentWithSCM: (source) ->
scm = @model.get('scm') || []
scm.forEach (s) ->
line = _.findWhere source, lineNumber: s[0]
line.scm = author: s[1], date: s[2]
@showBlocks.forEach (block) =>
scmForLine = @getSCMForLine block.from
if scmForLine?
line = _.findWhere source, lineNumber: block.from
line.scm = author: scmForLine[1], date: scmForLine[2]
source
augmentWithShow: (source) ->
source.forEach (sourceLine) =>
show = false
line = sourceLine.lineNumber
@showBlocks.forEach (block) ->
show = true if block.from <= line && block.to >= line
_.extend sourceLine, show: show
source
prepareSource: ->
source = @model.get 'formattedSource'
if source?
@augmentWithShow source
getStatColumnsCount: ->
count = 1 # line number
count += 2 if @options.main.settings.get 'coverage'
count += 1 if @options.main.settings.get 'duplications'
count += 1 if @options.main.settings.get 'issues'
count
showZeroLine: ->
r = false
r = true unless @options.main.state.get 'hasSource'
@showBlocks.forEach (block) ->
r = true if block.from <= 0
r
serializeData: ->
uid: @cid
source: @prepareSource()
settings: @options.main.settings.toJSON()
state: @options.main.state.toJSON()
showSettings: @showSettings
component: @options.main.component.toJSON()
columns: @getStatColumnsCount() + 1
showZeroLine: @showZeroLine()
issuesLimit: ISSUES_LIMIT
issuesLimitReached: @model.get('activeIssues')?.length > ISSUES_LIMIT
|