Пример #1
0
Файл: dom.go Проект: influx6/gu
// Render renders the giving set of resources into the provided body and header
// of the DOM.
func (dm *DOMRenderer) Render(rs ...*design.ResourceDefinition) {
	head := dm.Document.QuerySelector("head")
	body := dm.Document.QuerySelector("body")

	// clear all children of head and body if the belong to us.
	for _, item := range head.QuerySelectorAll("[data-gen*='gu']") {
		if !item.HasAttribute("gu-script-root") {
			item.ParentNode().RemoveChild(item)
		}
	}

	for _, item := range body.QuerySelectorAll("[data-gen*='gu']") {
		if !item.HasAttribute("gu-script-root") {
			item.ParentNode().RemoveChild(item)
		}
	}

	// Render the normal links first.
	for _, res := range rs {
		for _, item := range res.Links {
			markup := item.Render()

			markup.EachEvent(func(ev *trees.Event, root *trees.Markup) {
				dm.BindEvent(ev, head.Underlying())
			})

			js.Patch(js.CreateFragment(markup.HTML()), head.Underlying(), false)
		}
	}

	// Render all basic views which are not to be deffered.
	for _, res := range rs {
		for _, render := range res.Renderables {
			dm.RenderUpdate(render.View, render.Targets, false)
		}
	}

	// Render all defered views.
	for _, res := range rs {
		for _, render := range res.DRenderables {
			dm.RenderUpdate(render.View, render.Targets, false)
		}
	}

	// Render the defered links.
	for _, res := range rs {
		for _, item := range res.DeferLinks {
			markup := item.Render()

			markup.EachEvent(func(ev *trees.Event, root *trees.Markup) {
				dm.BindEvent(ev, body.Underlying())
			})

			js.Patch(js.CreateFragment(markup.HTML()), body.Underlying(), false)
		}
	}
}
Пример #2
0
Файл: dom.go Проект: influx6/gu
// RenderUpdate handles rendering calls for individual renderers which may have
// determined targets within the body.
func (dm *DOMRenderer) RenderUpdate(rv gu.Renderable, targets string, update bool) {
	body := dm.Document.QuerySelector("body")

	if targets == "" {
		markup := rv.Render()

		if !update {
			markup.EachEvent(func(ev *trees.Event, root *trees.Markup) {
				dm.BindEvent(ev, body.Underlying())
			})
		}

		if kvr, ok := rv.(gu.RenderView); ok {
			js.Patch(js.CreateFragment(markup.HTML()), body.Underlying(), !kvr.RenderedBefore())

			if cvs, ok := rv.(gu.ViewHooks); ok {
				mounted, _, _ := cvs.Hooks()
				if !mounted.Used() {
					mounted.Publish()
				}
			}

			return
		}

		js.Patch(js.CreateFragment(markup.HTML()), body.Underlying(), false)

		if cvs, ok := rv.(gu.ViewHooks); ok {
			mounted, _, _ := cvs.Hooks()
			if !mounted.Used() {
				mounted.Publish()
			}
		}

		return
	}

	kernels := body.QuerySelectorAll(targets)

	for _, targetDOM := range kernels {
		markup := rv.Render()

		if !update {
			markup.EachEvent(func(ev *trees.Event, root *trees.Markup) {
				dm.BindEvent(ev, targetDOM.Underlying())
			})
		}

		if kvr, ok := rv.(gu.RenderView); ok {
			js.Patch(js.CreateFragment(markup.HTML()), body.Underlying(), !kvr.RenderedBefore())
			continue
		}

		js.Patch(js.CreateFragment(markup.HTML()), targetDOM.Underlying(), false)
	}

	if cvs, ok := rv.(gu.ViewHooks); ok {
		mounted, _, _ := cvs.Hooks()

		if !mounted.Used() {
			mounted.Publish()
		}
	}
}