Пример #1
0
// Wrap returns a wrapper func that handles the conversion from native JavaScript *js.Object parameters
// to the following types.
//
// It supports *js.Object (left unmodified), dom.Document, dom.Element, dom.Event, dom.HTMLElement, dom.Node.
//
// For other types, the input is assumed to be a JSON string which is then unmarshalled into that type.
func Wrap(fn interface{}) func(...*js.Object) {
	v := reflect.ValueOf(fn)
	return func(args ...*js.Object) {
		in := make([]reflect.Value, v.Type().NumIn())
		for i := range in {
			switch t := v.Type().In(i); t {
			// *js.Object is passed through.
			case typeOf((**js.Object)(nil)):
				in[i] = reflect.ValueOf(args[i])

			// dom types are wrapped.
			case typeOf((*dom.Document)(nil)):
				in[i] = reflect.ValueOf(dom.WrapDocument(args[i]))
			case typeOf((*dom.Element)(nil)):
				in[i] = reflect.ValueOf(dom.WrapElement(args[i]))
			case typeOf((*dom.Event)(nil)):
				in[i] = reflect.ValueOf(dom.WrapEvent(args[i]))
			case typeOf((*dom.HTMLElement)(nil)):
				in[i] = reflect.ValueOf(dom.WrapHTMLElement(args[i]))
			case typeOf((*dom.Node)(nil)):
				in[i] = reflect.ValueOf(dom.WrapNode(args[i]))

			// Unmarshal incoming encoded JSON into the Go type.
			default:
				p := reflect.New(t)
				err := json.Unmarshal([]byte(args[i].String()), p.Interface())
				if err != nil {
					panic(err)
				}
				in[i] = reflect.Indirect(p)
			}
		}
		v.Call(in)
	}
}
Пример #2
0
// NewSVGPathEaser returns a new instance of the SVGPathEaser.
func NewSVGPathEaser(conf SVGConfig) *SVGPathEaser {
	var svg SVGPathEaser

	svg.conf = conf
	svg.svgElement = dom.WrapElement(Document().Underlying().Call("createElementNS", "http://www.w3.org/2000/svg", "path"))
	svg.svgElement.SetAttribute("d", conf.SVGPath)

	svg.pathlength = svg.svgElement.Underlying().Call("getTotalLength", nil).Int()

	svg.generateSampling()
	return &svg
}
Пример #3
0
// Position returns the current position of the dom.Element.
func Position(elem dom.Element) (float64, float64) {
	parent := OffsetParent(elem)

	var parentTop, parentLeft float64
	var marginTop, marginLeft float64
	var pBorderTop, pBorderLeft float64
	var pBorderTopObject *js.Object
	var pBorderLeftObject *js.Object

	nodeNameObject, err := GetProp(parent, "nodeName")
	if err == nil && !rootName.MatchString(strings.ToLower(nodeNameObject.String())) {
		parentElem := dom.WrapElement(parent)
		parentTop, parentLeft = Offset(parentElem)
	}

	if parent.Get("style") != nil {

		pBorderTopObject, err = GetProp(parent, "style.borderTopWidth")
		if err == nil {
			pBorderTop = ParseFloat(pBorderTopObject.String())
		}

		pBorderLeftObject, err = GetProp(parent, "style.borderLeftWidth")
		if err == nil {
			pBorderLeft = ParseFloat(pBorderLeftObject.String())
		}

		parentTop += pBorderTop
		parentLeft += pBorderLeft
	}

	css, _ := GetComputedStyle(elem, "")

	marginTopObject, err := GetComputedStyleValueWith(css, "margin-top")
	if err == nil {
		marginTop = ParseFloat(marginTopObject.String())
	}

	marginLeftObject, err := GetComputedStyleValueWith(css, "margin-left")
	if err == nil {
		marginLeft = ParseFloat(marginLeftObject.String())
	}

	elemTop, elemLeft := Offset(elem)

	elemTop -= marginTop
	elemLeft -= marginLeft

	return elemTop - parentTop, elemLeft - parentLeft
}
Пример #4
0
// CreateElement is a shortcut to create an element with the given name
func CreateElement(name string) dom.Element {
	return dom.WrapElement(js.Global.Get("document").Call("createElement", name))
}