All Projects → siongui → Godom

siongui / Godom

Licence: unlicense
Make DOM manipulation in Go as similar to JavaScript as possible. (via GopherJS or WebAssembly)

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Godom

Goplayspace
Advanced Go Playground frontend written in Go, with syntax highlighting, turtle graphics mode, and more
Stars: ✭ 765 (+512%)
Mutual labels:  gopherjs, frontend
Vecty
Vecty lets you build responsive and dynamic web frontends in Go using WebAssembly, competing with modern web frameworks like React & VueJS.
Stars: ✭ 2,161 (+1628.8%)
Mutual labels:  gopherjs, frontend
Bulmaswatch
Themes for Bulma
Stars: ✭ 1,525 (+1120%)
Mutual labels:  frontend
Fe note
📒 Front-end Development Notebook From Start to Finish! (Simplified Chinese)
Stars: ✭ 1,534 (+1127.2%)
Mutual labels:  frontend
Challenges Front End
Repositório referente à desafios de front-end da womakerscode
Stars: ✭ 116 (-7.2%)
Mutual labels:  frontend
React Multi Select
A Multi Select component built with and for React
Stars: ✭ 111 (-11.2%)
Mutual labels:  frontend
Ostap
CLI tool that fast checks if your bundle contains multiple versions of the same package, only by looking in package.json.
Stars: ✭ 117 (-6.4%)
Mutual labels:  frontend
Mort
Dead CSS detection
Stars: ✭ 109 (-12.8%)
Mutual labels:  frontend
Fast
Develop, build, deploy, redeploy, and teardown frontend projects fast.
Stars: ✭ 126 (+0.8%)
Mutual labels:  frontend
Fe Foundation
前端开发学习指南
Stars: ✭ 113 (-9.6%)
Mutual labels:  frontend
Components
Fully responsive and beautiful HTML components made with VueJS and TailwindCSS.
Stars: ✭ 121 (-3.2%)
Mutual labels:  frontend
Udacity
➿ 💡 My Udacity projects that I have made to improve my skills and complete my nanodegree. Please don't use it to copy the projects. Submit the PR if you want something to be added to this repository.
Stars: ✭ 113 (-9.6%)
Mutual labels:  frontend
Interview Problem Summary
🎤 Prepare for the interviews and sum up the most popular interview problems for front-end(HTML/CSS/Javascript), Web development, full-stack. Also did some typical coding practice questions, such as UI caculator
Stars: ✭ 112 (-10.4%)
Mutual labels:  frontend
Blog
人人贷大前端博客中心:打造优质大前端博客,欢迎关注我们
Stars: ✭ 118 (-5.6%)
Mutual labels:  frontend
React Textgradient
Text gradients with CSS with SVG fallback. [unmaintained]
Stars: ✭ 110 (-12%)
Mutual labels:  frontend
Soundize
🎧 A homemade Spotify application. It's a server side rendering client made with React and Redux, powered by the Spotify API
Stars: ✭ 121 (-3.2%)
Mutual labels:  frontend
Job Model
蚂蚁金服 - 国际事业群 - 前端 招聘
Stars: ✭ 110 (-12%)
Mutual labels:  frontend
Amazon Next
A simple mock and re-concept of Amazon - built with Next.js, Firebase, and Framer Motion
Stars: ✭ 112 (-10.4%)
Mutual labels:  frontend
Marko
A declarative, HTML-based language that makes building web apps fun
Stars: ✭ 10,796 (+8536.8%)
Mutual labels:  frontend
Eshoponcontainersddd
Fork of dotnet-architecture/eShopOnContainers in full DDD/CQRS design using my own patterns
Stars: ✭ 126 (+0.8%)
Mutual labels:  frontend

========================== DOM Manipulation_ in Go_

.. image:: https://img.shields.io/badge/Language-Go-blue.svg :target: https://golang.org/

.. image:: https://godoc.org/github.com/siongui/godom?status.svg :target: https://godoc.org/github.com/siongui/godom

.. image:: https://api.travis-ci.org/siongui/godom.svg?branch=master :target: https://travis-ci.org/siongui/godom

.. image:: https://goreportcard.com/badge/github.com/siongui/godom :target: https://goreportcard.com/report/github.com/siongui/godom

.. image:: https://img.shields.io/badge/license-Unlicense-blue.svg :target: https://raw.githubusercontent.com/siongui/godom/master/UNLICENSE

.. image:: https://img.shields.io/badge/Status-Beta-brightgreen.svg

.. image:: https://img.shields.io/twitter/url/https/github.com/siongui/godom.svg?style=social :target: https://twitter.com/intent/tweet?text=Wow:&url=%5Bobject%20Object%5D

Make DOM Manipulation_ in Go_ as similar to JavaScript_ as possible via GopherJS_. For DOM Manipulation via WebAssembly_, visit wasm_ directory.

  • Ubuntu 20.04_
  • Go_

.. contents:: Table of Content

Why? ++++

Why not use GopherJS directly? ##############################

Because the code written directly by GopherJS without any wrapper is really ugly. For example, if you want to getElementById, you need to write:

.. code-block:: go

import ( "github.com/gopherjs/gopherjs/js" )

foo := js.Global.Get("document").Call("getElementById", "foo")

With godom, you write:

.. code-block:: go

import ( . "github.com/siongui/godom" )

foo := Document.GetElementById("foo")

which looks like JavaScript and more readable.

Why not use existing go-js-dom_? ##################################

Because it's too restricted, and sometimes need to do a lot of type casting. For example, if you have an audio element with id foo and you want to call the Play() method, you need to write the following code:

.. code-block:: go

import ( "honnef.co/go/js/dom" )

a := dom.GetWindow().Document().GetElementByID("foo").(*dom.HTMLAudioElement) a.Play()

If you use querySelectorAll to select a lot of such elements, you need to do a lot of type casting, which is really disturbing.

With godom, you can write like this:

.. code-block:: go

import ( . "github.com/siongui/godom" )

a := Document.GetElementById("foo") a.Play()

What if the method/property is not implemented in godom? ##########################################################

godom is only a wrapper for GopherJS. If something is not implemented, you can still use the GopherJS methods to call or get the method/property you need. For example, if the Play() method of the audio element is not implemented, you can use GopherJS Call method to call play method directly:

.. code-block:: go

import ( . "github.com/siongui/godom" )

a := Document.GetElementById("foo") a.Call("play")

Code Example ++++++++++++

  • Frontend Programming in Go_: If you have no experience of GopherJS before, read this.
  • Synonyms - Go and JavaScript_: If you have some experience about GopherJS, this serves as references for quick start.

null test #########

Test if event.state is null in popstate event listener:

.. code-block:: go

ih := Document.QuerySelector("#infoHistory")

Window.AddEventListener("popstate", func(e Event) {
	if e.Get("state") == nil {
		ih.SetInnerHTML("Entry Page")
	} else {
		ih.SetInnerHTML(e.Get("state").String())
	}
})

HTML dataset (data-* attribute) ###############################

Assume we have the following element:

.. code-block:: html

You can access the data-content as follows:

.. code-block:: go

p := Document.QuerySelector("#foo") content := p.Dataset().Get("content").String()

XML/XSLT ########

We will transform Tipitaka XML to HTML and append it to the following div.

.. code-block:: html

The frontend code:

.. code-block:: go

// Basic Example - XSLT: Extensible Stylesheet Language Transformations | MDN // https://developer.mozilla.org/en-US/docs/Web/XSLT/XSLT_JS_interface_in_Gecko/Basic_Example xsltProcessor := NewXSLTProcessor()

// Load the xsl file using synchronous (third param is set to false) XMLHttpRequest myXMLHTTPRequest := NewXMLHttpRequest() //myXMLHTTPRequest.Open("GET", "https://tipitaka.org/romn/cscd/tipitaka-latn.xsl", false) myXMLHTTPRequest.Open("GET", "https://siongui.github.io/tipitaka-romn/cscd/tipitaka-latn.xsl", false) myXMLHTTPRequest.Send()

xslStylesheet := myXMLHTTPRequest.ResponseXML()

// Finally import the .xsl xsltProcessor.ImportStylesheet(xslStylesheet)

// load the xml file myXMLHTTPRequest2 := NewXMLHttpRequest() //myXMLHTTPRequest.Open("GET", "https://tipitaka.org/romn/cscd/vin01m.mul0.xml", false) myXMLHTTPRequest2.Open("GET", "https://siongui.github.io/tipitaka-romn/cscd/vin01m.mul0.xml", false) myXMLHTTPRequest2.Send()

xmlDoc := myXMLHTTPRequest2.ResponseXML()

fragment := xsltProcessor.TransformToFragment(xmlDoc, Document)

Document.GetElementById("xml").AppendChild(fragment)

HTML onevent Attribute ######################

This example show you how to register onclick event handler via HTML onclick attribute_.

HTML:

.. code-block:: html

Click me to say Hi

Go/GopherJS:

.. code-block:: go

Document.Set("myhandler", func(s string) { Alert(s) })

Element.getAttribute()_ #########################

Before using Element.getAttribute(), call Element.hasAttribute() first to check if the attribute exists or not. Otherwise something unexpected will happen.

UNLICENSE +++++++++

Released in public domain. See UNLICENSE_.

References ++++++++++

.. [1] GopherJS - A compiler from Go to JavaScript <http://www.gopherjs.org/>_ (GitHub <https://github.com/gopherjs/gopherjs>__, GopherJS Playground <http://www.gopherjs.org/playground/>_, |godoc|)

.. [2] dom - GopherJS bindings for the JavaScript DOM APIs <https://godoc.org/honnef.co/go/js/dom>_ (GitHub <https://github.com/dominikh/go-js-dom>__)

.. [3] | panic: interface conversion: ast.Expr is *ast.SelectorExpr, not *ast.Ident - Google search <https://www.google.com/search?q=panic:+interface+conversion:+ast.Expr+is+*ast.SelectorExpr,+not+*ast.Ident>_ | add a method to an external package - Google search <https://www.google.com/search?q=add+a+method+to+an+external+package>_

.. [4] [Golang] Add Method to Existing Type in External Package <https://siongui.github.io/2017/02/11/go-add-method-function-to-type-in-external-package/>_

.. [5] JavaScript Remove All Children of a DOM Element <https://siongui.github.io/2012/09/26/javascript-remove-all-children-of-dom-element/>_

.. [6] How to do insert After() in JavaScript without using a library? - Stack Overflow <http://stackoverflow.com/a/32135318>_

.. [7] javascript element position <https://www.google.com/search?q=javascript+element+position>_

   `javascript - Retrieve the position (X,Y) of an HTML element - Stack Overflow <http://stackoverflow.com/questions/442404/retrieve-the-position-x-y-of-an-html-element>`_

.. [8] javascript check class exists - Google search <https://www.google.com/search?q=javascript+check+class+exists>_

   `javascript - Test if an element contains a class? - Stack Overflow <http://stackoverflow.com/a/5898748>`_

.. [9] | Who is using GopherJS? : golang <https://www.reddit.com/r/golang/comments/5urqny/who_is_using_gopherjs/>_ | GopherJS 1.8-1 is released : golang <https://www.reddit.com/r/golang/comments/5upkkc/gopherjs_181_is_released/>_

.. [10] Go Report Card | Go project code quality report cards <https://goreportcard.com/>_ .. [11] Shields.io: Quality metadata badges for open source projects <https://shields.io/>_

.. [12] HTML DOM Style object <https://www.w3schools.com/jsref/dom_obj_style.asp>_

.. [13] | javascript is focused - Google search <https://www.google.com/search?q=javascript+is+focused>_ | javascript is focused - DuckDuckGo search <https://duckduckgo.com/?q=javascript+is+focused>_ | javascript is focused - Ecosia search <https://www.ecosia.org/search?q=javascript+is+focused>_ | javascript is focused - Qwant search <https://www.qwant.com/?q=javascript+is+focused>_ | javascript is focused - Bing search <https://www.bing.com/search?q=javascript+is+focused>_ | javascript is focused - Yahoo search <https://search.yahoo.com/search?p=javascript+is+focused>_ | javascript is focused - Baidu search <https://www.baidu.com/s?wd=javascript+is+focused>_ | javascript is focused - Yandex search <https://www.yandex.com/search/?text=javascript+is+focused>_

.. _DOM Manipulation: https://www.google.com/search?q=DOM+Manipulation .. _Go: https://golang.org/ .. _JavaScript: https://www.google.com/search?q=JavaScript .. _GopherJS: https://github.com/gopherjs/gopherjs .. _WebAssembly: https://duckduckgo.com/?q=webassembly .. _wasm: wasm .. _Ubuntu 20.04: https://releases.ubuntu.com/20.04/ .. _Go 1.8: https://golang.org/dl/ .. _go-js-dom: https://github.com/dominikh/go-js-dom .. _UNLICENSE: https://unlicense.org/ .. _Frontend Programming in Go: https://siongui.github.io/2017/12/04/frontend-programming-in-go/ .. _Synonyms - Go and JavaScript: https://siongui.github.io/2017/12/07/synonyms-go-and-javascript/ .. _HTML onclick attribute: https://www.google.com/search?q=HTML+onclick+attribute .. _Element.getAttribute(): https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute .. _Element.hasAttribute(): https://developer.mozilla.org/en-US/docs/Web/API/Element/hasAttribute

.. |godoc| image:: https://godoc.org/github.com/gopherjs/gopherjs/js?status.png :target: https://godoc.org/github.com/gopherjs/gopherjs/js

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].