Example #1
0
File: design.go Project: influx6/gu
// DoMarkup returns a new instance of a provided value which either is a function
// which returns a needed trees.Markup or a trees.Markup or slice of trees.Markup
// itself.
func DoMarkup(markup Viewable, targets string, deferRender bool, targetAlreadyInDom bool) {
	var markupFn []*trees.Markup

	switch mo := markup.(type) {
	case func() []*trees.Markup:
		markupFn = mo()
	case []*trees.Markup:
		markupFn = mo
	case func() *trees.Markup:
		markupFn = []*trees.Markup{mo()}
	case *trees.Markup:
		markupFn = []*trees.Markup{mo}
	case string:
		markupFn = trees.ParseTree(mo)
	default:
		panic("Unknown markup processable type")
	}

	current := getResources().MustCurrentResource()

	for _, markup := range markupFn {
		static := gu.Static(markup)
		static.Morph = true

		trees.NewAttr("resource-id", current.UUID()).Apply(static.Content)

		if deferRender {
			current.DRenderables = append(current.DRenderables, targetRenderable{
				Targets: targets,
				View:    static,
			})

			continue
		}

		if !deferRender && targetAlreadyInDom {
			current.Renderables = append(current.Renderables, targetRenderable{
				Targets: targets,
				View:    static,
			})

			continue
		}

		if !deferRender && !targetAlreadyInDom && targets != "" {
			current.DRenderables = append(current.DRenderables, targetRenderable{
				View:    static,
				Targets: targets,
			})

			continue
		}

		current.Renderables = append(current.Renderables, targetRenderable{
			View:    static,
			Targets: targets,
		})

	}
}
Example #2
0
func TestResourceRendering(t *testing.T) {
	trees.SetMode(trees.Testing)
	defer trees.SetMode(trees.Normal)

	_ = design.Resource(func() {
		design.DoOrder(design.Any)
		design.UseRoute("/home/*")

		design.DoCSS("../some.css", false)
		design.DoScript("../some.js", "text/javascript", false)

		design.DoMarkup(elems.Header1(elems.Text("Speed Dashboard")), "", false)

		design.DoMarkup(func() *trees.Markup {
			return elems.Div(
				elems.Section(
					elems.Label(elems.Text("Current Speed")),
				),
			)
		}, "", false)

		design.DoView(gu.Static(elems.Div(
			elems.Section(
				design.DoRoute("/models/*", "/:speed"),
				elems.Label(elems.Text("Total Speed")),
			),
		)), "", false)

	})

	master := design.New().Init()

	testRender := master.Render(dispatch.UseLocation("/home/"))
	if testRender.HTML() != fullRender {
		t.Logf("\t\tExpected: %q", fullRender)
		t.Logf("\t\tReceived: %q", testRender.HTML())
		tests.Failed(t, "Should have rendered the expected full markup")
	}
	tests.Passed(t, "Should have rendered the expected full markup")

	testRender2 := master.Render(dispatch.UseLocation("/home/models/340mph"))
	if testRender2.HTML() != routedRender {
		t.Logf("\t\tExpected: %q", routedRender)
		t.Logf("\t\tReceived: %q", testRender2.HTML())
		tests.Failed(t, "Should have rendered the routed full markup")
	}
	tests.Passed(t, "Should have rendered the routed full markup")

	testFlatRender := master.Render(dispatch.UseLocation("/root"))
	if testFlatRender.HTML() != flatRender {
		t.Logf("\t\tExpected: %q", flatRender)
		t.Logf("\t\tReceived: %q", testFlatRender.HTML())
		tests.Failed(t, "Should have rendered the expected markup")
	}
	tests.Passed(t, "Should have rendered the expected markup")

}