36
37:- module(http_tree,
38 [ http_tree_view//1
39 ]). 40:- use_module(library(http/http_dispatch)). 41:- use_module(library(http/http_path)). 42:- use_module(library(http/http_parameters)). 43:- use_module(library(http/http_json)). 44:- use_module(library(http/html_head)). 45:- use_module(library(http/html_write)). 46:- use_module(library(http/yui_resources)). 47:- use_module(library(option)). 48:- use_module(library(pairs)). 49
55
56:- http_handler(root(help/expand_http_node), expand_http_node, []). 57
62
63http_tree_view(Options) -->
64 tree_view(expand_http_node, Options).
65
66
70
71:- html_resource(yui_examples('treeview/assets/css/folders/tree.css'),
72 [ requires([ yui('treeview/treeview.js'),
73 yui('connection/connection.js'),
74 yui('treeview/assets/skins/sam/treeview.css')
75 ])
76 ]). 77
78
79tree_view(Handler, Options) -->
80 { http_location_by_id(Handler, Path),
81 TreeViewID = treeDiv1
82 },
83 html([ \html_requires(yui_examples('treeview/assets/css/folders/tree.css')),
84
85 div(id(TreeViewID), []),
86 \tree_view_script(Path, TreeViewID, Options)
87 ]).
88
89tree_view_script(Path, TreeViewID, Options) -->
90 html(script(type('text/javascript'), \[
91'var currentIconMode = 0;\n', 92'function buildTree() {\n',
93' tree = new YAHOO.widget.TreeView("~w");\n'-[TreeViewID],
94' tree.setDynamicLoad(loadNodeData, currentIconMode);\n',
95\tree_options(Options),
96' var root = tree.getRoot();\n',
97'\n',
98' var tempNode = new YAHOO.widget.TextNode("/", root, true);\n',
99' tempNode.data = { path:"/" };\n',
100' tree.draw();\n',
101'}\n',
102
103'function loadNodeData(node, fnLoadComplete) {\n',
104' var sUrl = "~w?node=" + encodeURIComponent(node.data.path);\n'-[Path],
105' var callback = {\n',
106' success: function(oResponse) {\n',
107'\t var children = eval(oResponse.responseText);\n',
108'\t for (var i=0, j=children.length; i<j; i++) {\n',
109'\t\t var tempNode = new YAHOO.widget.TextNode(children[i], node, false);\n',
110' }\n',
111' oResponse.argument.fnLoadComplete();\n',
112' },\n',
113' failure: function(oResponse) {\n',
114' oResponse.argument.fnLoadComplete();\n',
115' },\n',
116' argument: {\n',
117' "node": node,\n',
118' "fnLoadComplete": fnLoadComplete\n',
119' },\n',
120' timeout: 7000\n',
121' };\n',
122' YAHOO.util.Connect.asyncRequest("GET", sUrl, callback);\n',
123'}\n',
124
126'buildTree();\n'
127 ])).
128
129tree_options([]) --> [].
130tree_options([H|T]) --> tree_option(H), tree_options(T).
131
132tree_option(labelClick(JS)) -->
133 !,
134 html([ 'tree.subscribe("labelClick", ~w);\n'-[JS] ]).
135tree_option(_) -->
136 [].
137
141
142expand_http_node(Request) :-
143 http_parameters(Request,
144 [ node(Parent, [ description('HTTP location to refine')])
145 ]),
146 node_children(Parent, Children),
147 reply_json(Children, []).
148
149node_children(Parent, Children) :-
150 ensure_ends_slash(Parent, Parent1),
151 findall(Sub, sub_handler(Parent1, Sub), Subs),
152 map_list_to_pairs(first_component(Parent), Subs, Keyed0),
153 keysort(Keyed0, Keyed),
154 group_pairs_by_key(Keyed, Groups),
155 maplist(decorate, Groups, Children).
156
157ensure_ends_slash(Path, Path) :-
158 sub_atom(Path, _, _, 0, /),
159 !.
160ensure_ends_slash(Path, PathSlash) :-
161 atom_concat(Path, /, PathSlash).
162
163sub_handler(Parent, Sub) :-
164 http_current_handler(Sub, _:_, _),
165 sub_atom(Sub, 0, _, A, Parent),
166 A > 0.
167
168first_component(Parent, Path, ParentExt) :-
169 atom_length(Parent, PL),
170 sub_atom(Path, B, _, _, /),
171 B > PL,
172 !,
173 sub_atom(Path, 0, B, _, ParentExt).
174first_component(_, Path, Path).
175
176
177decorate(Prefix-[Only],
178 json([label(Label), isLeaf(@(true)), path(Only)])) :-
179 atom_concat(Prefix, Rest, Only),
180 ( Rest == ''
181 ; Rest == /
182 ),
183 !,
184 file_base_name(Prefix, Label0),
185 leaf_label(Only, Label0, Label).
186decorate(Prefix-_,
187 json([label(Label), isLeaf(@(false)), path(Prefix)])) :-
188 file_base_name(Prefix, Label).
189
190leaf_label(Only, Label0, Label) :-
191 http_current_handler(Only, _:_, Options),
192 ( memberchk(prefix(true), Options)
193 -> atom_concat(Label0, '...', Label)
194 ; Label = Label0
195 )