@@ -2,97 +2,75 @@ import React, {Component} from 'react';
2
2
import PropTypes from 'prop-types' ;
3
3
import { SearchIcon , ThumnailViewIcon , GraphIcon } from 'plotly-icons' ;
4
4
import Modal , { ModalContent } from 'components/containers/Modal' ;
5
- import {
6
- traceTypeToPlotlyInitFigure ,
7
- localize ,
8
- plotlyTraceToCustomTrace ,
9
- computeTraceOptionsFromSchema ,
10
- } from 'lib' ;
5
+ import { traceTypeToPlotlyInitFigure , localize } from 'lib' ;
11
6
12
- // to be removed when icons are converted to svg
13
- function slugify ( text ) {
14
- return text
15
- . toString ( )
16
- . toLowerCase ( )
17
- . replace ( / \s + / g, '-' ) // Replace spaces with -
18
- . replace ( / [ ^ \w \- ] + / g, '' ) // Remove all non-word chars
19
- . replace ( / \- \- + / g, '-' ) // Replace multiple - with single -
20
- . replace ( / ^ - + / , '' ) // Trim - from start of text
21
- . replace ( / - + $ / , '' ) ; // Trim - from end of text
22
- }
23
-
24
- const Item = ( { item, active, columnLength, columnIndex, handleClick} ) => {
25
- const isEven = value => value % 2 === 0 ;
26
- const middle = Math . floor ( columnLength / 2 ) ;
7
+ const actions = ( { value} ) => [
8
+ {
9
+ label : `Charts like this by Plotly users.` ,
10
+ href : `https://plot.ly/feed/?q=plottype:${ value } ` ,
11
+ icon : < SearchIcon /> ,
12
+ } ,
13
+ {
14
+ label : `View tutorials on this chart type.` ,
15
+ href : `#` , // update
16
+ icon : < ThumnailViewIcon /> ,
17
+ } ,
18
+ {
19
+ label : `See a basic example.` ,
20
+ href : `#` , // update
21
+ icon : < GraphIcon /> ,
22
+ } ,
23
+ ] ;
27
24
28
- // for left leaning columns
29
- let position = '-right' ;
30
-
31
- // if we have an even number of columns
32
- // we want to have the 2 center rows display their tooltip
33
- // in the middle vs left/right
34
- if ( isEven ( columnLength ) && columnLength > 3 ) {
35
- if ( columnIndex === middle || columnIndex === middle - 1 ) {
36
- position = '' ;
37
- }
38
- } else {
39
- if ( columnIndex === middle ) {
40
- position = '' ;
41
- }
42
- }
25
+ /**
26
+ * This renders our item actions
27
+ */
28
+ const renderActionItems = ( actionItems , item ) =>
29
+ actionItems ( item ) . map ( ( action , i ) => (
30
+ < a
31
+ className = "trace-item__actions__item"
32
+ key = { i }
33
+ aria-label = { action . label }
34
+ data-microtip-position = { `top-left` }
35
+ role = "tooltip"
36
+ href = { action . href }
37
+ target = "_blank"
38
+ >
39
+ { action . icon }
40
+ </ a >
41
+ ) ) ;
43
42
44
- // for right leaning columns
45
- if ( columnIndex > middle ) {
46
- position = '-left' ;
47
- }
48
- const { label, value, type } = item ;
43
+ /**
44
+ * Trace Type Item
45
+ */
46
+ const Item = ( { item , active , handleClick } ) => {
47
+ const { label, value} = item ;
49
48
return (
50
49
< div
51
50
className = { `trace-item${ active ? ' trace-item--active' : '' } ` }
52
51
onClick = { ( ) => handleClick ( ) }
53
52
>
54
53
< div className = "trace-item__actions" >
55
- < a
56
- className = "trace-item__actions__item"
57
- aria-label = "Charts like this by Plotly users."
58
- data-microtip-position = { `top${ position } ` }
59
- role = "tooltip"
60
- href = { `https://plot.ly/feed/?q=plottype:${ type } ` }
61
- target = "_blank"
62
- >
63
- < SearchIcon />
64
- </ a >
65
- < div
66
- className = "trace-item__actions__item"
67
- aria-label = "View tutorials on this chart type."
68
- data-microtip-position = { `top${ position } ` }
69
- role = "tooltip"
70
- >
71
- < ThumnailViewIcon />
72
- </ div >
73
- < div
74
- className = "trace-item__actions__item"
75
- aria-label = "See a basic example."
76
- data-microtip-position = { `bottom${ position } ` }
77
- role = "tooltip"
78
- >
79
- < GraphIcon />
80
- </ div >
54
+ { actions ? renderActionItems ( actions , item ) : null }
81
55
</ div >
82
56
< div className = "trace-item__image" >
83
- < img src = { `/_temp/ic-${ slugify ( value ) } .svg` } />
57
+ < img src = { `/_temp/ic-${ value } .svg` } />
84
58
</ div >
85
59
< div className = "trace-item__label" > { label } </ div >
86
60
</ div >
87
61
) ;
88
62
} ;
89
63
64
+ /**
65
+ * Trace Type Selector
66
+ */
90
67
class TraceTypeSelector extends Component {
91
68
selectAndClose ( value ) {
92
69
const computedValue = traceTypeToPlotlyInitFigure ( value ) ;
93
70
this . props . updateContainer ( computedValue ) ;
94
71
this . context . handleClose ( ) ;
95
72
}
73
+
96
74
renderCategories ( ) {
97
75
const { fullValue, localize : _ } = this . props ;
98
76
const { traces, categories} = this . context . traceSelectorConfig ;
@@ -101,14 +79,21 @@ class TraceTypeSelector extends Component {
101
79
const items = traces ( _ ) . filter (
102
80
( { category : { value} } ) => value === category . value
103
81
) ;
82
+
83
+ const MAX_ITEMS = 6 ;
84
+ let columnClasses = 'trace-grid__column' ;
85
+
86
+ // If the category has more than 6 items, it will span 2 columns
87
+ if ( items . length > MAX_ITEMS ) {
88
+ columnClasses += ' trace-grid__column--double' ;
89
+ }
90
+
104
91
return (
105
- < div className = "trace-grid__column" key = { i } >
92
+ < div className = { columnClasses } key = { i } >
106
93
< div className = "trace-grid__column__header" > { category . label } </ div >
107
94
< div className = "trace-grid__column__items" >
108
95
{ items . map ( item => (
109
96
< Item
110
- columnLength = { categories . length }
111
- columnIndex = { i }
112
97
key = { item . value }
113
98
active = { fullValue === item . value }
114
99
item = { item }
@@ -124,15 +109,16 @@ class TraceTypeSelector extends Component {
124
109
render ( ) {
125
110
return (
126
111
< Modal title = "Select Chart Type" >
127
- < ModalContent >
128
112
< div className = "trace-grid" > { this . renderCategories ( ) } </ div >
129
- </ ModalContent >
130
113
</ Modal >
131
114
) ;
132
115
}
133
116
}
117
+
134
118
TraceTypeSelector . propTypes = {
135
119
updateContainer : PropTypes . func ,
120
+ fullValue : PropTypes . string ,
121
+ localize : PropTypes . func ,
136
122
} ;
137
123
TraceTypeSelector . contextTypes = {
138
124
traceSelectorConfig : PropTypes . object ,
@@ -141,6 +127,7 @@ TraceTypeSelector.contextTypes = {
141
127
Item . propTypes = {
142
128
item : PropTypes . object ,
143
129
active : PropTypes . bool ,
130
+ handleClick : PropTypes . func ,
144
131
} ;
145
132
146
133
export default localize ( TraceTypeSelector ) ;
0 commit comments