3

I've created a custom ribbon button where i load external content into a Modal Dialog. I want to insert some text on the editor and close the dialog. The function OnPopClosed is running when I click something on my dialog but I'm having an error: RTE is not defined, so I can't insert anything on the editor. Any ideas?

function init(){
    var options = SP.UI.$create_DialogOptions();
    options.title = "Sharepoint Plugin";
    options.width = 600;
    options.height = 400;
    options.url = '/_Layouts/Test/Test.aspx';
    options.dialogReturnValueCallback = OnPopClosed;
    SP.UI.ModalDialog.showModalDialog(options);
}
function OnPopClosed(test) {

    var range = RTE.Cursor.get_range();
    range.deleteContent();
    var selection = range.parentElement();
    if (!selection) {
        return;
    }

    range.insertNode(elem);
    RTE.Cursor.get_range().moveToNode("test");
    RTE.Cursor.update();

    SP.UI.ModalDialog.commonModalDialogClose(1, "test");
}

2 Answers 2

1

Place RTE actions in SP.SOD.executeOrDelayUntilScriptLoaded in order to execute the specified function if the file containing it is loaded (in your case sp.ui.rte.js):

ExecuteOrDelayUntilScriptLoaded(RTEActions, "sp.ui.rte.js");

Example:

function RTEActions()
{
    var range = RTE.Cursor.get_range();
    //Place your remaining code here...
    SP.UI.ModalDialog.commonModalDialogClose(1, "test");
}

then declare the dialog handler like this:

function OnPopClosed() {
   ExecuteOrDelayUntilScriptLoaded(RTEActions, "sp.ui.rte.js");
}
3
  • It worked ! Actually I got over that RTE is undefined error, but I got another one that I'm going to post in a bit
    – nhenrique
    Commented Sep 4, 2013 at 9:04
  • Thanks for this answer! You might know the process to insert content into the editor after closing a Modal Dialog so if you could have a look at my new issue I'll appreciate. link
    – nhenrique
    Commented Sep 4, 2013 at 9:12
  • I'm glad I was able to help ;) Commented Sep 4, 2013 at 9:16
0

The RTE only exists inside the editor, and your function OnPopClosed runs after the popup is closed. You need to run your script on Test.aspx rather than whatever page you're launching the dialog from.

4
  • I'm "inside the editor". What I do is to click on the editor, then click on my custom ribbon button. A window popups up, and then I just want to return a value to be pasted wherever the cursor is on the editor. I tried to put all the javascript code on Test.aspx but it doesn't make real sense since that file is the page for the modal dialog.
    – nhenrique
    Commented Sep 3, 2013 at 15:57
  • I meant to type "inside the dialog", sorry about that. And you're right, test.aspx is clearly the dialog url. What a worthless answer! Moving on: Is RTE undefined in the init function?
    – Chloraphil
    Commented Sep 3, 2013 at 19:13
  • Also, why the last line (SP.UI.ModalDialog.commonModalDialogClose... ) in function OnPopClosed? This function will be called when the dialog is closed.
    – Chloraphil
    Commented Sep 3, 2013 at 19:14
  • Yeah I was also a bit confused. I've been able to fix this with the solution that @Vadim posted. Thanks!
    – nhenrique
    Commented Sep 4, 2013 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.