func ok(kp *js.Object) { print("ok") print(kp) fmt.Printf("%d\n", kp.Get("byteLength").Int()) x := tmp.Call("view", kp) log.Println(x) }
// TriggerBindEvent connects the giving event with the provided dom target. func (dm *DOMRenderer) TriggerBindEvent(event *gjs.Object, root *gjs.Object, source *trees.Event) { target := event.Get("target") children := root.Call("querySelectorAll", source.Target()) if children == nil || children == gjs.Undefined { return } kids := js.DOMObjectToList(children) var match bool for _, item := range kids { if item != target { continue } match = true break } // if we match then run the listeners registered. if match { dispatch.Dispatch(trees.EventBroadcast{ EventID: source.EventID, Event: trees.NewWrapperEvent(event), }) } }
func copyStruct(dst, src *js.Object, typ Type) { fields := jsType(typ).Get("fields") for i := 0; i < fields.Length(); i++ { prop := fields.Index(i).Get("prop").String() dst.Set(prop, src.Get(prop)) } }
// NewContext takes an HTML5 canvas object and optional context attributes. // If an error is returned it means you won't have access to WebGL // functionality. func NewContext(canvas js.Object, ca *ContextAttributes) (*Context, error) { if js.Global.Get("WebGLRenderingContext") == js.Undefined { return nil, errors.New("Your browser doesn't appear to support webgl.") } if ca == nil { ca = DefaultAttributes() } attrs := map[string]bool{ "alpha": ca.Alpha, "depth": ca.Depth, "stencil": ca.Stencil, "antialias": ca.Antialias, "premultipliedAlpha": ca.PremultipliedAlpha, "preserveDrawingBuffer": ca.PreserveDrawingBuffer, } gl := canvas.Call("getContext", "webgl", attrs) if gl == nil { gl = canvas.Call("getContext", "experimental-webgl", attrs) if gl == nil { return nil, errors.New("Creating a webgl context has failed.") } } ctx := new(Context) ctx.Object = gl return ctx, nil }
func ConsoleEvent(name string, event *jquery.Event, data *js.Object) { page := data.Get("toPage").String() if page == "[object Object]" { page = data.Get("toPage").Call("jqmData", "url").String() } console.Log("Event: %s, Current page: %s", name, page) }
func stringable(t *js.Object) bool { switch t.Get("kind").Int() { case boolKind, int8Kind, int16Kind, int32Kind, intKind, uint8Kind, uint16Kind, uint32Kind, uintKind, int64Kind, uint64Kind, float32Kind, float64Kind, stringKind: return true } return false }
func (ws *webSocket) sendPayload(action *Action) (err error) { defer func() { err = jsError(recover()) }() decodeDataURI := (action.String() == "update_user") for _, frame := range action.Payload { var data *js.Object = frame if decodeDataURI { base64 := data.Call("split", ",").Index(1) binaryString := js.Global.Call("atob", base64) length := binaryString.Length() data = js.Global.Get("ArrayBuffer").New(length) array := js.Global.Get("Uint8Array").New(data) for i := 0; i < length; i++ { array.SetIndex(i, binaryString.Call("charCodeAt", i)) } } ws.impl.Call("send", data) } return }
func (v Value) Slice3(i, j, k int) Value { var ( cap int typ Type s *js.Object ) switch kind := v.kind(); kind { case Array: if v.flag&flagAddr == 0 { panic("reflect.Value.Slice: slice of unaddressable array") } tt := (*arrayType)(unsafe.Pointer(v.typ)) cap = int(tt.len) typ = SliceOf(tt.elem) s = jsType(typ).New(v.object()) case Slice: typ = v.typ s = v.object() cap = s.Get("$capacity").Int() default: panic(&ValueError{"reflect.Value.Slice3", kind}) } if i < 0 || j < i || k < j || k > cap { panic("reflect.Value.Slice3: slice index out of bounds") } return makeValue(typ, js.Global.Call("$subslice", s, i, j, k), v.flag&flagRO) }
func copyStruct(dst, src js.Object, typ Type) { fields := jsType(typ).Get("fields") for i := 0; i < fields.Length(); i++ { name := fields.Index(i).Index(0).Str() dst.Set(name, src.Get(name)) } }
func makeValue(t Type, v *js.Object, fl flag) Value { rt := t.common() if t.Kind() == Array || t.Kind() == Struct || t.Kind() == Ptr { return Value{rt, unsafe.Pointer(v.Unsafe()), fl | flag(t.Kind())} } return Value{rt, unsafe.Pointer(js.Global.Call("$newDataPointer", v, jsType(rt.ptrTo())).Unsafe()), fl | flag(t.Kind()) | flagIndir} }
func cvtDirect(v Value, typ Type) Value { var srcVal = v.object() if srcVal == jsType(v.typ).Get("nil") { return makeValue(typ, jsType(typ).Get("nil"), v.flag) } var val *js.Object switch k := typ.Kind(); k { case Slice: slice := jsType(typ).New(srcVal.Get("$array")) slice.Set("$offset", srcVal.Get("$offset")) slice.Set("$length", srcVal.Get("$length")) slice.Set("$capacity", srcVal.Get("$capacity")) val = js.Global.Call("$newDataPointer", slice, jsType(PtrTo(typ))) case Ptr: if typ.Elem().Kind() == Struct { if typ.Elem() == v.typ.Elem() { val = srcVal break } val = jsType(typ).New() copyStruct(val, srcVal, typ.Elem()) break } val = jsType(typ).New(srcVal.Get("$get"), srcVal.Get("$set")) case Struct: val = jsType(typ).Get("ptr").New() copyStruct(val, srcVal, typ) case Array, Bool, Chan, Func, Interface, Map, String: val = js.InternalObject(v.ptr) default: panic(&ValueError{"reflect.Convert", k}) } return Value{typ.common(), unsafe.Pointer(val.Unsafe()), v.flag&(flagRO|flagIndir) | flag(typ.Kind())} }
func makeValue(t Type, v js.Object, fl flag) Value { rt := t.common() if t.Size() > ptrSize && t.Kind() != Array && t.Kind() != Struct { return Value{rt, unsafe.Pointer(js.Global.Call("go$newDataPointer", v, jsType(rt.ptrTo())).Unsafe()), fl | flag(t.Kind())<<flagKindShift | flagIndir} } return Value{rt, unsafe.Pointer(v.Unsafe()), fl | flag(t.Kind())<<flagKindShift} }
func (d *Domain) Register(endpoint string, handler *js.Object) *js.Object { cb := core.NewID() var p promise.Promise go func() { // From the want wrapper pull out the types they defined, // and pass them down into the core. h := handler.Get("types") tmp := h.Interface() types, hasTypes := tmp.([]interface{}) // handler can either be: // 1. an object that contains "types" and "fp" attributes. // 2. a naked function, in which case we tell the core that it doesn't // care about types. handlerFunction := handler handlerTypes := []interface{}{nil} if hasTypes { handlerFunction = handler.Get("fp") handlerTypes = types } if err := d.coreDomain.Register(endpoint, cb, handlerTypes); err == nil { d.app.registrations[cb] = handlerFunction p.Resolve(nil) } else { p.Reject(err) } }() return p.Js() }
// QuerySelectorAll returns the result of querySelectorAll on an object func QuerySelectorAll(o *js.Object, sel string) []*js.Object { if sad := o.Get("querySelectorAll"); sad == nil || sad == js.Undefined { return nil } return DOMObjectToList(o.Call("querySelectorAll", sel)) }
//horrific way to do map(func) func extractAllLetters(obj js.Object) js.Object { result := js.Global.Get("Array").New() for i := 0; i < obj.Length(); i++ { result.SetIndex(i, extractLetter(obj.Index(i))) } return result }
func MakeWorkspace(canvas *js.Object) *Workspace { doc := js.Global.Get("document") ctx := canvas.Call("getContext", "2d") w := &Workspace{ doc: doc, canvas: canvas, ctx: ctx, x: canvas.Get("offsetLeft").Int(), y: canvas.Get("offsetTop").Int(), dx: canvas.Get("offsetWidth").Int(), dy: canvas.Get("offsetHeight").Int(), images: make(chan schema.ImageManifest), disks: make(chan string), ingresses: make(chan int), draw: make(chan struct{}), mouseDown: make(chan point), mouseMove: make(chan point), mouseUp: make(chan point), makeItSo: make(chan struct{}), cut: make(chan struct{}), } doc.Call("addEventListener", "mousedown", js.MakeFunc(w.onMouseDown), "false") doc.Call("addEventListener", "mousemove", js.MakeFunc(w.onMouseMove), "false") doc.Call("addEventListener", "mouseup", js.MakeFunc(w.onMouseUp), "false") go w.run() return w }
// DOMObjectToList takes a jsobjects and returns a list of internal objects by calling the item method func DOMObjectToList(o *js.Object) []*js.Object { var out []*js.Object length := o.Get("length").Int() for i := 0; i < length; i++ { out = append(out, o.Call("item", i)) } return out }
func handleInputKeyUp(event *js.Object) { if keycode := event.Get("keyCode").Int(); keycode == 13 { // user press enter key w := word.Get("value").String() word.Call("blur") go httpGetWordJson(w) } }
func (c *controllerWrapper) SendVolume(ident string, volume int, cb *js.Object) { go func() { err := c.controller.SendVolume(ident, volume) if err != nil { cb.Invoke("Hello failed: " + err.Error()) } }() }
func wrapNodes(o *js.Object) []dom.Node { l := o.Length() toRet := make([]dom.Node, l) for i := 0; i < l; i++ { toRet[i] = dom.WrapNode(o.Index(i)) } return toRet }
func setMouseCursorFromEvent(e *js.Object) { scale := currentUI.scale rect := canvas.Call("getBoundingClientRect") x, y := e.Get("clientX").Int(), e.Get("clientY").Int() x -= rect.Get("left").Int() y -= rect.Get("top").Int() currentInput.setMouseCursor(int(float64(x)/scale), int(float64(y)/scale)) }
func (c *controllerWrapper) LoadTrack(ident string, gids []string, cb *js.Object) { go func() { err := c.controller.LoadTrack(ident, gids) if err != nil { cb.Invoke("Hello failed: " + err.Error()) } }() }
func NewContextMenus(contextMenusObj *js.Object) *ContextMenus { c := new(ContextMenus) c.o = contextMenusObj if c.o.String() != "undefined" { c.ACTION_MENU_TOP_LEVEL_LIMIT = contextMenusObj.Get("ACTION_MENU_TOP_LEVEL_LIMIT").Int() } return c }
func (c *controllerWrapper) SendHello(cb *js.Object) { go func() { err := c.controller.SendHello() if err != nil { cb.Invoke("Hello failed: " + err.Error()) } }() }
func setMouseCursorFromEvent(e *js.Object) { scale := canvas.Get("dataset").Get("ebitenScale").Int() rect := canvas.Call("getBoundingClientRect") x, y := e.Get("clientX").Int(), e.Get("clientY").Int() x -= rect.Get("left").Int() y -= rect.Get("top").Int() currentInput.SetMouseCursor(x/scale, y/scale) }
func NewProxy(proxyObj *js.Object) *Proxy { p := new(Proxy) p.o = proxyObj if proxyObj.String() != "undefined" { p.Settings = proxyObj.Get("settings") } return p }
func NewExtension(extensionObj *js.Object) *Extension { e := new(Extension) e.o = extensionObj if extensionObj.String() != "undefined" { e.LastError = e.o.Get("lastError") e.InIncognitoContext = e.o.Get("inIncognitoContext").Bool() } return e }
func uint8ArrayToArrayBuffer(p *js.Object) *js.Object { buffer := p.Get("buffer") byteOffset := p.Get("byteOffset").Int() byteLength := p.Get("byteLength").Int() if byteOffset != 0 || byteLength != buffer.Get("byteLength").Int() { return buffer.Call("slice", byteOffset, byteOffset+byteLength) } return buffer }
// CleanAllTextNode removes all texts nodes within the container root func CleanAllTextNode(o *js.Object) { for _, to := range ChildNodeList(o) { if istx, isem := EmptyTextNode(to); istx { if !isem { o.Call("removeChild", to) } } } }
func getFrameData(obj *js.Object) []byte { // Check if it's an array buffer. If so, convert it to a Go byte slice. if constructor := obj.Get("constructor"); constructor == js.Global.Get("ArrayBuffer") { int8Array := js.Global.Get("Uint8Array").New(obj) return int8Array.Interface().([]byte) } return []byte(obj.String()) }