View source with formatted 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)).   49
   50/** <module> Create YUI tree from HTTP locations
   51
   52This module provides the  component   http_tree_view//1  and  associated
   53helpers.
   54*/
   55
   56:- http_handler(root(help/expand_http_node), expand_http_node, []).   57
   58%!  http_tree_view(+Options)//
   59%
   60%   Show hierarchy of HTTP locations (paths).  The tree is a YUI
   61%   tree that can be expanded dynamically.
   62
   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'\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
  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    !,
  134    html([ 'tree.subscribe("labelClick", ~w);\n'-[JS] ]).
  135tree_option(_) -->
  136    [].
  137
  138%!  expand_http_node(+Request)
  139%
  140%   HTTP handler that returns the children of an HTTP node.
  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    )