2017-10-26 10:40:41 -04:00
import Vue from 'vue' ;
2017-10-26 06:14:31 -04:00
import flash from '../../flash' ;
import service from '../services' ;
import * as types from './mutation_types' ;
2017-11-06 08:07:59 -05:00
export const redirectToUrl = ( _ , url ) => gl . utils . visitUrl ( url ) ;
2017-10-26 06:14:31 -04:00
export const setInitialData = ( { commit } , data ) => commit ( types . SET _INITIAL _DATA , data ) ;
export const closeDiscardPopup = ( { commit } ) => commit ( types . TOGGLE _DISCARD _POPUP , false ) ;
2017-10-26 12:36:27 -04:00
export const discardAllChanges = ( { commit , getters , dispatch } ) => {
2017-10-26 10:12:34 -04:00
const changedFiles = getters . changedFiles ;
2017-10-26 06:14:31 -04:00
2017-10-26 11:44:26 -04:00
changedFiles . forEach ( ( file ) => {
commit ( types . DISCARD _FILE _CHANGES , file ) ;
if ( file . tempFile ) {
dispatch ( 'closeFile' , { file , force : true } ) ;
}
} ) ;
2017-10-26 06:14:31 -04:00
} ;
export const closeAllFiles = ( { state , dispatch } ) => {
2017-10-26 11:44:26 -04:00
state . openFiles . forEach ( file => dispatch ( 'closeFile' , { file } ) ) ;
2017-10-26 06:14:31 -04:00
} ;
2017-10-27 13:16:28 -04:00
export const toggleEditMode = ( { state , commit , getters , dispatch } , force = false ) => {
2017-10-26 10:12:34 -04:00
const changedFiles = getters . changedFiles ;
2017-10-26 06:14:31 -04:00
if ( changedFiles . length && ! force ) {
commit ( types . TOGGLE _DISCARD _POPUP , true ) ;
} else {
commit ( types . TOGGLE _EDIT _MODE ) ;
commit ( types . TOGGLE _DISCARD _POPUP , false ) ;
dispatch ( 'toggleBlobView' ) ;
2017-10-27 13:16:28 -04:00
if ( ! state . editMode ) {
dispatch ( 'discardAllChanges' ) ;
}
2017-10-26 06:14:31 -04:00
}
} ;
export const toggleBlobView = ( { commit , state } ) => {
if ( state . editMode ) {
commit ( types . SET _EDIT _MODE ) ;
} else {
commit ( types . SET _PREVIEW _MODE ) ;
}
} ;
export const checkCommitStatus = ( { state } ) => service . getBranchData (
state . project . id ,
state . currentBranch ,
)
. then ( ( data ) => {
const { id } = data . commit ;
if ( state . currentRef !== id ) {
return true ;
}
return false ;
} )
. catch ( ( ) => flash ( 'Error checking branch data. Please try again.' ) ) ;
2017-11-02 12:07:01 -04:00
export const commitChanges = ( { commit , state , dispatch , getters } , { payload , newMr } ) =>
2017-10-26 06:14:31 -04:00
service . commit ( state . project . id , payload )
. then ( ( data ) => {
2017-10-30 10:36:24 -04:00
const { branch } = payload ;
2017-10-26 06:14:31 -04:00
if ( ! data . short _id ) {
flash ( data . message ) ;
return ;
}
2017-11-02 12:07:01 -04:00
const lastCommit = {
commit _path : ` ${ state . project . url } /commit/ ${ data . id } ` ,
commit : {
message : data . message ,
authored _date : data . committed _date ,
} ,
} ;
2017-10-26 06:14:31 -04:00
flash ( ` Your changes have been committed. Commit ${ data . short _id } with ${ data . stats . additions } additions, ${ data . stats . deletions } deletions. ` , 'notice' ) ;
if ( newMr ) {
2017-11-10 12:40:20 -05:00
dispatch ( 'redirectToUrl' , ` ${ state . endpoints . newMergeRequestUrl } ${ branch } ` ) ;
2017-10-26 06:14:31 -04:00
} else {
commit ( types . SET _COMMIT _REF , data . id ) ;
2017-11-02 12:07:01 -04:00
getters . changedFiles . forEach ( ( entry ) => {
commit ( types . SET _LAST _COMMIT _DATA , {
entry ,
lastCommit ,
} ) ;
} ) ;
2017-10-26 06:14:31 -04:00
dispatch ( 'discardAllChanges' ) ;
dispatch ( 'closeAllFiles' ) ;
dispatch ( 'toggleEditMode' ) ;
2017-10-26 12:36:27 -04:00
window . scrollTo ( 0 , 0 ) ;
2017-10-26 06:14:31 -04:00
}
} )
. catch ( ( ) => flash ( 'Error committing changes. Please try again.' ) ) ;
2017-10-31 08:13:29 -04:00
export const createTempEntry = ( { state , dispatch } , { name , type , content = '' , base64 = false } ) => {
2017-10-26 10:12:34 -04:00
if ( type === 'tree' ) {
dispatch ( 'createTempTree' , name ) ;
} else if ( type === 'blob' ) {
dispatch ( 'createTempFile' , {
tree : state ,
name ,
2017-10-31 08:13:29 -04:00
base64 ,
content ,
2017-10-26 10:12:34 -04:00
} ) ;
}
} ;
export const popHistoryState = ( { state , dispatch , getters } ) => {
const treeList = getters . treeList ;
2017-10-26 06:14:31 -04:00
const tree = treeList . find ( file => file . url === state . previousUrl ) ;
if ( ! tree ) return ;
if ( tree . type === 'tree' ) {
dispatch ( 'toggleTreeOpen' , { endpoint : tree . url , tree } ) ;
}
} ;
2017-10-26 10:40:41 -04:00
export const scrollToTab = ( ) => {
Vue . nextTick ( ( ) => {
const tabs = document . getElementById ( 'tabs' ) ;
2017-10-27 13:16:28 -04:00
if ( tabs ) {
2017-10-30 12:25:32 -04:00
const tabEl = tabs . querySelector ( '.active .repo-tab' ) ;
2017-10-27 13:16:28 -04:00
2017-10-30 12:25:32 -04:00
tabEl . focus ( ) ;
2017-10-27 13:16:28 -04:00
}
2017-10-26 10:40:41 -04:00
} ) ;
} ;
2017-10-26 06:14:31 -04:00
export * from './actions/tree' ;
export * from './actions/file' ;
export * from './actions/branch' ;