Wiki source code of JavaScriptAPI

Version 17.2 by Sergiu Dumitriu on 2012/10/25

Hide last authors
Sergiu Dumitriu 10.1 1 {{box cssClass="floatinginfobox" title="**Contents**"}}{{toc/}}{{/box}}
Sergiu Dumitriu 9.1 2
3 = XWiki JavaScript API =
Jerome 1.1 4
Sergiu Dumitriu 9.1 5 == Observable XWiki Events ==
6
Jerome 1.1 7 Stay in touch with what happens in the wiki! XWiki will fire custom javascript events on certain moment and upon certain actions that occur in the navigation flow.
8
Sergiu Dumitriu 9.1 9 Event names are build on the following model: ##xwiki:modulename:eventname##. Your JavaScript script or extension can get notified of such an event the following way:
Jerome 1.1 10
Sergiu Dumitriu 10.1 11 {{code language="javascript"}}
12 document.observe("xwiki:modulename:eventname", function(event) {
13 // Here, do something that will be executed at the moment the event is fired
14 doSomething();
Jerome 1.1 15
Sergiu Dumitriu 10.1 16 // The event can have an option memo object to pass to its observers some information:
17 console.log(event.memo.somethingINeedToKnow);
Sergiu Dumitriu 9.1 18 });
19 {{/code}}
Jerome 1.1 20
Sergiu Dumitriu 9.1 21 Check out the real examples below, or [[read more>>http://prototypejs.org/api/element/fire]] about Prototype.js's event system
Jerome 1.1 22
Sergiu Dumitriu 9.1 23 === DOM Events (xwiki.js) ===
Jerome 1.1 24
Sergiu Dumitriu 11.1 25 * **##xwiki:dom:loaded##**
Sergiu Dumitriu 16.2 26 This event is similar to [[prototype's dom:loaded event>>http://www.prototypejs.org/api/document/observe]], with the difference that in the time-lapse between ##dom:loaded## and ##xwiki:dom:loaded##, XWiki may have transformed the DOM. Example of DOM transformations operated by XWiki is setting the right target of links that have rel="external" attribute so that the document can be XHTML valid and still have the desired effect, making internal rendering error messages expandable, insert document template handlers for links to non-existent documents, and so on. In the future there might be more transformations operated by XWiki upon DOM initialization. This event is meant for code to be notified of loading of the XWiki-transformed version of the initial DOM. As ##dom:loaded##, it can be used as follows:(((
Sergiu Dumitriu 17.2 27 {{code language="javascript"}}
Jerome 1.1 28 document.observe("xwiki:dom:loaded", function(){
Sergiu Dumitriu 10.1 29 // Initialization that can rely on the fact the DOM is XWiki-tranformed goes here.
Jerome 1.1 30 });
Sergiu Dumitriu 11.1 31 {{/code}})))
Sergiu Dumitriu 9.1 32 **It is recommended to bind startup scripts to this event** instead of ##window.load## or ##document.dom:loaded##.
Sergiu Dumitriu 11.1 33 * **##xwiki:dom:loading##**
Sergiu Dumitriu 9.1 34 ##xwiki:dom:loading## is sent between ##dom:loaded## and ##xwiki:dom:loaded##, before XWiki changes the DOM. This is the event that should start all scripts making important DOM changes that other scripts should see.
Sergiu Dumitriu 16.1 35 * **##xwiki:dom:updated##**
Sergiu Dumitriu 17.1 36 This event is sent whenever an important change in the DOM occurs, such as loading new content in a dialog box or tab, or refreshing the document content. Scripts that add behavior to certain elements, or which enhance the DOM, should listen to this event as well and re-apply their initialization process on the updated content, the same way that the whole DOM is enhanced on ##xwiki:dom:loaded##. The list of new or updated elements is sent in the ##event.memo.elements## property. For example:(((
37 {{code language="javascript"}}
38 var init = function(elements) {
39 // Search for special content to enhance in each DOM element in the "elements" list and enhance it
40 elements.each(function(element) {
41 element.select('.someBehavioralClass').each(function(item) {
42 enhance(item);
Sergiu Dumitriu 17.2 43 });
44 });
Sergiu Dumitriu 17.1 45 }
46 ['xwiki:dom:loaded', 'xwiki:dom:updated'].each(function(eventName) {
47 document.observe(eventName, function(event) {
48 init(event.memo && event.memo.elements || [document.documentElement]);
Sergiu Dumitriu 17.2 49 });
Sergiu Dumitriu 17.1 50 });
51 {{/code}}
Sergiu Dumitriu 1.5 52
Denis Gervalle 15.1 53 {{warning}}
54 If your script is loaded **deferred**, all these events may be triggered **before your script is executed** and therefore **before it has the ablity to observe these events**. Since 3.1.1, to prevent your handler to never being called, never use ##dom:loaded## anymore, and check ##XWiki.isInitialized## before waiting for ##xwiki:dom:loading##, and ##XWiki.domIsLoaded## before waiting for ##xwiki:dom:loaded##. If the flag is true, you should proceed immediately with your handler. Here is a simple construct to properly handle this:(((
55 {{code}}
56 function init() {
57 // This is your initialization handler, that you generally hook to xwiki:dom:loaded
58 }
59 (XWiki && XWiki.domIsLoaded && init()) || document.observe("xwiki:dom:loaded", init);
60 {{/code}})))
61 {{/warning}}
62
Sergiu Dumitriu 9.1 63 === Document content events (actionButtons.js) ===
Sergiu Dumitriu 3.1 64
Sergiu Dumitriu 10.1 65 * **##xwiki:document:saved##**
66 This event is sent after the document has been successfully saved in an asynchronous request (i.e. after clicking the //Save and Continue// button).
67 * **##xwiki:document:saveFailed##**
68 This event is sent when a save and continue attempt failed for some reason. The XMLHttpRequest response object is sent in the memo, as ##event.memo.response##.
Sergiu Dumitriu 3.1 69
Sergiu Dumitriu 9.1 70 === Action events (actionButtons.js) ===
Jerome 1.1 71
Sergiu Dumitriu 9.1 72 * **##xwiki:actions:cancel##**
Sergiu Dumitriu 3.2 73 This event is sent after the user clicks the "Cancel" button of an editor (Wiki, WYSIWYG, object, rights, etc.), but before actually cancelling the edit.
Sergiu Dumitriu 9.1 74 * **##xwiki:actions:preview##**
Guillaume Delhumeau 15.2 75 This event is sent after the user clicks the "Preview" button of an editor (Wiki, WYSIWYG, object, rights, etc.), but before actually leaving the edit mode.
Sergiu Dumitriu 11.1 76 * **##xwiki:actions:save##**
Sergiu Dumitriu 12.1 77 This event is sent after the user clicks the "Save" or "Save & Continue" button of an editor (Wiki, WYSIWYG, object, rights, etc.), but before actually submitting the form. A memo is available if you need to know if the intend is to continue after the save, in ##event.memo['continue']##. You can use it as follows:(((
Sergiu Dumitriu 10.1 78 {{code language="javascript"}}
Sergiu Dumitriu 12.1 79 document.observe("xwiki:actions:save", function(event){
80 var doContinue = event.memo['continue'];
Sergiu Dumitriu 10.1 81 if (doContinue) {
82 // do something specific
83 }
Jerome 1.1 84 });
Sergiu Dumitriu 11.1 85 {{/code}})))
Jerome 1.1 86
Sergiu Dumitriu 12.1 87 {{warning}}Caveat: While most properties can be accessed as ##event.memo.property##, this doesn't work with ##event.memo.continue## since ##continue## is a reserved keyword.{{/warning}}
88
Sergiu Dumitriu 10.1 89 All these events contain as extra information, in the second parameter sent to event listeners (the memo), the original click event (if any, and which can be stopped to prevent the action from completing), and the form being submitted, as ##event.memo.originalEvent##, and ##event.memo.form## respectively.
Sergiu Dumitriu 1.5 90
Sergiu Dumitriu 9.1 91 === Document extra events (xwiki.js) ===
Jerome 1.1 92
Sergiu Dumitriu 11.1 93 * **##xwiki:docextra:loaded##**
94 This event is fired upon reception of the content of a document footer tab by AJAX. This event is useful if you need to operate transformations of the received content. You can filter on which tab content to operate (comments or attachment or information or ...) using the event memo. The DOM element in which the retrieved content has been injected is also passed to facilitate transformations.(((
Sergiu Dumitriu 10.1 95 {{code language="javascript"}}
Jerome 1.1 96 document.observe("xwiki:docextra:loaded", function(event){
97 var tabID = event.memo.id;
98 if (tabID == "attachments") {
99 // do something with the attachments tab retrieved content.
100 doSomething(event.memo.element);
101 }
102 });
Sergiu Dumitriu 9.1 103 {{/code}}
Sergiu Dumitriu 10.1 104 )))
Sergiu Dumitriu 9.1 105 * **##xwiki:docextra:activated##**
106 This event is fired upon activation of a tab. It differs from the loaded event since tabs are loaded only once if the user clicks going back and forth between tabs. This event will notify of each tab activation, just after the tab content is actually made visible. The tab ID is passed in the memo as for ##xwiki:docextra:loaded##
Oana Florea 4.1 107
Sergiu Dumitriu 9.1 108 === WYSIWYG events (XWikiWysiwyg.js) ===
Ecaterina Moraru (Valica) 5.1 109
Vincent Massol 11.3 110 WYSIWYG has it's own custom [[events list>>extensions:Extension.WYSIWYG Editor Module#HCustomevents]].
Ecaterina Moraru (Valica) 5.1 111
Sergiu Dumitriu 9.1 112 === Suggest events (ajaxSuggest.js) ===
Ecaterina Moraru (Valica) 8.1 113
Sergiu Dumitriu 9.1 114 * **##xwiki:suggest:selected##** (since 2.3)
Sergiu Dumitriu 11.1 115 This event is fired on the target input when a value was selected.
Ecaterina Moraru (Valica) 8.1 116
Marius Dumitru Florea 14.1 117 === Fullscreen events (fullScreenEdit.js) ===
Oana Florea 4.1 118
Marius Dumitru Florea 14.1 119 * **##xwiki:fullscreen:enter##** (since 3.0 M3) (fired before entering full screen editing)
120 * **##xwiki:fullscreen:entered##** (since 2.5.1) (fired after entering full screen editing)
121 * **##xwiki:fullscreen:exit##** (since 3.0 M3) (fired before exiting full screen editing)
122 * **##xwiki:fullscreen:exited##** (since 2.5.1) (fired after exiting full screen editing)
123 * **##xwiki:fullscreen:resized##** (since 2.5.1)
Ecaterina Moraru (Valica) 6.1 124
Sergiu Dumitriu 11.1 125 All events have the target DOM element in ##event.memo.target##.
126
Sergiu Dumitriu 9.1 127 === Annotations events (AnnotationCode/Settings jsx) ===
Ecaterina Moraru (Valica) 6.1 128
Sergiu Dumitriu 9.1 129 * **##xwiki:annotations:filter:changed##**
130 * **##xwiki:annotations:settings:loaded##**
Ecaterina Moraru (Valica) 6.1 131
Sergiu Dumitriu 9.1 132 === Livetable events (livetable.js) ===
Ecaterina Moraru (Valica) 6.1 133
Sergiu Dumitriu 11.1 134 * **##xwiki:livetable:newrow##** (##event.memo.row## holds the new row)
Sergiu Dumitriu 9.1 135 * **##xwiki:livetable:loadingEntries##** (since 2.3 RC1)
Sergiu Dumitriu 11.1 136 * **##xwiki:livetable:receivedEntries##** (since 2.3 RC1) (##event.memo.data## contains the received JSON data)
137 * **##xwiki:livetable:loadingComplete##** (since 2.4 M1) (##event.memo.status## contains the response status code)
Sergiu Dumitriu 9.1 138 * **##xwiki:livetable:displayComplete##** (since 2.4 M1)
139 * **##xwiki:livetable:ready##** (since 2.4.4)
Denis Gervalle 15.1 140 * **##xwiki:livetable:loading##** (since 3.1.1) (should be used in place of ##xwiki:dom:loading## to startup livetables)
Sergiu Dumitriu 11.1 141
142 The livetable sends both generic events, named as above, and events specific to each livetable, containing the table name on the third position, such as ##xwiki:livetable:alldocs:loadingEntries##. The generic event has the table name in the memo, as ##event.memo.tableId##.

Get Connected