@@ -23,6 +23,87 @@ $(function() {
2323 self . neofluxMode = ko . observable ( "spatial_wave" ) ;
2424 self . neofluxSpeed = ko . observable ( 150 ) ;
2525
26+ self . neofluxPresets = ko . observableArray ( [ ] ) ;
27+ self . selectedPreset = ko . observable ( ) ;
28+
29+ self . neofluxEvents = ko . observableArray ( [
30+ "Startup" , "PrintStarted" , "PrintDone" , "PrintFailed" , "PrintPaused" , "PrintResumed"
31+ ] ) ;
32+ self . neofluxEventMappings = ko . observableArray ( [ ] ) ; // {event: "PrintStarted", preset: ko.observable("CyberPunk")}
33+
34+ self . refreshPresets = function ( ) {
35+ OctoPrint . simpleApiGet ( "livegcodecontrol" )
36+ . done ( function ( response ) {
37+ var presets = [ ] ;
38+ if ( response . presets ) {
39+ for ( var name in response . presets ) {
40+ presets . push ( { name : name , config : response . presets [ name ] } ) ;
41+ }
42+ }
43+ self . neofluxPresets ( presets ) ;
44+ } ) ;
45+ } ;
46+
47+ self . selectedPreset . subscribe ( function ( newPresetName ) {
48+ if ( newPresetName ) {
49+ var preset = ko . utils . arrayFirst ( self . neofluxPresets ( ) , function ( item ) {
50+ return item . name === newPresetName ;
51+ } ) ;
52+ if ( preset ) {
53+ self . neofluxMode ( preset . config . mode ) ;
54+ self . neofluxSpeed ( preset . config . speed ) ;
55+ if ( self . neofluxController && preset . config . colors ) {
56+ self . neofluxController . colors = preset . config . colors ; // Update colors
57+ }
58+ // Update preview immediately
59+ if ( self . neofluxController ) {
60+ self . neofluxController . updateConfig ( {
61+ mode : self . neofluxMode ( ) ,
62+ speed : parseInt ( self . neofluxSpeed ( ) )
63+ } ) ;
64+ }
65+ }
66+ }
67+ } ) ;
68+
69+ self . savePreset = function ( ) {
70+ var name = prompt ( "Enter preset name:" ) ;
71+ if ( name ) {
72+ if ( self . neofluxController ) {
73+ // Ensure local controller state is up to date before grabbing config
74+ self . neofluxController . updateConfig ( {
75+ mode : self . neofluxMode ( ) ,
76+ speed : parseInt ( self . neofluxSpeed ( ) )
77+ } ) ;
78+ }
79+
80+ var config = self . neofluxController ? self . neofluxController . getConfigPayload ( ) : {
81+ mode : self . neofluxMode ( ) ,
82+ speed : parseInt ( self . neofluxSpeed ( ) ) ,
83+ colors : [ "#FF0000" , "#0000FF" ] // Default fallback
84+ } ;
85+
86+ OctoPrint . simpleApiCommand ( "livegcodecontrol" , "save_preset" , {
87+ name : name ,
88+ config : config
89+ } ) . done ( function ( ) {
90+ self . refreshPresets ( ) ;
91+ } ) ;
92+ }
93+ } ;
94+
95+ self . deletePreset = function ( ) {
96+ var name = self . selectedPreset ( ) ;
97+ if ( name && confirm ( "Are you sure you want to delete preset '" + name + "'?" ) ) {
98+ OctoPrint . simpleApiCommand ( "livegcodecontrol" , "delete_preset" , {
99+ name : name
100+ } ) . done ( function ( ) {
101+ self . refreshPresets ( ) ;
102+ self . selectedPreset ( undefined ) ;
103+ } ) ;
104+ }
105+ } ;
106+
26107 self . applyNeoFluxConfig = function ( ) {
27108 if ( ! self . neofluxController ) return ;
28109
@@ -46,6 +127,21 @@ $(function() {
46127 console . error ( "Failed to update NEOFLUX config:" , response ) ;
47128 } ) ;
48129 } ;
130+
131+ self . testEventMapping = function ( mapping ) {
132+ var presetName = mapping . preset ( ) ;
133+ if ( presetName ) {
134+ var preset = ko . utils . arrayFirst ( self . neofluxPresets ( ) , function ( item ) {
135+ return item . name === presetName ;
136+ } ) ;
137+ if ( preset ) {
138+ // Just apply it live
139+ OctoPrint . simpleApiCommand ( "livegcodecontrol" , "update_led_config" , {
140+ payload : preset . config
141+ } ) ;
142+ }
143+ }
144+ } ;
49145 // ------------------------------
50146
51147 // --- Helper function to create a new rule object ---
@@ -135,12 +231,26 @@ $(function() {
135231 } ) ;
136232 self . rules ( mappedRules ) ;
137233 }
234+
235+ // NeoFlux Events
236+ self . refreshPresets ( ) ;
237+ var savedEvents = self . settingsViewModel . settings . plugins . livegcodecontrol . neoflux_events ( ) ;
238+ var mappings = [ ] ;
239+ ko . utils . arrayForEach ( self . neofluxEvents ( ) , function ( evt ) {
240+ var preset = savedEvents ? savedEvents [ evt ] : undefined ;
241+ mappings . push ( {
242+ event : evt ,
243+ preset : ko . observable ( preset )
244+ } ) ;
245+ } ) ;
246+ self . neofluxEventMappings ( mappings ) ;
138247 } ;
139248
140249 self . onSettingsShown = function ( ) {
141250 // Could refresh data from server if necessary, but usually onBeforeBinding is enough for settings
142251 // Ensure editing state is clear when settings are reshown
143252 self . cancelEdit ( ) ;
253+ self . refreshPresets ( ) ; // Ensure presets are up to date in settings
144254 } ;
145255
146256 self . onSettingsHidden = function ( ) {
@@ -160,6 +270,15 @@ $(function() {
160270 } ;
161271 } ) ;
162272 self . settingsViewModel . settings . plugins . livegcodecontrol . rules ( rulesToSave ) ;
273+
274+ // Save NeoFlux Events
275+ var eventsToSave = { } ;
276+ ko . utils . arrayForEach ( self . neofluxEventMappings ( ) , function ( mapping ) {
277+ if ( mapping . preset ( ) ) {
278+ eventsToSave [ mapping . event ] = mapping . preset ( ) ;
279+ }
280+ } ) ;
281+ self . settingsViewModel . settings . plugins . livegcodecontrol . neoflux_events ( eventsToSave ) ;
163282 } ;
164283 }
165284
0 commit comments