Exemple #1
0
func GetEditable(ctx context.Context, node *node.Node) editable.Editable {

	if node == nil || node.Null || node.Missing {
		return nil
	}

	// This is the recommended method of presenting an custom editor.
	if ed, ok := node.Value.(editable.Editable); ok {
		return ed
	}

	editors := clientctx.FromContext(ctx)

	// Don't do this. Implement the Editable interface instead. We can't do
	// this for system types so we use this method instead.
	if e, ok := editors.Get(node.Type.Id.String()); ok {
		return e
	}

	if node.JsonType != "" {
		if e, ok := editors.Get(string(node.JsonType)); ok {
			return e
		}
	}

	return nil
}
Exemple #2
0
func (v *StructView) Render() *vecty.HTML {
	if v.model == nil {
		return elem.Div(vecty.Text("Struct (nil)"))
	}

	if v.model.Node.Type.Basic {
		// This view is only for types that embed system:object
		panic(kerr.New("QHDQMXTNIH", "Basic type %s not supported by StructView", v.model.Node.Type.Id.String()))
	}

	out := vecty.List{}

	// Always show the editor for system:object first
	objectEditor, ok := clientctx.FromContext(v.Ctx).Get("kego.io/system:object")
	if !ok {
		panic(kerr.New("BJRMXESSUV", "Can't find editor for system:object"))
	}
	out = append(out, objectEditor.EditorView(v.Ctx, v.model.Node, editable.Block))

	out = append(out, NewStructFragmentView(v.Ctx, v.model.Node, v.model.Node.Type.Id))

	return elem.Div(
		out,
	)

}
Exemple #3
0
func GetEmbedEditable(ctx context.Context, node *node.Node, embed *system.Reference) (editable.Editable, error) {

	if node == nil || node.Null || node.Missing {
		return nil, nil
	}

	if *node.Type.Id == *embed {
		return GetEditable(ctx, node), nil
	}

	jcache := jsonctx.FromContext(ctx)
	nf, df, ok := jcache.GetNewFunc(embed.Package, embed.Name)
	if !ok {
		return nil, kerr.New("DGWDERFPVV", "Can't find %s in jsonctx", embed.String())
	}
	t := reflect.TypeOf(nf())
	if df != nil {
		t = t.Elem()
	}

	v := node.Val
	for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
		v = v.Elem()
	}

	var field reflect.Value
	for i := 0; i < v.Type().NumField(); i++ {
		f := v.Type().Field(i)
		if f.Anonymous && f.Type == t {
			field = v.Field(i)
			break
		}
	}
	if field == (reflect.Value{}) {
		return nil, kerr.New("UDBOWYUBER", "Can't find %s field in struct", t)
	}

	// This is the recommended method of presenting an custom editor.
	if ed, ok := field.Interface().(editable.Editable); ok {
		return ed, nil
	}

	editors := clientctx.FromContext(ctx)

	// Don't do this. Implement the Editable interface instead. We can't do this
	// for system types so we use this method instead.
	if e, ok := editors.Get(embed.String()); ok {
		return e, nil
	}

	return nil, nil

}
Exemple #4
0
func Register(ctx context.Context) {
	// Don't do this. Implement the Editable interface instead. We can't do this
	// for system types so we use this method instead.
	editors := clientctx.FromContext(ctx)

	editors.Set("string", new(StringEditor))
	editors.Set("kego.io/json:string", new(StringEditor))
	editors.Set("kego.io/system:string", new(StringEditor))

	editors.Set("number", new(NumberEditor))
	editors.Set("kego.io/json:number", new(NumberEditor))
	editors.Set("kego.io/system:number", new(NumberEditor))
	editors.Set("kego.io/system:int", new(NumberEditor))

	editors.Set("bool", new(BoolEditor))
	editors.Set("kego.io/json:bool", new(BoolEditor))
	editors.Set("kego.io/system:bool", new(BoolEditor))

	editors.Set("kego.io/system:object", new(ObjectEditor))
}