2018-11-08 19:23:39 +05:30
< script >
2018-12-05 23:21:45 +05:30
import { mapActions , mapGetters , mapState } from 'vuex' ;
2020-05-24 23:13:21 +05:30
import { escape } from 'lodash' ;
2020-11-24 15:15:51 +05:30
import { GlLoadingIcon , GlSafeHtmlDirective as SafeHtml } from '@gitlab/ui' ;
2020-10-24 23:57:45 +05:30
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin' ;
2018-11-08 19:23:39 +05:30
import { _ _ , sprintf } from '~/locale' ;
2020-10-24 23:57:45 +05:30
import { deprecatedCreateFlash as createFlash } from '~/flash' ;
2020-03-13 15:44:24 +05:30
import { hasDiff } from '~/helpers/diffs_helper' ;
2019-02-15 15:39:39 +05:30
import eventHub from '../../notes/event_hub' ;
2018-11-08 19:23:39 +05:30
import DiffFileHeader from './diff_file_header.vue' ;
import DiffContent from './diff_content.vue' ;
2019-07-07 11:18:12 +05:30
import { diffViewerErrors } from '~/ide/constants' ;
2018-11-08 19:23:39 +05:30
export default {
components : {
DiffFileHeader ,
DiffContent ,
2018-12-13 13:39:08 +05:30
GlLoadingIcon ,
2018-11-08 19:23:39 +05:30
} ,
2020-11-24 15:15:51 +05:30
directives : {
SafeHtml ,
} ,
2020-10-24 23:57:45 +05:30
mixins : [ glFeatureFlagsMixin ( ) ] ,
2018-11-08 19:23:39 +05:30
props : {
file : {
type : Object ,
required : true ,
} ,
canCurrentUserFork : {
type : Boolean ,
required : true ,
} ,
2019-02-15 15:39:39 +05:30
helpPagePath : {
type : String ,
required : false ,
default : '' ,
} ,
2020-07-28 23:09:34 +05:30
viewDiffsFileByFile : {
type : Boolean ,
required : true ,
} ,
2018-11-08 19:23:39 +05:30
} ,
data ( ) {
return {
isLoadingCollapsedDiff : false ,
forkMessageVisible : false ,
2019-07-07 11:18:12 +05:30
isCollapsed : this . file . viewer . collapsed || false ,
2018-11-08 19:23:39 +05:30
} ;
} ,
computed : {
2018-12-05 23:21:45 +05:30
... mapState ( 'diffs' , [ 'currentDiffFileId' ] ) ,
2018-12-13 13:39:08 +05:30
... mapGetters ( [ 'isNotesFetched' ] ) ,
... mapGetters ( 'diffs' , [ 'getDiffFileDiscussions' ] ) ,
2018-11-08 19:23:39 +05:30
viewBlobLink ( ) {
return sprintf (
_ _ ( 'You can %{linkStart}view the blob%{linkEnd} instead.' ) ,
{
2020-05-24 23:13:21 +05:30
linkStart : ` <a href=" ${ escape ( this . file . view _path ) } "> ` ,
2018-11-08 19:23:39 +05:30
linkEnd : '</a>' ,
} ,
false ,
) ;
} ,
2018-11-20 20:47:30 +05:30
showLoadingIcon ( ) {
return this . isLoadingCollapsedDiff || ( ! this . file . renderIt && ! this . isCollapsed ) ;
2018-11-08 19:23:39 +05:30
} ,
2019-12-26 22:10:19 +05:30
hasDiff ( ) {
2020-03-13 15:44:24 +05:30
return hasDiff ( this . file ) ;
2018-12-13 13:39:08 +05:30
} ,
2019-07-07 11:18:12 +05:30
isFileTooLarge ( ) {
return this . file . viewer . error === diffViewerErrors . too _large ;
} ,
errorMessage ( ) {
return this . file . viewer . error _message ;
} ,
2019-09-30 21:07:59 +05:30
forkMessage ( ) {
return sprintf (
_ _ (
"You're not allowed to %{tag_start}edit%{tag_end} files in this project directly. Please fork this project, make your changes there, and submit a merge request." ,
) ,
{
tag _start : '<span class="js-file-fork-suggestion-section-action">' ,
tag _end : '</span>' ,
} ,
false ,
) ;
} ,
2018-12-13 13:39:08 +05:30
} ,
watch : {
2019-07-07 11:18:12 +05:30
isCollapsed : function fileCollapsedWatch ( newVal , oldVal ) {
2019-12-26 22:10:19 +05:30
if ( ! newVal && oldVal && ! this . hasDiff ) {
2018-12-13 13:39:08 +05:30
this . handleLoadCollapsedDiff ( ) ;
}
2019-07-07 11:18:12 +05:30
this . setFileCollapsed ( { filePath : this . file . file _path , collapsed : newVal } ) ;
} ,
2020-10-24 23:57:45 +05:30
'file.file_hash' : {
handler : function watchFileHash ( ) {
2020-11-24 15:15:51 +05:30
if ( this . viewDiffsFileByFile && this . file . viewer . collapsed ) {
2020-10-24 23:57:45 +05:30
this . isCollapsed = false ;
this . handleLoadCollapsedDiff ( ) ;
} else {
this . isCollapsed = this . file . viewer . collapsed || false ;
}
} ,
immediate : true ,
} ,
2019-07-07 11:18:12 +05:30
'file.viewer.collapsed' : function setIsCollapsed ( newVal ) {
2020-11-24 15:15:51 +05:30
if ( ! this . viewDiffsFileByFile ) {
2020-10-24 23:57:45 +05:30
this . isCollapsed = newVal ;
}
2018-12-13 13:39:08 +05:30
} ,
2018-11-08 19:23:39 +05:30
} ,
2019-02-15 15:39:39 +05:30
created ( ) {
eventHub . $on ( ` loadCollapsedDiff/ ${ this . file . file _hash } ` , this . handleLoadCollapsedDiff ) ;
} ,
2018-11-08 19:23:39 +05:30
methods : {
2019-07-07 11:18:12 +05:30
... mapActions ( 'diffs' , [
'loadCollapsedDiff' ,
'assignDiscussionsToDiff' ,
'setRenderIt' ,
'setFileCollapsed' ,
] ) ,
2018-11-08 19:23:39 +05:30
handleToggle ( ) {
2019-12-26 22:10:19 +05:30
if ( ! this . hasDiff ) {
2018-11-08 19:23:39 +05:30
this . handleLoadCollapsedDiff ( ) ;
} else {
2019-07-07 11:18:12 +05:30
this . isCollapsed = ! this . isCollapsed ;
this . setRenderIt ( this . file ) ;
2018-11-08 19:23:39 +05:30
}
} ,
handleLoadCollapsedDiff ( ) {
this . isLoadingCollapsedDiff = true ;
this . loadCollapsedDiff ( this . file )
. then ( ( ) => {
this . isLoadingCollapsedDiff = false ;
2019-07-07 11:18:12 +05:30
this . isCollapsed = false ;
this . setRenderIt ( this . file ) ;
2018-11-20 20:47:30 +05:30
} )
. then ( ( ) => {
requestIdleCallback (
( ) => {
2018-12-13 13:39:08 +05:30
this . assignDiscussionsToDiff ( this . getDiffFileDiscussions ( this . file ) ) ;
2018-11-20 20:47:30 +05:30
} ,
{ timeout : 1000 } ,
) ;
2018-11-08 19:23:39 +05:30
} )
. catch ( ( ) => {
this . isLoadingCollapsedDiff = false ;
createFlash ( _ _ ( 'Something went wrong on our end. Please try again!' ) ) ;
} ) ;
} ,
showForkMessage ( ) {
this . forkMessageVisible = true ;
} ,
hideForkMessage ( ) {
this . forkMessageVisible = false ;
} ,
} ,
} ;
< / script >
< template >
< div
2019-02-15 15:39:39 +05:30
: id = "file.file_hash"
2018-12-05 23:21:45 +05:30
: class = " {
2019-02-15 15:39:39 +05:30
'is-active' : currentDiffFileId === file . file _hash ,
2020-10-24 23:57:45 +05:30
'comments-disabled' : Boolean ( file . brokenSymlink ) ,
2018-12-05 23:21:45 +05:30
} "
2020-04-22 19:07:51 +05:30
: data - path = "file.new_path"
2018-11-08 19:23:39 +05:30
class = "diff-file file-holder"
>
< diff-file-header
: can - current - user - fork = "canCurrentUserFork"
: diff - file = "file"
: collapsible = "true"
: expanded = "!isCollapsed"
: add - merge - request - buttons = "true"
2020-07-28 23:09:34 +05:30
: view - diffs - file - by - file = "viewDiffsFileByFile"
2018-11-08 19:23:39 +05:30
class = "js-file-title file-title"
@ toggleFile = "handleToggle"
@ showForkMessage = "showForkMessage"
/ >
2019-02-15 15:39:39 +05:30
< div v-if = "forkMessageVisible" class="js-file-fork-suggestion-section file-fork-suggestion" >
2020-11-24 15:15:51 +05:30
< span v -safe -html = " forkMessage " class = "file-fork-suggestion-note" > < / span >
2018-11-08 19:23:39 +05:30
< a
2019-02-15 15:39:39 +05:30
: href = "file.fork_path"
2018-11-08 19:23:39 +05:30
class = "js-fork-suggestion-button btn btn-grouped btn-inverted btn-success"
2019-09-30 21:07:59 +05:30
> { { _ _ ( 'Fork' ) } } < / a
2018-11-08 19:23:39 +05:30
>
< button
class = "js-cancel-fork-suggestion-button btn btn-grouped"
type = "button"
@ click = "hideForkMessage"
>
2019-09-30 21:07:59 +05:30
{ { _ _ ( 'Cancel' ) } }
2018-11-08 19:23:39 +05:30
< / button >
< / div >
2019-02-15 15:39:39 +05:30
< gl-loading-icon v-if = "showLoadingIcon" class="diff-content loading" / >
2019-07-07 11:18:12 +05:30
< template v-else >
< div :id = "`diff-content-${file.file_hash}`" >
< div v-if = "errorMessage" class="diff-viewer" >
2020-11-24 15:15:51 +05:30
< div v -safe -html = " errorMessage " class = "nothing-here-block" > < / div >
2019-07-07 11:18:12 +05:30
< / div >
2020-04-08 14:13:33 +05:30
< template v-else >
< div v-show = "isCollapsed" class="nothing-here-block diff-collapsed" >
{ { _ _ ( 'This diff is collapsed.' ) } }
< a class = "click-to-expand js-click-to-expand" href = "#" @click.prevent ="handleToggle" > { {
_ _ ( 'Click to expand it.' )
} } < / a >
< / div >
< diff-content
v - show = "!isCollapsed && !isFileTooLarge"
: diff - file = "file"
: help - page - path = "helpPagePath"
/ >
< / template >
2019-07-07 11:18:12 +05:30
< / div >
< / template >
2018-11-08 19:23:39 +05:30
< / div >
< / template >
2018-12-05 23:21:45 +05:30
< style >
@ keyframes shadow - fade {
from {
box - shadow : 0 0 4 px # 919191 ;
}
to {
box - shadow : 0 0 0 # dfdfdf ;
}
}
. diff - file . is - active {
box - shadow : 0 0 0 # dfdfdf ;
animation : shadow - fade 1.2 s 0.1 s 1 ;
}
< / style >