Changes for page XWiki JavaScript API

Last modified by Simon Urli on 2022/09/14

From version 16.1
edited by Sergiu Dumitriu
on 2012/10/25
Change comment: Document xwiki:dom:updated
To version 19.1
edited by Caleb James DeLisle
on 2013/12/05
Change comment: fixed

Summary

Details

Page properties
Title
... ... @@ -1,0 +1,1 @@
1 +XWiki JavaScript API
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.Sergiu
1 +XWiki.CalebJamesDeLisle
Content
... ... @@ -1,9 +1,9 @@
1 -{{box cssClass="floatinginfobox" title="**Contents**"}}{{toc/}}{{/box}}
1 +{{box cssClass="floatinginfobox" title="**Contents**"}}
2 +{{toc/}}
3 +{{/box}}
2 2  
3 -= XWiki JavaScript API =
5 += Observable XWiki Events =
4 4  
5 -== Observable XWiki Events ==
6 -
7 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 8  
9 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:
... ... @@ -18,23 +18,42 @@
18 18  });
19 19  {{/code}}
20 20  
21 -Check out the real examples below, or [[read more>>http://prototypejs.org/api/element/fire]] about Prototype.js's event system
21 +Check out the real examples below or read more about [[Prototype.js's event system>>http://prototypejs.org/doc/latest/dom/Element/fire/]].
22 22  
23 -=== DOM Events (xwiki.js) ===
23 +== DOM Events (xwiki.js) ==
24 24  
25 25  * **##xwiki:dom:loaded##**
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 follow:(((
27 -{{code}}
26 +This event is similar to [[prototype's dom:loaded event>>http://prototypejs.org/doc/latest/dom/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:(((
27 +{{code language="javascript"}}
28 28  document.observe("xwiki:dom:loaded", function(){
29 29   // Initialization that can rely on the fact the DOM is XWiki-tranformed goes here.
30 30  });
31 31  {{/code}})))
32 +(((
33 +{{info}}
34 +It is recommended to bind startup scripts to this event instead of ##window.load## or ##document.dom:loaded##.
35 +{{/info}}
36 +)))
32 32  
33 -**It is recommended to bind startup scripts to this event** instead of ##window.load## or ##document.dom:loaded##.
34 34  * **##xwiki:dom:loading##**
35 35  ##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.
36 36  * **##xwiki:dom:updated##**
37 -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.
41 +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:(((
42 +{{code language="javascript"}}
43 +var init = function(elements) {
44 + // Search for special content to enhance in each DOM element in the "elements" list and enhance it
45 + elements.each(function(element) {
46 + element.select('.someBehavioralClass').each(function(item) {
47 + enhance(item);
48 + });
49 + });
50 +}
51 +['xwiki:dom:loaded', 'xwiki:dom:updated'].each(function(eventName) {
52 + document.observe(eventName, function(event) {
53 + init(event.memo && event.memo.elements || [document.documentElement]);
54 + });
55 +});
56 +{{/code}}
38 38  
39 39  {{warning}}
40 40  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:(((
... ... @@ -46,7 +46,7 @@
46 46  {{/code}})))
47 47  {{/warning}}
48 48  
49 -=== Document content events (actionButtons.js) ===
68 +== Document content events (actionButtons.js) ==
50 50  
51 51  * **##xwiki:document:saved##**
52 52  This event is sent after the document has been successfully saved in an asynchronous request (i.e. after clicking the //Save and Continue// button).
... ... @@ -53,7 +53,7 @@
53 53  * **##xwiki:document:saveFailed##**
54 54  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##.
55 55  
56 -=== Action events (actionButtons.js) ===
75 +== Action events (actionButtons.js) ==
57 57  
58 58  * **##xwiki:actions:cancel##**
59 59  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.
... ... @@ -69,12 +69,16 @@
69 69   }
70 70  });
71 71  {{/code}})))
72 -
73 -{{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}}
74 -
91 +(((
92 +{{warning}}
93 +While most properties can be accessed as ##event.memo.property##, this doesn't work with ##event.memo.continue## since ##continue## is a reserved keyword.
94 +{{/warning}}
95 +)))
96 +(((
75 75  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.
98 +)))
76 76  
77 -=== Document extra events (xwiki.js) ===
100 +== Document extra events (xwiki.js) ==
78 78  
79 79  * **##xwiki:docextra:loaded##**
80 80  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.(((
... ... @@ -91,16 +91,16 @@
91 91  * **##xwiki:docextra:activated##**
92 92  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##
93 93  
94 -=== WYSIWYG events (XWikiWysiwyg.js) ===
117 +== WYSIWYG events (XWikiWysiwyg.js) ==
95 95  
96 96  WYSIWYG has it's own custom [[events list>>extensions:Extension.WYSIWYG Editor Module#HCustomevents]].
97 97  
98 -=== Suggest events (ajaxSuggest.js) ===
121 +== Suggest events (ajaxSuggest.js) ==
99 99  
100 100  * **##xwiki:suggest:selected##** (since 2.3)
101 101  This event is fired on the target input when a value was selected.
102 102  
103 -=== Fullscreen events (fullScreenEdit.js) ===
126 +== Fullscreen events (fullScreenEdit.js) ==
104 104  
105 105  * **##xwiki:fullscreen:enter##** (since 3.0 M3) (fired before entering full screen editing)
106 106  * **##xwiki:fullscreen:entered##** (since 2.5.1) (fired after entering full screen editing)
... ... @@ -110,12 +110,12 @@
110 110  
111 111  All events have the target DOM element in ##event.memo.target##.
112 112  
113 -=== Annotations events (AnnotationCode/Settings jsx) ===
136 +== Annotations events (AnnotationCode/Settings jsx) ==
114 114  
115 115  * **##xwiki:annotations:filter:changed##**
116 116  * **##xwiki:annotations:settings:loaded##**
117 117  
118 -=== Livetable events (livetable.js) ===
141 +== Livetable events (livetable.js) ==
119 119  
120 120  * **##xwiki:livetable:newrow##** (##event.memo.row## holds the new row)
121 121  * **##xwiki:livetable:loadingEntries##** (since 2.3 RC1)
... ... @@ -126,3 +126,15 @@
126 126  * **##xwiki:livetable:loading##** (since 3.1.1) (should be used in place of ##xwiki:dom:loading## to startup livetables)
127 127  
128 128  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##.
152 +
153 += RequireJS and jQuery APIs =
154 +
155 +By default XWiki uses PrototypeJS which is bound to the $ symbol. Starting in XWiki 5.2, you may use jQuery by //requiring// it using the [[RequireJS>>http://requirejs.org/]] AMD standard. To do this you would write your code as follows:
156 +
157 +{{code language="javascript"}}
158 +require(['jquery'], function ($) {
159 + $('#xwikicontent').append('<p>Inside of this function, $ becomes jquery!</p>');
160 +});
161 +{{/code}}
162 +
163 +The best part is, any scripts which are loaded using require are loaded //asynchronously// (all at the same time) and if they are not required, they are never loaded at all.

Get Connected