View source with raw comments or as raw
    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, 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(jquery, []).   36:- use_module(library(http/html_head)).   37:- use_module(library(http/http_server_files), []).   38:- use_module(library(settings)).   39:- use_module(library(broadcast)).   40
   41:- setting(version, atom, 'jquery.min.js',
   42           'File name for jquery.js, served as HTML resource "jquery"').

Provide JQuery

This module provides the HTML resource jquery. To get the default version of jquery included in a web page, make sure this file is loaded and include the following into the HTML generation DCG.

  html_requires(jquery),

The file served is determined by the setting jquery:version and loaded from the file search path js, the default for which is provided by library(http/http_server_files).

Note that including jquery into the HTTP infrastructure is not ideal. However, components, such as PlDoc and Pengines as well as user applications require jquery, causing this JavaScript to be installed in many places. That is even worse.

Using your own copy

To use your own copy of jquery, add your jquery file to a directory in the js file search path (see file_search_path/2 and absolute_file_name/3) and set jquery:version to the file version you provided. Alternatively, you can define the html resource jquery before loading this file. */

   72jquery_dir('/usr/share/javascript/jquery').
   73
   74global_jquery :-
   75    jquery_dir(JQuery),
   76    is_absolute_file_name(JQuery).
   77
   78:- if(global_jquery).   79:- multifile user:file_search_path/2.   80user:file_search_path(js, Dir) :-
   81    jquery_dir(Dir).
   82:- endif.   83
   84register_jquery :-
   85    setting(version, JQuery0),
   86    backward_compatibility_hack(JQuery0, JQuery),
   87    html_resource(jquery,
   88                  [ virtual(true),
   89                    requires([ js(JQuery)
   90                             ])
   91                  ]).
   92
   93% Older versions of this library used only the jQuery version as setting
   94% value. As Debian provides central jQuery   files  and uses a different
   95% naming convention, we now use the complete file name. The first clause
   96% recognises the old setting.
   97
   98backward_compatibility_hack(Version, File) :-
   99    sub_atom(Version, 0, 1, _, C0),
  100    char_type(C0, digit),
  101    !,
  102    atomic_list_concat(['jquery-', Version, '.js'], File).
  103backward_compatibility_hack(File, File).
  104
  105:- if(\+html_current_resource(jquery)).  106:- initialization register_jquery.  107:- listen(settings(changed(jquery:version, _, _)),
  108          register_jquery).  109:- endif.