├── .gitignore
├── demos
├── favicon.ico
├── svg
│ ├── p5.jpg
│ ├── svg1.css
│ ├── svg1.erl
│ ├── svg_pad4.html
│ ├── svg1.html
│ ├── svg_pad4.erl
│ ├── viserl1.html
│ └── svg_pad4.js
├── ebin
│ └── README
├── Readme.md
├── Makefile
├── interact
│ ├── interact1.erl
│ ├── interact1.html
│ └── interact1.css
├── clocks
│ ├── clock1.css
│ ├── clock2.html
│ ├── clock3.html
│ ├── clock1.erl
│ └── clock1.html
├── shell
│ ├── shell1.html
│ ├── shell1.css
│ └── shell1.erl
├── chat
│ ├── chat1.erl
│ ├── chat1.css
│ ├── chat2.css
│ ├── irc1.css
│ ├── chat1.html
│ ├── chat2.html
│ ├── chat2.erl
│ └── irc.erl
├── ezwebframe_demos.erl
└── index.html
├── Makefile
├── rebar.config
├── src
├── ezwebframe.app.src
├── ezwebframe_app.erl
├── ezwebframe_sup.erl
├── ezwebframe.erl
└── ezwebframe_mochijson2.erl
├── README.md
├── LICENSE
└── priv
├── websock.js
└── jquery-1.7.1.min.js
/.gitignore:
--------------------------------------------------------------------------------
1 | *.beam
2 | *~
3 | /deps
4 | /ebin
5 | .#*
6 |
--------------------------------------------------------------------------------
/demos/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/joearms/ezwebframe/HEAD/demos/favicon.ico
--------------------------------------------------------------------------------
/demos/svg/p5.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/joearms/ezwebframe/HEAD/demos/svg/p5.jpg
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | all:
2 | @test -d deps || rebar get-deps
3 | rebar compile
4 | cd demos; make
5 |
--------------------------------------------------------------------------------
/rebar.config:
--------------------------------------------------------------------------------
1 | {deps, [
2 | {cowboy, ".*", {git, "git://github.com/extend/cowboy.git", "master"}}
3 | ]}.
4 |
--------------------------------------------------------------------------------
/demos/ebin/README:
--------------------------------------------------------------------------------
1 | This is where the beam code will go
2 | This file is here so that git will create a directory here
3 |
--------------------------------------------------------------------------------
/demos/Readme.md:
--------------------------------------------------------------------------------
1 | Demos
2 | =====
3 |
4 | There is one directory per demo
5 |
6 | index.html - is an index
7 | js/ - common code
8 | XXX/ - demo XXX
9 |
--------------------------------------------------------------------------------
/demos/svg/svg1.css:
--------------------------------------------------------------------------------
1 | body {margin-left:100px;
2 | border-radius:10px;
3 | padding:1em;
4 | width:180px;
5 | height:140px;
6 | border: 2px solid black;
7 | box-shadow:black 0.2em 0.2em 0.2em;
8 | }
9 |
--------------------------------------------------------------------------------
/src/ezwebframe.app.src:
--------------------------------------------------------------------------------
1 | {application, ezwebframe,
2 | [
3 | {description, ""},
4 | {vsn, "1"},
5 | {registered, []},
6 | {applications, [
7 | kernel,
8 | stdlib
9 | ]},
10 | {mod, { ezwebframe_app, []}},
11 | {env, []}
12 | ]}.
13 |
--------------------------------------------------------------------------------
/demos/Makefile:
--------------------------------------------------------------------------------
1 | ## set the paths for a default setup
2 |
3 | all: beam
4 | erl -pa ../deps/cowboy/ebin\
5 | -pa ../deps/ranch/ebin\
6 | -pa ../deps/cowlib/ebin\
7 | -pa ../ebin\
8 | -pa ./ebin\
9 | -s ezwebframe_demos start
10 |
11 | beam:
12 | erlc -o ebin *.erl */*.erl
13 |
14 | clean:
15 | rm -rf *~ *.beam *.tmp
16 |
--------------------------------------------------------------------------------
/demos/interact/interact1.erl:
--------------------------------------------------------------------------------
1 | -module(interact1).
2 | -export([start/1]).
3 |
4 | start(Browser) -> running(Browser).
5 |
6 | running(Browser) ->
7 | receive
8 | {Browser, {struct, [{entry,<<"input">>},{txt, Bin}]}} ->
9 | Time = clock1:current_time(),
10 | Browser ! [{cmd,append_div},{id,scroll},
11 | {txt, list_to_binary([Time, " > ", Bin, " "])}]
12 | end,
13 | running(Browser).
14 |
--------------------------------------------------------------------------------
/demos/clocks/clock1.css:
--------------------------------------------------------------------------------
1 | body {margin-left:100px; width:70%}
2 |
3 | pre {font-weight:bold;
4 | border:1px dotted blue;}
5 |
6 |
7 | #clock
8 | {
9 | font-size:3em;
10 | font-family: "Courier New", Courier, monospace;
11 | font-weight: bold;
12 | background-color:#efefef;
13 | padding:10px;
14 | width:5em;
15 | box-shadow:black 0.1em 0.1em 0.1em;
16 | border-radius: 10px;
17 | margin:1.12em 0;
18 | }
19 |
--------------------------------------------------------------------------------
/src/ezwebframe_app.erl:
--------------------------------------------------------------------------------
1 | -module(ezwebframe_app).
2 |
3 | -behaviour(application).
4 |
5 | %% Application callbacks
6 | -export([start/2, stop/1]).
7 |
8 | %% ===================================================================
9 | %% Application callbacks
10 | %% ===================================================================
11 |
12 | start(_StartType, _StartArgs) ->
13 | ezwebframe_sup:start_link().
14 |
15 | stop(_State) ->
16 | ok.
17 |
--------------------------------------------------------------------------------
/demos/svg/svg1.erl:
--------------------------------------------------------------------------------
1 | -module(svg1).
2 | -export([start/1]).
3 |
4 | start(Browser) ->
5 | Browser ! [{cmd,add_canvas},{tag,svg},{width,180},{height,120}],
6 | running(Browser, 10, 10).
7 |
8 |
9 | running(Browser, X, Y) ->
10 | receive
11 | {Browser,{struct,[{clicked,<<"draw rectangle">>}]}} ->
12 | Browser ! [{cmd,add_svg_thing},{type,rect},
13 | {rx,3},{ry,3},{x,X},{y,Y},{width,100},{height,50},
14 | {stroke,blue},{'stroke-width',2},{fill, red}],
15 | running(Browser, X+10, Y+10)
16 | end.
17 |
--------------------------------------------------------------------------------
/demos/interact/interact1.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Interaction
7 |
8 |
9 |
10 |
11 |
12 |
20 |
--------------------------------------------------------------------------------
/demos/interact/interact1.css:
--------------------------------------------------------------------------------
1 | body {margin-left:100px;
2 | border-radius:10px;
3 | padding:1em;
4 | width:320px;
5 | height:280px;
6 | border: 2px solid black;
7 | box-shadow:black 0.2em 0.2em 0.2em;
8 | }
9 |
10 | #scroll{height:10em;
11 | width:300px;
12 | background-color:#efefef;
13 | padding:10px;
14 | overflow-y:scroll;
15 | box-shadow:black 0.1em 0.1em 0.1em;
16 | border-radius: 10px;
17 | }
18 |
19 | #input { width:320px;
20 | box-shadow:black 0.1em 0.1em 0.1em;
21 | border-radius: 2px;
22 | }
23 |
--------------------------------------------------------------------------------
/demos/shell/shell1.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 | Erlang shell
10 |
11 |
12 |
13 |
14 |
22 |
--------------------------------------------------------------------------------
/demos/shell/shell1.css:
--------------------------------------------------------------------------------
1 | body {margin-left:100px;
2 | border-radius:10px;
3 | padding:1em;
4 | width:420px;
5 | height:300px;
6 | border: 2px solid black;
7 | box-shadow:black 0.2em 0.2em 0.2em;
8 | }
9 |
10 | #scroll{height:12em;
11 | width:400px;
12 | font-family: "Courier New", Courier, monospace;
13 | font-weight:bold;
14 | background-color:#efefef;
15 | padding:10px;
16 | overflow-y:scroll;
17 | box-shadow:black 0.1em 0.1em 0.1em;
18 | border-radius: 10px;
19 | }
20 |
21 | #input { width:420px;
22 | box-shadow:black 0.1em 0.1em 0.1em;
23 | border-radius: 2px;
24 | }
25 |
--------------------------------------------------------------------------------
/demos/clocks/clock2.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | start
8 | stop
9 | For a detailed explantion of how this works see
10 | clock1.html
11 |
12 |
13 |
22 |
--------------------------------------------------------------------------------
/demos/chat/chat1.erl:
--------------------------------------------------------------------------------
1 | -module(chat1).
2 | -export([start/1]).
3 |
4 | start(Browser) ->
5 | running(Browser, []).
6 |
7 | running(Browser, L) ->
8 | receive
9 | {Browser, {struct, [{join,Who}]}} ->
10 | Browser ! [{cmd,append_div},{id,scroll},
11 | {txt, list_to_binary([Who, " joined the group\n"])}],
12 | L1 = [Who," "|L],
13 | Browser ! [{cmd,fill_div}, {id,users},
14 | {txt, list_to_binary(L1)}],
15 | running(Browser, L1);
16 | {Browser,{struct, [{entry,<<"tell">>},{txt,Txt}]}} ->
17 | Browser ! [{cmd, append_div}, {id,scroll},
18 | {txt,list_to_binary([" > ", Txt, " "])}],
19 | running(Browser, L);
20 | X ->
21 | io:format("chat received:~p~n",[X])
22 | end,
23 | running(Browser, L).
24 |
25 |
26 |
--------------------------------------------------------------------------------
/demos/clocks/clock3.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | start
8 | stop
9 | For a detailed explantion of how this works see
10 | clock1.html . Or read the chapter on websockets in
11 | Programming Erlang (2'nd edition)
12 |
13 |
14 |
15 |
16 |
17 |
24 |
--------------------------------------------------------------------------------
/src/ezwebframe_sup.erl:
--------------------------------------------------------------------------------
1 |
2 | -module(ezwebframe_sup).
3 |
4 | -behaviour(supervisor).
5 |
6 | %% API
7 | -export([start_link/0]).
8 |
9 | %% Supervisor callbacks
10 | -export([init/1]).
11 |
12 | %% Helper macro for declaring children of supervisor
13 | -define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
14 |
15 | %% ===================================================================
16 | %% API functions
17 | %% ===================================================================
18 |
19 | start_link() ->
20 | supervisor:start_link({local, ?MODULE}, ?MODULE, []).
21 |
22 | %% ===================================================================
23 | %% Supervisor callbacks
24 | %% ===================================================================
25 |
26 | init([]) ->
27 | {ok, { {one_for_one, 5, 10}, []} }.
28 |
29 |
--------------------------------------------------------------------------------
/demos/chat/chat1.css:
--------------------------------------------------------------------------------
1 | body {margin-left:100px;
2 | border-radius:10px;
3 | padding:1em;
4 | width:450px;
5 | height:320px;
6 | border: 2px solid black;
7 | box-shadow:black 0.2em 0.2em 0.2em;
8 | }
9 |
10 | #scroll{height:200px;
11 | width:300px;
12 | background-color:#efefef;
13 | padding:10px;
14 | overflow-y:scroll;
15 | box-shadow:black 0.1em 0.1em 0.1em;
16 | border-radius: 10px;
17 | }
18 |
19 | #users{width:100px;
20 | height:200px;
21 | padding: 10px;
22 | box-shadow:black 0.1em 0.1em 0.1em;
23 | border-radius: 10px;
24 | border:1px solid black}
25 |
26 | #tell { width:320px;
27 | box-shadow:black 0.1em 0.1em 0.1em;
28 | border-radius: 2px;
29 | }
30 |
31 | #nick_input { width:120px;
32 | box-shadow:black 0.1em 0.1em 0.1em;
33 | border-radius: 2px;
34 | }
35 |
36 |
37 |
--------------------------------------------------------------------------------
/demos/chat/chat2.css:
--------------------------------------------------------------------------------
1 | body {margin-left:100px;
2 | border-radius:10px;
3 | padding:1em;
4 | width:450px;
5 | height:320px;
6 | border: 2px solid black;
7 | box-shadow:black 0.2em 0.2em 0.2em;
8 | }
9 |
10 | #scroll{height:200px;
11 | width:300px;
12 | background-color:#efefef;
13 | padding:10px;
14 | overflow-y:scroll;
15 | box-shadow:black 0.1em 0.1em 0.1em;
16 | border-radius: 10px;
17 | }
18 |
19 | #users{width:100px;
20 | height:200px;
21 | padding: 10px;
22 | box-shadow:black 0.1em 0.1em 0.1em;
23 | border-radius: 10px;
24 | border:1px solid black}
25 |
26 | #tell { width:320px;
27 | box-shadow:black 0.1em 0.1em 0.1em;
28 | border-radius: 2px;
29 | }
30 |
31 | #nick_input { width:120px;
32 | box-shadow:black 0.1em 0.1em 0.1em;
33 | border-radius: 2px;
34 | }
35 |
36 |
37 |
--------------------------------------------------------------------------------
/demos/chat/irc1.css:
--------------------------------------------------------------------------------
1 | body {margin-left:100px;
2 | border-radius:10px;
3 | padding:1em;
4 | width:450px;
5 | height:320px;
6 | border: 2px solid black;
7 | box-shadow:black 0.2em 0.2em 0.2em;
8 | }
9 |
10 | #scroll{height:200px;
11 | width:300px;
12 | background-color:#efefef;
13 | padding:10px;
14 | overflow-y:scroll;
15 | box-shadow:black 0.1em 0.1em 0.1em;
16 | border-radius: 10px;
17 | }
18 |
19 | #users{width:100px;
20 | height:200px;
21 | padding: 10px;
22 | box-shadow:black 0.1em 0.1em 0.1em;
23 | border-radius: 10px;
24 | border:1px solid black}
25 |
26 | #tell { width:320px;
27 | box-shadow:black 0.1em 0.1em 0.1em;
28 | border-radius: 2px;
29 | }
30 |
31 | #nick_input { width:120px;
32 | box-shadow:black 0.1em 0.1em 0.1em;
33 | border-radius: 2px;
34 | }
35 |
36 |
37 |
--------------------------------------------------------------------------------
/demos/ezwebframe_demos.erl:
--------------------------------------------------------------------------------
1 | -module(ezwebframe_demos).
2 | -export([start/0]).
3 |
4 | %% This demo assumes that all your code
5 | %% the code paths to ezwebframe and simple_demo are correct
6 |
7 | start() ->
8 | io:format("a simple_demo of websockets....~n"),
9 | Port = 1456,
10 | io:format("Load the page http://localhost:~p/ in your browser~n",[Port]),
11 | ezwebframe:start_link(fun dispatch/1, Port).
12 |
13 | %% dispatch maps names in the HTML onto fixed paths
14 |
15 | dispatch(F) ->
16 | F1 = dispatch1(F),
17 | io:format("ezwebframe_demos::dispatch ~s => ~s~n",[F,F1]),
18 | F1.
19 |
20 | dispatch1("/ezwebframe/" ++ F) ->
21 | Dir = dir(2, code:which(ezwebframe)) ++ "/priv/",
22 | Dir ++ F;
23 | dispatch1("/" ++ F) ->
24 | Dir = dir(2,code:which(?MODULE)) ++ "/",
25 | Dir ++ F.
26 |
27 | dir(0, F) -> F;
28 | dir(K, F) -> dir(K-1, filename:dirname(F)).
29 |
30 |
--------------------------------------------------------------------------------
/demos/chat/chat1.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Chat
6 |
7 | Join
8 |
9 |
20 |
21 |
36 |
--------------------------------------------------------------------------------
/demos/clocks/clock1.erl:
--------------------------------------------------------------------------------
1 | -module(clock1).
2 | -export([start/1, current_time/0]).
3 | %%% NOTE: lines with three %%% show code when frames are introduced
4 |
5 | start(Browser) ->
6 | Browser ! [{cmd,fill_div}, {id,clock}, {txt,current_time()}],
7 | running(Browser).
8 |
9 | running(Browser) ->
10 | receive
11 | {Browser, {struct, [{clicked,<<"stop">>}]}} ->
12 | Browser ! [{cmd,fill_div}, {id,clock}, {txt,<<"Stopped">>}],
13 | idle(Browser)
14 | after 1000 ->
15 | Browser ! [{cmd,fill_div}, {id,clock}, {txt,current_time()}],
16 | running(Browser)
17 | end.
18 |
19 | idle(Browser) ->
20 | receive
21 | {Browser, {struct, [{clicked,<<"start">>}]}} ->
22 | Browser ! [{cmd,fill_div}, {id,clock}, {txt,<<"Starting">>}],
23 | running(Browser)
24 | end.
25 |
26 | current_time() ->
27 | {Hour,Min,Sec} = time(),
28 | list_to_binary(io_lib:format("~2.2.0w:~2.2.0w:~2.2.0w",
29 | [Hour,Min,Sec])).
30 |
31 |
--------------------------------------------------------------------------------
/demos/svg/svg_pad4.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
8 |
9 |
10 | SVG Pad
11 |
12 | This tests the following:
13 | svg_pad4.js svg_pad4.html svg_pad4.erl websocket.js
14 |
15 |
To do add foreign objects - I have a working demo
16 | in foreign_objects
17 |
18 |
The program svg_test4.erl pushes objects to this window
19 | Here we push random rectangles to the pad
20 |
21 |
To Do
22 |
23 | Make Horizontally constainder draggables
24 | Make elastic lines
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | toggle
33 |
34 |
35 |
43 |
44 |
--------------------------------------------------------------------------------
/demos/svg/svg1.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | draw rectangle
8 |
9 |
10 |
38 |
--------------------------------------------------------------------------------
/demos/chat/chat2.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Chat
6 |
7 |
8 | Join
9 |
10 |
11 |
25 |
26 |
49 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ezwebframe
2 | ==========
3 |
4 | Pronounced "Easy web frame."
5 |
6 | Ezwebframe attempts to make web programming just a little bit easier.
7 |
8 | Erlang thinks that the browser is an Erlang process. To get the browser
9 | to do something, Erlang sends a message containing a command to the browser.
10 |
11 | A typical message might be:
12 |
13 |
14 | Browser ! [{cmd,fill_div},{id,div1},{txt, Bin}]
15 |
16 |
17 | Assuming the browser has a div with id = div1, then the div will be filled with
18 | some HTML contained in the binary Bin.
19 |
20 | Likewise buttons and controls in the browser, when pressed, send
21 | messages to Erlang.
22 |
23 | This system is built using websockets together with cowboy and is
24 | described in my book Programming Erlang (2'nd edition) (To be
25 | published in 2013).
26 |
27 | NOTE
28 | ====
29 |
30 | This has only been tested in the chrome browser. Life is too short to
31 | test this in all known browsers.
32 |
33 | INSTALLATION
34 | ============
35 |
36 | This program uses rebar to fetch and install the necessary dependencies.
37 | First you need to install rebar. If you don't have rebar then you can install
38 | a pre-build binary from https://github.com/rebar/rebar/wiki/rebar.
39 |
40 | To run the demos
41 |
42 |
43 | $ make
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/demos/chat/chat2.erl:
--------------------------------------------------------------------------------
1 | -module(chat2).
2 | -export([start/1]).
3 |
4 | start(Browser) ->
5 | case whereis(irc) of
6 | undefined -> irc:start();
7 | _ -> true
8 | end,
9 | idle(Browser).
10 |
11 | idle(Browser) ->
12 | receive
13 | {Browser, {struct, [{join,Who}]}} ->
14 | irc ! {join, self(), Who},
15 | idle(Browser);
16 | {irc, welcome, Who} ->
17 | Browser ! [{cmd,hide_div},{id,idle}],
18 | Browser ! [{cmd,show_div},{id,running}],
19 | running(Browser, Who);
20 | X ->
21 | io:format("chat idle received:~p~n",[X]),
22 | idle(Browser)
23 | end.
24 |
25 | running(Browser, Who) ->
26 | receive
27 | {Browser,{struct, [{entry,<<"tell">>},{txt,Txt}]}} ->
28 | irc ! {broadcast, Who, Txt},
29 | running(Browser, Who);
30 | {Browser,{struct, [{clicked,<<"Leave">>}]}} ->
31 | irc ! {leave, Who},
32 | Browser ! [{cmd,hide_div},{id,running}],
33 | Browser ! [{cmd,show_div},{id,idle}],
34 | idle(Browser);
35 | {irc, scroll, Bin} ->
36 | Browser ! [{cmd,append_div},{id,scroll}, {txt, Bin}],
37 | running(Browser, Who);
38 | {irc, groups, Bin} ->
39 | Browser ! [{cmd,fill_div},{id,users}, {txt, Bin}],
40 | running(Browser, Who);
41 | X ->
42 | io:format("chat running received:~p~n",[X]),
43 | running(Browser, Who)
44 | end.
45 |
46 |
47 |
--------------------------------------------------------------------------------
/demos/chat/irc.erl:
--------------------------------------------------------------------------------
1 | -module(irc).
2 | -export([start/0]).
3 |
4 | start() ->
5 | register(irc, spawn(fun() -> start1() end)).
6 |
7 | start1() ->
8 | process_flag(trap_exit, true),
9 | loop([]).
10 |
11 | loop(L) ->
12 | receive
13 | {join, Pid, Who} ->
14 | case lists:keysearch(Who,1,L) of
15 | false ->
16 | L1 = L ++ [{Who,Pid}],
17 | Pid ! {irc, welcome, Who},
18 | Msg = [Who, <<" joined the chat ">>],
19 | broadcast(L1, scroll, list_to_binary(Msg)),
20 | broadcast(L1, groups, list_users(L1)),
21 | loop(L1);
22 | {value,_} ->
23 | Pid ! {irc, error, <<"Name taken">>},
24 | loop(L)
25 | end;
26 | {leave, Who} ->
27 | case lists:keysearch(Who,1,L) of
28 | false ->
29 | loop(L);
30 | {value,{Who,Pid}} ->
31 | L1 = L -- [{Who,Pid}],
32 | Msg = [Who, <<" left the chat ">>],
33 | broadcast(L1, scroll, list_to_binary(Msg)),
34 | broadcast(L1, groups, list_users(L1)),
35 | loop(L1)
36 | end;
37 | {broadcast, Who, Txt} ->
38 | broadcast(L, scroll,
39 | list_to_binary([" > ", Who, " >> ", Txt, " "])),
40 | loop(L);
41 | X ->
42 | io:format("irc:received:~p~n",[X]),
43 | loop(L)
44 | end.
45 |
46 | broadcast(L, Tag, B) ->
47 | [Pid ! {irc, Tag, B} || {_,Pid} <- L].
48 |
49 | list_users(L) ->
50 | L1 = [[Who," "] || {Who,_}<- L],
51 | list_to_binary(L1).
52 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2012 Joe Armstrong. All rights reserved.
2 |
3 | Redistribution and use in source and binary forms, with or without
4 | modification, are permitted provided that the following conditions are
5 | met:
6 |
7 | 1. Redistributions of source code must retain the above copyright
8 | notice, this list of conditions and the following disclaimer.
9 |
10 | 2. Redistributions in binary form must reproduce the above
11 | copyright notice, this list of conditions and the following
12 | disclaimer in the documentation and/or other materials provided
13 | with the distribution.
14 |
15 | THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDER ``AS IS'' AND ANY
16 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR
19 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 |
27 | The views and conclusions contained in the software and documentation
28 | are those of the authors and should not be interpreted as representing
29 | official policies, either expressed or implied, of copyright holder.
--------------------------------------------------------------------------------
/demos/index.html:
--------------------------------------------------------------------------------
1 |
5 |
6 | Ezwebframe
7 |
8 | This is a pre-release of ezwebframe (pronounced "Easy web frame") the easy
9 | way to connect Erlang to the browser.
10 |
11 | The techniques used in this program are described in the second edition
12 | of my book Programming Erlang which will be published in 2013.
13 |
14 |
Warning
15 |
16 | I have only tested this in the chrome browser.
17 |
18 |
19 | Demos
20 |
21 | These examples are more or less in order of increasing complexity
22 |
23 |
32 |
33 |
34 | How does ezwebframe work?
35 |
36 | Assume we have some divs on a web page, something like this:
37 |
38 |
39 | <div id="a">
40 | ..
41 | </div>
42 | <div id="b">
43 | ..
44 | </div>
45 |
46 |
47 | To fill a div with HTML, Erlang evaluates code like this:
48 |
49 |
50 | Browser ! [{cmd, fill_div}, {div, a}, {text, Bin}]
51 |
52 |
53 | Where Bin is a binary containing HTML.
54 |
55 | In the browser this is converted to the javascript function call
56 | fill_div(o) where o = {cmd:'fill_div', id='a', text=Bin} .
57 | fill_div makes use of jQuery and is defined like this:
58 |
59 |
60 | function fill_div(o){
61 | $('#'+o.id).html(o.txt);
62 | }
63 |
64 |
65 | This design is easily extensible so you add your own commands.
66 |
67 |
We can add controls (buttons, links) etc. to the page. When they
68 | are pressed, messages are sent to Erlang. For example, when we press a
69 | button in the browser, we might evalute the javascript command:
70 |
71 |
72 | send_json({'clicked':'mybutton'});
73 |
74 |
75 | And the controlling Erlang process can receive the
76 | message like this:
77 |
78 |
79 | receive
80 | {Browser, {struct,[{clicked,mybutton}]}} ->
81 | ...
82 |
83 |
84 | For a more detailed explanation, take a look at the first example program .
86 |
87 |
88 | Experimental
89 |
93 |
94 | Files
95 |
96 |
97 |
98 | Files
99 |
100 |
101 |
--------------------------------------------------------------------------------
/priv/websock.js:
--------------------------------------------------------------------------------
1 | function connect_to_erlang(host, port, mod){
2 | // console.log('connect', [host,port,mod]);
3 | make_live_buttons();
4 | make_live_inputs();
5 | var ws = 'ws://' + host + ':' + port + '/websocket/' + mod;
6 | start_session(ws);
7 | }
8 |
9 | function onClose(evt) {
10 | // change the color of the display when the socket closes
11 | // so we can see it closed
12 | // console.log('closed');
13 | document.body.style.backgroundColor='#ffb2b2';
14 | alert("Socket closed - your erlang probably crashed");
15 | }
16 |
17 | function onMessage(evt) {
18 | var json = JSON.parse(evt.data);
19 | do_cmds(json);
20 | }
21 |
22 | function onError(evt) {
23 | // if we get an error change the color of the display so we
24 | // can see we got an error
25 | document.body.style.backgroundColor='orange';
26 | }
27 |
28 | function send(msg) {
29 | websocket.send(msg);
30 | }
31 |
32 | function start_session(wsUri){
33 | // console.log('start_session', wsUri);
34 | websocket = new WebSocket(wsUri);
35 | websocket.onopen = onOpen;
36 | websocket.onclose = onClose;
37 | websocket.onmessage = onMessage;
38 | websocket.onerror = onError;
39 | return(false);
40 | }
41 |
42 | function onOpen(evt) {
43 | // console.log("connected");
44 | }
45 |
46 | // START:do
47 | function do_cmds(objs){
48 | // console.log('do_cmds', objs);
49 | for(var i = 0; i < objs.length; i++){
50 | var o = objs[i];
51 | // as a safety measure we only evaluate js that is loaded
52 | if(eval("typeof("+o.cmd+")") == "function"){
53 | eval(o.cmd + "(o)");
54 | } else {
55 | // console.log('bad_cmd', o);
56 | alert("bad_command:"+o.cmd);
57 | };
58 | };
59 | }
60 | // END:do
61 |
62 | function make_live_buttons(){
63 | $(".live_button").each(
64 | function(){
65 | var b=$(this);
66 | var txt = b.text();
67 | b.click(function(){
68 | // console.log('clicked',txt);
69 | send_json({clicked:txt});
70 | });
71 | });
72 | }
73 |
74 | function send_json(x){
75 | // console.log('send',x);
76 | send(JSON.stringify(x));
77 | }
78 |
79 | // We want the inputs to send a message when we hit CR in the input
80 |
81 | function make_live_inputs(){
82 | $(".live_input").each(
83 | function(){
84 | var e=$(this);
85 | var id = e.attr('id');
86 | // console.log("entry",[e,id]);
87 | e.keyup(function(ev){
88 | if(ev.keyCode==13){
89 | read_entry(e, id);
90 | };
91 | });
92 |
93 | });
94 | }
95 |
96 | function read_entry(x, id){
97 | var val = x.val();
98 | x.val(" ");
99 | send_json({'entry':id, txt:val});
100 | }
101 |
102 | // browser commands
103 |
104 | function append_div(o){
105 | var x = $("#"+o.id);
106 | x.append(o.txt);
107 | x.animate({scrollTop: x.prop("scrollHeight") }, 1000);
108 | }
109 |
110 | function fill_div(o){
111 | $('#'+o.id).html(o.txt);
112 | }
113 |
114 |
--------------------------------------------------------------------------------
/demos/shell/shell1.erl:
--------------------------------------------------------------------------------
1 | -module(shell1).
2 | -export([start/1]).
3 |
4 | %%START:shell1
5 | start(Browser) ->
6 | Browser ! [{cmd,append_div}, {id, scroll},
7 | {txt, <<"Starting Erlang shell: ">>}],
8 | B0 = erl_eval:new_bindings(),
9 | IO = grab_io(),
10 | running(Browser, IO, B0, 1).
11 |
12 | running(Browser, IO, B0, N) ->
13 | receive
14 | {Browser, {struct, [{entry,<<"input">>},{txt, Bin}]}} ->
15 | Echo = bf("~w > ~s ", [N, Bin]),
16 | Browser ! [{cmd,append_div},{id, scroll}, {txt, Echo}],
17 | {Value, B1} = string2value(binary_to_list(Bin), B0),
18 | BV = bf("~p ", [Value]),
19 | Browser ! [{cmd,append_div},{id, scroll}, {txt, BV}],
20 | running(Browser, IO, B1, N+1);
21 | {IO, {output, Bin}} ->
22 | Browser ! [{cmd, append_div}, {id,scroll},
23 | {txt, <<"", Bin/binary, " ">>}],
24 | running(Browser, IO, B0, N)
25 | end.
26 | %%END:shell1
27 |
28 | %%START:shell2
29 | string2value(Str, Bindings0) ->
30 | case erl_scan:string(Str, 0) of
31 | {ok, Tokens, _} ->
32 | case erl_parse:parse_exprs(Tokens) of
33 | {ok, Exprs} ->
34 | {value, Value, Bindings1} = erl_eval:exprs(Exprs, Bindings0),
35 | {Value, Bindings1};
36 | Other ->
37 | io:format("cannot parse:~p Reason=~p~n",[Tokens,Other]),
38 | {parse_error, Bindings0}
39 | end;
40 | Other ->
41 | io:format("cannot tokenise:~p Reason=~p~n",[Str,Other])
42 | end.
43 | %%END:shell2
44 |
45 | bf(F, D) ->
46 | list_to_binary(io_lib:format(F, D)).
47 |
48 | grab_io() ->
49 | P = self(),
50 | spawn(fun() ->
51 | group_leader(self(), P),
52 | _ = erlang:monitor(process, P),
53 | io_loop(P)
54 | end).
55 |
56 | io_loop(P) ->
57 | receive
58 | {io_request,From,ReplyAs,Req} when is_pid(From) ->
59 | Res = io_request(Req, P),
60 | io_reply(From, ReplyAs, Res),
61 | io_loop(P);
62 | {'DOWN',_,process,P,_} ->
63 | exit(normal)
64 | end.
65 |
66 | io_request({put_chars, unicode, Binary}, P) when is_binary(Binary) ->
67 | output(P, Binary);
68 | io_request({put_chars, unicode, M, F, As}, P) ->
69 | case catch apply(M, F, As) of
70 | Binary when is_binary(Binary) ->
71 | output(P, Binary);
72 | Chars ->
73 | case catch unicode:characters_to_binary(Chars,utf8) of
74 | B when is_binary(B) ->
75 | output(P, B);
76 | _ ->
77 | {error,{error,F}}
78 | end
79 | end;
80 | io_request({put_chars, latin1, Chars}, P) ->
81 | output(P, unicode:characters_to_binary(Chars, latin1));
82 | io_request({put_chars, latin1, M, F, As}, P) ->
83 | case catch apply(M, F, As) of
84 | Binary when is_binary(Binary) ->
85 | output(P, unicode:characters_to_binary(Binary,latin1)),
86 | ok;
87 | Chars ->
88 | case catch unicode:characters_to_binary(Chars,latin1) of
89 | B when is_binary(B) ->
90 | output(P, B);
91 | _ ->
92 | {error,{error,F}}
93 | end
94 | end;
95 | io_request(_, _) ->
96 | {error, not_supported}.
97 |
98 | output(P, Cs) ->
99 | P ! {self(), {output, Cs}},
100 | ok.
101 |
102 | io_reply(From, ReplyAs, Reply) ->
103 | From ! {io_reply,ReplyAs,Reply}.
104 |
105 |
106 |
--------------------------------------------------------------------------------
/demos/clocks/clock1.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | start
11 | stop
12 |
13 | Notes
14 | This example has a fairly detailed explanation of
15 | how the browser is interfaced to the server. I suggest you read this
16 | first.
17 |
18 | clock2.html has the same code
19 | as this example, but with all the explanations removed.
20 | clock3.html is more or less the same as the version in my book
21 |
22 |
23 |
24 | Explanation
25 | 1. We define a region on the page to receive messages.
26 | This is a div called clock . We define this as follows:
27 |
28 | <div id='clock'></div>
29 |
30 | 2. When the page is loaded we connect to the server by calling:
31 |
32 | connect_to_erlang("localhost", 1456, "clock1");
33 |
34 | 3. On the server side an Erlang server is started by evaluating
35 | clock1:start(B) .
36 |
37 | start(Browser) ->
38 | Browser ! [{cmd,fill_div}, {id,clock}, {txt,current_time()}],
39 | running(Browser).
40 |
41 | The server starts by sending a fill_div command to
42 | the browser with arguments {id,clock} and
43 | {txt,current_time()} .
44 | This updates the content of the clock div in the browser.
45 |
46 | 4. On the server we call running(Browser) this is as
47 | follows:
48 |
49 | running(Browser) ->
50 | receive
51 | {Browser, {struct, [{clicked,<<"stop">>}]}} ->
52 | Browser ! [{cmd,fill_div}, {id,clock}, {txt,<<"Stopped">>}],
53 | idle(Browser)
54 | after 1000 ->
55 | Browser ! [{cmd,fill_div}, {id,clock}, {txt,current_time()}],
56 | running(Browser)
57 | end.
58 |
59 |
60 | When the timeout triggers (after every second) we send a message
61 | to update the clock.
62 |
63 | Interacting with the clock
64 |
65 | Now we might want to start or stop the clock. This
66 | needs some jquery trickery.
67 |
68 | The stop button is created with:
69 |
70 | <button id="b1">stop</button>
71 |
72 | We add a small amount of jquery code to the button:
73 |
74 | $("#b1").click(function(){
75 | send_json({clicked:'stop'});
76 | });
77 |
78 | And when we click the stop button, a JSON message is sent to the
79 | Erlang server.
80 |
81 | The message to the server can be received with:
82 |
83 |
84 | receive
85 | {Browser, {struct, [{clicked,<<"stop">>}]}} ->
86 | ...
87 | end
88 |
89 |
90 |
91 |
111 |
--------------------------------------------------------------------------------
/src/ezwebframe.erl:
--------------------------------------------------------------------------------
1 | -module(ezwebframe).
2 |
3 | -export([start_link/2,
4 | init/2,
5 | websocket_handle/3,
6 | terminate/3, websocket_terminate/3,
7 | websocket_info/3,
8 | append_div/3,
9 | pre/1,
10 | fill_div/3
11 | ]).
12 |
13 | -import(ezwebframe_mochijson2, [encode/1, decode/1]).
14 |
15 | %% env has only one parameter - reserved for future expansion
16 |
17 | -record(env, {dispatch}).
18 |
19 | start_link(Dispatch, Port) ->
20 | io:format("Starting:~p~n",[file:get_cwd()]),
21 | ok = application:start(crypto),
22 | ok = application:start(ranch),
23 | ok = application:start(cowlib),
24 | ok = application:start(cowboy),
25 | ok = web_server_start(Port, Dispatch).
26 |
27 | web_server_start(Port, Dispatcher) ->
28 | E0 = #env{dispatch=Dispatcher},
29 | Dispatch = cowboy_router:compile([{'_',[{'_', ?MODULE, E0}]}]),
30 | %% server is the name of this module
31 | NumberOfAcceptors = 100,
32 | Status =
33 | cowboy:start_http(ezwebframe,
34 | NumberOfAcceptors,
35 | [{port, Port}],
36 | [{env, [{dispatch, Dispatch}]}]),
37 | case Status of
38 | {error, _} ->
39 | io:format("websockets could not be started -- "
40 | "port ~p probably in use~n", [Port]),
41 | init:stop();
42 | {ok, _Pid} ->
43 | io:format("websockets started on port:~p~n",[Port])
44 | end.
45 |
46 | init(Req, E0) ->
47 | %% io:format("init:~n"),
48 | Resource = path(Req),
49 | %% io:format("Resource:~p~n",[Resource]),
50 | case Resource of
51 | ["/", "websocket",ModStr] ->
52 | Self = self(),
53 | Mod = list_to_atom(ModStr),
54 | %% Spawn an erlang handler
55 | %% The return value will cause cowboy
56 | %% to call this module at the entry point
57 | %% websocket_handle
58 | Pid = spawn_link(Mod, start, [Self]),
59 | {cowboy_websocket, Req, Pid};
60 | _ ->
61 | handle(Req, E0)
62 | end.
63 |
64 | handle(Req, Env) ->
65 | Resource = filename:join(path(Req)),
66 | io:format("ezwebframe:handle ~p~n",[Resource]),
67 | F = Env#env.dispatch,
68 | Res1 = F(Resource),
69 | io:format("mapped to:~p~n",[Res1]),
70 | case Resource of
71 | "/" ->
72 | serve_file("index.html", Req, Env);
73 | "/files" ->
74 | list_dir(F("/"), Req, Env);
75 | _ ->
76 | serve_file(Res1, Req, Env)
77 | end.
78 |
79 | serve_file(File, Req, Env) ->
80 | case filelib:is_dir(File) of
81 | true ->
82 | list_dir(File, Req, Env);
83 | false ->
84 | serve_abs_file(File, Req, Env)
85 | end.
86 |
87 | serve_abs_file(File, Req, Env) ->
88 | Val = file:read_file(File),
89 | case Val of
90 | {error, _} ->
91 | io:format("*** no page called ~p~n",[File]),
92 | reply_html(pre({no_page_called,File}), Req, Env);
93 | {ok, Bin} ->
94 | Ext = filename:extension(File),
95 | Bin1 = if_erlang_add_pre(Ext, Bin),
96 | Req1 = send_page(classify_extension(Ext), Bin1, Req),
97 | {ok, Req1, Env}
98 | end.
99 |
100 | if_erlang_add_pre(".erl", B) -> ["", B, " "];
101 | if_erlang_add_pre(_, B) -> B.
102 |
103 | list_dir(Root, Req, Env) ->
104 | {ok, Files} = file:list_dir(Root),
105 | Files1 = [add_slash(I, Root) || I <- Files],
106 | L1 = [["",I," \n"] || I <- lists:sort(Files1)],
107 | reply_html([" Directory ",Root, " \n",
108 | "\n"], Req, Env).
109 |
110 | add_slash(I, Root) ->
111 | Full = filename:join(Root, I),
112 | case filelib:is_dir(Full) of
113 | true -> I ++ "/";
114 | false -> I
115 | end.
116 |
117 | send_page(Type, Data, Req) ->
118 | cowboy_req:reply(200, [{<<"Content-Type">>,
119 | list_to_binary(mime_type(Type))}],
120 | Data, Req).
121 |
122 |
123 | path(Req) ->
124 | Path = cowboy_req:path(Req),
125 | filename:split(binary_to_list(Path)).
126 |
127 | reply_html(Obj, Req, Env) ->
128 | Req1 = send_page(html, Obj, Req),
129 | {ok, Req1, Env}.
130 |
131 | %%----------------------------------------------------------------------
132 | %% other callbacks
133 |
134 | terminate(_Reason,_Req,_State) ->
135 | %% ignore why we terminate
136 | ok.
137 |
138 | %%----------------------------------------------------------------------
139 | %% websocket stuff
140 |
141 | websocket_handle({text, Msg}, Req, Pid) ->
142 | %% This is a Json message from the browser
143 | case catch decode(Msg) of
144 | {'EXIT', _Why} ->
145 | Pid ! {invalidMessageNotJSON, Msg};
146 | {struct, _} = Z ->
147 | X1 = atomize(Z),
148 | Pid ! {self(), X1};
149 | Other ->
150 | Pid ! {invalidMessageNotStruct, Other}
151 | end,
152 | {ok, Req, Pid}.
153 |
154 | websocket_info({send,Str}, Req, Pid) ->
155 | {reply, {text, Str}, Req, Pid, hibernate};
156 | websocket_info([{cmd,_}|_]=L, Req, Pid) ->
157 | B = list_to_binary(encode([{struct,L}])),
158 | {reply, {text, B}, Req, Pid, hibernate};
159 | websocket_info(Info, Req, Pid) ->
160 | io:format("Handle_info Info:~p Pid:~p~n",[Info,Pid]),
161 | {ok, Req, Pid, hibernate}.
162 |
163 | websocket_terminate(_Reason, _Req, Pid) ->
164 | io:format("websocket.erl terminate:~n"),
165 | exit(Pid, socketClosed),
166 | ok.
167 |
168 | binary_to_atom(B) ->
169 | list_to_atom(binary_to_list(B)).
170 |
171 | %%----------------------------------------------------------------------
172 | %% atomize turns all the keys in a struct to atoms
173 |
174 | atomize({struct,L}) ->
175 | {struct, [{binary_to_atom(I), atomize(J)} || {I, J} <- L]};
176 | atomize(L) when is_list(L) ->
177 | [atomize(I) || I <- L];
178 | atomize(X) ->
179 | X.
180 |
181 | %%----------------------------------------------------------------------
182 | %% these are called from the gui client code
183 |
184 | append_div(Ws, Div, X) ->
185 | Bin = list_to_binary(X),
186 | send_websocket(Ws,
187 | [{cmd,append_div},{id,Div},{txt,Bin}]).
188 |
189 | fill_div(Ws, Div, X) ->
190 | Bin = list_to_binary(X),
191 | send_websocket(Ws,
192 | [{cmd,fill_div},{id,Div},{txt,Bin}]).
193 |
194 | send_websocket(Ws, X) ->
195 | Ws ! {send, list_to_binary(encode([{struct,X}]))}.
196 |
197 |
198 | %%----------------------------------------------------------------------
199 | %% Miscellaneous small functions
200 |
201 | classify_extension(".gif") -> gif;
202 | classify_extension(".jpg") -> jpg;
203 | classify_extension(".png") -> png;
204 | classify_extension(".js") -> js;
205 | classify_extension(".css") -> css;
206 | classify_extension(_) -> html.
207 |
208 | mime_type(gif) -> "image/gif";
209 | mime_type(jpg) -> "image/jpeg";
210 | mime_type(png) -> "image/png";
211 | mime_type(css) -> "text/css";
212 | mime_type(special) -> "text/plain; charset=x-user-defined";
213 | mime_type(json) -> "application/json";
214 | mime_type(swf) -> "application/x-shockwave-flash";
215 | mime_type(html) -> "text/html";
216 | mime_type(xul) -> "application/vnd.mozilla.xul+xml";
217 | mime_type(js) -> "application/x-javascript";
218 | mime_type(svg) -> "image/svg+xml".
219 |
220 | pre(X) ->
221 | ["\n",quote(lists:flatten(io_lib:format("~p",[X]))), " "].
222 |
223 | %% quote HTML characters "<" and "&"
224 |
225 | quote("<" ++ T) -> "<" ++ quote(T);
226 | quote("&" ++ T) -> "&" ++ quote(T);
227 | quote([H|T]) -> [H|quote(T)];
228 | quote([]) -> [].
229 |
--------------------------------------------------------------------------------
/demos/svg/svg_pad4.erl:
--------------------------------------------------------------------------------
1 | -module(svg_pad4).
2 | -compile(export_all).
3 |
4 | start(Ws) ->
5 | process_flag(trap_exit, true),
6 | put(free, 1),
7 |
8 | %% INIT MUST be the first command
9 | make_svg_object(Ws, init1,
10 | [{id, svg}, {parent, here},
11 | {width,800}, {ht,400}, {color,'#ddebdd'}]),
12 | make_svg_object(Ws, add_grid,
13 | [{parent,svg}, {step,50},{width,800}, {ht,400}]),
14 |
15 | make_svg_object(Ws, add_drag_rect,
16 | [{parent,svg},
17 | {id,3456},
18 | {x,300},{y,350},{width,100},{ht,25}]),
19 | text(Ws, 300, 390, "Hozontally constrained draggagle"),
20 | add_standard_objects(Ws),
21 | add_generic_objects(Ws),
22 |
23 | make_svg_object(Ws, add_button,
24 | [{parent,svg},{id,23},
25 | {x,200},{y,300},{width,100},{str,<<"click me">>}
26 | ]),
27 |
28 | make_svg_object(Ws, add_button,
29 | [{parent,svg},{id,223},
30 | {x,50},{y,300},{width,120},{str,<<"click 12">>}
31 | ]),
32 |
33 |
34 | %% To make an elastic line we make two drag blobs
35 |
36 | make_svg_object(Ws, add_dragblob,
37 | [{id,100},{parent,svg},{x,20},{y,350},
38 | {r,10}]),
39 |
40 | make_svg_object(Ws, add_dragblob,
41 | [{id,101},{parent,svg},{x,100},{y,350},
42 | {r,10}]),
43 |
44 | make_svg_object(Ws, add_dragblob,
45 | [{id,103},{parent,svg},{x,20},{y,250},
46 | {r,10}]),
47 |
48 | make_svg_object(Ws, add_elastic_arrow,
49 | [{parent,svg},{id,268},
50 | {id1,100},
51 | {r1,10},{r2,10},
52 | {id2,101}]),
53 | make_svg_object(Ws, add_elastic_arrow,
54 | [{parent,svg},{id,269},
55 | {id1,101},
56 | {r1,10},{r2,10},
57 | {id2,103}]),
58 | make_dashed_container(Ws, rect_container()),
59 | loop(Ws, true).
60 |
61 | add_generic_objects(Ws) ->
62 | Scale = 0.03,
63 | Y=230,
64 | make_generic_object(Ws, rect,
65 | [{x,50},{y,50},{rx,5},{ry,5},
66 | {parent,svg},
67 | {width,50},{height,20},
68 | {fill,<<"#bacdff">>}
69 | ]),
70 |
71 | text(Ws, 55, 65,"rect1"),
72 |
73 | make_generic_object(Ws,rect,
74 | [{x,50},
75 | {y,100},
76 | {rx,5},
77 | {ry,5},{width,50},{height,30},
78 | {'stroke-dasharray', <<"2,2">>},
79 | {'stroke', blue},
80 | {'stroke-width',3},
81 | {fill,yellow},
82 | {parent,svg}]),
83 | text(Ws, 55,120,"rect2"),
84 |
85 | make_generic_object(Ws,circle,
86 | [{r,35},
87 | {cx,150},{cy, 100},
88 | {'stroke-dasharray', <<"2,2">>},
89 | {'stroke', blue},
90 | {'stroke-width',3},
91 | {fill,orange},
92 | {parent,svg}]),
93 | text(Ws, 120,110,"circle1"),
94 |
95 | make_generic_object(Ws, circle,
96 | [{r,35},
97 | {cx,150},{cy, 200},
98 | {fill,green},
99 | {parent,svg}]),
100 |
101 | text(Ws, 130,210,"circle2"),
102 |
103 | make_generic_object(Ws, path,
104 | [{parent,svg},
105 | transform(250,100,Scale),
106 | {d,wow()}]),
107 | text(Ws, 250, 140, "Music!"),
108 |
109 | %% Here X=100 Y=270
110 | make_generic_object(Ws, path,
111 | [
112 | {parent,svg},
113 | {style,<<"stroke:black">>},
114 | %% H is horizional line to
115 | %% h is in relative units (easier)
116 | {d,<<"M100 280 H 250 M 100 290 h 150">>}]
117 | ),
118 | text(Ws, 100, 270, "horizontal lines").
119 |
120 |
121 | rect_container() ->
122 | {550,50,200,300}.
123 |
124 |
125 | make_dashed_container(Ws, {X, Y, W, H}) ->
126 | make_svg_object(Ws, add_rect,[{x,X},{y,Y},{ht,H},{width,W},
127 | {parent,svg},
128 | {color,none},
129 | {thickness,6},
130 | {dash,<<"4,4">>}]).
131 |
132 | add_standard_objects(Ws) ->
133 | make_svg_object(Ws, add_rect,[{x,200},{y,50},{ht,200},
134 | {width,150},
135 | {parent,svg},
136 | {color,none},
137 | {thickness,4},
138 | {dash,<<"4,4">>}]),
139 |
140 | make_svg_object(Ws, add_line,[{x1,350},{y1,350},{x2,475},{y2,325},
141 | {width,2},
142 | {parent,svg}]),
143 |
144 | make_svg_object(Ws, add_line,[{parent,svg},
145 | {x1,350},{y1,340},{x2,475},{y2,315},
146 | {width,2}, {arrow,true}]),
147 |
148 | make_svg_object(Ws, add_image,[{img,<<"./p5.jpg">>},
149 | {x,200},
150 | {y,350}, {width,100}, {ht,50},
151 | {parent,svg}]),
152 |
153 | make_svg_object(Ws, add_text,
154 | [{parent,svg},{x,350},
155 | {y,250}, {str,<<"Drag the Red blob below">>}]),
156 |
157 | make_svg_object(Ws, add_dragblob,[{id,12}, {parent,svg},{x,450},{y,280}]),
158 |
159 |
160 | %% green ellipse
161 | make_svg_object(Ws, add_ellipse, [{cx,300},{cy,200},{rx,45},{ry,25},
162 | {fill,pink},{parent,svg}]),
163 | text(Ws,270,200,"ellipse1"),
164 |
165 | %% an arcs
166 |
167 | make_svg_object(Ws, add_arc,[{parent,svg},
168 | {fill,orange},
169 | {startangle,20}, {angle,65},
170 | {cx,300}, {cy,100}, {r,45}]),
171 | text(Ws,315,75,"arc"),
172 |
173 | %% groups can be made draggable
174 |
175 | make_svg_object(Ws, add_group,
176 | [{parent,svg},{id,99},{x,400},
177 | {draggable,true},
178 | {y,50}]),
179 | make_svg_object(Ws, add_rect,[{parent,99}, {x,0},{y,0},
180 | {width,100},{ht,150},
181 | {color,<<"#fedcba">>}]),
182 | make_svg_object(Ws, add_text,
183 | [{parent,99},{y,20},{str,<<"Drag Me">>}]),
184 |
185 | make_svg_object(Ws, add_arc,[{parent,99}, {startangle,10}, {angle,320},
186 | {cx,50}, {cy,60}, {r,35}, {fill, red}]).
187 |
188 | make_generic_object(Ws, Type, L) ->
189 | L1 = [{cmd,'SVG.mk_generic'},{type,Type}|L],
190 | Ws ! {send, list_to_binary(ezwebframe_mochijson2:encode([{struct,L1}]))}.
191 |
192 | make_svg_object(Ws, X, L) ->
193 | Cmd= list_to_binary(atom_to_list(X)),
194 | L1 = [{cmd,<<"SVG.",Cmd/binary>>}|L],
195 | Ws ! {send, list_to_binary(ezwebframe_mochijson2:encode([{struct,L1}]))}.
196 |
197 |
198 | new_index() ->
199 | N = get(free),
200 | put(free, N+1),
201 | N.
202 |
203 | enc(L) ->
204 | L1 = [{struct,J} || J <- L],
205 | C = ezwebframe_mochijson2:encode(L),
206 | list_to_binary(C).
207 |
208 | transform(X, Y, Scale) ->
209 | {transform,f2b("translate(~p,~p) scale(~p,~p)", [X,Y,Scale,-Scale])}.
210 |
211 | text(Ws, X, Y, Str) ->
212 | make_svg_object(Ws, add_text,
213 | [{parent,svg},{x,X},{y,Y},{str,list_to_binary(Str)}]).
214 |
215 |
216 | random_rect(Ws) ->
217 | {X0,Y0,Width,Ht} = rect_container(),
218 | Id = new_index(),
219 | X = X0 + pos_ran(Width),
220 | Y = Y0 + pos_ran(Ht),
221 | W = pos_ran(Width+X0-X) - 5,
222 | H = pos_ran(Ht+Y0 -Y) - 5,
223 | if
224 | H < 0; W < 0 ->
225 | random_rect(Ws);
226 | true ->
227 | make_generic_object(Ws,rect,
228 | [{rx,3},
229 | {ry,3},
230 | {parent,svg},
231 | {x,X},{y,Y},{width,W},{height,H},
232 | {fill,ran_color()}
233 | ])
234 | end.
235 |
236 | pos_ran(N) when N =< 0 ->
237 | 1;
238 | pos_ran(N) ->
239 | random:uniform(N).
240 |
241 | ran_color() ->
242 | B1 = unsigned_byte_to_hex_string(random:uniform(255)),
243 | B2 = unsigned_byte_to_hex_string(random:uniform(255)),
244 | B3 = unsigned_byte_to_hex_string(random:uniform(255)),
245 | list_to_binary([$#,B1,B2,B3]).
246 |
247 | wow() ->
248 | <<"M643 2c0 -102 -65 -214 -190 -248c0 -13 1 -27 1 -40c0 -46 -1 -92 -4 -138c-7 -119 -92 -227 -214 -227c-111 0 -202 92 -202 205c0 58 54 104 113 104c54 0 95 -48 95 -104c0 -52 -43 -95 -95 -95c-13 0 -27 4 -39 10c26 -47 74 -80 130 -80c100 0 166 94 172 193c3 44 4 89 4 133v31c-31 -5 -63 -6 -79 -6c-189 0 -333 173 -333 372c0 181 134 314 254 451c-37 129 -54 211 -54 379c0 197 147 308 159 308c25 0 151 -219 151 -388c0 -150 -90 -267 -190 -380c22 -73 42 -147 61 -221h6c154 0 254 -127 254 -259zM452 -207c66 20 124 84 124 170c0 90 -64 178 -168 192c27 -129 40 -239 44 -362zM338 -220c7 0 45 1 75 5c-4 127 -19 241 -47 372c-87 -5 -136 -62 -136 -124c0 -45 26 -92 83 -125c4 -4 7 -9 7 -14c0 -11 -9 -21 -20 -21c-15 0 -125 63 -125 186c0 90 61 178 168 198c-16 64 -35 127 -53 190c-110 -124 -220 -249 -220 -414c0 -149 144 -253 268 -253zM409 1108c-100 -55 -162 -159 -162 -273c0 -94 27 -190 40 -236c86 102 158 209 158 342c0 77 -11 110 -36 167z">>.
249 |
250 |
251 | f2b(F, D) ->
252 | list_to_binary(lists:flatten(io_lib:format(F, D))).
253 |
254 |
255 | loop(Ws, B) ->
256 | receive
257 | {msg,Json} ->
258 | io:format("Received:~p~n",[Json]),
259 | loop(Ws, B);
260 | X ->
261 | io:format("handler got unexpected::~p~n",[X]),
262 | loop(Ws, B)
263 | after 1000 ->
264 | case B of
265 | true ->
266 | random_rect(Ws),
267 | loop(Ws, B);
268 | false ->
269 | loop(Ws, B)
270 | end
271 | end.
272 |
273 | -spec unsigned_byte_to_hex_string(integer()) -> [byte()].
274 |
275 | %% N is in -128 .. 127
276 |
277 | unsigned_byte_to_hex_string(N) when N >= 0, N < 256 ->
278 | [nibble_to_hex_char(N bsr 4),nibble_to_hex_char(N band 15)].
279 |
280 | nibble_to_hex_char(X) when X < 10 -> $0 + X;
281 | nibble_to_hex_char(X) -> $a + X - 10.
282 |
--------------------------------------------------------------------------------
/demos/svg/viserl1.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
497 |
498 | Visual erlang
499 |
500 | need to have absolute tree like name for all rendered attributes.
501 |
502 |
503 |
504 |
505 |
506 |
507 |
508 |
509 |
510 |
511 |
update
512 |
513 |
514 |
515 |
516 |
517 |
518 |
519 |
--------------------------------------------------------------------------------
/demos/svg/svg_pad4.js:
--------------------------------------------------------------------------------
1 | /*
2 | * This file defines a single function SVG to use the module
3 | * var x = new SVG();
4 | * x.mk_canvas({width:200, id:1, ht:100, color:blue});
5 | * x.mk_circle({parent:1, cx:10, cy:20, r:15, color:"green"});
6 | * x.render([cmd]) interprets a list of commands
7 | *
8 | */
9 |
10 | var SVG = {};
11 |
12 | SVG.svg_ns = 'http://www.w3.org/2000/svg';
13 | SVG.xhtml_ns = 'http://www.w3.org/1999/xhtml';
14 | SVG.xlinkns = 'http://www.w3.org/1999/xlink';
15 |
16 | // drag variables
17 | SVG.xstart = 0;
18 | SVG.ystart = 0;
19 | SVG.dragging = false;
20 | SVG.dragobj = null;
21 | SVG.canvas = null;
22 |
23 |
24 | SVG.init1 = function(o){
25 | // console.log('***** INIT ***** ', [SVG,o.indiv]);
26 | var canvas = SVG.add_canvas(o);
27 | }
28 |
29 | SVG.id = function(tag) {
30 | return document.getElementById(tag);
31 | };
32 |
33 | SVG.D = function(x, y) {
34 | return x == undefined ? y : x;
35 | };
36 |
37 | SVG.save = function(key, val){
38 | if(key !== undefined){
39 | if(V.hasOwnProperty(key)){
40 | alert('Error SVG created object with duplicate ID = ' + key);
41 | } else {
42 | V[key] = val;
43 | }
44 | }
45 | }
46 |
47 | SVG.add_canvas = function(o) {
48 | var canvas = document.createElementNS(SVG.svg_ns, 'svg');
49 | canvas.setAttribute("width", SVG.D(o.width, 200));
50 | canvas.setAttribute("height", SVG.D(o.ht, 100));
51 | canvas.setAttribute("style","background-color:" +
52 | SVG.D(o.color, "#eeffbb"));
53 | canvas.setAttribute("id", o.id);
54 | canvas.addEventListener("mousemove", SVG.mouse_move, false);
55 | canvas.addEventListener("mousedown", SVG.mouse_down, false);
56 | canvas.addEventListener("mouseup", SVG.mouse_up, false);
57 | SVG.id(o.parent).appendChild(canvas);
58 | // add a marker that be used to add the tip to the end of the arrows
59 | var marker = SVG.make_arrow_marker();
60 | canvas.appendChild(marker);
61 | SVG.save(o.id, canvas);
62 | return canvas;
63 | };
64 |
65 | // Jarc_arc code from osé M. Vidal
66 | // http://jmvidal.cse.sc.edu/talks/canvassvg/
67 |
68 | SVG.add_arc = function(o)
69 | {
70 | var path = document.createElementNS(SVG.svg_ns, "path");
71 | var startangle = o.startangle*Math.PI/180;
72 | var angle = o.angle*Math.PI/180;
73 | var endangle = startangle + angle;
74 |
75 | // Compute the two points where our wedge intersects the circle
76 | // These formulas are chosen so that an angle of 0 is at 12 o'clock
77 | // and positive angles increase clockwise.
78 | var x1 = o.cx + o.r * Math.sin(startangle);
79 | var y1 = o.cy - o.r * Math.cos(startangle);
80 | var x2 = o.cx + o.r * Math.sin(endangle);
81 | var y2 = o.cy - o.r * Math.cos(endangle);
82 |
83 | // This is a flag for angles larger than than a half circle
84 | var big = 0;
85 | if (endangle - startangle > Math.PI) big = 1;
86 |
87 | var d = "M " + o.cx + "," + o.cy + // Start at circle center
88 | " L " + x1 + "," + y1 + // Draw line to (x1,y1)
89 | " A " + o.r + "," + o.r + // Draw an arc of radius r
90 | " 0 " + big + " 1 " + // Arc details...
91 | x2 + "," + y2 + // Arc goes to to (x2,y2)
92 | " Z"; // Close path back to (cx,cy)
93 | path.setAttribute("d", d); // Set this path
94 | path.setAttribute("fill", SVG.D(o.fill, "blue")); // Set wedge color
95 | path.setAttribute("stroke", "black"); // Outline wedge in black
96 | path.setAttribute("stroke-width", "2"); // 2 units thick
97 | V[o.parent].appendChild(path);
98 | SVG.save(o.id, path);
99 | };
100 |
101 | SVG.add_bezier = function(o)
102 | {
103 | var path = document.createElementNS(SVG.svg_ns, "path");
104 | path.setAttribute("d", o.path); // Set this path
105 | path.setAttribute("fill", SVG.D(o.fill, "blue")); // Set wedge color
106 | path.setAttribute("stroke", "black"); // Outline in black
107 | path.setAttribute("stroke-width", SVG.D(o.width, "2")); // 2 units thick
108 | V[o.parent].appendChild(path);
109 | SVG.save(o.id, path);
110 | };
111 |
112 | SVG.add_path = function(o)
113 | {
114 | var group = document.createElementNS(SVG.svg_ns, 'g');
115 | group.setAttribute('transform', o.transform);
116 | V[o.parent].appendChild(group);
117 | var path = document.createElementNS(SVG.svg_ns, "path");
118 | path.setAttribute("d", o.d); // Set this path
119 | path.setAttribute("fill", SVG.D(o.fill, "blue")); // Set wedge color
120 | path.setAttribute("stroke", "black"); // Outline wedge in black
121 | path.setAttribute("stroke-width", SVG.D(o.width, "2")); // 2 units thick
122 | group.appendChild(path);
123 | };
124 |
125 | SVG.add_circle = function(o)
126 | {
127 | var obj = document.createElementNS(SVG.svg_ns, 'circle');
128 | obj.setAttribute('cx', o.x);
129 | obj.setAttribute('cy', o.y);
130 | obj.setAttribute('r', SVG.D(o.r, 10));
131 | obj.setAttribute('fill', SVG.D(o.fill,"red"));
132 | obj.setAttribute('stroke', SVG.D(o.stroke,
133 | SVG.D(o.outer, "blue")));
134 | obj.setAttribute('stroke-width', SVG.D(o.stroke,2));
135 | V[o.parent].appendChild(obj);
136 | SVG.save(o.id, obj);
137 | };
138 |
139 | SVG.add_ellipse = function(o)
140 | {
141 | var obj = document.createElementNS(SVG.svg_ns, "ellipse");
142 | obj.setAttribute("cx", o.cx);
143 | obj.setAttribute("cy", o.cy);
144 | obj.setAttribute("rx", o.rx);
145 | obj.setAttribute("ry", o.ry);
146 | obj.setAttribute("fill", SVG.D(o.fill,"black"));
147 | V[o.parent].appendChild(obj);
148 | SVG.save(o.id, obj);
149 | };
150 |
151 | SVG.add_group = function(o)
152 | {
153 | var group = document.createElementNS(SVG.svg_ns, 'g');
154 | if(SVG.D(o.draggable, false)){
155 | group.type="draggable";
156 | group.id = o.id;
157 | };
158 | var x = SVG.D(o.x,10);
159 | var y = SVG.D(o.y,10);
160 | var t = "translate("+x+","+y+")";
161 | group.setAttribute('transform', t);
162 | group.setAttribute('x',x);
163 | group.setAttribute('y',y);
164 | V[o.parent].appendChild(group);
165 | SVG.save(o.id, group);
166 | return group;
167 | };
168 |
169 | SVG.add_image = function(o)
170 | {
171 | var img = document.createElementNS(SVG.svg_ns, "image");
172 | img.setAttribute("x", SVG.D(o.x,0));
173 | img.setAttribute("y", SVG.D(o.y,0));
174 | img.setAttribute("width", SVG.D(o.width, 20));
175 | img.setAttribute("height", SVG.D(o.ht, 20));
176 | img.setAttributeNS(SVG.xlinkns, "href", o.img);
177 | V[o.parent].appendChild(img);
178 | SVG.save(o.id, img);
179 | };
180 |
181 | SVG.add_line = function(o)
182 | {
183 | // console.log('add_line',o);
184 | var obj= document.createElementNS(SVG.svg_ns,"line");
185 | obj.setAttribute("x1", o.x1);
186 | obj.setAttribute("y1", o.y1);
187 | obj.setAttribute("x2", o.x2);
188 | obj.setAttribute("y2", o.y2);
189 | obj.setAttribute("stroke","black");
190 | var width = SVG.D(o.width,1);
191 | obj.setAttribute("stroke-width",width+"px");
192 | obj.setAttribute("fill",SVG.D(o.fill,"black"));
193 | if (o.arrow == true)
194 | obj.setAttribute("marker-end","url(#myArrowTip)");
195 | obj.setAttribute("id", o.id);
196 | V[o.parent].appendChild(obj);
197 | SVG.save(o.id, obj);
198 | };
199 |
200 | SVG.add_rect = function(o)
201 | {
202 | var obj = document.createElementNS(SVG.svg_ns, 'rect');
203 | obj.setAttribute('x', o.x);
204 | obj.setAttribute('y', o.y);
205 | obj.setAttribute('width', SVG.D(o.width,80));
206 | obj.setAttribute('height', SVG.D(o.ht, 20));
207 | obj.setAttribute('fill', SVG.D(o.color,"#aaaaaa"));
208 | obj.setAttribute('stroke', SVG.D(o.stroke,SVG.
209 | D(o.fill, "aaaaaa")));
210 | obj.setAttribute('stroke-width', SVG.D(o.thickness,1));
211 | obj.setAttribute("rx", SVG.D(o.rx, 3));
212 | obj.setAttribute("ry", SVG.D(o.ry, 3));
213 | obj.setAttribute("id", o.id);
214 | if(o.dash != undefined)
215 | obj.setAttribute("style", "stroke-dasharray: "+ o.dash);
216 | V[o.parent].appendChild(obj);
217 | SVG.save(o.id,obj);
218 |
219 | return obj;
220 | };
221 |
222 | SVG.add_text = function(o)
223 | {
224 | var text = document.createElementNS(SVG.svg_ns, "text");
225 | text.setAttribute("fill", SVG.D(o.fill,"black"));
226 | var size = SVG.D(o.size, 1);
227 | text.setAttribute("font-size", size+"em");
228 | var font = SVG.D(o.font,"Arial");
229 | text.setAttribute("font-family", font);
230 | text.setAttribute("x", SVG.D(o.x,10));
231 | text.setAttribute("y", SVG.D(o.y,10));
232 | // start middle end
233 | text.setAttribute("text-anchor", SVG.D(o.anchor,"start"));
234 | var str = SVG.D(o.str,"** missing str in text **");
235 | var data = document.createTextNode(o.str);
236 | text.setAttribute("id", o.id);
237 | text.appendChild(data);
238 | V[o.parent].appendChild(text);
239 | SVG.save(o.id, text);
240 | };
241 |
242 | SVG.mouse_up = function(evt)
243 | {
244 | // can get mouse up from a click on a button
245 | if(SVG.dragging){
246 | SVG.dragging = false;
247 | var t = evt.target.parentNode;
248 | var x = t.getAttribute("x");
249 | var y = t.getAttribute("y");
250 | var z = t.getAttribute("id");
251 | // console.log('stop dragging', [t,z]);
252 | // SVG.log("stop dragging x=" + x +" y=" + y);
253 | send_json({cmd:'stop_drag',x:x,y:y,id:z});
254 | if(t.ondrop){
255 | t.ondrop(x, y);
256 | }
257 | }
258 | };
259 |
260 | SVG.mouse_move = function(evt)
261 | {
262 | if (SVG.dragging)
263 | {
264 | var t = evt.target;
265 | var x = evt.clientX + window.scrollX;
266 | var y = evt.clientY + window.scrollY;
267 | // Move drag element by the same amount the cursor has moved.
268 | var x1 = (x-SVG.xstart);
269 | var y1 = (y-SVG.ystart);
270 | if (SVG.dragobj.constrain == "h"){
271 | // if we want to constrain the dragging we
272 | // freeze y1
273 | y1 = SVG.dragobj.getAttribute("y");
274 | };
275 | if (SVG.dragobj.constrain == "v"){
276 | // if we want to constrain the dragging we
277 | // freeze y1
278 | x1 = SVG.dragobj.getAttribute("x");
279 | };
280 | SVG.dragobj.setAttribute("x", x1);
281 | SVG.dragobj.setAttribute("y", y1);
282 | var t = "translate("+x1+","+y1+")";
283 | SVG.dragobj.setAttribute('transform', t);
284 | SVG.update_draggables(SVG.dragobj);
285 | }
286 | };
287 |
288 | SVG.update_draggables = function(x){
289 | // console.log('update_draggables', [x.elastic, x.id]);
290 | var lines= x.elastic;
291 | for(var i=0; i < lines.length; i++){
292 | var o = V[lines[i]];
293 | var g1 = V[o.id1];
294 | var x1 = parseInt(g1.getAttribute("x"));
295 | var y1 = parseInt(g1.getAttribute("y"));
296 | var r1 = o.r1;
297 | var g2 = V[o.id2];
298 | var x2 = parseInt(g2.getAttribute("x"));
299 | var y2 = parseInt(g2.getAttribute("y"));
300 | var r2 = o.r2;
301 | var obj = o.obj;
302 | var r1 = o.r1;
303 | var r2 = o.r2;
304 | var f = SVG.endpoints(x1,y1,r1,x2,y2,r2);
305 | obj.setAttribute("x1",f.x1);
306 | obj.setAttribute("y1",f.y1);
307 | obj.setAttribute("x2",f.x2);
308 | obj.setAttribute("y2",f.y2);
309 | // console.log('here', endpoints);
310 | }
311 | }
312 |
313 | SVG.mouse_down = function(evt)
314 | {
315 | var t = evt.target.parentNode;
316 | // console.log("clicked on type=", [t,t.type]);
317 | if(t.type == "button"){
318 | // fire the button method
319 | t.obj.clicked();
320 | } else if(t.type == "draggable") {
321 | SVG.dragging = true;
322 | SVG.dragobj = t;
323 | var x = evt.clientX + window.scrollX;
324 | var y = evt.clientY + window.scrollY;
325 | var cxstart = t.getAttribute("x");
326 | var cystart = t.getAttribute("y");
327 | SVG.xstart = x - cxstart;
328 | SVG.ystart = y - cystart;
329 | // console.log('start drag',[t.id,t.type,t]);
330 | send_json({cmd:'start_drag',x:cxstart,y:cystart,id:t.id});
331 | // SVG.log("start dragging t.type="+t.type+
332 | // " x=" +cxstart+" y="+cystart);
333 | }
334 | };
335 |
336 | SVG.make_arrow_marker = function()
337 | {
338 | var marker = document.createElementNS(SVG.svg_ns, "marker");
339 | marker.setAttribute("id", "myArrowTip"); // <-- the name in url(#...)
340 | marker.setAttribute("viewBox","0 0 10 10");
341 | marker.setAttribute("refX",1);
342 | marker.setAttribute("refY",5);
343 | marker.setAttribute("markerUnits", 8);
344 | marker.setAttribute("orient","auto");
345 | marker.setAttribute("markerWidth",8);
346 | marker.setAttribute("markerHeight",6);
347 | var path = document.createElementNS(SVG.svg_ns, "polyline");
348 | path.setAttribute("points","0,0 10,5 0,10 1,5");
349 | path.setAttribute("fill","darkblue");
350 | marker.appendChild(path);
351 | return marker;
352 | };
353 |
354 | SVG.log = function(x){
355 | SVG.id("log").innerHTML += x + " ";
356 | };
357 |
358 | SVG.add_elastic_arrow = function(o){
359 | // console.log('add_elastic', o);
360 | var g1 = V[o.id1];
361 | var x1 = parseInt(g1.getAttribute("x"));
362 | var y1 = parseInt(g1.getAttribute("y"));
363 | var r1 = o.r1;
364 | var g2 = V[o.id2];
365 | var x2 = parseInt(g2.getAttribute("x"));
366 | var y2 = parseInt(g2.getAttribute("y"));
367 | var r2 = o.r2;
368 | var f = SVG.endpoints(x1,y1,r1,x2,y2,r2);
369 | SVG.add_line({parent:o.parent,id:o.id,x1:f.x1,y1:f.y1,x2:f.x2,y2:f.y2,
370 | arrow:true});
371 | // need to tell the drag blogs that they elastic endpoints
372 | // make a new_id
373 | var newId = o.id +'e';
374 | SVG.save(newId, {id1:o.id1, id2:o.id2, r1:r1, r2:r2, obj:V[o.id]});
375 | g1.elastic.push(newId);
376 | g2.elastic.push(newId);
377 | }
378 |
379 | SVG.endpoints = function(x1,y1,r1,x2,y2,r2)
380 | {
381 | // line between two circles
382 | var m,a,b,x3,y3,x4,y4;
383 | m = 5; // marker offset
384 | d = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
385 | b = x1;
386 | a = (x2 - b)/d;
387 | x3 = r1*a + b;
388 | x4 = (d-r2-m)*a + b;
389 | b = y1;
390 | a = (y2 - b)/d;
391 | y3 = (r1)*a + b;
392 | y4 = (d-r2-m)*a + b;
393 | return {x1:x3, y1:y3, x2:x4, y2:y4}
394 | }
395 |
396 | SVG.add_drag_rect = function(o){
397 | var g1 = SVG.add_group({parent:o.parent, draggable:true,
398 | id:o.id, x:o.x,y:o.y});
399 | // console.log('g1', [g1,o.id]);
400 | g1.id = o.id;
401 | g1.constrain = 'h';
402 | SVG.add_rect({parent:o.id, x:0,y:0,width:o.width,color:'pink',
403 | ht:o.ht, rx:2,ry:2});
404 | };
405 |
406 |
407 |
408 | SVG.add_dragblob = function(o)
409 | {
410 | // red circle - black border
411 | var g1 = SVG.add_group({parent:o.parent, draggable:true,
412 | id:o.id, x:o.x,y:o.y});
413 | // console.log('g1', [g1,o.id]);
414 | g1.id = o.id;
415 | g1.elastic = new Array();
416 | SVG.add_circle({parent:o.id, x:0,y:0,r:o.r,stroke:2});
417 | };
418 |
419 |
420 | /*
421 | * Now for some objects that are built using SVG
422 | * Button
423 | *
424 | */
425 |
426 | SVG.add_button = function(o){
427 | new SVG.Button(o);
428 | }
429 |
430 | SVG.Button = function(o)
431 | {
432 | var g = SVG.add_group({parent:o.parent,id:o.id,x:o.x,y:o.y});
433 | this.parentId = o.id;
434 | this.group = g;
435 | g.type = "button";
436 | g.obj = this;
437 | this.width = o.width;
438 | this.rect = SVG.add_rect({parent:o.id,
439 | id:o.id+'r',
440 | x:0, y:0, str:o.str, stroke:"black",
441 | rx:1, ry:1, width:o.width, ht:25, thickness:1,
442 | color:"#dddddd"});
443 | this.text = SVG.add_text({parent:o.id,
444 | id:o.id+'t',
445 | x:(o.width/2),y:18, str:o.str,
446 | anchor:"middle"});
447 | };
448 |
449 | SVG.Button.prototype.clicked = function(){
450 | // r is the dotted line rectange that appears
451 | // this does not need an id
452 | var r = SVG.add_rect({parent:this.parentId,
453 | x:3,y:3,color:"none",
454 | stroke:"black",
455 | width: this.width - 6,
456 | ht:19,
457 | thickness:1, dash:"1,1"});
458 | var oldcolor = this.rect.getAttribute('fill');
459 | var g = this.group;
460 | var f = function(){g.removeChild(r);};
461 | send_json({cmd:'clicked',id:this.parentId});
462 | setTimeout(f, 500);
463 | };
464 |
465 | // make a design grid -- easier to see the coordinates
466 |
467 | SVG.add_grid = function(o){
468 | var width = o.width;
469 | var ht = o.ht;
470 | var step = o.step;
471 | var parent = o.parent;
472 | // draw horizontal grid
473 | var i = 0;
474 | while(i < ht){
475 | SVG.add_line({parent:parent, x1:0,y1:i,x2:width,y2:i});
476 | SVG.add_text({parent:parent, x:5,y:i+15,str:i});
477 | i += step;
478 | };
479 | // draw vertical grid
480 | i = 0;
481 | while(i < width){
482 | SVG.add_line({parent:o.parent, x1:i,y1:0,x2:i,y2:ht});
483 | SVG.add_text({parent:parent, x:i+5,y:15,str:i});
484 | i += step;
485 | };
486 | }
487 |
488 | function send_json(o){
489 | send(JSON.stringify(o));
490 | }
491 |
492 | SVG.mk_generic = function(o){
493 | var obj = document.createElementNS(SVG.svg_ns, o.type);
494 | for(key in o){
495 | var val = o[key];
496 | obj.setAttributeNS(null, key, val);
497 | };
498 | V[o.parent].appendChild(obj);
499 | SVG.save(o.id, obj);
500 | }
501 |
--------------------------------------------------------------------------------
/src/ezwebframe_mochijson2.erl:
--------------------------------------------------------------------------------
1 | %% Origonal module name
2 | %% -module(mochijson2).
3 | %% Renamed to adhere to OTP packaging conventions
4 | %% Fetched from
5 | %% https://github.com/mochi/mochiweb/blob/master/src/mochijson2.erl
6 |
7 | -module(ezwebframe_mochijson2).
8 |
9 | %% @author Bob Ippolito
10 | %% @copyright 2007 Mochi Media, Inc.
11 |
12 | %% @doc Yet another JSON (RFC 4627) library for Erlang. mochijson2 works
13 | %% with binaries as strings, arrays as lists (without an {array, _})
14 | %% wrapper and it only knows how to decode UTF-8 (and ASCII).
15 | %%
16 | %% JSON terms are decoded as follows (javascript -> erlang):
17 | %%
18 | %% {"key": "value"} ->
19 | %% {struct, [{<<"key">>, <<"value">>}]}
20 | %% ["array", 123, 12.34, true, false, null] ->
21 | %% [<<"array">>, 123, 12.34, true, false, null]
22 | %%
23 | %%
24 | %%
25 | %% Strings in JSON decode to UTF-8 binaries in Erlang
26 | %% Objects decode to {struct, PropList}
27 | %% Numbers decode to integer or float
28 | %% true, false, null decode to their respective terms.
29 | %%
30 | %% The encoder will accept the same format that the decoder will produce,
31 | %% but will also allow additional cases for leniency:
32 | %%
33 | %% atoms other than true, false, null will be considered UTF-8
34 | %% strings (even as a proplist key)
35 | %%
36 | %% {json, IoList} will insert IoList directly into the output
37 | %% with no validation
38 | %%
39 | %% {array, Array} will be encoded as Array
40 | %% (legacy mochijson style)
41 | %%
42 | %% A non-empty raw proplist will be encoded as an object as long
43 | %% as the first pair does not have an atom key of json, struct,
44 | %% or array
45 | %%
46 | %%
47 |
48 | %% -module(mochijson2).
49 | -author('bob@mochimedia.com').
50 | -export([encoder/1, encode/1]).
51 | -export([decoder/1, decode/1, decode/2]).
52 |
53 | %% This is a macro to placate syntax highlighters..
54 | -define(Q, $\").
55 | -define(ADV_COL(S, N), S#decoder{offset=N+S#decoder.offset,
56 | column=N+S#decoder.column}).
57 | -define(INC_COL(S), S#decoder{offset=1+S#decoder.offset,
58 | column=1+S#decoder.column}).
59 | -define(INC_LINE(S), S#decoder{offset=1+S#decoder.offset,
60 | column=1,
61 | line=1+S#decoder.line}).
62 | -define(INC_CHAR(S, C),
63 | case C of
64 | $\n ->
65 | S#decoder{column=1,
66 | line=1+S#decoder.line,
67 | offset=1+S#decoder.offset};
68 | _ ->
69 | S#decoder{column=1+S#decoder.column,
70 | offset=1+S#decoder.offset}
71 | end).
72 | -define(IS_WHITESPACE(C),
73 | (C =:= $\s orelse C =:= $\t orelse C =:= $\r orelse C =:= $\n)).
74 |
75 | %% @type json_string() = atom | binary()
76 | %% @type json_number() = integer() | float()
77 | %% @type json_array() = [json_term()]
78 | %% @type json_object() = {struct, [{json_string(), json_term()}]}
79 | %% @type json_eep18_object() = {[{json_string(), json_term()}]}
80 | %% @type json_iolist() = {json, iolist()}
81 | %% @type json_term() = json_string() | json_number() | json_array() |
82 | %% json_object() | json_eep18_object() | json_iolist()
83 |
84 | -record(encoder, {handler=null,
85 | utf8=false}).
86 |
87 | -record(decoder, {object_hook=null,
88 | offset=0,
89 | line=1,
90 | column=1,
91 | state=null}).
92 |
93 | %% @spec encoder([encoder_option()]) -> function()
94 | %% @doc Create an encoder/1 with the given options.
95 | %% @type encoder_option() = handler_option() | utf8_option()
96 | %% @type utf8_option() = boolean(). Emit unicode as utf8 (default - false)
97 | encoder(Options) ->
98 | State = parse_encoder_options(Options, #encoder{}),
99 | fun (O) -> json_encode(O, State) end.
100 |
101 | %% @spec encode(json_term()) -> iolist()
102 | %% @doc Encode the given as JSON to an iolist.
103 |
104 | encode(Any) ->
105 | %% io:format("encode ...:~p~n",[Any]),
106 | json_encode(Any, #encoder{}).
107 |
108 | %% @spec decoder([decoder_option()]) -> function()
109 | %% @doc Create a decoder/1 with the given options.
110 | decoder(Options) ->
111 | State = parse_decoder_options(Options, #decoder{}),
112 | fun (O) -> json_decode(O, State) end.
113 |
114 | %% @spec decode(iolist(), [{format, proplist | eep18 | struct}]) -> json_term()
115 | %% @doc Decode the given iolist to Erlang terms using the given object format
116 | %% for decoding, where proplist returns JSON objects as [{binary(), json_term()}]
117 | %% proplists, eep18 returns JSON objects as {[binary(), json_term()]}, and struct
118 | %% returns them as-is.
119 | decode(S, Options) ->
120 | json_decode(S, parse_decoder_options(Options, #decoder{})).
121 |
122 | %% @spec decode(iolist()) -> json_term()
123 | %% @doc Decode the given iolist to Erlang terms.
124 | decode(S) ->
125 | json_decode(S, #decoder{}).
126 |
127 | %% Internal API
128 |
129 | parse_encoder_options([], State) ->
130 | State;
131 | parse_encoder_options([{handler, Handler} | Rest], State) ->
132 | parse_encoder_options(Rest, State#encoder{handler=Handler});
133 | parse_encoder_options([{utf8, Switch} | Rest], State) ->
134 | parse_encoder_options(Rest, State#encoder{utf8=Switch}).
135 |
136 | parse_decoder_options([], State) ->
137 | State;
138 | parse_decoder_options([{object_hook, Hook} | Rest], State) ->
139 | parse_decoder_options(Rest, State#decoder{object_hook=Hook});
140 | parse_decoder_options([{format, Format} | Rest], State)
141 | when Format =:= struct orelse Format =:= eep18 orelse Format =:= proplist ->
142 | parse_decoder_options(Rest, State#decoder{object_hook=Format}).
143 |
144 | json_encode(true, _State) ->
145 | <<"true">>;
146 | json_encode(false, _State) ->
147 | <<"false">>;
148 | json_encode(null, _State) ->
149 | <<"null">>;
150 | json_encode(I, _State) when is_integer(I) ->
151 | integer_to_list(I);
152 | json_encode(F, _State) when is_float(F) ->
153 | mochinum:digits(F);
154 | json_encode(S, State) when is_binary(S); is_atom(S) ->
155 | json_encode_string(S, State);
156 | json_encode([{K, _}|_] = Props, State) when (K =/= struct andalso
157 | K =/= array andalso
158 | K =/= json) ->
159 | json_encode_proplist(Props, State);
160 | json_encode({struct, Props}, State) when is_list(Props) ->
161 | json_encode_proplist(Props, State);
162 | json_encode({Props}, State) when is_list(Props) ->
163 | json_encode_proplist(Props, State);
164 | json_encode({}, State) ->
165 | json_encode_proplist([], State);
166 | json_encode(Array, State) when is_list(Array) ->
167 | json_encode_array(Array, State);
168 | json_encode({array, Array}, State) when is_list(Array) ->
169 | json_encode_array(Array, State);
170 | json_encode({json, IoList}, _State) ->
171 | IoList;
172 | json_encode(Bad, #encoder{handler=null}) ->
173 | exit({json_encode, {bad_term, Bad}});
174 | json_encode(Bad, State=#encoder{handler=Handler}) ->
175 | json_encode(Handler(Bad), State).
176 |
177 | json_encode_array([], _State) ->
178 | <<"[]">>;
179 | json_encode_array(L, State) ->
180 | F = fun (O, Acc) ->
181 | [$,, json_encode(O, State) | Acc]
182 | end,
183 | [$, | Acc1] = lists:foldl(F, "[", L),
184 | lists:reverse([$\] | Acc1]).
185 |
186 | json_encode_proplist([], _State) ->
187 | <<"{}">>;
188 | json_encode_proplist(Props, State) ->
189 | F = fun ({K, V}, Acc) ->
190 | KS = json_encode_string(K, State),
191 | VS = json_encode(V, State),
192 | [$,, VS, $:, KS | Acc]
193 | end,
194 | [$, | Acc1] = lists:foldl(F, "{", Props),
195 | lists:reverse([$\} | Acc1]).
196 |
197 | json_encode_string(A, State) when is_atom(A) ->
198 | L = atom_to_list(A),
199 | case json_string_is_safe(L) of
200 | true ->
201 | [?Q, L, ?Q];
202 | false ->
203 | json_encode_string_unicode(xmerl_ucs:from_utf8(L), State, [?Q])
204 | end;
205 | json_encode_string(B, State) when is_binary(B) ->
206 | case json_bin_is_safe(B) of
207 | true ->
208 | [?Q, B, ?Q];
209 | false ->
210 | json_encode_string_unicode(xmerl_ucs:from_utf8(B), State, [?Q])
211 | end;
212 | json_encode_string(I, _State) when is_integer(I) ->
213 | [?Q, integer_to_list(I), ?Q];
214 | json_encode_string(L, State) when is_list(L) ->
215 | case json_string_is_safe(L) of
216 | true ->
217 | [?Q, L, ?Q];
218 | false ->
219 | json_encode_string_unicode(L, State, [?Q])
220 | end.
221 |
222 | json_string_is_safe([]) ->
223 | true;
224 | json_string_is_safe([C | Rest]) ->
225 | case C of
226 | ?Q ->
227 | false;
228 | $\\ ->
229 | false;
230 | $\b ->
231 | false;
232 | $\f ->
233 | false;
234 | $\n ->
235 | false;
236 | $\r ->
237 | false;
238 | $\t ->
239 | false;
240 | C when C >= 0, C < $\s; C >= 16#7f, C =< 16#10FFFF ->
241 | false;
242 | C when C < 16#7f ->
243 | json_string_is_safe(Rest);
244 | _ ->
245 | false
246 | end.
247 |
248 | json_bin_is_safe(<<>>) ->
249 | true;
250 | json_bin_is_safe(<>) ->
251 | case C of
252 | ?Q ->
253 | false;
254 | $\\ ->
255 | false;
256 | $\b ->
257 | false;
258 | $\f ->
259 | false;
260 | $\n ->
261 | false;
262 | $\r ->
263 | false;
264 | $\t ->
265 | false;
266 | C when C >= 0, C < $\s; C >= 16#7f ->
267 | false;
268 | C when C < 16#7f ->
269 | json_bin_is_safe(Rest)
270 | end.
271 |
272 | json_encode_string_unicode([], _State, Acc) ->
273 | lists:reverse([$\" | Acc]);
274 | json_encode_string_unicode([C | Cs], State, Acc) ->
275 | Acc1 = case C of
276 | ?Q ->
277 | [?Q, $\\ | Acc];
278 | %% Escaping solidus is only useful when trying to protect
279 | %% against "" injection attacks which are only
280 | %% possible when JSON is inserted into a HTML document
281 | %% in-line. mochijson2 does not protect you from this, so
282 | %% if you do insert directly into HTML then you need to
283 | %% uncomment the following case or escape the output of encode.
284 | %%
285 | %% $/ ->
286 | %% [$/, $\\ | Acc];
287 | %%
288 | $\\ ->
289 | [$\\, $\\ | Acc];
290 | $\b ->
291 | [$b, $\\ | Acc];
292 | $\f ->
293 | [$f, $\\ | Acc];
294 | $\n ->
295 | [$n, $\\ | Acc];
296 | $\r ->
297 | [$r, $\\ | Acc];
298 | $\t ->
299 | [$t, $\\ | Acc];
300 | C when C >= 0, C < $\s ->
301 | [unihex(C) | Acc];
302 | C when C >= 16#7f, C =< 16#10FFFF, State#encoder.utf8 ->
303 | [xmerl_ucs:to_utf8(C) | Acc];
304 | C when C >= 16#7f, C =< 16#10FFFF, not State#encoder.utf8 ->
305 | [unihex(C) | Acc];
306 | C when C < 16#7f ->
307 | [C | Acc];
308 | _ ->
309 | exit({json_encode, {bad_char, C}})
310 | end,
311 | json_encode_string_unicode(Cs, State, Acc1).
312 |
313 | hexdigit(C) when C >= 0, C =< 9 ->
314 | C + $0;
315 | hexdigit(C) when C =< 15 ->
316 | C + $a - 10.
317 |
318 | unihex(C) when C < 16#10000 ->
319 | <> = <>,
320 | Digits = [hexdigit(D) || D <- [D3, D2, D1, D0]],
321 | [$\\, $u | Digits];
322 | unihex(C) when C =< 16#10FFFF ->
323 | N = C - 16#10000,
324 | S1 = 16#d800 bor ((N bsr 10) band 16#3ff),
325 | S2 = 16#dc00 bor (N band 16#3ff),
326 | [unihex(S1), unihex(S2)].
327 |
328 | json_decode(L, S) when is_list(L) ->
329 | json_decode(iolist_to_binary(L), S);
330 | json_decode(B, S) ->
331 | {Res, S1} = decode1(B, S),
332 | {eof, _} = tokenize(B, S1#decoder{state=trim}),
333 | Res.
334 |
335 | decode1(B, S=#decoder{state=null}) ->
336 | case tokenize(B, S#decoder{state=any}) of
337 | {{const, C}, S1} ->
338 | {C, S1};
339 | {start_array, S1} ->
340 | decode_array(B, S1);
341 | {start_object, S1} ->
342 | decode_object(B, S1)
343 | end.
344 |
345 | make_object(V, #decoder{object_hook=N}) when N =:= null orelse N =:= struct ->
346 | V;
347 | make_object({struct, P}, #decoder{object_hook=eep18}) ->
348 | {P};
349 | make_object({struct, P}, #decoder{object_hook=proplist}) ->
350 | P;
351 | make_object(V, #decoder{object_hook=Hook}) ->
352 | Hook(V).
353 |
354 | decode_object(B, S) ->
355 | decode_object(B, S#decoder{state=key}, []).
356 |
357 | decode_object(B, S=#decoder{state=key}, Acc) ->
358 | case tokenize(B, S) of
359 | {end_object, S1} ->
360 | V = make_object({struct, lists:reverse(Acc)}, S1),
361 | {V, S1#decoder{state=null}};
362 | {{const, K}, S1} ->
363 | {colon, S2} = tokenize(B, S1),
364 | {V, S3} = decode1(B, S2#decoder{state=null}),
365 | decode_object(B, S3#decoder{state=comma}, [{K, V} | Acc])
366 | end;
367 | decode_object(B, S=#decoder{state=comma}, Acc) ->
368 | case tokenize(B, S) of
369 | {end_object, S1} ->
370 | V = make_object({struct, lists:reverse(Acc)}, S1),
371 | {V, S1#decoder{state=null}};
372 | {comma, S1} ->
373 | decode_object(B, S1#decoder{state=key}, Acc)
374 | end.
375 |
376 | decode_array(B, S) ->
377 | decode_array(B, S#decoder{state=any}, []).
378 |
379 | decode_array(B, S=#decoder{state=any}, Acc) ->
380 | case tokenize(B, S) of
381 | {end_array, S1} ->
382 | {lists:reverse(Acc), S1#decoder{state=null}};
383 | {start_array, S1} ->
384 | {Array, S2} = decode_array(B, S1),
385 | decode_array(B, S2#decoder{state=comma}, [Array | Acc]);
386 | {start_object, S1} ->
387 | {Array, S2} = decode_object(B, S1),
388 | decode_array(B, S2#decoder{state=comma}, [Array | Acc]);
389 | {{const, Const}, S1} ->
390 | decode_array(B, S1#decoder{state=comma}, [Const | Acc])
391 | end;
392 | decode_array(B, S=#decoder{state=comma}, Acc) ->
393 | case tokenize(B, S) of
394 | {end_array, S1} ->
395 | {lists:reverse(Acc), S1#decoder{state=null}};
396 | {comma, S1} ->
397 | decode_array(B, S1#decoder{state=any}, Acc)
398 | end.
399 |
400 | tokenize_string(B, S=#decoder{offset=O}) ->
401 | case tokenize_string_fast(B, O) of
402 | {escape, O1} ->
403 | Length = O1 - O,
404 | S1 = ?ADV_COL(S, Length),
405 | <<_:O/binary, Head:Length/binary, _/binary>> = B,
406 | tokenize_string(B, S1, lists:reverse(binary_to_list(Head)));
407 | O1 ->
408 | Length = O1 - O,
409 | <<_:O/binary, String:Length/binary, ?Q, _/binary>> = B,
410 | {{const, String}, ?ADV_COL(S, Length + 1)}
411 | end.
412 |
413 | tokenize_string_fast(B, O) ->
414 | case B of
415 | <<_:O/binary, ?Q, _/binary>> ->
416 | O;
417 | <<_:O/binary, $\\, _/binary>> ->
418 | {escape, O};
419 | <<_:O/binary, C1, _/binary>> when C1 < 128 ->
420 | tokenize_string_fast(B, 1 + O);
421 | <<_:O/binary, C1, C2, _/binary>> when C1 >= 194, C1 =< 223,
422 | C2 >= 128, C2 =< 191 ->
423 | tokenize_string_fast(B, 2 + O);
424 | <<_:O/binary, C1, C2, C3, _/binary>> when C1 >= 224, C1 =< 239,
425 | C2 >= 128, C2 =< 191,
426 | C3 >= 128, C3 =< 191 ->
427 | tokenize_string_fast(B, 3 + O);
428 | <<_:O/binary, C1, C2, C3, C4, _/binary>> when C1 >= 240, C1 =< 244,
429 | C2 >= 128, C2 =< 191,
430 | C3 >= 128, C3 =< 191,
431 | C4 >= 128, C4 =< 191 ->
432 | tokenize_string_fast(B, 4 + O);
433 | _ ->
434 | throw(invalid_utf8)
435 | end.
436 |
437 | tokenize_string(B, S=#decoder{offset=O}, Acc) ->
438 | case B of
439 | <<_:O/binary, ?Q, _/binary>> ->
440 | {{const, iolist_to_binary(lists:reverse(Acc))}, ?INC_COL(S)};
441 | <<_:O/binary, "\\\"", _/binary>> ->
442 | tokenize_string(B, ?ADV_COL(S, 2), [$\" | Acc]);
443 | <<_:O/binary, "\\\\", _/binary>> ->
444 | tokenize_string(B, ?ADV_COL(S, 2), [$\\ | Acc]);
445 | <<_:O/binary, "\\/", _/binary>> ->
446 | tokenize_string(B, ?ADV_COL(S, 2), [$/ | Acc]);
447 | <<_:O/binary, "\\b", _/binary>> ->
448 | tokenize_string(B, ?ADV_COL(S, 2), [$\b | Acc]);
449 | <<_:O/binary, "\\f", _/binary>> ->
450 | tokenize_string(B, ?ADV_COL(S, 2), [$\f | Acc]);
451 | <<_:O/binary, "\\n", _/binary>> ->
452 | tokenize_string(B, ?ADV_COL(S, 2), [$\n | Acc]);
453 | <<_:O/binary, "\\r", _/binary>> ->
454 | tokenize_string(B, ?ADV_COL(S, 2), [$\r | Acc]);
455 | <<_:O/binary, "\\t", _/binary>> ->
456 | tokenize_string(B, ?ADV_COL(S, 2), [$\t | Acc]);
457 | <<_:O/binary, "\\u", C3, C2, C1, C0, Rest/binary>> ->
458 | C = erlang:list_to_integer([C3, C2, C1, C0], 16),
459 | if C > 16#D7FF, C < 16#DC00 ->
460 | %% coalesce UTF-16 surrogate pair
461 | <<"\\u", D3, D2, D1, D0, _/binary>> = Rest,
462 | D = erlang:list_to_integer([D3,D2,D1,D0], 16),
463 | [CodePoint] = xmerl_ucs:from_utf16be(<>),
465 | Acc1 = lists:reverse(xmerl_ucs:to_utf8(CodePoint), Acc),
466 | tokenize_string(B, ?ADV_COL(S, 12), Acc1);
467 | true ->
468 | Acc1 = lists:reverse(xmerl_ucs:to_utf8(C), Acc),
469 | tokenize_string(B, ?ADV_COL(S, 6), Acc1)
470 | end;
471 | <<_:O/binary, C1, _/binary>> when C1 < 128 ->
472 | tokenize_string(B, ?INC_CHAR(S, C1), [C1 | Acc]);
473 | <<_:O/binary, C1, C2, _/binary>> when C1 >= 194, C1 =< 223,
474 | C2 >= 128, C2 =< 191 ->
475 | tokenize_string(B, ?ADV_COL(S, 2), [C2, C1 | Acc]);
476 | <<_:O/binary, C1, C2, C3, _/binary>> when C1 >= 224, C1 =< 239,
477 | C2 >= 128, C2 =< 191,
478 | C3 >= 128, C3 =< 191 ->
479 | tokenize_string(B, ?ADV_COL(S, 3), [C3, C2, C1 | Acc]);
480 | <<_:O/binary, C1, C2, C3, C4, _/binary>> when C1 >= 240, C1 =< 244,
481 | C2 >= 128, C2 =< 191,
482 | C3 >= 128, C3 =< 191,
483 | C4 >= 128, C4 =< 191 ->
484 | tokenize_string(B, ?ADV_COL(S, 4), [C4, C3, C2, C1 | Acc]);
485 | _ ->
486 | throw(invalid_utf8)
487 | end.
488 |
489 | tokenize_number(B, S) ->
490 | case tokenize_number(B, sign, S, []) of
491 | {{int, Int}, S1} ->
492 | {{const, list_to_integer(Int)}, S1};
493 | {{float, Float}, S1} ->
494 | {{const, list_to_float(Float)}, S1}
495 | end.
496 |
497 | tokenize_number(B, sign, S=#decoder{offset=O}, []) ->
498 | case B of
499 | <<_:O/binary, $-, _/binary>> ->
500 | tokenize_number(B, int, ?INC_COL(S), [$-]);
501 | _ ->
502 | tokenize_number(B, int, S, [])
503 | end;
504 | tokenize_number(B, int, S=#decoder{offset=O}, Acc) ->
505 | case B of
506 | <<_:O/binary, $0, _/binary>> ->
507 | tokenize_number(B, frac, ?INC_COL(S), [$0 | Acc]);
508 | <<_:O/binary, C, _/binary>> when C >= $1 andalso C =< $9 ->
509 | tokenize_number(B, int1, ?INC_COL(S), [C | Acc])
510 | end;
511 | tokenize_number(B, int1, S=#decoder{offset=O}, Acc) ->
512 | case B of
513 | <<_:O/binary, C, _/binary>> when C >= $0 andalso C =< $9 ->
514 | tokenize_number(B, int1, ?INC_COL(S), [C | Acc]);
515 | _ ->
516 | tokenize_number(B, frac, S, Acc)
517 | end;
518 | tokenize_number(B, frac, S=#decoder{offset=O}, Acc) ->
519 | case B of
520 | <<_:O/binary, $., C, _/binary>> when C >= $0, C =< $9 ->
521 | tokenize_number(B, frac1, ?ADV_COL(S, 2), [C, $. | Acc]);
522 | <<_:O/binary, E, _/binary>> when E =:= $e orelse E =:= $E ->
523 | tokenize_number(B, esign, ?INC_COL(S), [$e, $0, $. | Acc]);
524 | _ ->
525 | {{int, lists:reverse(Acc)}, S}
526 | end;
527 | tokenize_number(B, frac1, S=#decoder{offset=O}, Acc) ->
528 | case B of
529 | <<_:O/binary, C, _/binary>> when C >= $0 andalso C =< $9 ->
530 | tokenize_number(B, frac1, ?INC_COL(S), [C | Acc]);
531 | <<_:O/binary, E, _/binary>> when E =:= $e orelse E =:= $E ->
532 | tokenize_number(B, esign, ?INC_COL(S), [$e | Acc]);
533 | _ ->
534 | {{float, lists:reverse(Acc)}, S}
535 | end;
536 | tokenize_number(B, esign, S=#decoder{offset=O}, Acc) ->
537 | case B of
538 | <<_:O/binary, C, _/binary>> when C =:= $- orelse C=:= $+ ->
539 | tokenize_number(B, eint, ?INC_COL(S), [C | Acc]);
540 | _ ->
541 | tokenize_number(B, eint, S, Acc)
542 | end;
543 | tokenize_number(B, eint, S=#decoder{offset=O}, Acc) ->
544 | case B of
545 | <<_:O/binary, C, _/binary>> when C >= $0 andalso C =< $9 ->
546 | tokenize_number(B, eint1, ?INC_COL(S), [C | Acc])
547 | end;
548 | tokenize_number(B, eint1, S=#decoder{offset=O}, Acc) ->
549 | case B of
550 | <<_:O/binary, C, _/binary>> when C >= $0 andalso C =< $9 ->
551 | tokenize_number(B, eint1, ?INC_COL(S), [C | Acc]);
552 | _ ->
553 | {{float, lists:reverse(Acc)}, S}
554 | end.
555 |
556 | tokenize(B, S=#decoder{offset=O}) ->
557 | case B of
558 | <<_:O/binary, C, _/binary>> when ?IS_WHITESPACE(C) ->
559 | tokenize(B, ?INC_CHAR(S, C));
560 | <<_:O/binary, "{", _/binary>> ->
561 | {start_object, ?INC_COL(S)};
562 | <<_:O/binary, "}", _/binary>> ->
563 | {end_object, ?INC_COL(S)};
564 | <<_:O/binary, "[", _/binary>> ->
565 | {start_array, ?INC_COL(S)};
566 | <<_:O/binary, "]", _/binary>> ->
567 | {end_array, ?INC_COL(S)};
568 | <<_:O/binary, ",", _/binary>> ->
569 | {comma, ?INC_COL(S)};
570 | <<_:O/binary, ":", _/binary>> ->
571 | {colon, ?INC_COL(S)};
572 | <<_:O/binary, "null", _/binary>> ->
573 | {{const, null}, ?ADV_COL(S, 4)};
574 | <<_:O/binary, "true", _/binary>> ->
575 | {{const, true}, ?ADV_COL(S, 4)};
576 | <<_:O/binary, "false", _/binary>> ->
577 | {{const, false}, ?ADV_COL(S, 5)};
578 | <<_:O/binary, "\"", _/binary>> ->
579 | tokenize_string(B, ?INC_COL(S));
580 | <<_:O/binary, C, _/binary>> when (C >= $0 andalso C =< $9)
581 | orelse C =:= $- ->
582 | tokenize_number(B, S);
583 | <<_:O/binary>> ->
584 | trim = S#decoder.state,
585 | {eof, S}
586 | end.
587 | %%
588 | %% Tests
589 | %%
590 | -ifdef(TEST).
591 | -include_lib("eunit/include/eunit.hrl").
592 |
593 |
594 | %% testing constructs borrowed from the Yaws JSON implementation.
595 |
596 | %% Create an object from a list of Key/Value pairs.
597 |
598 | obj_new() ->
599 | {struct, []}.
600 |
601 | is_obj({struct, Props}) ->
602 | F = fun ({K, _}) when is_binary(K) -> true end,
603 | lists:all(F, Props).
604 |
605 | obj_from_list(Props) ->
606 | Obj = {struct, Props},
607 | ?assert(is_obj(Obj)),
608 | Obj.
609 |
610 | %% Test for equivalence of Erlang terms.
611 | %% Due to arbitrary order of construction, equivalent objects might
612 | %% compare unequal as erlang terms, so we need to carefully recurse
613 | %% through aggregates (tuples and objects).
614 |
615 | equiv({struct, Props1}, {struct, Props2}) ->
616 | equiv_object(Props1, Props2);
617 | equiv(L1, L2) when is_list(L1), is_list(L2) ->
618 | equiv_list(L1, L2);
619 | equiv(N1, N2) when is_number(N1), is_number(N2) -> N1 == N2;
620 | equiv(B1, B2) when is_binary(B1), is_binary(B2) -> B1 == B2;
621 | equiv(A, A) when A =:= true orelse A =:= false orelse A =:= null -> true.
622 |
623 | %% Object representation and traversal order is unknown.
624 | %% Use the sledgehammer and sort property lists.
625 |
626 | equiv_object(Props1, Props2) ->
627 | L1 = lists:keysort(1, Props1),
628 | L2 = lists:keysort(1, Props2),
629 | Pairs = lists:zip(L1, L2),
630 | true = lists:all(fun({{K1, V1}, {K2, V2}}) ->
631 | equiv(K1, K2) and equiv(V1, V2)
632 | end, Pairs).
633 |
634 | %% Recursively compare tuple elements for equivalence.
635 |
636 | equiv_list([], []) ->
637 | true;
638 | equiv_list([V1 | L1], [V2 | L2]) ->
639 | equiv(V1, V2) andalso equiv_list(L1, L2).
640 |
641 | decode_test() ->
642 | [1199344435545.0, 1] = decode(<<"[1199344435545.0,1]">>),
643 | <<16#F0,16#9D,16#9C,16#95>> = decode([34,"\\ud835","\\udf15",34]).
644 |
645 | e2j_vec_test() ->
646 | test_one(e2j_test_vec(utf8), 1).
647 |
648 | test_one([], _N) ->
649 | %% io:format("~p tests passed~n", [N-1]),
650 | ok;
651 | test_one([{E, J} | Rest], N) ->
652 | %% io:format("[~p] ~p ~p~n", [N, E, J]),
653 | true = equiv(E, decode(J)),
654 | true = equiv(E, decode(encode(E))),
655 | test_one(Rest, 1+N).
656 |
657 | e2j_test_vec(utf8) ->
658 | [
659 | {1, "1"},
660 | {3.1416, "3.14160"}, %% text representation may truncate, trail zeroes
661 | {-1, "-1"},
662 | {-3.1416, "-3.14160"},
663 | {12.0e10, "1.20000e+11"},
664 | {1.234E+10, "1.23400e+10"},
665 | {-1.234E-10, "-1.23400e-10"},
666 | {10.0, "1.0e+01"},
667 | {123.456, "1.23456E+2"},
668 | {10.0, "1e1"},
669 | {<<"foo">>, "\"foo\""},
670 | {<<"foo", 5, "bar">>, "\"foo\\u0005bar\""},
671 | {<<"">>, "\"\""},
672 | {<<"\n\n\n">>, "\"\\n\\n\\n\""},
673 | {<<"\" \b\f\r\n\t\"">>, "\"\\\" \\b\\f\\r\\n\\t\\\"\""},
674 | {obj_new(), "{}"},
675 | {obj_from_list([{<<"foo">>, <<"bar">>}]), "{\"foo\":\"bar\"}"},
676 | {obj_from_list([{<<"foo">>, <<"bar">>}, {<<"baz">>, 123}]),
677 | "{\"foo\":\"bar\",\"baz\":123}"},
678 | {[], "[]"},
679 | {[[]], "[[]]"},
680 | {[1, <<"foo">>], "[1,\"foo\"]"},
681 |
682 | %% json array in a json object
683 | {obj_from_list([{<<"foo">>, [123]}]),
684 | "{\"foo\":[123]}"},
685 |
686 | %% json object in a json object
687 | {obj_from_list([{<<"foo">>, obj_from_list([{<<"bar">>, true}])}]),
688 | "{\"foo\":{\"bar\":true}}"},
689 |
690 | %% fold evaluation order
691 | {obj_from_list([{<<"foo">>, []},
692 | {<<"bar">>, obj_from_list([{<<"baz">>, true}])},
693 | {<<"alice">>, <<"bob">>}]),
694 | "{\"foo\":[],\"bar\":{\"baz\":true},\"alice\":\"bob\"}"},
695 |
696 | %% json object in a json array
697 | {[-123, <<"foo">>, obj_from_list([{<<"bar">>, []}]), null],
698 | "[-123,\"foo\",{\"bar\":[]},null]"}
699 | ].
700 |
701 | %% test utf8 encoding
702 | encoder_utf8_test() ->
703 | %% safe conversion case (default)
704 | [34,"\\u0001","\\u0442","\\u0435","\\u0441","\\u0442",34] =
705 | encode(<<1,"\321\202\320\265\321\201\321\202">>),
706 |
707 | %% raw utf8 output (optional)
708 | Enc = mochijson2:encoder([{utf8, true}]),
709 | [34,"\\u0001",[209,130],[208,181],[209,129],[209,130],34] =
710 | Enc(<<1,"\321\202\320\265\321\201\321\202">>).
711 |
712 | input_validation_test() ->
713 | Good = [
714 | {16#00A3, <>}, %% pound
715 | {16#20AC, <>}, %% euro
716 | {16#10196, <>} %% denarius
717 | ],
718 | lists:foreach(fun({CodePoint, UTF8}) ->
719 | Expect = list_to_binary(xmerl_ucs:to_utf8(CodePoint)),
720 | Expect = decode(UTF8)
721 | end, Good),
722 |
723 | Bad = [
724 | %% 2nd, 3rd, or 4th byte of a multi-byte sequence w/o leading byte
725 | <>,
726 | %% missing continuations, last byte in each should be 80-BF
727 | <>,
728 | <>,
729 | <>,
730 | %% we don't support code points > 10FFFF per RFC 3629
731 | <>,
732 | %% escape characters trigger a different code path
733 | <>
734 | ],
735 | lists:foreach(
736 | fun(X) ->
737 | ok = try decode(X) catch invalid_utf8 -> ok end,
738 | %% could be {ucs,{bad_utf8_character_code}} or
739 | %% {json_encode,{bad_char,_}}
740 | {'EXIT', _} = (catch encode(X))
741 | end, Bad).
742 |
743 | inline_json_test() ->
744 | ?assertEqual(<<"\"iodata iodata\"">>,
745 | iolist_to_binary(
746 | encode({json, [<<"\"iodata">>, " iodata\""]}))),
747 | ?assertEqual({struct, [{<<"key">>, <<"iodata iodata">>}]},
748 | decode(
749 | encode({struct,
750 | [{key, {json, [<<"\"iodata">>, " iodata\""]}}]}))),
751 | ok.
752 |
753 | big_unicode_test() ->
754 | UTF8Seq = list_to_binary(xmerl_ucs:to_utf8(16#0001d120)),
755 | ?assertEqual(
756 | <<"\"\\ud834\\udd20\"">>,
757 | iolist_to_binary(encode(UTF8Seq))),
758 | ?assertEqual(
759 | UTF8Seq,
760 | decode(iolist_to_binary(encode(UTF8Seq)))),
761 | ok.
762 |
763 | custom_decoder_test() ->
764 | ?assertEqual(
765 | {struct, [{<<"key">>, <<"value">>}]},
766 | (decoder([]))("{\"key\": \"value\"}")),
767 | F = fun ({struct, [{<<"key">>, <<"value">>}]}) -> win end,
768 | ?assertEqual(
769 | win,
770 | (decoder([{object_hook, F}]))("{\"key\": \"value\"}")),
771 | ok.
772 |
773 | atom_test() ->
774 | %% JSON native atoms
775 | [begin
776 | ?assertEqual(A, decode(atom_to_list(A))),
777 | ?assertEqual(iolist_to_binary(atom_to_list(A)),
778 | iolist_to_binary(encode(A)))
779 | end || A <- [true, false, null]],
780 | %% Atom to string
781 | ?assertEqual(
782 | <<"\"foo\"">>,
783 | iolist_to_binary(encode(foo))),
784 | ?assertEqual(
785 | <<"\"\\ud834\\udd20\"">>,
786 | iolist_to_binary(encode(list_to_atom(xmerl_ucs:to_utf8(16#0001d120))))),
787 | ok.
788 |
789 | key_encode_test() ->
790 | %% Some forms are accepted as keys that would not be strings in other
791 | %% cases
792 | ?assertEqual(
793 | <<"{\"foo\":1}">>,
794 | iolist_to_binary(encode({struct, [{foo, 1}]}))),
795 | ?assertEqual(
796 | <<"{\"foo\":1}">>,
797 | iolist_to_binary(encode({struct, [{<<"foo">>, 1}]}))),
798 | ?assertEqual(
799 | <<"{\"foo\":1}">>,
800 | iolist_to_binary(encode({struct, [{"foo", 1}]}))),
801 | ?assertEqual(
802 | <<"{\"foo\":1}">>,
803 | iolist_to_binary(encode([{foo, 1}]))),
804 | ?assertEqual(
805 | <<"{\"foo\":1}">>,
806 | iolist_to_binary(encode([{<<"foo">>, 1}]))),
807 | ?assertEqual(
808 | <<"{\"foo\":1}">>,
809 | iolist_to_binary(encode([{"foo", 1}]))),
810 | ?assertEqual(
811 | <<"{\"\\ud834\\udd20\":1}">>,
812 | iolist_to_binary(
813 | encode({struct, [{[16#0001d120], 1}]}))),
814 | ?assertEqual(
815 | <<"{\"1\":1}">>,
816 | iolist_to_binary(encode({struct, [{1, 1}]}))),
817 | ok.
818 |
819 | unsafe_chars_test() ->
820 | Chars = "\"\\\b\f\n\r\t",
821 | [begin
822 | ?assertEqual(false, json_string_is_safe([C])),
823 | ?assertEqual(false, json_bin_is_safe(<>)),
824 | ?assertEqual(<>, decode(encode(<>)))
825 | end || C <- Chars],
826 | ?assertEqual(
827 | false,
828 | json_string_is_safe([16#0001d120])),
829 | ?assertEqual(
830 | false,
831 | json_bin_is_safe(list_to_binary(xmerl_ucs:to_utf8(16#0001d120)))),
832 | ?assertEqual(
833 | [16#0001d120],
834 | xmerl_ucs:from_utf8(
835 | binary_to_list(
836 | decode(encode(list_to_atom(xmerl_ucs:to_utf8(16#0001d120))))))),
837 | ?assertEqual(
838 | false,
839 | json_string_is_safe([16#110000])),
840 | ?assertEqual(
841 | false,
842 | json_bin_is_safe(list_to_binary(xmerl_ucs:to_utf8([16#110000])))),
843 | %% solidus can be escaped but isn't unsafe by default
844 | ?assertEqual(
845 | <<"/">>,
846 | decode(<<"\"\\/\"">>)),
847 | ok.
848 |
849 | int_test() ->
850 | ?assertEqual(0, decode("0")),
851 | ?assertEqual(1, decode("1")),
852 | ?assertEqual(11, decode("11")),
853 | ok.
854 |
855 | large_int_test() ->
856 | ?assertEqual(<<"-2147483649214748364921474836492147483649">>,
857 | iolist_to_binary(encode(-2147483649214748364921474836492147483649))),
858 | ?assertEqual(<<"2147483649214748364921474836492147483649">>,
859 | iolist_to_binary(encode(2147483649214748364921474836492147483649))),
860 | ok.
861 |
862 | float_test() ->
863 | ?assertEqual(<<"-2147483649.0">>, iolist_to_binary(encode(-2147483649.0))),
864 | ?assertEqual(<<"2147483648.0">>, iolist_to_binary(encode(2147483648.0))),
865 | ok.
866 |
867 | handler_test() ->
868 | ?assertEqual(
869 | {'EXIT',{json_encode,{bad_term,{x,y}}}},
870 | catch encode({x,y})),
871 | F = fun ({x,y}) -> [] end,
872 | ?assertEqual(
873 | <<"[]">>,
874 | iolist_to_binary((encoder([{handler, F}]))({x, y}))),
875 | ok.
876 |
877 | encode_empty_test_() ->
878 | [{A, ?_assertEqual(<<"{}">>, iolist_to_binary(encode(B)))}
879 | || {A, B} <- [{"eep18 {}", {}},
880 | {"eep18 {[]}", {[]}},
881 | {"{struct, []}", {struct, []}}]].
882 |
883 | encode_test_() ->
884 | P = [{<<"k">>, <<"v">>}],
885 | JSON = iolist_to_binary(encode({struct, P})),
886 | [{atom_to_list(F),
887 | ?_assertEqual(JSON, iolist_to_binary(encode(decode(JSON, [{format, F}]))))}
888 | || F <- [struct, eep18, proplist]].
889 |
890 | format_test_() ->
891 | P = [{<<"k">>, <<"v">>}],
892 | JSON = iolist_to_binary(encode({struct, P})),
893 | [{atom_to_list(F),
894 | ?_assertEqual(A, decode(JSON, [{format, F}]))}
895 | || {F, A} <- [{struct, {struct, P}},
896 | {eep18, {P}},
897 | {proplist, P}]].
898 |
899 | -endif.
900 |
--------------------------------------------------------------------------------
/priv/jquery-1.7.1.min.js:
--------------------------------------------------------------------------------
1 | /*! jQuery v1.7.1 jquery.com | jquery.org/license */
2 | (function(a,b){function cy(a){return f.isWindow(a)?a:a.nodeType===9?a.defaultView||a.parentWindow:!1}function cv(a){if(!ck[a]){var b=c.body,d=f("<"+a+">").appendTo(b),e=d.css("display");d.remove();if(e==="none"||e===""){cl||(cl=c.createElement("iframe"),cl.frameBorder=cl.width=cl.height=0),b.appendChild(cl);if(!cm||!cl.createElement)cm=(cl.contentWindow||cl.contentDocument).document,cm.write((c.compatMode==="CSS1Compat"?"":"")+""),cm.close();d=cm.createElement(a),cm.body.appendChild(d),e=f.css(d,"display"),b.removeChild(cl)}ck[a]=e}return ck[a]}function cu(a,b){var c={};f.each(cq.concat.apply([],cq.slice(0,b)),function(){c[this]=a});return c}function ct(){cr=b}function cs(){setTimeout(ct,0);return cr=f.now()}function cj(){try{return new a.ActiveXObject("Microsoft.XMLHTTP")}catch(b){}}function ci(){try{return new a.XMLHttpRequest}catch(b){}}function cc(a,c){a.dataFilter&&(c=a.dataFilter(c,a.dataType));var d=a.dataTypes,e={},g,h,i=d.length,j,k=d[0],l,m,n,o,p;for(g=1;g0){if(c!=="border")for(;g=0===c})}function S(a){return!a||!a.parentNode||a.parentNode.nodeType===11}function K(){return!0}function J(){return!1}function n(a,b,c){var d=b+"defer",e=b+"queue",g=b+"mark",h=f._data(a,d);h&&(c==="queue"||!f._data(a,e))&&(c==="mark"||!f._data(a,g))&&setTimeout(function(){!f._data(a,e)&&!f._data(a,g)&&(f.removeData(a,d,!0),h.fire())},0)}function m(a){for(var b in a){if(b==="data"&&f.isEmptyObject(a[b]))continue;if(b!=="toJSON")return!1}return!0}function l(a,c,d){if(d===b&&a.nodeType===1){var e="data-"+c.replace(k,"-$1").toLowerCase();d=a.getAttribute(e);if(typeof d=="string"){try{d=d==="true"?!0:d==="false"?!1:d==="null"?null:f.isNumeric(d)?parseFloat(d):j.test(d)?f.parseJSON(d):d}catch(g){}f.data(a,c,d)}else d=b}return d}function h(a){var b=g[a]={},c,d;a=a.split(/\s+/);for(c=0,d=a.length;c)[^>]*$|#([\w\-]*)$)/,j=/\S/,k=/^\s+/,l=/\s+$/,m=/^<(\w+)\s*\/?>(?:<\/\1>)?$/,n=/^[\],:{}\s]*$/,o=/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,p=/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,q=/(?:^|:|,)(?:\s*\[)+/g,r=/(webkit)[ \/]([\w.]+)/,s=/(opera)(?:.*version)?[ \/]([\w.]+)/,t=/(msie) ([\w.]+)/,u=/(mozilla)(?:.*? rv:([\w.]+))?/,v=/-([a-z]|[0-9])/ig,w=/^-ms-/,x=function(a,b){return(b+"").toUpperCase()},y=d.userAgent,z,A,B,C=Object.prototype.toString,D=Object.prototype.hasOwnProperty,E=Array.prototype.push,F=Array.prototype.slice,G=String.prototype.trim,H=Array.prototype.indexOf,I={};e.fn=e.prototype={constructor:e,init:function(a,d,f){var g,h,j,k;if(!a)return this;if(a.nodeType){this.context=this[0]=a,this.length=1;return this}if(a==="body"&&!d&&c.body){this.context=c,this[0]=c.body,this.selector=a,this.length=1;return this}if(typeof a=="string"){a.charAt(0)!=="<"||a.charAt(a.length-1)!==">"||a.length<3?g=i.exec(a):g=[null,a,null];if(g&&(g[1]||!d)){if(g[1]){d=d instanceof e?d[0]:d,k=d?d.ownerDocument||d:c,j=m.exec(a),j?e.isPlainObject(d)?(a=[c.createElement(j[1])],e.fn.attr.call(a,d,!0)):a=[k.createElement(j[1])]:(j=e.buildFragment([g[1]],[k]),a=(j.cacheable?e.clone(j.fragment):j.fragment).childNodes);return e.merge(this,a)}h=c.getElementById(g[2]);if(h&&h.parentNode){if(h.id!==g[2])return f.find(a);this.length=1,this[0]=h}this.context=c,this.selector=a;return this}return!d||d.jquery?(d||f).find(a):this.constructor(d).find(a)}if(e.isFunction(a))return f.ready(a);a.selector!==b&&(this.selector=a.selector,this.context=a.context);return e.makeArray(a,this)},selector:"",jquery:"1.7.1",length:0,size:function(){return this.length},toArray:function(){return F.call(this,0)},get:function(a){return a==null?this.toArray():a<0?this[this.length+a]:this[a]},pushStack:function(a,b,c){var d=this.constructor();e.isArray(a)?E.apply(d,a):e.merge(d,a),d.prevObject=this,d.context=this.context,b==="find"?d.selector=this.selector+(this.selector?" ":"")+c:b&&(d.selector=this.selector+"."+b+"("+c+")");return d},each:function(a,b){return e.each(this,a,b)},ready:function(a){e.bindReady(),A.add(a);return this},eq:function(a){a=+a;return a===-1?this.slice(a):this.slice(a,a+1)},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},slice:function(){return this.pushStack(F.apply(this,arguments),"slice",F.call(arguments).join(","))},map:function(a){return this.pushStack(e.map(this,function(b,c){return a.call(b,c,b)}))},end:function(){return this.prevObject||this.constructor(null)},push:E,sort:[].sort,splice:[].splice},e.fn.init.prototype=e.fn,e.extend=e.fn.extend=function(){var a,c,d,f,g,h,i=arguments[0]||{},j=1,k=arguments.length,l=!1;typeof i=="boolean"&&(l=i,i=arguments[1]||{},j=2),typeof i!="object"&&!e.isFunction(i)&&(i={}),k===j&&(i=this,--j);for(;j0)return;A.fireWith(c,[e]),e.fn.trigger&&e(c).trigger("ready").off("ready")}},bindReady:function(){if(!A){A=e.Callbacks("once memory");if(c.readyState==="complete")return setTimeout(e.ready,1);if(c.addEventListener)c.addEventListener("DOMContentLoaded",B,!1),a.addEventListener("load",e.ready,!1);else if(c.attachEvent){c.attachEvent("onreadystatechange",B),a.attachEvent("onload",e.ready);var b=!1;try{b=a.frameElement==null}catch(d){}c.documentElement.doScroll&&b&&J()}}},isFunction:function(a){return e.type(a)==="function"},isArray:Array.isArray||function(a){return e.type(a)==="array"},isWindow:function(a){return a&&typeof a=="object"&&"setInterval"in a},isNumeric:function(a){return!isNaN(parseFloat(a))&&isFinite(a)},type:function(a){return a==null?String(a):I[C.call(a)]||"object"},isPlainObject:function(a){if(!a||e.type(a)!=="object"||a.nodeType||e.isWindow(a))return!1;try{if(a.constructor&&!D.call(a,"constructor")&&!D.call(a.constructor.prototype,"isPrototypeOf"))return!1}catch(c){return!1}var d;for(d in a);return d===b||D.call(a,d)},isEmptyObject:function(a){for(var b in a)return!1;return!0},error:function(a){throw new Error(a)},parseJSON:function(b){if(typeof b!="string"||!b)return null;b=e.trim(b);if(a.JSON&&a.JSON.parse)return a.JSON.parse(b);if(n.test(b.replace(o,"@").replace(p,"]").replace(q,"")))return(new Function("return "+b))();e.error("Invalid JSON: "+b)},parseXML:function(c){var d,f;try{a.DOMParser?(f=new DOMParser,d=f.parseFromString(c,"text/xml")):(d=new ActiveXObject("Microsoft.XMLDOM"),d.async="false",d.loadXML(c))}catch(g){d=b}(!d||!d.documentElement||d.getElementsByTagName("parsererror").length)&&e.error("Invalid XML: "+c);return d},noop:function(){},globalEval:function(b){b&&j.test(b)&&(a.execScript||function(b){a.eval.call(a,b)})(b)},camelCase:function(a){return a.replace(w,"ms-").replace(v,x)},nodeName:function(a,b){return a.nodeName&&a.nodeName.toUpperCase()===b.toUpperCase()},each:function(a,c,d){var f,g=0,h=a.length,i=h===b||e.isFunction(a);if(d){if(i){for(f in a)if(c.apply(a[f],d)===!1)break}else for(;g0&&a[0]&&a[j-1]||j===0||e.isArray(a));if(k)for(;i1?i.call(arguments,0):b,j.notifyWith(k,e)}}function l(a){return function(c){b[a]=arguments.length>1?i.call(arguments,0):c,--g||j.resolveWith(j,b)}}var b=i.call(arguments,0),c=0,d=b.length,e=Array(d),g=d,h=d,j=d<=1&&a&&f.isFunction(a.promise)?a:f.Deferred(),k=j.promise();if(d>1){for(;ca ",d=q.getElementsByTagName("*"),e=q.getElementsByTagName("a")[0];if(!d||!d.length||!e)return{};g=c.createElement("select"),h=g.appendChild(c.createElement("option")),i=q.getElementsByTagName("input")[0],b={leadingWhitespace:q.firstChild.nodeType===3,tbody:!q.getElementsByTagName("tbody").length,htmlSerialize:!!q.getElementsByTagName("link").length,style:/top/.test(e.getAttribute("style")),hrefNormalized:e.getAttribute("href")==="/a",opacity:/^0.55/.test(e.style.opacity),cssFloat:!!e.style.cssFloat,checkOn:i.value==="on",optSelected:h.selected,getSetAttribute:q.className!=="t",enctype:!!c.createElement("form").enctype,html5Clone:c.createElement("nav").cloneNode(!0).outerHTML!=="<:nav>",submitBubbles:!0,changeBubbles:!0,focusinBubbles:!1,deleteExpando:!0,noCloneEvent:!0,inlineBlockNeedsLayout:!1,shrinkWrapBlocks:!1,reliableMarginRight:!0},i.checked=!0,b.noCloneChecked=i.cloneNode(!0).checked,g.disabled=!0,b.optDisabled=!h.disabled;try{delete q.test}catch(s){b.deleteExpando=!1}!q.addEventListener&&q.attachEvent&&q.fireEvent&&(q.attachEvent("onclick",function(){b.noCloneEvent=!1}),q.cloneNode(!0).fireEvent("onclick")),i=c.createElement("input"),i.value="t",i.setAttribute("type","radio"),b.radioValue=i.value==="t",i.setAttribute("checked","checked"),q.appendChild(i),k=c.createDocumentFragment(),k.appendChild(q.lastChild),b.checkClone=k.cloneNode(!0).cloneNode(!0).lastChild.checked,b.appendChecked=i.checked,k.removeChild(i),k.appendChild(q),q.innerHTML="",a.getComputedStyle&&(j=c.createElement("div"),j.style.width="0",j.style.marginRight="0",q.style.width="2px",q.appendChild(j),b.reliableMarginRight=(parseInt((a.getComputedStyle(j,null)||{marginRight:0}).marginRight,10)||0)===0);if(q.attachEvent)for(o in{submit:1,change:1,focusin:1})n="on"+o,p=n in q,p||(q.setAttribute(n,"return;"),p=typeof q[n]=="function"),b[o+"Bubbles"]=p;k.removeChild(q),k=g=h=j=q=i=null,f(function(){var a,d,e,g,h,i,j,k,m,n,o,r=c.getElementsByTagName("body")[0];!r||(j=1,k="position:absolute;top:0;left:0;width:1px;height:1px;margin:0;",m="visibility:hidden;border:0;",n="style='"+k+"border:5px solid #000;padding:0;'",o=""+"",a=c.createElement("div"),a.style.cssText=m+"width:0;height:0;position:static;top:0;margin-top:"+j+"px",r.insertBefore(a,r.firstChild),q=c.createElement("div"),a.appendChild(q),q.innerHTML="",l=q.getElementsByTagName("td"),p=l[0].offsetHeight===0,l[0].style.display="",l[1].style.display="none",b.reliableHiddenOffsets=p&&l[0].offsetHeight===0,q.innerHTML="",q.style.width=q.style.paddingLeft="1px",f.boxModel=b.boxModel=q.offsetWidth===2,typeof q.style.zoom!="undefined"&&(q.style.display="inline",q.style.zoom=1,b.inlineBlockNeedsLayout=q.offsetWidth===2,q.style.display="",q.innerHTML="
",b.shrinkWrapBlocks=q.offsetWidth!==2),q.style.cssText=k+m,q.innerHTML=o,d=q.firstChild,e=d.firstChild,h=d.nextSibling.firstChild.firstChild,i={doesNotAddBorder:e.offsetTop!==5,doesAddBorderForTableAndCells:h.offsetTop===5},e.style.position="fixed",e.style.top="20px",i.fixedPosition=e.offsetTop===20||e.offsetTop===15,e.style.position=e.style.top="",d.style.overflow="hidden",d.style.position="relative",i.subtractsBorderForOverflowNotVisible=e.offsetTop===-5,i.doesNotIncludeMarginInBodyOffset=r.offsetTop!==j,r.removeChild(a),q=a=null,f.extend(b,i))});return b}();var j=/^(?:\{.*\}|\[.*\])$/,k=/([A-Z])/g;f.extend({cache:{},uuid:0,expando:"jQuery"+(f.fn.jquery+Math.random()).replace(/\D/g,""),noData:{embed:!0,object:"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",applet:!0},hasData:function(a){a=a.nodeType?f.cache[a[f.expando]]:a[f.expando];return!!a&&!m(a)},data:function(a,c,d,e){if(!!f.acceptData(a)){var g,h,i,j=f.expando,k=typeof c=="string",l=a.nodeType,m=l?f.cache:a,n=l?a[j]:a[j]&&j,o=c==="events";if((!n||!m[n]||!o&&!e&&!m[n].data)&&k&&d===b)return;n||(l?a[j]=n=++f.uuid:n=j),m[n]||(m[n]={},l||(m[n].toJSON=f.noop));if(typeof c=="object"||typeof c=="function")e?m[n]=f.extend(m[n],c):m[n].data=f.extend(m[n].data,c);g=h=m[n],e||(h.data||(h.data={}),h=h.data),d!==b&&(h[f.camelCase(c)]=d);if(o&&!h[c])return g.events;k?(i=h[c],i==null&&(i=h[f.camelCase(c)])):i=h;return i}},removeData:function(a,b,c){if(!!f.acceptData(a)){var d,e,g,h=f.expando,i=a.nodeType,j=i?f.cache:a,k=i?a[h]:h;if(!j[k])return;if(b){d=c?j[k]:j[k].data;if(d){f.isArray(b)||(b in d?b=[b]:(b=f.camelCase(b),b in d?b=[b]:b=b.split(" ")));for(e=0,g=b.length;e-1)return!0;return!1},val:function(a){var c,d,e,g=this[0];{if(!!arguments.length){e=f.isFunction(a);return this.each(function(d){var g=f(this),h;if(this.nodeType===1){e?h=a.call(this,d,g.val()):h=a,h==null?h="":typeof h=="number"?h+="":f.isArray(h)&&(h=f.map(h,function(a){return a==null?"":a+""})),c=f.valHooks[this.nodeName.toLowerCase()]||f.valHooks[this.type];if(!c||!("set"in c)||c.set(this,h,"value")===b)this.value=h}})}if(g){c=f.valHooks[g.nodeName.toLowerCase()]||f.valHooks[g.type];if(c&&"get"in c&&(d=c.get(g,"value"))!==b)return d;d=g.value;return typeof d=="string"?d.replace(q,""):d==null?"":d}}}}),f.extend({valHooks:{option:{get:function(a){var b=a.attributes.value;return!b||b.specified?a.value:a.text}},select:{get:function(a){var b,c,d,e,g=a.selectedIndex,h=[],i=a.options,j=a.type==="select-one";if(g<0)return null;c=j?g:0,d=j?g+1:i.length;for(;c=0}),c.length||(a.selectedIndex=-1);return c}}},attrFn:{val:!0,css:!0,html:!0,text:!0,data:!0,width:!0,height:!0,offset:!0},attr:function(a,c,d,e){var g,h,i,j=a.nodeType;if(!!a&&j!==3&&j!==8&&j!==2){if(e&&c in f.attrFn)return f(a)[c](d);if(typeof a.getAttribute=="undefined")return f.prop(a,c,d);i=j!==1||!f.isXMLDoc(a),i&&(c=c.toLowerCase(),h=f.attrHooks[c]||(u.test(c)?x:w));if(d!==b){if(d===null){f.removeAttr(a,c);return}if(h&&"set"in h&&i&&(g=h.set(a,d,c))!==b)return g;a.setAttribute(c,""+d);return d}if(h&&"get"in h&&i&&(g=h.get(a,c))!==null)return g;g=a.getAttribute(c);return g===null?b:g}},removeAttr:function(a,b){var c,d,e,g,h=0;if(b&&a.nodeType===1){d=b.toLowerCase().split(p),g=d.length;for(;h=0}})});var z=/^(?:textarea|input|select)$/i,A=/^([^\.]*)?(?:\.(.+))?$/,B=/\bhover(\.\S+)?\b/,C=/^key/,D=/^(?:mouse|contextmenu)|click/,E=/^(?:focusinfocus|focusoutblur)$/,F=/^(\w*)(?:#([\w\-]+))?(?:\.([\w\-]+))?$/,G=function(a){var b=F.exec(a);b&&(b[1]=(b[1]||"").toLowerCase(),b[3]=b[3]&&new RegExp("(?:^|\\s)"+b[3]+"(?:\\s|$)"));return b},H=function(a,b){var c=a.attributes||{};return(!b[1]||a.nodeName.toLowerCase()===b[1])&&(!b[2]||(c.id||{}).value===b[2])&&(!b[3]||b[3].test((c["class"]||{}).value))},I=function(a){return f.event.special.hover?a:a.replace(B,"mouseenter$1 mouseleave$1")};
3 | f.event={add:function(a,c,d,e,g){var h,i,j,k,l,m,n,o,p,q,r,s;if(!(a.nodeType===3||a.nodeType===8||!c||!d||!(h=f._data(a)))){d.handler&&(p=d,d=p.handler),d.guid||(d.guid=f.guid++),j=h.events,j||(h.events=j={}),i=h.handle,i||(h.handle=i=function(a){return typeof f!="undefined"&&(!a||f.event.triggered!==a.type)?f.event.dispatch.apply(i.elem,arguments):b},i.elem=a),c=f.trim(I(c)).split(" ");for(k=0;k=0&&(h=h.slice(0,-1),k=!0),h.indexOf(".")>=0&&(i=h.split("."),h=i.shift(),i.sort());if((!e||f.event.customEvent[h])&&!f.event.global[h])return;c=typeof c=="object"?c[f.expando]?c:new f.Event(h,c):new f.Event(h),c.type=h,c.isTrigger=!0,c.exclusive=k,c.namespace=i.join("."),c.namespace_re=c.namespace?new RegExp("(^|\\.)"+i.join("\\.(?:.*\\.)?")+"(\\.|$)"):null,o=h.indexOf(":")<0?"on"+h:"";if(!e){j=f.cache;for(l in j)j[l].events&&j[l].events[h]&&f.event.trigger(c,d,j[l].handle.elem,!0);return}c.result=b,c.target||(c.target=e),d=d!=null?f.makeArray(d):[],d.unshift(c),p=f.event.special[h]||{};if(p.trigger&&p.trigger.apply(e,d)===!1)return;r=[[e,p.bindType||h]];if(!g&&!p.noBubble&&!f.isWindow(e)){s=p.delegateType||h,m=E.test(s+h)?e:e.parentNode,n=null;for(;m;m=m.parentNode)r.push([m,s]),n=m;n&&n===e.ownerDocument&&r.push([n.defaultView||n.parentWindow||a,s])}for(l=0;le&&i.push({elem:this,matches:d.slice(e)});for(j=0;j0?this.on(b,null,a,c):this.trigger(b)},f.attrFn&&(f.attrFn[b]=!0),C.test(b)&&(f.event.fixHooks[b]=f.event.keyHooks),D.test(b)&&(f.event.fixHooks[b]=f.event.mouseHooks)}),function(){function x(a,b,c,e,f,g){for(var h=0,i=e.length;h0){k=j;break}}j=j[a]}e[h]=k}}}function w(a,b,c,e,f,g){for(var h=0,i=e.length;h+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g,d="sizcache"+(Math.random()+"").replace(".",""),e=0,g=Object.prototype.toString,h=!1,i=!0,j=/\\/g,k=/\r\n/g,l=/\W/;[0,0].sort(function(){i=!1;return 0});var m=function(b,d,e,f){e=e||[],d=d||c;var h=d;if(d.nodeType!==1&&d.nodeType!==9)return[];if(!b||typeof b!="string")return e;var i,j,k,l,n,q,r,t,u=!0,v=m.isXML(d),w=[],x=b;do{a.exec(""),i=a.exec(x);if(i){x=i[3],w.push(i[1]);if(i[2]){l=i[3];break}}}while(i);if(w.length>1&&p.exec(b))if(w.length===2&&o.relative[w[0]])j=y(w[0]+w[1],d,f);else{j=o.relative[w[0]]?[d]:m(w.shift(),d);while(w.length)b=w.shift(),o.relative[b]&&(b+=w.shift()),j=y(b,j,f)}else{!f&&w.length>1&&d.nodeType===9&&!v&&o.match.ID.test(w[0])&&!o.match.ID.test(w[w.length-1])&&(n=m.find(w.shift(),d,v),d=n.expr?m.filter(n.expr,n.set)[0]:n.set[0]);if(d){n=f?{expr:w.pop(),set:s(f)}:m.find(w.pop(),w.length===1&&(w[0]==="~"||w[0]==="+")&&d.parentNode?d.parentNode:d,v),j=n.expr?m.filter(n.expr,n.set):n.set,w.length>0?k=s(j):u=!1;while(w.length)q=w.pop(),r=q,o.relative[q]?r=w.pop():q="",r==null&&(r=d),o.relative[q](k,r,v)}else k=w=[]}k||(k=j),k||m.error(q||b);if(g.call(k)==="[object Array]")if(!u)e.push.apply(e,k);else if(d&&d.nodeType===1)for(t=0;k[t]!=null;t++)k[t]&&(k[t]===!0||k[t].nodeType===1&&m.contains(d,k[t]))&&e.push(j[t]);else for(t=0;k[t]!=null;t++)k[t]&&k[t].nodeType===1&&e.push(j[t]);else s(k,e);l&&(m(l,h,e,f),m.uniqueSort(e));return e};m.uniqueSort=function(a){if(u){h=i,a.sort(u);if(h)for(var b=1;b0},m.find=function(a,b,c){var d,e,f,g,h,i;if(!a)return[];for(e=0,f=o.order.length;e":function(a,b){var c,d=typeof b=="string",e=0,f=a.length;if(d&&!l.test(b)){b=b.toLowerCase();for(;e=0)?c||d.push(h):c&&(b[g]=!1));return!1},ID:function(a){return a[1].replace(j,"")},TAG:function(a,b){return a[1].replace(j,"").toLowerCase()},CHILD:function(a){if(a[1]==="nth"){a[2]||m.error(a[0]),a[2]=a[2].replace(/^\+|\s*/g,"");var b=/(-?)(\d*)(?:n([+\-]?\d*))?/.exec(a[2]==="even"&&"2n"||a[2]==="odd"&&"2n+1"||!/\D/.test(a[2])&&"0n+"+a[2]||a[2]);a[2]=b[1]+(b[2]||1)-0,a[3]=b[3]-0}else a[2]&&m.error(a[0]);a[0]=e++;return a},ATTR:function(a,b,c,d,e,f){var g=a[1]=a[1].replace(j,"");!f&&o.attrMap[g]&&(a[1]=o.attrMap[g]),a[4]=(a[4]||a[5]||"").replace(j,""),a[2]==="~="&&(a[4]=" "+a[4]+" ");return a},PSEUDO:function(b,c,d,e,f){if(b[1]==="not")if((a.exec(b[3])||"").length>1||/^\w/.test(b[3]))b[3]=m(b[3],null,null,c);else{var g=m.filter(b[3],c,d,!0^f);d||e.push.apply(e,g);return!1}else if(o.match.POS.test(b[0])||o.match.CHILD.test(b[0]))return!0;return b},POS:function(a){a.unshift(!0);return a}},filters:{enabled:function(a){return a.disabled===!1&&a.type!=="hidden"},disabled:function(a){return a.disabled===!0},checked:function(a){return a.checked===!0},selected:function(a){a.parentNode&&a.parentNode.selectedIndex;return a.selected===!0},parent:function(a){return!!a.firstChild},empty:function(a){return!a.firstChild},has:function(a,b,c){return!!m(c[3],a).length},header:function(a){return/h\d/i.test(a.nodeName)},text:function(a){var b=a.getAttribute("type"),c=a.type;return a.nodeName.toLowerCase()==="input"&&"text"===c&&(b===c||b===null)},radio:function(a){return a.nodeName.toLowerCase()==="input"&&"radio"===a.type},checkbox:function(a){return a.nodeName.toLowerCase()==="input"&&"checkbox"===a.type},file:function(a){return a.nodeName.toLowerCase()==="input"&&"file"===a.type},password:function(a){return a.nodeName.toLowerCase()==="input"&&"password"===a.type},submit:function(a){var b=a.nodeName.toLowerCase();return(b==="input"||b==="button")&&"submit"===a.type},image:function(a){return a.nodeName.toLowerCase()==="input"&&"image"===a.type},reset:function(a){var b=a.nodeName.toLowerCase();return(b==="input"||b==="button")&&"reset"===a.type},button:function(a){var b=a.nodeName.toLowerCase();return b==="input"&&"button"===a.type||b==="button"},input:function(a){return/input|select|textarea|button/i.test(a.nodeName)},focus:function(a){return a===a.ownerDocument.activeElement}},setFilters:{first:function(a,b){return b===0},last:function(a,b,c,d){return b===d.length-1},even:function(a,b){return b%2===0},odd:function(a,b){return b%2===1},lt:function(a,b,c){return bc[3]-0},nth:function(a,b,c){return c[3]-0===b},eq:function(a,b,c){return c[3]-0===b}},filter:{PSEUDO:function(a,b,c,d){var e=b[1],f=o.filters[e];if(f)return f(a,c,b,d);if(e==="contains")return(a.textContent||a.innerText||n([a])||"").indexOf(b[3])>=0;if(e==="not"){var g=b[3];for(var h=0,i=g.length;h=0}},ID:function(a,b){return a.nodeType===1&&a.getAttribute("id")===b},TAG:function(a,b){return b==="*"&&a.nodeType===1||!!a.nodeName&&a.nodeName.toLowerCase()===b},CLASS:function(a,b){return(" "+(a.className||a.getAttribute("class"))+" ").indexOf(b)>-1},ATTR:function(a,b){var c=b[1],d=m.attr?m.attr(a,c):o.attrHandle[c]?o.attrHandle[c](a):a[c]!=null?a[c]:a.getAttribute(c),e=d+"",f=b[2],g=b[4];return d==null?f==="!=":!f&&m.attr?d!=null:f==="="?e===g:f==="*="?e.indexOf(g)>=0:f==="~="?(" "+e+" ").indexOf(g)>=0:g?f==="!="?e!==g:f==="^="?e.indexOf(g)===0:f==="$="?e.substr(e.length-g.length)===g:f==="|="?e===g||e.substr(0,g.length+1)===g+"-":!1:e&&d!==!1},POS:function(a,b,c,d){var e=b[2],f=o.setFilters[e];if(f)return f(a,c,b,d)}}},p=o.match.POS,q=function(a,b){return"\\"+(b-0+1)};for(var r in o.match)o.match[r]=new RegExp(o.match[r].source+/(?![^\[]*\])(?![^\(]*\))/.source),o.leftMatch[r]=new RegExp(/(^(?:.|\r|\n)*?)/.source+o.match[r].source.replace(/\\(\d+)/g,q));var s=function(a,b){a=Array.prototype.slice.call(a,0);if(b){b.push.apply(b,a);return b}return a};try{Array.prototype.slice.call(c.documentElement.childNodes,0)[0].nodeType}catch(t){s=function(a,b){var c=0,d=b||[];if(g.call(a)==="[object Array]")Array.prototype.push.apply(d,a);else if(typeof a.length=="number")for(var e=a.length;c ",e.insertBefore(a,e.firstChild),c.getElementById(d)&&(o.find.ID=function(a,c,d){if(typeof c.getElementById!="undefined"&&!d){var e=c.getElementById(a[1]);return e?e.id===a[1]||typeof e.getAttributeNode!="undefined"&&e.getAttributeNode("id").nodeValue===a[1]?[e]:b:[]}},o.filter.ID=function(a,b){var c=typeof a.getAttributeNode!="undefined"&&a.getAttributeNode("id");return a.nodeType===1&&c&&c.nodeValue===b}),e.removeChild(a),e=a=null}(),function(){var a=c.createElement("div");a.appendChild(c.createComment("")),a.getElementsByTagName("*").length>0&&(o.find.TAG=function(a,b){var c=b.getElementsByTagName(a[1]);if(a[1]==="*"){var d=[];for(var e=0;c[e];e++)c[e].nodeType===1&&d.push(c[e]);c=d}return c}),a.innerHTML=" ",a.firstChild&&typeof a.firstChild.getAttribute!="undefined"&&a.firstChild.getAttribute("href")!=="#"&&(o.attrHandle.href=function(a){return a.getAttribute("href",2)}),a=null}(),c.querySelectorAll&&function(){var a=m,b=c.createElement("div"),d="__sizzle__";b.innerHTML="
";if(!b.querySelectorAll||b.querySelectorAll(".TEST").length!==0){m=function(b,e,f,g){e=e||c;if(!g&&!m.isXML(e)){var h=/^(\w+$)|^\.([\w\-]+$)|^#([\w\-]+$)/.exec(b);if(h&&(e.nodeType===1||e.nodeType===9)){if(h[1])return s(e.getElementsByTagName(b),f);if(h[2]&&o.find.CLASS&&e.getElementsByClassName)return s(e.getElementsByClassName(h[2]),f)}if(e.nodeType===9){if(b==="body"&&e.body)return s([e.body],f);if(h&&h[3]){var i=e.getElementById(h[3]);if(!i||!i.parentNode)return s([],f);if(i.id===h[3])return s([i],f)}try{return s(e.querySelectorAll(b),f)}catch(j){}}else if(e.nodeType===1&&e.nodeName.toLowerCase()!=="object"){var k=e,l=e.getAttribute("id"),n=l||d,p=e.parentNode,q=/^\s*[+~]/.test(b);l?n=n.replace(/'/g,"\\$&"):e.setAttribute("id",n),q&&p&&(e=e.parentNode);try{if(!q||p)return s(e.querySelectorAll("[id='"+n+"'] "+b),f)}catch(r){}finally{l||k.removeAttribute("id")}}}return a(b,e,f,g)};for(var e in a)m[e]=a[e];b=null}}(),function(){var a=c.documentElement,b=a.matchesSelector||a.mozMatchesSelector||a.webkitMatchesSelector||a.msMatchesSelector;if(b){var d=!b.call(c.createElement("div"),"div"),e=!1;try{b.call(c.documentElement,"[test!='']:sizzle")}catch(f){e=!0}m.matchesSelector=function(a,c){c=c.replace(/\=\s*([^'"\]]*)\s*\]/g,"='$1']");if(!m.isXML(a))try{if(e||!o.match.PSEUDO.test(c)&&!/!=/.test(c)){var f=b.call(a,c);if(f||!d||a.document&&a.document.nodeType!==11)return f}}catch(g){}return m(c,null,null,[a]).length>0}}}(),function(){var a=c.createElement("div");a.innerHTML="
";if(!!a.getElementsByClassName&&a.getElementsByClassName("e").length!==0){a.lastChild.className="e";if(a.getElementsByClassName("e").length===1)return;o.order.splice(1,0,"CLASS"),o.find.CLASS=function(a,b,c){if(typeof b.getElementsByClassName!="undefined"&&!c)return b.getElementsByClassName(a[1])},a=null}}(),c.documentElement.contains?m.contains=function(a,b){return a!==b&&(a.contains?a.contains(b):!0)}:c.documentElement.compareDocumentPosition?m.contains=function(a,b){return!!(a.compareDocumentPosition(b)&16)}:m.contains=function(){return!1},m.isXML=function(a){var b=(a?a.ownerDocument||a:0).documentElement;return b?b.nodeName!=="HTML":!1};var y=function(a,b,c){var d,e=[],f="",g=b.nodeType?[b]:b;while(d=o.match.PSEUDO.exec(a))f+=d[0],a=a.replace(o.match.PSEUDO,"");a=o.relative[a]?a+"*":a;for(var h=0,i=g.length;h0)for(h=g;h=0:f.filter(a,this).length>0:this.filter(a).length>0)},closest:function(a,b){var c=[],d,e,g=this[0];if(f.isArray(a)){var h=1;while(g&&g.ownerDocument&&g!==b){for(d=0;d-1:f.find.matchesSelector(g,a)){c.push(g);break}g=g.parentNode;if(!g||!g.ownerDocument||g===b||g.nodeType===11)break}}c=c.length>1?f.unique(c):c;return this.pushStack(c,"closest",a)},index:function(a){if(!a)return this[0]&&this[0].parentNode?this.prevAll().length:-1;if(typeof a=="string")return f.inArray(this[0],f(a));return f.inArray(a.jquery?a[0]:a,this)},add:function(a,b){var c=typeof a=="string"?f(a,b):f.makeArray(a&&a.nodeType?[a]:a),d=f.merge(this.get(),c);return this.pushStack(S(c[0])||S(d[0])?d:f.unique(d))},andSelf:function(){return this.add(this.prevObject)}}),f.each({parent:function(a){var b=a.parentNode;return b&&b.nodeType!==11?b:null},parents:function(a){return f.dir(a,"parentNode")},parentsUntil:function(a,b,c){return f.dir(a,"parentNode",c)},next:function(a){return f.nth(a,2,"nextSibling")},prev:function(a){return f.nth(a,2,"previousSibling")},nextAll:function(a){return f.dir(a,"nextSibling")},prevAll:function(a){return f.dir(a,"previousSibling")},nextUntil:function(a,b,c){return f.dir(a,"nextSibling",c)},prevUntil:function(a,b,c){return f.dir(a,"previousSibling",c)},siblings:function(a){return f.sibling(a.parentNode.firstChild,a)},children:function(a){return f.sibling(a.firstChild)},contents:function(a){return f.nodeName(a,"iframe")?a.contentDocument||a.contentWindow.document:f.makeArray(a.childNodes)}},function(a,b){f.fn[a]=function(c,d){var e=f.map(this,b,c);L.test(a)||(d=c),d&&typeof d=="string"&&(e=f.filter(d,e)),e=this.length>1&&!R[a]?f.unique(e):e,(this.length>1||N.test(d))&&M.test(a)&&(e=e.reverse());return this.pushStack(e,a,P.call(arguments).join(","))}}),f.extend({filter:function(a,b,c){c&&(a=":not("+a+")");return b.length===1?f.find.matchesSelector(b[0],a)?[b[0]]:[]:f.find.matches(a,b)},dir:function(a,c,d){var e=[],g=a[c];while(g&&g.nodeType!==9&&(d===b||g.nodeType!==1||!f(g).is(d)))g.nodeType===1&&e.push(g),g=g[c];return e},nth:function(a,b,c,d){b=b||1;var e=0;for(;a;a=a[c])if(a.nodeType===1&&++e===b)break;return a},sibling:function(a,b){var c=[];for(;a;a=a.nextSibling)a.nodeType===1&&a!==b&&c.push(a);return c}});var V="abbr|article|aside|audio|canvas|datalist|details|figcaption|figure|footer|header|hgroup|mark|meter|nav|output|progress|section|summary|time|video",W=/ jQuery\d+="(?:\d+|null)"/g,X=/^\s+/,Y=/<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/ig,Z=/<([\w:]+)/,$=/",""],legend:[1,""," "],thead:[1,""],tr:[2,""],td:[3,""],col:[2,""],area:[1,""," "],_default:[0,"",""]},bh=U(c);bg.optgroup=bg.option,bg.tbody=bg.tfoot=bg.colgroup=bg.caption=bg.thead,bg.th=bg.td,f.support.htmlSerialize||(bg._default=[1,"div","
"]),f.fn.extend({text:function(a){if(f.isFunction(a))return this.each(function(b){var c=f(this);c.text(a.call(this,b,c.text()))});if(typeof a!="object"&&a!==b)return this.empty().append((this[0]&&this[0].ownerDocument||c).createTextNode(a));return f.text(this)},wrapAll:function(a){if(f.isFunction(a))return this.each(function(b){f(this).wrapAll(a.call(this,b))});if(this[0]){var b=f(a,this[0].ownerDocument).eq(0).clone(!0);this[0].parentNode&&b.insertBefore(this[0]),b.map(function(){var a=this;while(a.firstChild&&a.firstChild.nodeType===1)a=a.firstChild;return a}).append(this)}return this},wrapInner:function(a){if(f.isFunction(a))return this.each(function(b){f(this).wrapInner(a.call(this,b))});return this.each(function(){var b=f(this),c=b.contents();c.length?c.wrapAll(a):b.append(a)})},wrap:function(a){var b=f.isFunction(a);return this.each(function(c){f(this).wrapAll(b?a.call(this,c):a)})},unwrap:function(){return this.parent().each(function(){f.nodeName(this,"body")||f(this).replaceWith(this.childNodes)}).end()},append:function(){return this.domManip(arguments,!0,function(a){this.nodeType===1&&this.appendChild(a)})},prepend:function(){return this.domManip(arguments,!0,function(a){this.nodeType===1&&this.insertBefore(a,this.firstChild)})},before:function(){if(this[0]&&this[0].parentNode)return this.domManip(arguments,!1,function(a){this.parentNode.insertBefore(a,this)});if(arguments.length){var a=f.clean(arguments);a.push.apply(a,this.toArray());return this.pushStack(a,"before",arguments)}},after:function(){if(this[0]&&this[0].parentNode)return this.domManip(arguments,!1,function(a){this.parentNode.insertBefore(a,this.nextSibling)});if(arguments.length){var a=this.pushStack(this,"after",arguments);a.push.apply(a,f.clean(arguments));return a}},remove:function(a,b){for(var c=0,d;(d=this[c])!=null;c++)if(!a||f.filter(a,[d]).length)!b&&d.nodeType===1&&(f.cleanData(d.getElementsByTagName("*")),f.cleanData([d])),d.parentNode&&d.parentNode.removeChild(d);return this},empty:function()
4 | {for(var a=0,b;(b=this[a])!=null;a++){b.nodeType===1&&f.cleanData(b.getElementsByTagName("*"));while(b.firstChild)b.removeChild(b.firstChild)}return this},clone:function(a,b){a=a==null?!1:a,b=b==null?a:b;return this.map(function(){return f.clone(this,a,b)})},html:function(a){if(a===b)return this[0]&&this[0].nodeType===1?this[0].innerHTML.replace(W,""):null;if(typeof a=="string"&&!ba.test(a)&&(f.support.leadingWhitespace||!X.test(a))&&!bg[(Z.exec(a)||["",""])[1].toLowerCase()]){a=a.replace(Y,"<$1>$2>");try{for(var c=0,d=this.length;c1&&l0?this.clone(!0):this).get();f(e[h])[b](j),d=d.concat(j)}return this.pushStack(d,a,e.selector)}}),f.extend({clone:function(a,b,c){var d,e,g,h=f.support.html5Clone||!bc.test("<"+a.nodeName)?a.cloneNode(!0):bo(a);if((!f.support.noCloneEvent||!f.support.noCloneChecked)&&(a.nodeType===1||a.nodeType===11)&&!f.isXMLDoc(a)){bk(a,h),d=bl(a),e=bl(h);for(g=0;d[g];++g)e[g]&&bk(d[g],e[g])}if(b){bj(a,h);if(c){d=bl(a),e=bl(h);for(g=0;d[g];++g)bj(d[g],e[g])}}d=e=null;return h},clean:function(a,b,d,e){var g;b=b||c,typeof b.createElement=="undefined"&&(b=b.ownerDocument||b[0]&&b[0].ownerDocument||c);var h=[],i;for(var j=0,k;(k=a[j])!=null;j++){typeof k=="number"&&(k+="");if(!k)continue;if(typeof k=="string")if(!_.test(k))k=b.createTextNode(k);else{k=k.replace(Y,"<$1>$2>");var l=(Z.exec(k)||["",""])[1].toLowerCase(),m=bg[l]||bg._default,n=m[0],o=b.createElement("div");b===c?bh.appendChild(o):U(b).appendChild(o),o.innerHTML=m[1]+k+m[2];while(n--)o=o.lastChild;if(!f.support.tbody){var p=$.test(k),q=l==="table"&&!p?o.firstChild&&o.firstChild.childNodes:m[1]===""&&!p?o.childNodes:[];for(i=q.length-1;i>=0;--i)f.nodeName(q[i],"tbody")&&!q[i].childNodes.length&&q[i].parentNode.removeChild(q[i])}!f.support.leadingWhitespace&&X.test(k)&&o.insertBefore(b.createTextNode(X.exec(k)[0]),o.firstChild),k=o.childNodes}var r;if(!f.support.appendChecked)if(k[0]&&typeof (r=k.length)=="number")for(i=0;i=0)return b+"px"}}}),f.support.opacity||(f.cssHooks.opacity={get:function(a,b){return br.test((b&&a.currentStyle?a.currentStyle.filter:a.style.filter)||"")?parseFloat(RegExp.$1)/100+"":b?"1":""},set:function(a,b){var c=a.style,d=a.currentStyle,e=f.isNumeric(b)?"alpha(opacity="+b*100+")":"",g=d&&d.filter||c.filter||"";c.zoom=1;if(b>=1&&f.trim(g.replace(bq,""))===""){c.removeAttribute("filter");if(d&&!d.filter)return}c.filter=bq.test(g)?g.replace(bq,e):g+" "+e}}),f(function(){f.support.reliableMarginRight||(f.cssHooks.marginRight={get:function(a,b){var c;f.swap(a,{display:"inline-block"},function(){b?c=bz(a,"margin-right","marginRight"):c=a.style.marginRight});return c}})}),c.defaultView&&c.defaultView.getComputedStyle&&(bA=function(a,b){var c,d,e;b=b.replace(bs,"-$1").toLowerCase(),(d=a.ownerDocument.defaultView)&&(e=d.getComputedStyle(a,null))&&(c=e.getPropertyValue(b),c===""&&!f.contains(a.ownerDocument.documentElement,a)&&(c=f.style(a,b)));return c}),c.documentElement.currentStyle&&(bB=function(a,b){var c,d,e,f=a.currentStyle&&a.currentStyle[b],g=a.style;f===null&&g&&(e=g[b])&&(f=e),!bt.test(f)&&bu.test(f)&&(c=g.left,d=a.runtimeStyle&&a.runtimeStyle.left,d&&(a.runtimeStyle.left=a.currentStyle.left),g.left=b==="fontSize"?"1em":f||0,f=g.pixelLeft+"px",g.left=c,d&&(a.runtimeStyle.left=d));return f===""?"auto":f}),bz=bA||bB,f.expr&&f.expr.filters&&(f.expr.filters.hidden=function(a){var b=a.offsetWidth,c=a.offsetHeight;return b===0&&c===0||!f.support.reliableHiddenOffsets&&(a.style&&a.style.display||f.css(a,"display"))==="none"},f.expr.filters.visible=function(a){return!f.expr.filters.hidden(a)});var bD=/%20/g,bE=/\[\]$/,bF=/\r?\n/g,bG=/#.*$/,bH=/^(.*?):[ \t]*([^\r\n]*)\r?$/mg,bI=/^(?:color|date|datetime|datetime-local|email|hidden|month|number|password|range|search|tel|text|time|url|week)$/i,bJ=/^(?:about|app|app\-storage|.+\-extension|file|res|widget):$/,bK=/^(?:GET|HEAD)$/,bL=/^\/\//,bM=/\?/,bN=/