Multiple Ckeditor using javascript Class

Eddy Goh

I try to use CKEDITOR to edit my website content. But there are a lot of CKEDITOR used, I put all the javascript functions in a Class, but not sure how it works. Anybody could help to see which part i need to change?

There are 2 files associated with my problems:

1) test.php (main page for update the content of website using Ckeditor)

2) CmsAjaxClass.js (Class contains all operation of Ajax and Ckeditor)

test.php

<!DOCTYPE html>
<html>
    <head>
        <title>A Simple Page with CKEditor</title>
        <!-- Make sure the path to CKEditor is correct. -->
        <script src="ckeditor/ckeditor.js"></script>
    </head>
    <script src="assets/js/jquery.js" type="text/javascript"></script> 
    <body>
        <div>
        </div>
        <form method="post" action="test2.php">
            <h1>Editor1</h1>
            <textarea name="editor1" id="editor1"><?php
                    include_once('php/get_cms.php');
                    echo get_cms(1);
                ?></textarea>
            _____________________________________<br/>
            <h1>Editor2</h1>
            <textarea name="editor2" id="editor2"><?php
                    include_once('php/get_cms.php');
                    echo get_cms(2);
                ?></textarea>
            <script src="assets/js/CmsAjaxClass.js"></script>
            <script>
                var editor1 = new CmsAjaxClass("editor1", document.getElementById("editor1").value) ;
                var editor2 = new CmsAjaxClass("editor2", document.getElementById("editor2").value) ;
            </script>
        </form>
    </body>
</html>

CmsAjaxClass.js

function CmsAjaxClass(editorName, seteditorData)
{
    //For nested Class usage
    var myClass = this;

    //Declare editorName to keep the editor name
    this.editorName = editorName;

    //Declare dataString to keep the data retrieve from editor
    this.seteditorData = seteditorData;

    //Function to update the CKEDITOR before it is parsed
    this.updateCkeditor = function() {
        for ( instance in CKEDITOR.instances )
        {
            CKEDITOR.instances[instance].updateElement();
        }
    }

    //Function to run the AJAX if the element is blured
    this.onBlur = function() {  
        //Update the Ckeditor data first before retrieve edited data
        myClass.updateCkeditor();

        //Retrieve edited data from HTML DOM
        myClass.seteditorData = seteditorData;

        //Call AJAX function to pass the value
        myClass.startAjax();
    }

    //Catch the focus and blur status of inline toolbar
    this.editor = CKEDITOR.inline( editorName, {
        on: {
            //focus: onFocus,
            blur: myClass.onBlur,
            }
    });

    //Declare which cms section we currently editing, parse the integer from the id of the textarea
    this.setcmsID = parseInt(editorName.replace("editor",""));

    //Create an AJAX function
    this.startAjax = function() {
        alert(this.seteditorData);
        $.ajax
        ({
            type: "POST",
            url: "test2.php",
            data: {editorData: this.seteditorData, cmsID: this.setcmsID},
            cache: false,
            beforeSend: function()
            {
                //Loading
            },
            success: function(result)
            {
                alert("Successfully updated!");
            }
        });
    }
}

enter image description here

Eddy Goh

Thanks @sabithpocker and @DaniëlKnippers Finally it works, the Ckeditor update database when "blur" action is triggered.

test.php

<!DOCTYPE html>
<html>
    <head>
        <title>A Simple Page with CKEditor</title>
        <!-- Make sure the path to CKEditor is correct. -->
        <script src="ckeditor/ckeditor.js"></script>
    </head>
    <script src="assets/js/jquery.js" type="text/javascript"></script> 
    <body>
        <div>
        </div>
        <form method="post" action="test2.php">
            <h1>Editor1</h1>
            <textarea name="editor1" id="editor1"><?php
                    include_once('php/get_cms.php');
                    echo get_cms(1);
                ?></textarea>
            _____________________________________<br/>
            <h1>Editor2</h1>
            <textarea name="editor2" id="editor2"><?php
                    include_once('php/get_cms.php');
                    echo get_cms(2);
                ?></textarea>
            <script src="assets/js/CmsAjaxClass.js"></script>
            <script>
                var editor1 = new CmsAjaxClass("editor1") ;
                var editor2 = new CmsAjaxClass("editor2") ;
            </script>
        </form>
    </body>
</html>

CmsAjaxClass.js

function CmsAjaxClass(editorName)
{
    //For nested Class usage
    var myClass = this;

    //Declare editorName to keep the editor name
    this.editorName = editorName;

    //Declare dataString to keep the data retrieve from editor
    this.seteditorData;

    //Function to update the CKEDITOR before it is parsed
    this.updateCkeditor = function() {
        for ( instance in CKEDITOR.instances )
        {
            CKEDITOR.instances[instance].updateElement();
        }
    }

    //Function to run the AJAX if the element is blured
    this.onBlur = function() {  
        //Update the Ckeditor data first before retrieve edited data
        myClass.updateCkeditor();

        //Retrieve edited data from HTML DOM
        myClass.seteditorData = document.getElementById(editorName).value;

        //Call AJAX function to pass the value
        myClass.startAjax();
    }

    //Catch the focus and blur status of inline toolbar
    this.editor = CKEDITOR.inline( editorName, {
        on: {
            //focus: onFocus,
            blur: myClass.onBlur,
            }
    });

    //Declare which cms section we currently editing, parse the integer from the id of the textarea
    this.setcmsID = parseInt(editorName.replace("editor",""));

    //Create an AJAX function
    this.startAjax = function() {
        alert(this.seteditorData);
        $.ajax
        ({
            type: "POST",
            url: "test2.php",
            data: {editorData: this.seteditorData, cmsID: this.setcmsID},
            cache: false,
            beforeSend: function()
            {
                //Loading
            },
            success: function(result)
            {
                alert("Successfully updated!");
            }
        });
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Adding multiple class using ng-class

From Dev

CKEditor multiple style class not working

From Dev

Animation using Class Name in Javascript

From Dev

Install CKeditor using Bower

From Dev

Changing content of class using Javascript

From Dev

Multiple iterators for class using Iterable

From Dev

using keyword this with multiple constructors in a Class

From Dev

AngularJS Multiple class using expression

From Dev

How to replace a class by using javascript when multiple classes present

From Dev

Changing fontSize for a class using Javascript

From Dev

Using ckeditor in jsf page

From Dev

CKEditor Image Upload Not working using javascript

From Dev

How to insert data into Yii framework 2.0 CKEditor extension using JavaScript?

From Dev

Upload files using CKeditor

From Dev

Set Multiple HTML Attributes Using Class Attributes in JavaScript

From Dev

CKEditor: multiple widget templates

From Dev

Changing content of class using Javascript

From Dev

Multiple iterators for class using Iterable

From Dev

using keyword this with multiple constructors in a Class

From Dev

AngularJS Multiple class using expression

From Dev

JavaScript print for CKEditor content

From Dev

Javascript error when using CKEditor in JSF page

From Dev

Using ckeditor in jsf page

From Dev

Upload files using CKeditor

From Dev

Using jQuery in javascript class

From Dev

How to scraping there are not class using javascript

From Dev

using a setInterval within a class and then making multiple of those classes javascript

From Dev

How to add content in ckeditor using jQuery or JavaScript?

From Dev

Call Specific Javascript/Jquery Function On multiple Images without using id or class attribute

Related Related

HotTag

Archive