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

Call jQuery on the SWISH interface

Select objects in the SWISH interface using jQuery, run an arbitrary JavaScript function on them and return the result. This predicate was introduced while adding functionality to request the contents of tabs in the SWISH interface. */

 jquery(+Selector, +Function) is det
 jquery(+Selector, +Function, -Reply) is det
Run a jQuery query in the SWISH interface. Selector defines the receiver of the jQuery method, Function is the JavaScript function to run on the receiver and Reply is the Prolog representation of the result.
Arguments:
Selector- selects the jQuery receiver. It takes three forms:
  • If the selector is a string, it is simply interpreted as $(Selector).
  • If the selector is a compound, the functor defines the start point. If a (single) argument provided it is handed to the jQuery find method. Defines starting points are:
    this
    The current SWISH runner object, jQuery class prologRunner.
    cell
    The current notebook (query) cell. jQuery class nbCell.
    notebook
    The current notebook. jQuery class notebook
    swish
    The SWISH instance. jQuery class swish.
Function- is a compound term representing a JavaScript call. The functor name is used as method and the remaining arguments are converted by json_write_dict/2.
Reply- is the JavaScript reply, converted to Prolog by the Pengine.stringify() method.
   83jquery(Selector, Function) :-
   84	jquery(Selector, Function, _).
   85jquery(Selector, Function, Reply) :-
   86	map_selector(Selector, Selector1),
   87	compound_name_arguments(Function, Method, Args),
   88	pengine_input(_{ type: "jQuery",
   89			 selector: Selector1,
   90			 method: Method,
   91			 arguments: Args
   92		       },
   93		      Reply).
   94
   95map_selector(Selector, Selector) :-
   96	string(Selector), !.
   97map_selector(Selector, Selector) :-
   98	atom(Selector), !.
   99map_selector(Selector, json{root:Name, sub:SubSelector}) :-
  100	compound_name_arguments(Selector, Name, Args),
  101	root_selector(Name),
  102	(   Args == []
  103	->  SubSelector = ""
  104	;   Args = [SubSelector]
  105	->  must_be(string, SubSelector)
  106	;   domain_error(arity_one, Selector)
  107	).
  108
  109root_selector(this) :- !.
  110root_selector(cell) :- !.
  111root_selector(notebook) :- !.
  112root_selector(swish) :- !.
  113root_selector(Selector) :-
  114	domain_error(root_selector, Selector).
  115
  116:- multifile
  117	sandbox:safe_primitive/1.  118
  119sandbox:safe_primitive(swish_jquery:jquery(_,_,_))