@@ -41,73 +41,72 @@ function getCustomValue(mechanismInRobot: storageModuleContent.MechanismInRobot
4141 // If the event is defined in the robot, mechanismInRobot is null.
4242 return ( mechanismInRobot === null )
4343 ? CUSTOM_CATEGORY_EVENT_HANDLERS_ROBOT
44- : CUSTOM_CATEGORY_EVENT_HANDLERS_MECHANISM_PREFIX + mechanismInRobot . name ;
44+ : CUSTOM_CATEGORY_EVENT_HANDLERS_MECHANISM_PREFIX + mechanismInRobot . mechanismId ;
4545}
4646
47- export function registerRobotEventHandlersCategory ( blocklyWorkspace : Blockly . WorkspaceSvg ) : void {
48- new EventHandlersCategory ( blocklyWorkspace , null ) ;
49- }
47+ export function getRobotEventHandlersCategory ( editor : Editor ) : toolboxItems . Category {
48+ const blocklyWorkspace = editor . getBlocklyWorkspace ( ) ;
5049
51- export function getRobotEventHandlersCategory ( ) : toolboxItems . Category {
50+ // If this category hasn't been register yet, do it now.
51+ const customValue = getCustomValue ( null ) ;
52+ if ( ! blocklyWorkspace . getToolboxCategoryCallback ( customValue ) ) {
53+ const category = new EventHandlersCategory ( null ) ;
54+ blocklyWorkspace . registerToolboxCategoryCallback ( customValue , category . robotEventHandlersFlyout . bind ( category ) ) ;
55+ }
5256 return {
5357 kind : 'category' ,
5458 name : Blockly . Msg [ 'MRC_CATEGORY_EVENTS' ] ,
55- custom : getCustomValue ( null ) ,
59+ custom : customValue ,
5660 } ;
5761}
5862
59- export function registerMechanismEventHandlersCategory (
60- blocklyWorkspace : Blockly . WorkspaceSvg , mechanismInRobot : storageModuleContent . MechanismInRobot ) : void {
61- new EventHandlersCategory ( blocklyWorkspace , mechanismInRobot ) ;
62- }
63-
6463export function getMechanismEventHandlersCategory (
64+ editor : Editor ,
6565 mechanismInRobot : storageModuleContent . MechanismInRobot ) : toolboxItems . Category {
66+ const blocklyWorkspace = editor . getBlocklyWorkspace ( ) ;
67+
68+ // If this category hasn't been register yet, do it now.
69+ const customValue = getCustomValue ( mechanismInRobot ) ;
70+ if ( ! blocklyWorkspace . getToolboxCategoryCallback ( customValue ) ) {
71+ const category = new EventHandlersCategory ( mechanismInRobot ) ;
72+ blocklyWorkspace . registerToolboxCategoryCallback ( customValue , category . mechanismEventHandlersFlyout . bind ( category ) ) ;
73+ }
6674 return {
6775 kind : 'category' ,
6876 name : Blockly . Msg [ 'MRC_CATEGORY_EVENTS' ] ,
69- custom : getCustomValue ( mechanismInRobot ) ,
77+ custom : customValue ,
7078 } ;
7179}
7280
7381class EventHandlersCategory {
7482 // If the event is defined in the robot, mechanismInRobot is null.
7583 mechanismInRobot : storageModuleContent . MechanismInRobot | null ;
7684
77- constructor (
78- blocklyWorkspace : Blockly . WorkspaceSvg ,
79- mechanismInRobot : storageModuleContent . MechanismInRobot | null ) {
85+ constructor ( mechanismInRobot : storageModuleContent . MechanismInRobot | null ) {
8086 this . mechanismInRobot = mechanismInRobot ;
81- if ( mechanismInRobot === null ) {
82- blocklyWorkspace . registerToolboxCategoryCallback (
83- getCustomValue ( mechanismInRobot ) ,
84- this . robotEventHandlersFlyout . bind ( this ) ) ;
85- } else {
86- blocklyWorkspace . registerToolboxCategoryCallback (
87- getCustomValue ( mechanismInRobot ) ,
88- this . mechanismEventHandlersFlyout . bind ( this ) ) ;
89- }
9087 }
9188
9289 public robotEventHandlersFlyout ( workspace : Blockly . WorkspaceSvg ) {
93- const contents : toolboxItems . ContentsType [ ] = [ ] ;
94-
9590 const editor = Editor . getEditorForBlocklyWorkspace ( workspace ) ;
96- if ( editor ) {
97- // Get the list of events from the robot.
98- const eventsFromRobot = editor . getEventsFromRobot ( ) ;
99- // Remove events if there is already a corresponding handler in the workspace.
100- const eventHandlerBlocks = editor . getRobotEventHandlersAlreadyInWorkspace ( ) ;
101- const eventIds : string [ ] = [ ] ;
102- eventHandlerBlocks . forEach ( eventHandlerBlock => {
103- eventIds . push ( eventHandlerBlock . getEventId ( ) ) ;
104- } ) ;
105- const eventsToShow = eventsFromRobot . filter ( event => {
106- return ! eventIds . includes ( event . eventId ) ;
107- } ) ;
108- addRobotEventHandlerBlocks ( eventsToShow , contents ) ;
91+ if ( ! editor ) {
92+ throw new Error ( 'No editor for blockly workspace' ) ;
10993 }
11094
95+ const contents : toolboxItems . ContentsType [ ] = [ ] ;
96+
97+ // Get the list of events from the robot.
98+ const eventsFromRobot = editor . getEventsFromRobot ( ) ;
99+ // Remove events if there is already a corresponding handler in the workspace.
100+ const eventHandlerBlocks = editor . getRobotEventHandlersAlreadyInWorkspace ( ) ;
101+ const eventIds : string [ ] = [ ] ;
102+ eventHandlerBlocks . forEach ( eventHandlerBlock => {
103+ eventIds . push ( eventHandlerBlock . getEventId ( ) ) ;
104+ } ) ;
105+ const eventsToShow = eventsFromRobot . filter ( event => {
106+ return ! eventIds . includes ( event . eventId ) ;
107+ } ) ;
108+ addRobotEventHandlerBlocks ( eventsToShow , contents ) ;
109+
111110 const toolboxInfo = {
112111 contents : contents ,
113112 } ;
@@ -116,29 +115,34 @@ class EventHandlersCategory {
116115 }
117116
118117 public mechanismEventHandlersFlyout ( workspace : Blockly . WorkspaceSvg ) {
118+ const editor = Editor . getEditorForBlocklyWorkspace ( workspace ) ;
119+ if ( ! editor ) {
120+ throw new Error ( 'No editor for blockly workspace' ) ;
121+ }
122+ if ( ! this . mechanismInRobot ) {
123+ throw new Error ( 'mechanismInRobot is null' ) ;
124+ }
125+
119126 const contents : toolboxItems . ContentsType [ ] = [ ] ;
120127
121- const editor = Editor . getEditorForBlocklyWorkspace ( workspace ) ;
122- if ( editor && this . mechanismInRobot ) {
123- // Get the list of events from the mechanism.
124- const mechanism = editor . getMechanism ( this . mechanismInRobot ) ;
125- if ( mechanism ) {
126- const eventsFromMechanism = editor . getEventsFromMechanism ( mechanism ) ;
127- // Remove events if there is already a corresponding handler in the workspace.
128- const eventHandlerBlocks = editor . getMechanismEventHandlersAlreadyInWorkspace (
129- this . mechanismInRobot ) ;
130- const eventIds : string [ ] = [ ] ;
131- eventHandlerBlocks . forEach ( eventHandlerBlock => {
132- eventIds . push ( eventHandlerBlock . getEventId ( ) ) ;
133- } ) ;
134- const eventsToShow = eventsFromMechanism . filter ( event => {
135- return ! eventIds . includes ( event . eventId ) ;
136- } ) ;
137- addMechanismEventHandlerBlocks ( this . mechanismInRobot , eventsToShow , contents ) ;
138- if ( contents . length === 0 ) {
139- const label : toolboxItems . Label = new toolboxItems . Label ( Blockly . Msg [ 'NO_MECHANISM_CONTENTS' ] ) ;
140- contents . push ( label ) ;
141- }
128+ // Get the list of events from the mechanism.
129+ const mechanism = editor . getMechanism ( this . mechanismInRobot ) ;
130+ if ( mechanism ) {
131+ const eventsFromMechanism = editor . getEventsFromMechanism ( mechanism ) ;
132+ // Remove events if there is already a corresponding handler in the workspace.
133+ const eventHandlerBlocks = editor . getMechanismEventHandlersAlreadyInWorkspace (
134+ this . mechanismInRobot ) ;
135+ const eventIds : string [ ] = [ ] ;
136+ eventHandlerBlocks . forEach ( eventHandlerBlock => {
137+ eventIds . push ( eventHandlerBlock . getEventId ( ) ) ;
138+ } ) ;
139+ const eventsToShow = eventsFromMechanism . filter ( event => {
140+ return ! eventIds . includes ( event . eventId ) ;
141+ } ) ;
142+ addMechanismEventHandlerBlocks ( this . mechanismInRobot , eventsToShow , contents ) ;
143+ if ( contents . length === 0 ) {
144+ const label : toolboxItems . Label = new toolboxItems . Label ( Blockly . Msg [ 'NO_MECHANISM_CONTENTS' ] ) ;
145+ contents . push ( label ) ;
142146 }
143147 }
144148
0 commit comments