github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/scroll_xy.go (about)

     1  package html
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/MontFerret/ferret/pkg/drivers"
     7  	"github.com/MontFerret/ferret/pkg/runtime/core"
     8  	"github.com/MontFerret/ferret/pkg/runtime/values"
     9  	"github.com/MontFerret/ferret/pkg/runtime/values/types"
    10  )
    11  
    12  // SCROLL scrolls by given coordinates.
    13  // @param {HTMLDocument} document - HTML document.
    14  // @param {Int | Float} x - X coordinate.
    15  // @param {Int | Float} y - Y coordinate.
    16  // @param {Object} [params] - Scroll params.
    17  // @param {String} [params.behavior="instant"] - Scroll behavior
    18  // @param {String} [params.block="center"] - Scroll vertical alignment.
    19  // @param {String} [params.inline="center"] - Scroll horizontal alignment.
    20  func ScrollXY(ctx context.Context, args ...core.Value) (core.Value, error) {
    21  	err := core.ValidateArgs(args, 3, 4)
    22  
    23  	if err != nil {
    24  		return values.None, err
    25  	}
    26  
    27  	doc, err := drivers.ToDocument(args[0])
    28  
    29  	if err != nil {
    30  		return values.None, err
    31  	}
    32  
    33  	err = core.ValidateType(args[1], types.Int, types.Float)
    34  
    35  	if err != nil {
    36  		return values.None, err
    37  	}
    38  
    39  	err = core.ValidateType(args[2], types.Int, types.Float)
    40  
    41  	if err != nil {
    42  		return values.None, err
    43  	}
    44  
    45  	x := values.ToFloat(args[1])
    46  	y := values.ToFloat(args[2])
    47  
    48  	var opts drivers.ScrollOptions
    49  	opts.Top = x
    50  	opts.Left = y
    51  
    52  	if len(args) > 3 {
    53  		opts, err = toScrollOptions(args[3])
    54  
    55  		if err != nil {
    56  			return values.None, err
    57  		}
    58  
    59  		opts.Top = x
    60  		opts.Left = y
    61  	}
    62  
    63  	return values.True, doc.Scroll(ctx, opts)
    64  }