• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

shashi/Patchwork.jl: Virtual DOM for Julia

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

shashi/Patchwork.jl

开源软件地址:

https://github.com/shashi/Patchwork.jl

开源编程语言:

JavaScript 78.5%

开源软件介绍:

Patchwork

Build Status

A library for representing browser DOM in Julia. It supports element creation, diff computation and browser-side patching.

Setup

From the REPL, run

Pkg.add("Patchwork")

Creating Elements

The Elem constructor can be used to create an element.

# E.g.
using Patchwork

Elem(:h1, "Hello, World!")

creates an h1 heading element equivalent to the HTML <h1>Hello, World!</h1>

You can attach any DOM property (e.g. className, style, height, width) that you would like the DOM node to have by passing it as a keyword argument to Elem

# E.g.
Elem(:h1, "Hello, World!", className="welcome", style=[:color => :white, :backgroundColor => :black])

This creates a h1 with white text on a black background.

You can of course nest elements inside another

Elem(:div, [
    Elem(:h1, "Hello, World!"),
    Elem(:p, "How are you doing today?")])

Elem objects are immutable children(::Elem) returns the children of the element as a persistent vector properties(::Elem) returns the dictionary of its properties. There are some infix operators defined for Elem.

The & operator can set attributes

# E.g.
div_with_class = Elem(:div, "This div's class can change") & [:className => "shiny"]

The << operator can append an element to the end of another.

h1_and_p = Elem(:div, Elem(:h1, "Hello, World!")) << Elem(:p, "How are you doing today?")

SVG graphics are DOM nodes too, and hence can be created in Patchwork.

    Elem(:svg, Elem(:circle, cx=250, cy=250, r=100, fill="orange"),
         width=500, height=500)

draws a circle.

If you are using IJulia, you can use the Interact.jl's @manipulate statement to draw a circle whose position, radius and color can be changed:

using Interact, Patchwork
@manipulate for r=1:100, cx = 1:500, cy=1:400, color=["orange", "green", "blue"]
    Elem(:svg, Elem(:circle, cx=cx, cy=cy, r=r, fill=color),
         width=500, height=500)
end

Diff computation

The diff function computes the difference between two elements.

# E.g.
patch = diff(left::Elem, right::Elem)

Returns a "patch". A patch is a Dict which maps node indices to a list of patches on that node. The node index is a number representing the position of the node in a depth-first ordering starting at the root node (here left), whose index is 0.

Elems are based on immutable datastructures. & and << operations return new Elems, which may share structure with the operands. The more structure two nodes share, the faster the diffing.

For example, if you have a big Elem, say averybigelem, the running time of the following diff call

diff(averybigelem, averybigelem & [:className => "shiny"])

will not depend on the size and complexity of averybigelem because diffing gets short-circuited since left.children === right.children. It will probably be helpful to keep this in mind while building something with Patchwork.

JavaScript setup and patching

Patchwork has a javascript "runtime" in runtime/build.js that needs to be included into a page where you would like to display Patchwork nodes.

<script src="/path/to/build.js"></script>

This is automatically done for you if you are using Patchwork from IJulia.

Patchwork defines the writemime(io::IO, ::MIME"text/html", ::Elem) method which can use JavaScript to display nodes and/or apply patches to nodes that are already displayed.

At a lower level, the runtime exposes the window.Patchwork object, which can be used to render nodes from their JSON representations and also apply patches.

// E.g.
node = new Patchwork.Node(mountId, elemJSON)

this renders the node represented by elemJSON and appends it to a DOM element with id mountId.

Patchwork.Node instances have an applyPatch method which can be used to patch the node.

// E.g.
node.applyPatch(patchJSON)

With Compose and Gadfly

If Patchwork is installed, interactive plots or Compose graphics automatically use Patchwork to efficiently render them into SVG Virtual DOM. Any updates to the plot get turned into patches, sent over to the browser and applied to the plot.

Usage in IJulia

When you load Patchwork in IJulia, the runtime is setup automatically for you. If the result of executing a cell is an Elem object, it gets rendered in the cell's output area. display(::Elem) will work too.

When used with Reactive (or Interact), any Signal{Elem} values (see Reactive.Signal) get displayed with its initial value first. Subsequent updates are sent as patches and applied at the front-end.

Development

You will need a recent nodejs and npm installed to hack on the JavaScript part of this package.

To build the JS files run the following from runtime/ directory:

npm install .
npm install -g browserify
npm install -g uglifyjs
make

Thanks

  • This package is largely based on Matt-Esch's excellent virtual-dom and vtree JavaScript modules. Patchwork's JS makes use of virtual-dom and virtual-hyperscript by Raynos.
  • Thanks to Evan Czaplicki for creating Elm which inspired me to take the FRP road.



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap