11import React , { PropTypes } from 'react'
2+ import ReactDOM from 'react-dom'
23import CSSModules from 'browser/lib/CSSModules'
34import styles from './StorageItem.styl'
45import consts from 'browser/lib/consts'
@@ -8,6 +9,7 @@ import store from 'browser/main/store'
89const electron = require ( 'electron' )
910const { shell, remote } = electron
1011const { Menu, MenuItem } = remote
12+ import { SketchPicker } from 'react-color'
1113
1214class UnstyledFolderItem extends React . Component {
1315 constructor ( props ) {
@@ -16,6 +18,8 @@ class UnstyledFolderItem extends React.Component {
1618 this . state = {
1719 status : 'IDLE' ,
1820 folder : {
21+ showColumnPicker : false ,
22+ colorPickerPos : { left : 0 , top : 0 } ,
1923 color : props . color ,
2024 name : props . name
2125 }
@@ -50,22 +54,32 @@ class UnstyledFolderItem extends React.Component {
5054 }
5155
5256 handleColorButtonClick ( e ) {
53- var menu = new Menu ( )
54-
55- consts . FOLDER_COLORS . forEach ( ( color , index ) => {
56- menu . append ( new MenuItem ( {
57- label : consts . FOLDER_COLOR_NAMES [ index ] ,
58- click : ( e ) => {
59- let { folder } = this . state
60- folder . color = color
61- this . setState ( {
62- folder
63- } )
57+ const folder = Object . assign ( { } , this . state . folder , { showColumnPicker : true , colorPickerPos : { left : 0 , top : 0 } } )
58+ this . setState ( { folder } , function ( ) {
59+ // After the color picker has been painted, re-calculate its position
60+ // by comparing its dimensions to the host dimensions.
61+ const { hostBoundingBox } = this . props ;
62+ const colorPickerNode = ReactDOM . findDOMNode ( this . refs . colorPicker )
63+ const colorPickerBox = colorPickerNode . getBoundingClientRect ( )
64+ const offsetTop = hostBoundingBox . bottom - colorPickerBox . bottom
65+ const folder = Object . assign ( { } , this . state . folder , {
66+ colorPickerPos : {
67+ left : 25 ,
68+ top : offsetTop < 0 ? offsetTop - 5 : 0 // subtract 5px for aestetics
6469 }
65- } ) )
70+ } )
71+ this . setState ( { folder } )
6672 } )
73+ }
6774
68- menu . popup ( remote . getCurrentWindow ( ) )
75+ handleColorChange ( color ) {
76+ const folder = Object . assign ( { } , this . state . folder , { color : color . hex } )
77+ this . setState ( { folder } )
78+ }
79+
80+ handleColorPickerClose ( event ) {
81+ const folder = Object . assign ( { } , this . state . folder , { showColumnPicker : false } )
82+ this . setState ( { folder } )
6983 }
7084
7185 handleCancelButtonClick ( e ) {
@@ -75,12 +89,33 @@ class UnstyledFolderItem extends React.Component {
7589 }
7690
7791 renderEdit ( e ) {
92+ const popover = { position : 'absolute' , zIndex : 2 }
93+ const cover = {
94+ position : 'fixed' ,
95+ top : 0 , right : 0 , bottom : 0 , left : 0
96+ }
97+ const pickerStyle = Object . assign ( { } , {
98+ position : 'absolute'
99+ } , this . state . folder . colorPickerPos )
78100 return (
79101 < div styleName = 'folderList-item' >
80102 < div styleName = 'folderList-item-left' >
81103 < button styleName = 'folderList-item-left-colorButton' style = { { color : this . state . folder . color } }
82- onClick = { ( e ) => this . handleColorButtonClick ( e ) }
104+ onClick = { ( e ) => ! this . state . folder . showColumnPicker && this . handleColorButtonClick ( e ) }
83105 >
106+ { this . state . folder . showColumnPicker ?
107+ < div style = { popover } >
108+ < div style = { cover }
109+ onClick = { ( ) => this . handleColorPickerClose ( ) } />
110+ < div style = { pickerStyle } >
111+ < SketchPicker
112+ ref = "colorPicker"
113+ color = { this . state . folder . color }
114+ onChange = { ( color ) => this . handleColorChange ( color ) }
115+ onChangeComplete = { ( color ) => this . handleColorChange ( color ) } />
116+ </ div >
117+ </ div >
118+ : null }
84119 < i className = 'fa fa-square' />
85120 </ button >
86121 < input styleName = 'folderList-item-left-nameInput'
@@ -141,13 +176,12 @@ class UnstyledFolderItem extends React.Component {
141176 }
142177
143178 handleEditButtonClick ( e ) {
144- let { folder } = this . props
179+ let { folder : propsFolder } = this . props
180+ let { folder : stateFolder } = this . state
181+ const folder = Object . assign ( { } , stateFolder , propsFolder )
145182 this . setState ( {
146183 status : 'EDIT' ,
147- folder : {
148- color : folder . color ,
149- name : folder . name
150- }
184+ folder
151185 } , ( ) => {
152186 this . refs . nameInput . select ( )
153187 } )
@@ -280,11 +314,12 @@ class StorageItem extends React.Component {
280314 }
281315
282316 render ( ) {
283- let { storage } = this . props
317+ let { storage, hostBoundingBox } = this . props
284318 let folderList = storage . folders . map ( ( folder ) => {
285319 return < FolderItem key = { folder . key }
286320 folder = { folder }
287321 storage = { storage }
322+ hostBoundingBox = { hostBoundingBox }
288323 />
289324 } )
290325 return (
@@ -347,6 +382,14 @@ class StorageItem extends React.Component {
347382}
348383
349384StorageItem . propTypes = {
385+ hostBoundingBox : PropTypes . shape ( {
386+ bottom : PropTypes . number ,
387+ height : PropTypes . number ,
388+ left : PropTypes . number ,
389+ right : PropTypes . number ,
390+ top : PropTypes . number ,
391+ width : PropTypes . number
392+ } ) ,
350393 storage : PropTypes . shape ( {
351394 key : PropTypes . string
352395 } ) ,
0 commit comments