1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2017-2025, VU University Amsterdam 7 SWI-Prolog Solutions b.v. 8 All rights reserved. 9 10 Redistribution and use in source and binary forms, with or without 11 modification, are permitted provided that the following conditions 12 are met: 13 14 1. Redistributions of source code must retain the above copyright 15 notice, this list of conditions and the following disclaimer. 16 17 2. Redistributions in binary form must reproduce the above copyright 18 notice, this list of conditions and the following disclaimer in 19 the documentation and/or other materials provided with the 20 distribution. 21 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 POSSIBILITY OF SUCH DAMAGE. 34*/ 35 36:- module(jquery, []). 37:- use_module(library(http/html_head)). 38:- use_module(library(http/http_server_files), []). 39:- use_module(library(settings)). 40:- use_module(library(broadcast)). 41 42:- setting(version, atom, 'jquery-3.6.0.min.js', 43 'File name for jquery.js, served as HTML resource "jquery"'). 44 45/** <module> Provide JQuery 46 47This module provides the HTML resource `jquery`. To get the default 48version of jquery included in a web page, make sure this file is loaded 49and include the following into the HTML generation DCG. 50 51``` 52 html_requires(jquery), 53``` 54 55The file served is determined by the setting `jquery:version` and loaded 56from the file search path `js`, the default for which is provided by 57library(http/http_server_files). 58 59Note that including jquery into the HTTP infrastructure is not ideal. 60However, components, such as PlDoc and Pengines as well as user 61applications require jquery, causing this JavaScript to be installed in 62many places. That is even worse. 63 64# Using your own copy 65 66To use your own copy of jquery, add your jquery file to a directory in 67the `js` file search path (see file_search_path/2 and 68absolute_file_name/3) and set `jquery:version` to the file version you 69provided. Alternatively, you can define the html resource `jquery` 70before loading this file. 71*/ 72 73jquery_dir('web/js'). 74 75global_jquery :- 76 jquery_dir(JQuery), 77 is_absolute_file_name(JQuery). 78 79:- if(global_jquery). 80:- multifile user:file_search_path/2. 81user:file_search_path(js, Dir) :- 82 jquery_dir(Dir). 83:- endif. 84 85ensure_jquery :- 86 html_current_resource(jquery), 87 !. 88ensure_jquery :- 89 register_jquery. 90 91register_jquery :- 92 setting(version, JQuery), 93 html_resource(jquery, 94 [ virtual(true), 95 requires([ js(JQuery) 96 ]) 97 ]). 98 99:- initialization ensure_jquery. 100:- listen(settings(changed(jquery:version, _, _)), 101 register_jquery).