View source with raw comments or as raw
    1/*  Part of ClioPatria
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2010 University of Amsterdam
    7		        CWI, Asterdam
    8			VU University Amsterdam
    9    All rights reserved.
   10
   11    Redistribution and use in source and binary forms, with or without
   12    modification, are permitted provided that the following conditions
   13    are met:
   14
   15    1. Redistributions of source code must retain the above copyright
   16       notice, this list of conditions and the following disclaimer.
   17
   18    2. Redistributions in binary form must reproduce the above copyright
   19       notice, this list of conditions and the following disclaimer in
   20       the documentation and/or other materials provided with the
   21       distribution.
   22
   23    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   24    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   25    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   26    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   27    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   28    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   29    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   30    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   31    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   33    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   34    POSSIBILITY OF SUCH DAMAGE.
   35*/
   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)).

Create YUI tree from HTTP locations

This module provides the component http_tree_view//1 and associated helpers. */

   56:- http_handler(root(help/expand_http_node), expand_http_node, []).
 http_tree_view(+Options)//
Show hierarchy of HTTP locations (paths). The tree is a YUI tree that can be expanded dynamically.
   63http_tree_view(Options) -->
   64	tree_view(expand_http_node, Options).
   65
   66
   67% the folders/tree.css file must be last.  Because requirements are made
   68% unique and sorted using toplogical sort, this  can only be achieved by
   69% declaring the other files as dependencies.
   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'	     var children = eval(oResponse.responseText);\n',
  108'	     for (var i=0, j=children.length; i<j; i++) {\n',
  109'		 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
  125%'YAHOO.util.Event.onDOMReady(buildTree());\n'
  126'buildTree();\n'
  127					     ])).
  128
  129tree_options([]) --> [].
  130tree_options([H|T]) --> tree_option(H), tree_options(T).
  131
  132tree_option(labelClick(JS)) --> !,
  133	html([ 'tree.subscribe("labelClick", ~w);\n'-[JS] ]).
  134tree_option(_) -->
  135	[].
 expand_http_node(+Request)
HTTP handler that returns the children of an HTTP node.
  141expand_http_node(Request) :-
  142	http_parameters(Request,
  143			[ node(Parent, [ description('HTTP location to refine')])
  144			]),
  145	node_children(Parent, Children),
  146	reply_json(Children, []).
  147
  148node_children(Parent, Children) :-
  149	ensure_ends_slash(Parent, Parent1),
  150	findall(Sub, sub_handler(Parent1, Sub), Subs),
  151	map_list_to_pairs(first_component(Parent), Subs, Keyed0),
  152	keysort(Keyed0, Keyed),
  153	group_pairs_by_key(Keyed, Groups),
  154	maplist(decorate, Groups, Children).
  155
  156ensure_ends_slash(Path, Path) :-
  157	sub_atom(Path, _, _, 0, /), !.
  158ensure_ends_slash(Path, PathSlash) :-
  159	atom_concat(Path, /, PathSlash).
  160
  161sub_handler(Parent, Sub) :-
  162	http_current_handler(Sub, _:_, _),
  163	sub_atom(Sub, 0, _, A, Parent),
  164	A > 0.
  165
  166first_component(Parent, Path, ParentExt) :-
  167	atom_length(Parent, PL),
  168	sub_atom(Path, B, _, _, /),
  169	B > PL, !,
  170	sub_atom(Path, 0, B, _, ParentExt).
  171first_component(_, Path, Path).
  172
  173
  174decorate(Prefix-[Only],
  175	 json([label(Label), isLeaf(@(true)), path(Only)])) :-
  176	atom_concat(Prefix, Rest, Only),
  177	(   Rest == ''
  178	;   Rest == /
  179	), !,
  180	file_base_name(Prefix, Label0),
  181	leaf_label(Only, Label0, Label).
  182decorate(Prefix-_,
  183	 json([label(Label), isLeaf(@(false)), path(Prefix)])) :-
  184	file_base_name(Prefix, Label).
  185
  186leaf_label(Only, Label0, Label) :-
  187	http_current_handler(Only, _:_, Options),
  188	(   memberchk(prefix(true), Options)
  189	->  atom_concat(Label0, '...', Label)
  190	;   Label = Label0
  191	)