Generating JavaScript

Uray Web Library (UWeb)

Uray M. János © 2022-2024

To use the dynamic features of UWeb (such as actions, forms, AJAX – see also active.php), the corresponding JavaScript code must be available for the generated HTML file. This documentation page describes the possible ways to make it available.

The simplest way to do this is to use the pregenerated JavaScript file. A more efficient solution is to generate a custom JavaScript file using UWeb.

Contents

Contents

1. Pregenerated JS file

2. Generating JS files

3. Configuration variables

3.1. Header comment

3.1.1. $title

3.1.2. $project

3.1.3. $author

3.1.4. $date

3.2. Forms

3.2.1. $has_form

3.2.2. $has_key

3.2.3. $has_first_action

3.2.4. $has_highlight

3.2.5. $has_get_value

3.2.6. $has_focus

3.2.7. $has_check

3.2.8. $has_details

3.2.9. $has_confirm

3.3. AJAX queries

3.3.1. $has_ajax

3.3.2. $has_component

3.3.3. $has_global

3.3.4. $has_param

3.3.5. $has_send_command

3.4. Popups, marks

3.4.1. $has_tooltip

3.4.2. $has_marks

3.4.3. $has_popup

3.4.4. $has_dropdown

3.4.5. $has_close_box

3.5. File handling

3.5.1. $has_upload

3.5.2. $has_load_file

3.5.3. $has_save_file

3.5.4. $has_select_file

3.5.5. $has_load_js

3.6. Miscellaneous

3.6.1. $has_scroll

3.6.2. $has_scroll_modes

3.6.3. $has_scroll_relative

3.6.4. $has_tabbed

3.6.5. $has_autogrow

3.6.6. $has_window_size

3.6.7. $lng_langs

3.7. create_*() functions

3.7.1. $has_create

3.7.2. $has_create_image

3.7.3. $has_create_link

3.7.4. $has_create_button

3.7.5. $has_create_input

3.7.6. $has_create_form

4. Adding custom JS

4.1. open()

4.2. close()

4.3. title()

4.4. include_file()

4.5. generate()

1. Pregenerated JS file

One JavaScript file is automatically generated by UWeb: all.js, which contains all JavaScript code for UWeb.

However, this file may be too big if the webpage needs only a part of it.

The intended way to use this file is to copy (or better, symlink) it next to the HTML file (renamed to e.g. uweb.js for clarity), and then set basic_page::js() to this file.

2. Generating JS files

There is a more efficient and flexible solution than using the pregenerated all.js file. Webpages can generate their own versions of the JavaScript file using the tools provided by UWeb. In this way, they can select exactly what functions they need, so the clients are not overloaded with unused code (as they could be with all.js). Webpages can also add new JavaScript code to the same generated file (under the same license as UWeb, i.e. GPLv3), which may improve load speed, compared to multiple separate JS files.

A JavaScript file can be generated by the following steps:

Here is a simple example:

<?php
// Configure the header comment
$project = "Some Website";
$author = "Uray M. János";
$date = "2024";

// Inclusion and configuration of components
$has_highlight = false;
$has_confirm = true;
$has_ajax = true;
$has_tooltip = true;
$has_upload = true;
$lng_langs = ["en", "hu"];

// Include UWeb's JavaScript templates
require_once __DIR__."/uweb/js.php";

// Add some custom JavaScript (optionally)
open();?><script>
function some_code () {
   do_something();
}
</script><?php close();

// Actually generate the content
generate();

3. Configuration variables

This section lists all the variables that can be used to control which parts are included in the generated JavaScript file, and possibly to customize these parts.

3.1. Header comment

The generated JavaScript file will start with a comment similar to this:

//
// JavaScript -- Some Website
// Generated by the Uray Web Library (UWeb).
// License: GPLv3 or later, see ...
// Uray M. János © 2024
//

Its content can be customized by the following variables.

3.1.1. $title

The title of the JavaScript file in the header comment.

Default value: "JavaScript"

3.1.2. $project

The title of the project name (website name) in the header comment, e.g. "Some Website".

This variable is required.

3.1.3. $author

The author of the project in the header comment.

Default value: "Uray M. János"

3.1.4. $date

The copyright date of the project in the header comment, e.g. "2024" or "2020-2024".

3.2. Forms

3.2.1. $has_form

Include JavaScript for UWeb forms (data-form).

Default value: true

3.2.2. $has_key

Include JavaScript for keyboard actions (data-key, add_key()).

Default value: false

3.2.3. $has_first_action

Include JavaScript for data-first-action.

Default value: true

3.2.4. $has_highlight

Include JavaScript for highlighting the submit button (data-highlight, highlight_submit()).

Default value: $has_form

3.2.5. $has_get_value

Include the JavaScript function get_value().

Default value: $has_form

3.2.6. $has_focus

Include JavaScript for auto-focus with the data-focus attribute.

Note: this attribute is also used by confirm_popup(), and by popup_*() in active.php.

Default value: true

3.2.7. $has_check

Include JavaScript for automatic checks in forms (data-check, data-required, uweb_check).

Default value: false

3.2.8. $has_details

Include JavaScript for data-details.

Default value: false

3.2.9. $has_confirm

Include JavaScript for confirmation popups: data-confirm, uweb_confirm, confirm_popup().

Note: this option requires $has_form.

Default value: false

3.3. AJAX queries

3.3.1. $has_ajax

Include JavaScript for AJAX.

Default value: false

3.3.2. $has_component

Include JavaScript for data-component.

Default value: $has_ajax

3.3.3. $has_global

Include JavaScript for data-global.

Default value: false

3.3.4. $has_param

Include JavaScript for data-param.

Default value: false

3.3.5. $has_send_command

Make send_command() work even when loading the page (i.e. outside an AJAX query).

Default value: false

3.4. Popups, marks

3.4.1. $has_tooltip

Include JavaScript for popup tooltips (data-tip). See also info_text() and qmark().

Note: tooltips also require $has_tooltip in CSS.

Default value: false

3.4.2. $has_marks

Include JavaScript for form marks, see show_mark(), <!mark> and input_error().

Default value: true

3.4.3. $has_popup

Include JavaScript for popup dialog boxes, see show_popup() and <!popup>.

Default value: true

3.4.4. $has_dropdown

Include JavaScript for dropdown boxes, see show_dropdown(), dropdown_menu() and data-action='dropdown'.

Note: dropdowns also require $has_dropdown in CSS.

Default value: false

3.4.5. $has_close_box

Include JavaScript for closing marks, popups etc., see close_box(), close_popup() and data-action='close'.

Default value: true if $has_marks, $has_popup or $has_dropdown

3.5. File handling

3.5.1. $has_upload

Include JavaScript for uploading in forms: data-upload.

Note: this option requires $has_form.

Default value: false

3.5.2. $has_load_file

Include the JavaScript function load_file().

Default value: false

3.5.3. $has_save_file

Include the JavaScript function save_file() and the AJAX command <!download>.

Default value: false

3.5.4. $has_select_file

Include the JavaScript functions select_file(), read_file() and open_file().

Default value: false

3.5.5. $has_load_js

Include the JavaScript function load_js().

Default value: false

3.6. Miscellaneous

3.6.1. $has_scroll

Include JavaScript for scrolling: scroll_to(), data-action='scroll'.

Default value: true

3.6.2. $has_scroll_modes

Include scrolling modes for scroll_to() (e.g. mode = "center"), and also data-action='scroll-center'.

Default value: false

3.6.3. $has_scroll_relative

Make scroll_to() scroll relatively to the innermost scrollable element, not to the full window. (This requires substantially more code for scroll_to()).

Default value: false

3.6.4. $has_tabbed

Include JavaScript for multi-tab content (class='tabbed-head' and class='tabbed-body'), see tabbed().

Note: this feature also requires $has_tabbed in CSS.

Default value: false

3.6.5. $has_autogrow

Include JavaScript for auto-growing text area: <textarea data-autogrow='true'>.

Default value: false

3.6.6. $has_window_size

Include the JavaScript function get_window_size().

Default value: false

3.6.7. $lng_langs

If this option is defined, the JavaScript function lng() will be included.

The value of this variable is an array of language codes, e.g. ["hu", "en"].

Default value: null (but ["hu", "en"] in all.js)

3.7. create_*() functions

3.7.1. $has_create

Include the create_*() JavaScript functions.

Default value: true

3.7.2. $has_create_image

Include the JavaScript functions create_image() and create_button_image() (the latter also requires $has_create_button).

Default value: false

Include the JavaScript functions create_link() and create_link_button() (the latter also requires $has_create_button).

Default value: $has_create

3.7.4. $has_create_button

Include the JavaScript function create_button() and other button-related create_*() functions (some of which may also require other options).

Default value: $has_create

3.7.5. $has_create_input

Include those create_*() functions which are related to form input elements, such as create_input(), create_textbox(), create_checkbox() etc.

Default value: $has_create

3.7.6. $has_create_form

Include the JavaScript function create_form().

Default value: true if both $has_create and $has_form are on

4. Adding custom JS

Custom JavaScript code can be inserted into the generated JS file like this:

open();?><script>
function some_code () {
   do_something();
}
</script><?php close();

The JavaScript code must be surrounded by the following code:

The custom JavaScript code may be split into any number of blocks by open()/close() pairs. Adjacent blocks will be separated by empty lines in the generated JS file. They may also be separated by comments for better readability, see title().

Note that since this custom JavaScript code will be added to the same file as UWeb's JavaScript, they will share the same license (GPLv3). If this is not desired, the custom JavaScript must be put into a separate file.

4.1. open()
function open ();

Open a custom JavaScript block. It must be closed by a subsequent close().

4.2. close()
function close ();

Close the custom JavaScript block opened by the last open().

4.3. title()
function title ($title);

Insert a title comment to the generated JS file. It can be used to separate custom JavaScript blocks.

For example, title("Initialization") generates the following comment (separated by two empty lines from the previous block and one empty line from the next one):

//
// Initialization
// --------------
//

Note: the title is only printed if there is any JavaScript block (open()/close()) following title() before the next title. This can be useful if the subsequent code appears conditionally, e.g. inside PHP if statements.

4.4. include_file()
function include_file ($js_file);

Insert the whole content of the given JavaScript file into the generated JS file.

4.5. generate()
function generate ();

Actually generate the JavaScript code.

This must be the last statement in the PHP file that generates the JS file. Without this, no output is produced.