22// Distributed under the terms of the Modified BSD License.
33
44import {
5- Toolbar , ToolbarButton
5+ Message
6+ } from '@phosphor/messaging' ;
7+
8+ import {
9+ Widget
10+ } from '@phosphor/widgets' ;
11+
12+ import {
13+ Styling , Toolbar , ToolbarButton
614} from '@quantlab/apputils' ;
715
816import {
917 SpreadsheetPanel
1018} from './panel' ;
1119
20+ import {
21+ Spreadsheet
22+ } from './widget' ;
1223
1324/**
1425 * The class name added to toolbar save button.
1526 */
1627const TOOLBAR_SAVE_CLASS = 'jp-SaveIcon' ;
1728
29+ const TOOLBAR_LABEL_CLASS = 'jp-CSVToolbar-label' ;
30+
31+ const TOOLBAR_DROPDOWN_CLASS = 'jp-Spreadsheet-toolbarDropdown' ;
1832
1933/**
2034 * A namespace for the default toolbar items.
@@ -40,6 +54,86 @@ namespace ToolbarItems {
4054 } ) ;
4155 }
4256
57+ /**
58+ * Create bold button toolbar item.
59+ */
60+ export
61+ function createBoldButton ( panel : SpreadsheetPanel ) : ToolbarButton {
62+ return new ToolbarButton ( {
63+ className : 'fa fa-bold' ,
64+ onClick : ( ) => {
65+ panel . spreadsheet . style ( 'fontWeight' , 'bold' ) ;
66+ } ,
67+ tooltip : 'Set font weight to Bold'
68+ } ) ;
69+ }
70+
71+ /**
72+ * Create italic button toolbar item.
73+ */
74+ export
75+ function createItalicButton ( panel : SpreadsheetPanel ) : ToolbarButton {
76+ return new ToolbarButton ( {
77+ className : 'fa fa-italic' ,
78+ onClick : ( ) => {
79+ panel . spreadsheet . style ( 'fontStyle' , 'italic' ) ;
80+ } ,
81+ tooltip : 'Set font style to Italic'
82+ } ) ;
83+ }
84+
85+ /**
86+ * Create align left button toolbar item.
87+ */
88+ export
89+ function createAlignLeftButton ( panel : SpreadsheetPanel ) : ToolbarButton {
90+ return new ToolbarButton ( {
91+ className : 'fa fa-align-left' ,
92+ onClick : ( ) => {
93+ panel . spreadsheet . style ( 'className' , 'htLeft' ) ;
94+ } ,
95+ tooltip : 'Align to left'
96+ } ) ;
97+ }
98+
99+ /**
100+ * Create align center button toolbar item.
101+ */
102+ export
103+ function createAlignCenterButton ( panel : SpreadsheetPanel ) : ToolbarButton {
104+ return new ToolbarButton ( {
105+ className : 'fa fa-align-center' ,
106+ onClick : ( ) => {
107+ panel . spreadsheet . style ( 'className' , 'htCenter' ) ;
108+ } ,
109+ tooltip : 'Align to center'
110+ } ) ;
111+ }
112+
113+ /**
114+ * Create align left button toolbar item.
115+ */
116+ export
117+ function createAlignRightButton ( panel : SpreadsheetPanel ) : ToolbarButton {
118+ return new ToolbarButton ( {
119+ className : 'fa fa-align-right' ,
120+ onClick : ( ) => {
121+ panel . spreadsheet . style ( 'className' , 'htRight' ) ;
122+ } ,
123+ tooltip : 'Align to right'
124+ } ) ;
125+ }
126+
127+
128+ export
129+ function createFillColorItem ( panel : SpreadsheetPanel ) : Widget {
130+ return new ColorSwitcher ( panel . spreadsheet , 'Fill Color' ) ;
131+ }
132+ export
133+ function createFontColorItem ( panel : SpreadsheetPanel ) : Widget {
134+ return new ColorSwitcher ( panel . spreadsheet , 'Font Color' ) ;
135+ }
136+
43137 /**
44138 * Add the default items to the panel toolbar.
45139 */
@@ -49,9 +143,180 @@ namespace ToolbarItems {
49143 toolbar . addItem ( 'save' , createSaveButton ( panel ) ) ;
50144 toolbar . addItem ( 'interrupt' , Toolbar . createInterruptButton ( panel . session ) ) ;
51145 toolbar . addItem ( 'restart' , Toolbar . createRestartButton ( panel . session ) ) ;
146+
147+ toolbar . addItem ( 'bold' , createBoldButton ( panel ) ) ;
148+ toolbar . addItem ( 'italic' , createItalicButton ( panel ) ) ;
149+ toolbar . addItem ( 'left' , createAlignLeftButton ( panel ) ) ;
150+ toolbar . addItem ( 'center' , createAlignCenterButton ( panel ) ) ;
151+ toolbar . addItem ( 'right' , createAlignRightButton ( panel ) ) ;
152+
153+ toolbar . addItem ( 'fill' , createFillColorItem ( panel ) ) ;
154+ toolbar . addItem ( 'font' , createFontColorItem ( panel ) ) ;
155+
52156 toolbar . addItem ( 'spacer' , Toolbar . createSpacerItem ( ) ) ;
53157 toolbar . addItem ( 'kernelName' , Toolbar . createKernelNameItem ( panel . session ) ) ;
54158 toolbar . addItem ( 'kernelStatus' , Toolbar . createKernelStatusItem ( panel . session ) ) ;
55159 }
56160
57161}
162+
163+
164+ /**
165+ * A toolbar widget that switches fill or font color
166+ */
167+ class ColorSwitcher extends Widget {
168+ /**
169+ * Construct a new color switcher.
170+ */
171+ constructor ( widget : Spreadsheet , prompt : string ) {
172+ super ( { node : createColorSwitcherNode ( prompt ) } ) ;
173+ //this.addClass(TOOLBAR_COLOR_CLASS);
174+
175+ this . _select = this . node . firstChild as HTMLSelectElement ;
176+ Styling . wrapSelect ( this . _select ) ;
177+ this . _wildCard = document . createElement ( 'option' ) ;
178+ this . _wildCard . value = '-' ;
179+ this . _wildCard . textContent = '-' ;
180+ this . _spreadsheet = widget ;
181+
182+ // Set the initial value.
183+ if ( widget . model ) {
184+ this . _updateValue ( ) ;
185+ }
186+
187+ // Follow the type of the active cell.
188+ widget . activeCellChanged . connect ( this . _updateValue , this ) ;
189+
190+ // Follow a change in the selection.
191+ widget . selectionChanged . connect ( this . _updateValue , this ) ;
192+ }
193+
194+ /**
195+ * Handle the DOM events for the widget.
196+ *
197+ * @param event - The DOM event sent to the widget.
198+ *
199+ * #### Notes
200+ * This method implements the DOM `EventListener` interface and is
201+ * called in response to events on the dock panel's node. It should
202+ * not be called directly by user code.
203+ */
204+ handleEvent ( event : Event ) : void {
205+ switch ( event . type ) {
206+ case 'change' :
207+ this . _evtChange ( event ) ;
208+ break ;
209+ case 'keydown' :
210+ this . _evtKeyDown ( event as KeyboardEvent ) ;
211+ break ;
212+ default :
213+ break ;
214+ }
215+ }
216+
217+ /**
218+ * Handle `after-attach` messages for the widget.
219+ */
220+ protected onAfterAttach ( msg : Message ) : void {
221+ this . _select . addEventListener ( 'change' , this ) ;
222+ this . _select . addEventListener ( 'keydown' , this ) ;
223+ }
224+
225+ /**
226+ * Handle `before-detach` messages for the widget.
227+ */
228+ protected onBeforeDetach ( msg : Message ) : void {
229+ this . _select . removeEventListener ( 'change' , this ) ;
230+ this . _select . removeEventListener ( 'keydown' , this ) ;
231+ }
232+
233+ /**
234+ * Handle `changed` events for the widget.
235+ */
236+ private _evtChange ( event : Event ) : void {
237+ let select = this . _select ;
238+ let widget = this . _spreadsheet ;
239+ if ( select . value === '-' ) {
240+ return ;
241+ }
242+ if ( ! this . _changeGuard ) {
243+ let value = select . value ;
244+ if ( select . options [ 0 ] . value == 'fill color' ) {
245+ widget . style ( 'background' , value ) ;
246+ select . value = 'fill color' ;
247+ } else if ( select . options [ 0 ] . value == 'font color' ) {
248+ widget . style ( 'color' , value ) ;
249+ select . value = 'font color' ;
250+ }
251+ widget . activate ( ) ;
252+ }
253+ }
254+
255+ /**
256+ * Handle `keydown` events for the widget.
257+ */
258+ private _evtKeyDown ( event : KeyboardEvent ) : void {
259+ if ( event . keyCode === 13 ) { // Enter
260+ this . _spreadsheet . activate ( ) ;
261+ }
262+ }
263+
264+ /**
265+ * Update the value of the dropdown from the widget state.
266+ */
267+ private _updateValue ( ) : void {
268+ let widget = this . _spreadsheet ;
269+ let select = this . _select ;
270+ if ( ! widget . activeCell ) {
271+ return ;
272+ }
273+ let mColor : string = '' ; //widget.activeCell.model.type;
274+ //for (let i = 0; i < widget.widgets.length; i++) {
275+ // let child = widget.widgets[i];
276+ // if (widget.isSelected(child)) {
277+ // if (child.model.type !== mType) {
278+ // mType = '-';
279+ // select.appendChild(this._wildCard);
280+ // break;
281+ // }
282+ // }
283+ //}
284+ if ( mColor !== '-' ) {
285+ select . remove ( 3 ) ;
286+ }
287+ this . _changeGuard = true ;
288+ select . value = mColor ;
289+ this . _changeGuard = false ;
290+
291+ }
292+
293+ private _changeGuard = false ;
294+ private _wildCard : HTMLOptionElement = null ;
295+ private _select : HTMLSelectElement = null ;
296+ private _spreadsheet : Spreadsheet = null ;
297+ }
298+
299+
300+ /**
301+ * Create the node for the cell type switcher.
302+ */
303+ function createColorSwitcherNode ( prompt : string ) : HTMLElement {
304+ let div = document . createElement ( 'div' ) ;
305+ let label = document . createElement ( 'span' ) ;
306+ label . textContent = prompt ;
307+ label . className = TOOLBAR_LABEL_CLASS ;
308+ let select = document . createElement ( 'select' ) ;
309+ for ( let t of [ prompt , 'red' , 'yellow' , 'blue' ] ) {
310+ let option = document . createElement ( 'option' ) ;
311+ option . value = t . toLowerCase ( ) ;
312+ option . textContent = t ;
313+ option . style . backgroundColor = t ;
314+ select . appendChild ( option ) ;
315+ }
316+ select . className = TOOLBAR_DROPDOWN_CLASS ;
317+ //div.appendChild(label);
318+ //let node = Styling.wrapSelect(select);
319+ //node.classList.add(TOOLBAR_DROPDOWN_CLASS);
320+ div . appendChild ( select ) ;
321+ return div ;
322+ }
0 commit comments