Friday, March 12, 2010

Making Sharepoint RTE aways create a popup window for editing

stick this hackjob of a function in your head tag and call it at the end of your document.

    function fixEditorButtons() {
        // rewire rich text editor buttons to popup source view RTE window in content mode
        var anchors = document.getElementsByTagName("A");
        if(anchors.length == 0) return;
       
        var rteArray = new Array();
       
        // gather up the rich text editor edit buttons
        for(var i = 0; i < anchors.length; i++) {
            if(anchors[i].attributes.getNamedItem("title") != null) {
                    if(anchors[i].attributes.getNamedItem("title").value == "Edit Content") {
                        rteArray.push(anchors[i]); }}}
        for(var i = 0; i < rteArray.length; i++) {
            var oEditButton = rteArray[i];
           
            // grab the code as SP created it
            var code = new String(oEditButton.onclick);

            // snag the control name of the RTE current display control
            var controlnameBeginIndex = code.search(/RTE2_LaunchEditor/) + 27;
            var temp = code.substring(controlnameBeginIndex);
            var controlnameEndIndex = temp.search(/\'/);
            var controlName = temp.substring(0,controlnameEndIndex) + "_displayContent";
       
            // find where we need to insert our additional code that creates the popup window
            var insertPoint = code.search(/cancelBubble/) - 67;
                       
            // build new code, start with old code
            var newcode = code.substring(0,insertPoint);
            newcode += "RTE2_SaveHtmlStateIfChanged('" + controlName + "');";
            newcode += "var instanceVariables=RTE_GetEditorInstanceVariables('"+ controlName +"');";
            newcode += "RTE2_LaunchEditor( instanceVariables.aSettings, instanceVariables.clientId,
                                        instanceVariables.displayContentElement.id, instanceVariables.emptyPanel.id,
                                        instanceVariables.hiddenInputField.id, instanceVariables.webUrl,
                                        'True', false,true);";

            newcode += code.substring(insertPoint); rteArray[i].onclick =
                                    Function(newcode.slice(20,newcode.length - 1));
        }
    }

No comments: