Events

Description

The events section allows to change all the functions pertaining to the events.

  • onCollaborativeChanges - the function called when the document is co-edited by the other user in the strict co-editing mode.

    Example
    var onCollaborativeChanges = function () {
        console.log("The document changed by collaborative user");
    };
    
    var docEditor = new DocsAPI.DocEditor("placeholder", {
        events: {
            "onCollaborativeChanges": onCollaborativeChanges,
            ...
        },
        ...
    });
    
  • onDocumentStateChange - the function called when the document is modified. It is called with the parameter: {"data" : true} when the current user is editing the document and with the parameter: {"data" : false} when the current user's changes are sent to the document editing service.

    Example
    var onDocumentStateChange = function (event) {
        if (event.data) {
            console.log("The document changed");
        } else {
            console.log("Changes are collected on document editing service");
        }
    };
    
    var docEditor = new DocsAPI.DocEditor("placeholder", {
        events: {
            "onDocumentStateChange": onDocumentStateChange,
            ...
        },
        ...
    });
    
  • onDownloadAs - the function called with the link to the edited file when the downloadAs method is being called. The link to the document to be downloaded is sent in the "data" parameter.

    Example
    var onDownloadAs = function (event) {
        console.log("ONLYOFFICE Document Editor create file: " + event.data);
    };
    
    var docEditor = new DocsAPI.DocEditor("placeholder", {
        events: {
            "onDownloadAs": onDownloadAs,
            ...
        },
        ...
    });
    
  • onError - the function called when an error or some other specific event occurs. The error message is sent in the "data" parameter.

    Example
    var onError = function (event) {
        console.log("ONLYOFFICE Document Editor reports an error: " + event.data);
    };
    
    var docEditor = new DocsAPI.DocEditor("placeholder", {
        events: {
            "onError": onError,
            ...
        },
        ...
    });
    
  • onOutdatedVersion - the function called after the error is shown, when the document is opened for editing with the old document.key value, which was used to edit the previous document version and was successfully saved. When this event is called the editor must be reinitialized with a new document.key.

    Example
    var onOutdatedVersion = function () {
        location.reload(true);
    };
    
    var docEditor = new DocsAPI.DocEditor("placeholder", {
        events: {
            "onOutdatedVersion": onOutdatedVersion,
            ...
        },
        ...
    });
    
  • onReady - the function called when the document is loaded into the document editor.

    Example
    var onReady = function() {
        console.log("ONLYOFFICE Document Editor is ready");
    };
    
    var docEditor = new DocsAPI.DocEditor("placeholder", {
        events: {
            "onReady": onReady,
            ...
        },
        ...
    });
    
  • onRequestEditRights - the function called when the user is trying to switch the document from the viewing into the editing mode by clicking the Edit Document button. When the function is called, the editor must be initialized again, in editing mode. If the method is not declared the Edit button will not be displayed.

    onRequestEditRights parameter is obligatory when the editorConfig mode parameter is set to view and the permission to edit the document (document permissions) is set to true so that the user could switch to the editing mode.
    Example
    var onRequestEditRights = function () {
        console.log("ONLYOFFICE Document Editor requests editing rights");
        document.location.reload();
    };
    
    var docEditor = new DocsAPI.DocEditor("placeholder", {
        events: {
            "onRequestEditRights": onRequestEditRights,
            ...
        },
        ...
    });
    
  • onRequestHistory - the function called when the user is trying to show the document version history by clicking the Version History button. To show the document version history you must call the refreshHistory method.

    Example
    var onRequestHistory = function() {
        docEditor.refreshHistory({
            "currentVersion": 2,
            "history": [
                {
                    "changes": changes, //the changes from the history object returned after saving the document
                    "created": "2010-07-06 10:13 AM",
                    "key": "af86C7e71Ca8",
                    "serverVersion": serverVersion, //the serverVersion from the history object returned after saving the document
                    "user": {
                        "id": "F89d8069ba2b",
                        "name": "Kate Cage"
                    },
                    "version": 1
                },
                {
                    "changes": changes,
                    "created": "2010-07-07 3:46 PM",
                    "key": "Khirz6zTPdfd7",
                    "user": {
                        "id": "78e1e841",
                        "name": "John Smith"
                    },
                    "version": 2
                },
                ...
            ]
        });
    };
    
    var docEditor = new DocsAPI.DocEditor("placeholder", {
        events: {
            "onRequestHistory": onRequestHistory,
            ...
        },
        ...
    });
    
  • onRequestHistoryData - the function called when the user is trying to click the specific document version in the document version history. To show the changes corresponding to the specific document version you must call the setHistoryData method. The document version number is sent in the "data" parameter.

    Example
    var onRequestHistoryData = function(event) {
        var version = event.data;
        docEditor.setHistoryData({
            "changesUrl": "http://example.com/url-to-changes.zip", //the changesUrl from the JSON object returned after saving the document
            "key": "Khirz6zTPdfd7",
            "previous": {
                "key": "af86C7e71Ca8",
                "url": "http://example.com/url-to-the-previous-version-of-the-document.docx"
            },
            "url": "http://example.com/url-to-example-document.docx",
            "version": version
        })
    };
    
    var docEditor = new DocsAPI.DocEditor("placeholder", {
        events: {
            "onRequestHistoryData": onRequestHistoryData,
            ...
        },
        ...
    });
    
    Where the example.com is the name of the the server where document manager and document storage service are installed. See the How it works section to find out more on Document Server service client-server interactions.
  • onRequestHistoryClose - the function called when the user is trying to go back to the document from viewing the document version history by clicking the Back to Document button. When the function is called, the editor must be initialized again, in editing mode. If the method is not declared the Back to Document button will not be displayed.

    Example
    var onRequestHistoryClose = function() {
        document.location.reload();
    };
    
    var docEditor = new DocsAPI.DocEditor("placeholder", {
        events: {
            "onRequestHistoryClose": onRequestHistoryClose,
            ...
        },
        ...
    });