User:PerfektesChaos/js/preferencesGadgetOptions

Library with JavaScript functions to support configurable gadgets.

Core issue is an interactive dialog form easy to create on. Utilities are also manipulating the MediaWiki user preferences. Gadget options are available for anonymous users, too.

Features
Storing of information is done via API on MediaWiki server.

For retrieving the options set of the current page is evaluated, which has been transferred recently. However, an option might have been changed on a different page in another browser window.

Gadget configuration by GUI
From the form information a GUI is built on on the fly. If confirmed by + the last desires of the user will be stored as preferences and may be retrieved on any other page.

Project wide defined tools listed on MediaWiki:Gadgets-definition appear on anyway. If a configuration dialog has been defined and the user selected this particular tool, a button is added to the existing entry.

User scripts which are not registered as project gadgets get their own section on top of the special page.

Even if no configuration is supported, a link to documentation page may be displayed, and correct installation and presence is confirmd by visible entry.

Type safe user JS
The MediaWiki supports only string values. Since  shows very different behaviour on control structures than   (and   is not   nor is   the same as  ) single values are returned as stored.

Entire option sets of a gadget are stored by JSON which keeps type.

Preferences
Some utility functions support common MediaWiki user preferences: Only strings may be assigned as MediaWiki standard preferences, and the value has to be valid as if entered on Special:Preferences.
 * String setting
 * Updating

Updating
It is possible to update a particular option within the current page. The page might have been delivered some time ago, while the value has been changed within another browser window.

Callback
If wiki server needs to be contacted, the return values of functions are not the ultimate responses. If an updated value is to be retrieved from server, it will arrive with some delay on a callback function. Notification about successful storage may be requested as well as alerting on exchange errors.

User defined preferences storing
Currently the following component range is used for storing of preferences other than from MediaWiki extensions:
 * gadgetID

This might change one day, perhaps
 * gadgetID

The script maintains independent access to this storage, even automatic migration.

Data consumption

 * Every time a user is requesting a page (after login), the entire set of options is transferred from server. No caching on local browser storage is done.
 * For better management and faster network communication there is only one object with the entire option set for each gadget.
 * This is much more efficient than a gadget identifier combine with a single option name for each value.
 * It also enables easy combination with an entire set of default values modified by user preferences.
 * The amount of data which needs to be transferred every time should be kept small. Persistent configuration information should be put into specific configuration script pages hold by browser cache, or put into localStorage/sessionStorage (Web Storage) after first access.

Anonymous users
While preferences and gadget options are stored on a wiki server if login performed, anonymous users may recall gadget options at least within the same browser profile.

The web storage feature, which is available on all recent major browsers will save information between pages and sessions.

Anonymous users may use greasemonkey to trigger any script.

Gadget maintainers need not to know whether options have been saved on server or locally.

Installation
Gadget programmers need to wait for correct installation of this script before any function can be used.

If the gadget decided that the library is needed, the following code will load the source if not yet present. The state of this script is checked first to avoid repeated loading. Another gadget which also uses this script library might be loading already. Therefore the state is set to  immediately.

A callback function will be executed as soon as loading has been completed, or right now, if already available. All functionality relying on the library is to be accessed within the callback function. The callback function will receive the library object as first parameter.

API
After the    has been triggered functions may be called. The application object is the parameter of the mw.hook callback function and should be identical with.

All functions are components of the application object.

.fetch
Retrieve a “Gadgets” option set stored by  or.

This functionality is also available as hook.

.form
Show gadget (entry, and dialog) on. If already registered by project, only append button to open form, if any. This functionality is also available as hook.

.forward
Store a "Gadgets" options object. This functionality is also available as hook.

.$button
Retrieve a jQuery button in current design, opening the. This functionality is also available as hook.

.get
Retrieve a typesafe USERJS option value stored by .put.

.put
Store a typesafe USERJS option value. Supposed to be retrieved by .get.

.remove
Remove a user option entry from preferences.

.string
Store a string value of a MEDIAWIKI option.

.update
Update the value of a MEDIAWIKI option.

GUI for
User and project defined gadgets may be advertised and configured on page. If the  is specified a button is appended to open a dialog form, which may be closed again later. From the following components all but  are optional; but some more than the gadget ID only make look the GUI pretty and functional.
 * If already defined by MediaWiki:Gadgets-definition and chosen by user, only append button to open form, if any.
 * If user script has not been registered as project gadget a particular section will be inserted on top of the special page. Every unregistered user script gets an entry and may link to its documentation page.
 * The form provides a submit button + that launches a page and server update if clicked.

Callback function
If desired a user defined function is called as soon as an action on Wiki server has been completed. These functions share the following specification:
 * Called with one parameter:
 * For a function of successful execution this is the current option value.
 * For a function in case of failure it is a string with best error description available.
 * is the application object.

Synchronisation
For vulnerable operations it might be crucial that configuration has not been changed within another window since the page has been displayed. By the updating function and callback functions it can be ensured that operations are not executed before the most recent state was taken into account.

Waiting for availability
It might happen that this library is unavailable or could crash due to syntax error or other situation.
 * The success function of  should check first availability of the object component   and the desired function components. If those are missing it needs to be continued with standard values without supporting specific user options.
 * This might be logged on console to help quick remediation.

Configuration of special page
For the entire project the default rearrangement of might be influenced by project administration, in particular by MediaWiki:Common.js which is executed in advance.

Change of project configuration by gadgets is not desired and might be treated by sysops as misuse.

Instead of default styles jQuery objects will be used if present. The following code could be completed to provide project specific design:

The default design may be taken from the following CSS:

Hooks
The following identifiers are registered utilizing  system: when library has been provided and initialized, and user options are available. The parameter is the application library. Request for a  object stored by   or  ; parameters: Like ; parameters: Like ; parameters: Request for a [options button] design; parameters: Hook listeners give the opportunity to fire and forget requests, with no need to wait until library has loaded.
 * 1) Fired:
 * 1) Listening:
 * 1) **  – string with gadget identification
 * 2) **  – callback function, retrieves   result when available
 * 3) **  – optional fallback value object, if undefined or failure
 * 1) **  – object with gadget description
 * 1) **  – string with gadget identification
 * 2) **  – options object (not null)
 * 3) **  – optional callback function, or false
 * 4) **  – optional callback function, or false
 * 1) **  – callback function, retrieves jQuery button when available
 * 2) **  – optional string with gadget name

Known applications

 * ../browserStorageManager/
 * ../citoidWikitext/
 * ../clickDivertimento/
 * ../externalLinkProblem/
 * ../idResolver/
 * ../keyboardMapper/
 * ../lintHint/
 * ../listPageOptions/
 * ../loadResourceFile/
 * paneMarker
 * resultListSort
 * ../WikiSyntaxTextMod/

Other languages
This gadget would be prepared for multilingual support but does not need any.
 * All language dependent strings are provided by the application if correctly used.
 * RTL of entire page is automatically taken into account for element arrangement.
 * If the application is not configured well red error messages in English might appear. These should be fixed by the programmer first.

Translations of this documentation are welcome.
 * German version

de:User:PerfektesChaos/js/preferencesGadgetOptions