6
6
"github.com/maddalax/htmgo/framework/hx"
7
7
"github.com/maddalax/htmgo/framework/internal/util"
8
8
"strings"
9
+ "time"
9
10
)
10
11
11
12
type LifeCycle struct {
@@ -163,7 +164,7 @@ func NewComplexJsCommand(command string) ComplexJsCommand {
163
164
// SetText sets the inner text of the element.
164
165
func SetText (text string ) SimpleJsCommand {
165
166
// language=JavaScript
166
- return SimpleJsCommand {Command : fmt .Sprintf ("this.innerText = '%s'" , text )}
167
+ return SimpleJsCommand {Command : fmt .Sprintf ("(self || this) .innerText = '%s'" , text )}
167
168
}
168
169
169
170
// SetTextOnChildren sets the inner text of all the children of the element that match the selector.
@@ -180,25 +181,25 @@ func SetTextOnChildren(selector, text string) ComplexJsCommand {
180
181
// Increment increments the inner text of the element by the given amount.
181
182
func Increment (amount int ) SimpleJsCommand {
182
183
// language=JavaScript
183
- return SimpleJsCommand {Command : fmt .Sprintf ("this.innerText = parseInt(this.innerText) + %d" , amount )}
184
+ return SimpleJsCommand {Command : fmt .Sprintf ("(self || this) .innerText = parseInt((self || this) .innerText) + %d" , amount )}
184
185
}
185
186
186
187
// SetInnerHtml sets the inner HTML of the element.
187
188
func SetInnerHtml (r Ren ) SimpleJsCommand {
188
189
// language=JavaScript
189
- return SimpleJsCommand {Command : fmt .Sprintf ("this.innerHTML = `%s`" , Render (r ))}
190
+ return SimpleJsCommand {Command : fmt .Sprintf ("(self || this) .innerHTML = `%s`" , Render (r ))}
190
191
}
191
192
192
193
// SetOuterHtml sets the outer HTML of the element.
193
194
func SetOuterHtml (r Ren ) SimpleJsCommand {
194
195
// language=JavaScript
195
- return SimpleJsCommand {Command : fmt .Sprintf ("this.outerHTML = `%s`" , Render (r ))}
196
+ return SimpleJsCommand {Command : fmt .Sprintf ("(self || this) .outerHTML = `%s`" , Render (r ))}
196
197
}
197
198
198
199
// AddAttribute adds the given attribute to the element.
199
200
func AddAttribute (name , value string ) SimpleJsCommand {
200
201
// language=JavaScript
201
- return SimpleJsCommand {Command : fmt .Sprintf ("this.setAttribute('%s', '%s')" , name , value )}
202
+ return SimpleJsCommand {Command : fmt .Sprintf ("(self || this) .setAttribute('%s', '%s')" , name , value )}
202
203
}
203
204
204
205
// SetDisabled sets the disabled attribute on the element.
@@ -213,25 +214,25 @@ func SetDisabled(disabled bool) SimpleJsCommand {
213
214
// RemoveAttribute removes the given attribute from the element.
214
215
func RemoveAttribute (name string ) SimpleJsCommand {
215
216
// language=JavaScript
216
- return SimpleJsCommand {Command : fmt .Sprintf ("this.removeAttribute('%s')" , name )}
217
+ return SimpleJsCommand {Command : fmt .Sprintf ("(self || this) .removeAttribute('%s')" , name )}
217
218
}
218
219
219
220
// AddClass adds the given class to the element.
220
221
func AddClass (class string ) SimpleJsCommand {
221
222
// language=JavaScript
222
- return SimpleJsCommand {Command : fmt .Sprintf ("this.classList.add('%s')" , class )}
223
+ return SimpleJsCommand {Command : fmt .Sprintf ("(self || this) .classList.add('%s')" , class )}
223
224
}
224
225
225
226
// RemoveClass removes the given class from the element.
226
227
func RemoveClass (class string ) SimpleJsCommand {
227
228
// language=JavaScript
228
- return SimpleJsCommand {Command : fmt .Sprintf ("this.classList.remove('%s')" , class )}
229
+ return SimpleJsCommand {Command : fmt .Sprintf ("(self || this) .classList.remove('%s')" , class )}
229
230
}
230
231
231
232
// ToggleClass toggles the given class on the element.
232
233
func ToggleClass (class string ) SimpleJsCommand {
233
234
// language=JavaScript
234
- return SimpleJsCommand {Command : fmt .Sprintf ("this.classList.toggle('%s')" , class )}
235
+ return SimpleJsCommand {Command : fmt .Sprintf ("(self || this) .classList.toggle('%s')" , class )}
235
236
}
236
237
237
238
// ToggleText toggles the given text on the element.
@@ -391,23 +392,29 @@ func Alert(text string) SimpleJsCommand {
391
392
// Remove removes the element from the DOM.
392
393
func Remove () SimpleJsCommand {
393
394
// language=JavaScript
394
- return SimpleJsCommand {Command : "this.remove()" }
395
+ return SimpleJsCommand {Command : "(self || this) .remove()" }
395
396
}
396
397
397
398
// EvalJs evaluates the given JavaScript code.
398
399
func EvalJs (js string ) ComplexJsCommand {
399
400
return NewComplexJsCommand (js )
400
401
}
401
402
402
- func EvalCommandsOnSelector ( selector string , cmds ... Command ) ComplexJsCommand {
403
+ func CombineCommands ( cmds ... Command ) string {
403
404
lines := make ([]string , len (cmds ))
404
405
for i , cmd := range cmds {
405
406
lines [i ] = Render (cmd )
407
+ lines [i ] = strings .ReplaceAll (lines [i ], "(self || this)." , "self." )
406
408
lines [i ] = strings .ReplaceAll (lines [i ], "this." , "self." )
407
409
// some commands set the element we need to fix it so we arent redeclaring it
408
410
lines [i ] = strings .ReplaceAll (lines [i ], "let element =" , "element =" )
409
411
}
410
412
code := strings .Join (lines , "\n " )
413
+ return code
414
+ }
415
+
416
+ func EvalCommandsOnSelector (selector string , cmds ... Command ) ComplexJsCommand {
417
+ code := CombineCommands (cmds ... )
411
418
return EvalJs (fmt .Sprintf (`
412
419
let element = document.querySelector("%s");
413
420
@@ -444,7 +451,7 @@ func ConsoleLog(text string) SimpleJsCommand {
444
451
// SetValue sets the value of the element.
445
452
func SetValue (value string ) SimpleJsCommand {
446
453
// language=JavaScript
447
- return SimpleJsCommand {Command : fmt .Sprintf ("this.value = '%s'" , value )}
454
+ return SimpleJsCommand {Command : fmt .Sprintf ("(self || this) .value = '%s'" , value )}
448
455
}
449
456
450
457
// SubmitFormOnEnter submits the form when the user presses the enter key.
@@ -478,3 +485,31 @@ func InjectScriptIfNotExist(src string) ComplexJsCommand {
478
485
}
479
486
` , src , src ))
480
487
}
488
+
489
+ func RunOnInterval (time time.Duration , cmds ... Command ) ComplexJsCommand {
490
+ code := strings.Builder {}
491
+
492
+ for _ , cmd := range cmds {
493
+ code .WriteString (fmt .Sprintf (`
494
+ setInterval(function() {
495
+ %s
496
+ }, %d)
497
+ ` , Render (cmd ), time .Milliseconds ()))
498
+ }
499
+
500
+ return EvalJs (code .String ())
501
+ }
502
+
503
+ func RunAfterTimeout (time time.Duration , cmds ... Command ) ComplexJsCommand {
504
+ code := strings.Builder {}
505
+
506
+ for _ , cmd := range cmds {
507
+ code .WriteString (fmt .Sprintf (`
508
+ setTimeout(function() {
509
+ %s
510
+ }, %d)
511
+ ` , Render (cmd ), time .Milliseconds ()))
512
+ }
513
+
514
+ return EvalJs (code .String ())
515
+ }
0 commit comments