Search code examples
javascriptckeditorwysiwygckeditor4.x

Ckeditor custom plugin - dialog with radio buttons


I'm trying to build a custom plugin in CKEditor where a selection of an element in a radio buttons' list change the class of the selected element. Example :

Seleting BIG will add the class big, MEDIUM => med and SMALL => sml.

I'm blocked in the part where I have to retrieve the value of the selected element. Eveything else is going fine and I managed in the code bellow to apply a class "MYCLASS" to the closest li tag.

Question : How can I get the value of the selected radio button in the dialog element of the CKeditor ?

Here's the code :

CKEDITOR.dialog.add( 'MyDialog', function ( editor ) {
    function getListElement( editor, listTag ) {
      var range;
      try {
        range = editor.getSelection().getRanges()[ 0 ];
      } catch ( e ) {
        return null;
      }

      range.shrink( CKEDITOR.SHRINK_TEXT );
      return editor.elementPath( range.getCommonAncestor() ).contains( listTag, 1 );
    }

    return {
      title: 'Size of the element',
      minWidth: 400,
      minHeight: 200,
      contents: [
           {
               id: 'tab-basic',
               label: 'Size of an element',
               elements: [
                {
                  type: 'radio',
                  id: 'bullet-size',
                  label: 'Size of the bullets',
                  items: [ [ 'BIG', 'big' ], [ 'MEDIUM', 'mdm' ],[ 'SMALL', 'sml' ] ],
                  style: 'color: green',
                  'default': 'big',
                },
               ]
           },
       ],
       onOk: function() {

         var editor = this.getParentEditor(),
             element = getListElement( editor, 'ul' ),
             dialog = this,
             config = editor.config, 
             lang = editor.lang,
             style = new CKEDITOR.style(config.coreStyles_alpha);

         editor.attachStyleStateChange(style, function(state) {
           !editor.readOnly;
         });
         count = element.getChildren().count();
         for(k=1; k <= count; k++){
           element.getChild(k-1).setAttribute('class', 'MyClass');
         }
     }
    }
});

Solution

  • Here's the way to get the value. Inside the onOk function :

    var my_variable = this.getVazlueOf(Id_of_you_tab, id_of_the_radio_list);