2017-05-16 21:05:53 +00:00
import React , { Component } from 'react' ;
2017-07-13 23:35:37 +00:00
import { StyleSheet , Text } from 'react-native' ;
2017-05-16 21:05:53 +00:00
import Icon from 'react-native-vector-icons/Ionicons' ;
import ReactNativeActionButton from 'react-native-action-button' ;
import { connect } from 'react-redux'
2017-06-24 18:06:28 +00:00
import { Log } from 'lib/log.js'
2017-07-13 23:35:37 +00:00
import { _ } from 'lib/locale.js'
2017-05-16 21:05:53 +00:00
const styles = StyleSheet . create ( {
actionButtonIcon : {
fontSize : 20 ,
height : 22 ,
color : 'white' ,
} ,
} ) ;
class ActionButtonComponent extends React . Component {
2017-07-13 23:35:37 +00:00
constructor ( ) {
super ( ) ;
this . state = {
2017-07-15 18:21:39 +00:00
buttonIndex : 0 ,
2017-07-13 23:35:37 +00:00
} ;
}
2017-07-14 23:12:32 +00:00
componentWillReceiveProps ( newProps ) {
2017-07-15 18:21:39 +00:00
if ( 'buttonIndex' in newProps ) {
this . setState ( { buttonIndex : newProps . buttonIndex } ) ;
2017-07-14 23:12:32 +00:00
}
}
2017-05-24 20:51:50 +00:00
newTodo _press ( ) {
this . props . dispatch ( {
2017-07-25 18:41:53 +00:00
type : 'NAV_GO' ,
2017-05-24 20:51:50 +00:00
routeName : 'Note' ,
noteId : null ,
folderId : this . props . parentFolderId ,
itemType : 'todo' ,
} ) ;
}
2017-05-16 21:05:53 +00:00
newNote _press ( ) {
this . props . dispatch ( {
2017-07-25 18:41:53 +00:00
type : 'NAV_GO' ,
2017-05-16 21:05:53 +00:00
routeName : 'Note' ,
noteId : null ,
folderId : this . props . parentFolderId ,
2017-05-24 20:51:50 +00:00
itemType : 'note' ,
2017-05-16 21:05:53 +00:00
} ) ;
}
newFolder _press ( ) {
this . props . dispatch ( {
2017-07-25 18:41:53 +00:00
type : 'NAV_GO' ,
2017-05-16 21:05:53 +00:00
routeName : 'Folder' ,
folderId : null ,
} ) ;
}
render ( ) {
2017-07-13 23:35:37 +00:00
let buttons = this . props . buttons ? this . props . buttons : [ ] ;
2017-05-24 20:51:50 +00:00
2017-07-13 23:35:37 +00:00
if ( this . props . addFolderNoteButtons ) {
if ( this . props . folders . length ) {
buttons . push ( {
title : _ ( 'New todo' ) ,
onPress : ( ) => { this . newTodo _press ( ) } ,
color : '#9b59b6' ,
icon : 'md-checkbox-outline' ,
} ) ;
buttons . push ( {
title : _ ( 'New note' ) ,
onPress : ( ) => { this . newNote _press ( ) } ,
color : '#9b59b6' ,
icon : 'md-document' ,
} ) ;
}
buttons . push ( {
2017-07-21 22:42:24 +00:00
title : _ ( 'New notebook' ) ,
2017-07-13 23:35:37 +00:00
onPress : ( ) => { this . newFolder _press ( ) } ,
color : '#3498db' ,
icon : 'md-folder' ,
} ) ;
}
2017-05-24 20:51:50 +00:00
2017-07-13 23:35:37 +00:00
let buttonComps = [ ] ;
for ( let i = 0 ; i < buttons . length ; i ++ ) {
let button = buttons [ i ] ;
let buttonTitle = button . title ? button . title : '' ;
let key = buttonTitle . replace ( /\s/g , '_' ) + '_' + button . icon ;
buttonComps . push (
< ReactNativeActionButton . Item key = { key } buttonColor = { button . color } title = { buttonTitle } onPress = { button . onPress } >
< Icon name = { button . icon } style = { styles . actionButtonIcon } / >
2017-05-16 21:05:53 +00:00
< / R e a c t N a t i v e A c t i o n B u t t o n . I t e m >
2017-07-08 22:57:09 +00:00
) ;
}
2017-05-24 20:51:50 +00:00
2017-07-13 23:35:37 +00:00
if ( ! buttonComps . length && ! this . props . mainButton ) {
return < ReactNativeActionButton style = { { display : 'none' } } / >
}
let mainButton = this . props . mainButton ? this . props . mainButton : { } ;
2017-07-21 21:40:02 +00:00
let mainIcon = mainButton . icon ? < Icon name = { mainButton . icon } style = { styles . actionButtonIcon } / > : < Icon name = "md-add" style = { styles . actionButtonIcon } / >
2017-07-13 23:35:37 +00:00
2017-07-15 18:21:39 +00:00
if ( this . props . multiStates ) {
if ( ! this . props . buttons || ! this . props . buttons . length ) throw new Error ( 'Multi-state button requires at least one state' ) ;
2017-07-15 18:37:17 +00:00
if ( this . state . buttonIndex < 0 || this . state . buttonIndex >= this . props . buttons . length ) throw new Error ( 'Button index out of bounds: ' + this . state . buttonIndex + '/' + this . props . buttons . length ) ;
2017-07-15 18:21:39 +00:00
let button = this . props . buttons [ this . state . buttonIndex ] ;
2017-07-13 23:35:37 +00:00
let mainIcon = < Icon name = { button . icon } style = { styles . actionButtonIcon } / >
return (
< ReactNativeActionButton
icon = { mainIcon }
buttonColor = "rgba(231,76,60,1)"
2017-07-15 18:21:39 +00:00
onPress = { ( ) => { button . onPress ( ) } }
2017-07-13 23:35:37 +00:00
/ >
) ;
} else {
return (
< ReactNativeActionButton icon = { mainIcon } buttonColor = "rgba(231,76,60,1)" onPress = { function ( ) { } } >
{ buttonComps }
< / R e a c t N a t i v e A c t i o n B u t t o n >
) ;
}
2017-05-16 21:05:53 +00:00
}
}
const ActionButton = connect (
( state ) => {
2017-07-08 22:57:09 +00:00
return {
folders : state . folders ,
} ;
2017-05-16 21:05:53 +00:00
}
) ( ActionButtonComponent )
export { ActionButton } ;