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)). 41 42/** <module> Call jQuery on the SWISH interface 43 44Select objects in the SWISH interface using jQuery, run an arbitrary 45JavaScript function on them and return the result. This predicate was 46introduced while adding functionality to request the contents of tabs in 47the SWISH interface. 48*/ 49 50%! jquery(+Selector, +Function) is det. 51%! jquery(+Selector, +Function, -Reply) is det. 52% 53% Run a jQuery query in the SWISH interface. Selector defines the 54% receiver of the jQuery method, Function is the JavaScript 55% function to run on the receiver and Reply is the Prolog 56% representation of the result. 57% 58% @arg Selector selects the jQuery receiver. It takes three forms: 59% 60% - If the selector is a string, it is simply interpreted as 61% =|$(Selector)|=. 62% - If the selector is a compound, the _functor_ defines the 63% start point. If a (single) argument provided it is handed to 64% the jQuery `find` method. Defines starting points are: 65% - this 66% The current SWISH _runner_ object, jQuery class 67% `prologRunner`. 68% - cell 69% The current notebook (query) cell. jQuery class 70% `nbCell`. 71% - notebook 72% The current notebook. jQuery class `notebook` 73% - swish 74% The SWISH instance. jQuery class `swish`. 75% 76% @arg Function is a compound term representing a JavaScript call. 77% The functor name is used as method and the remaining arguments 78% are converted by json_write_dict/2. 79% 80% @arg Reply is the JavaScript reply, converted to Prolog by 81% the Pengine.stringify() method. 82 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(_,_,_))