From version < 16.2 >
edited by Sergiu Dumitriu
on 2012/10/25
To version < 17.1 >
edited by Sergiu Dumitriu
on 2012/10/25
< >
Change comment: Example of proper initialization using xwiki:dom:loaded and xwiki:dom:updated

Summary

Details

Page properties
Content
... ... @@ -33,7 +33,22 @@
33 33  * **##xwiki:dom:loading##**
34 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.
35 35  * **##xwiki:dom:updated##**
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.
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);
43 + })
44 + }
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]);
49 + }
50 +});
51 +{{/code}}
37 37  
38 38  {{warning}}
39 39  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:(((

XWiki JavaScript API

Observable XWiki Events

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.

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:

document.observe("xwiki:modulename:eventname", function(event) {
 // Here, do something that will be executed at the moment the event is fired
 doSomething();

 // The event can have an option memo object to pass to its observers some information:
 console.log(event.memo.somethingINeedToKnow);
});

Check out the real examples below, or read more about Prototype.js's event system

DOM Events (xwiki.js)

  • xwiki:dom:loaded
    This event is similar to prototype's dom:loaded event, 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:
    document.observe("xwiki:dom:loaded", function(){
     // Initialization that can rely on the fact the DOM is XWiki-tranformed goes here.
    });
    It is recommended to bind startup scripts to this event instead of window.load or document.dom:loaded.
  • xwiki:dom:loading
    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.
  • xwiki:dom:updated
    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.

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:

function init() {
 // This is your initialization handler, that you generally hook to xwiki:dom:loaded
}
(XWiki && XWiki.domIsLoaded && init()) || document.observe("xwiki:dom:loaded", init);

Document content events (actionButtons.js)

  • xwiki:document:saved
    This event is sent after the document has been successfully saved in an asynchronous request (i.e. after clicking the Save and Continue button).
  • xwiki:document:saveFailed
    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.

Action events (actionButtons.js)

  • xwiki:actions:cancel
    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.
  • xwiki:actions:preview
    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.
  • xwiki:actions:save
    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:
    document.observe("xwiki:actions:save", function(event){
     var doContinue = event.memo['continue'];
     if (doContinue) {
       // do something specific
     }
    });

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.

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.

Document extra events (xwiki.js)

  • xwiki:docextra:loaded
    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.
    document.observe("xwiki:docextra:loaded", function(event){
      var tabID = event.memo.id;
      if (tabID == "attachments") {
        // do something with the attachments tab retrieved content.
        doSomething(event.memo.element);
       }
    });
  • xwiki:docextra:activated
    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

WYSIWYG events (XWikiWysiwyg.js)

WYSIWYG has it's own custom events list.

Suggest events (ajaxSuggest.js)

  • xwiki:suggest:selected (since 2.3)
    This event is fired on the target input when a value was selected.

Fullscreen events (fullScreenEdit.js)

  • xwiki:fullscreen:enter (since 3.0 M3) (fired before entering full screen editing)
  • xwiki:fullscreen:entered (since 2.5.1) (fired after entering full screen editing)
  • xwiki:fullscreen:exit (since 3.0 M3) (fired before exiting full screen editing)
  • xwiki:fullscreen:exited (since 2.5.1) (fired after exiting full screen editing)
  • xwiki:fullscreen:resized (since 2.5.1)

All events have the target DOM element in event.memo.target.

Annotations events (AnnotationCode/Settings jsx)

  • xwiki:annotations:filter:changed
  • xwiki:annotations:settings:loaded

Livetable events (livetable.js)

  • xwiki:livetable:newrow (event.memo.row holds the new row)
  • xwiki:livetable:loadingEntries (since 2.3 RC1)
  • xwiki:livetable:receivedEntries (since 2.3 RC1) (event.memo.data contains the received JSON data)
  • xwiki:livetable:loadingComplete (since 2.4 M1) (event.memo.status contains the response status code)
  • xwiki:livetable:displayComplete (since 2.4 M1)
  • xwiki:livetable:ready (since 2.4.4)
  • xwiki:livetable:loading (since 3.1.1) (should be used in place of xwiki:dom:loading to startup livetables)

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.

  • xwiki:dom:loaded
    This event is similar to prototype's dom:loaded event, 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:
    document.observe("xwiki:dom:loaded", function(){
     // Initialization that can rely on the fact the DOM is XWiki-tranformed goes here.
    });
    It is recommended to bind startup scripts to this event instead of window.load or document.dom:loaded.
  • xwiki:dom:loading
    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.
  • xwiki:dom:updated
    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:
    var init = function(elements) {
     // Search for special content to enhance in each DOM element in the "elements" list and enhance it
     elements.each(function(element) {
        element.select('.someBehavioralClass').each(function(item) {
          enhance(item);
        })
      }
    }
    ['xwiki:dom:loaded', 'xwiki:dom:updated'].each(function(eventName) {
     document.observe(eventName, function(event) {
        init(event.memo && event.memo.elements || [document.documentElement]);
      }
    });

    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:

    function init() {
     // This is your initialization handler, that you generally hook to xwiki:dom:loaded
    }
    (XWiki && XWiki.domIsLoaded && init()) || document.observe("xwiki:dom:loaded", init);

    Document content events (actionButtons.js)

    • xwiki:document:saved
      This event is sent after the document has been successfully saved in an asynchronous request (i.e. after clicking the Save and Continue button).
    • xwiki:document:saveFailed
      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.

    Action events (actionButtons.js)

    • xwiki:actions:cancel
      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.
    • xwiki:actions:preview
      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.
    • xwiki:actions:save
      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:
      document.observe("xwiki:actions:save", function(event){
       var doContinue = event.memo['continue'];
       if (doContinue) {
         // do something specific
       }
      });

    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.

    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.

    Document extra events (xwiki.js)

    • xwiki:docextra:loaded
      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.
      document.observe("xwiki:docextra:loaded", function(event){
        var tabID = event.memo.id;
        if (tabID == "attachments") {
          // do something with the attachments tab retrieved content.
          doSomething(event.memo.element);
         }
      });
    • xwiki:docextra:activated
      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

    WYSIWYG events (XWikiWysiwyg.js)

    WYSIWYG has it's own custom events list.

    Suggest events (ajaxSuggest.js)

    • xwiki:suggest:selected (since 2.3)
      This event is fired on the target input when a value was selected.

    Fullscreen events (fullScreenEdit.js)

    • xwiki:fullscreen:enter (since 3.0 M3) (fired before entering full screen editing)
    • xwiki:fullscreen:entered (since 2.5.1) (fired after entering full screen editing)
    • xwiki:fullscreen:exit (since 3.0 M3) (fired before exiting full screen editing)
    • xwiki:fullscreen:exited (since 2.5.1) (fired after exiting full screen editing)
    • xwiki:fullscreen:resized (since 2.5.1)

    All events have the target DOM element in event.memo.target.

    Annotations events (AnnotationCode/Settings jsx)

    • xwiki:annotations:filter:changed
    • xwiki:annotations:settings:loaded

    Livetable events (livetable.js)

    • xwiki:livetable:newrow (event.memo.row holds the new row)
    • xwiki:livetable:loadingEntries (since 2.3 RC1)
    • xwiki:livetable:receivedEntries (since 2.3 RC1) (event.memo.data contains the received JSON data)
    • xwiki:livetable:loadingComplete (since 2.4 M1) (event.memo.status contains the response status code)
    • xwiki:livetable:displayComplete (since 2.4 M1)
    • xwiki:livetable:ready (since 2.4.4)
    • xwiki:livetable:loading (since 3.1.1) (should be used in place of xwiki:dom:loading to startup livetables)

    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