CKEditor 4 Documentation

CKEDITOR.plugins.embedBase.baseDefinition

Hierarchy

CKEDITOR.plugins.widget.definition
CKEDITOR.plugins.embedBase.baseDefinition

Inherited mixins

Files

An embed widget base definition. It predefines a few widget definition properties such as mask, template and pathName and adds methods related to content embedding.

To create a base definition use the CKEDITOR.plugins.embedBase.createWidgetBaseDefinition method.

Note: For easier browsing of this class's API you can hide inherited method using the "Show" drop-down on the right-hand side.

Defined By

Properties

CKEDITOR.plugins.embedBase.baseDefinition
view source
: Objectprivate
Response cache. ...

Response cache. This cache object will be shared between all instances of this widget.

Defaults to: {}

HTML code that can be generated by this feature. ...

HTML code that can be generated by this feature.

For example a basic image feature (image button displaying the image dialog window) may allow 'img[!src,alt,width,height]'.

During the feature activation this value is passed to CKEDITOR.filter.allow.

Defaults to: null

The label for the widget toolbar button. ...

The label for the widget toolbar button.

editor.widgets.add( 'simplebox', {
    button: 'Create a simple box'
} );

editor.widgets.add( 'simplebox', {
    button: editor.lang.simplebox.title
} );
Feature content forms to be registered in the CKEDITOR.editor.filter during the feature activation. ...

Feature content forms to be registered in the CKEDITOR.editor.filter during the feature activation.

See CKEDITOR.filter.addContentForms for more details.

Defaults to: null

Transformations (usually for content generated by this feature, but not necessarily) that will be registered in the C...

Transformations (usually for content generated by this feature, but not necessarily) that will be registered in the CKEDITOR.editor.filter during the feature activation.

See CKEDITOR.filter.addTransformations for more details.

Defaults to: null

If set, it will be added as the CKEDITOR.plugins.widget.data event listener. ...

If set, it will be added as the CKEDITOR.plugins.widget.data event listener. This means that it will be executed every time the widget data changes.

The data object which will be used to populate the data of a newly created widget. ...

The data object which will be used to populate the data of a newly created widget. See CKEDITOR.plugins.widget.data.

defaults: {
    showCaption: true,
    align: 'none'
}
The name of a dialog window which will be opened on CKEDITOR.plugins.widget.edit. ...

The name of a dialog window which will be opened on CKEDITOR.plugins.widget.edit. If not defined, then the CKEDITOR.plugins.widget.edit method will not perform any action and widget's command will insert a new widget without opening a dialog window first.

The function to be used to downcast this widget or a name of the downcast option from the downcasts object. ...

The function to be used to downcast this widget or a name of the downcast option from the downcasts object.

The downcast funciton will be executed in the CKEDITOR.plugins.widget context and with widgetElement (CKEDITOR.htmlParser.element) argument which is the widget's main element.

The function may return an instance of the CKEDITOR.htmlParser.node class if the widget needs to be downcasted to a different node than the widget's main element.

The object containing functions which can be used to downcast this widget. ...

The object containing functions which can be used to downcast this widget. Only the one pointed by the downcast property will be used.

In most cases it is appropriate to use downcast directly, because majority of widgets have just one variant of downcasting (or none at all). However, in some cases the widget author may want to expose more than one variant and then this property may be used.

downcasts: {
    // This downcast may transform the widget into the figure element.
    figure: function() {
        // ...
    },
    // This downcast may transform the widget into the image element with data-* attributes.
    image: function() {
        // ...
    }
}

// Then, the widget user may choose one of the downcast options when setting up his editor.
editor.on( 'widgetDefinition', function( evt ) {
    if ( evt.data.name == 'image' )
            evt.data.downcast = 'figure';
} );
Whether widget should be draggable. ...

Whether widget should be draggable. Defaults to true. If set to false drag handler will not be displayed when hovering widget.

If set, it will be added as the CKEDITOR.plugins.widget.edit event listener. ...

If set, it will be added as the CKEDITOR.plugins.widget.edit event listener. This means that it will be executed when a widget is being edited. See the CKEDITOR.plugins.widget.edit method.

An object containing definitions of nested editables (editable name => CKEDITOR.plugins.widget.nestedEditable.definit...

An object containing definitions of nested editables (editable name => CKEDITOR.plugins.widget.nestedEditable.definition). Note that editables have to be defined in the same order as they are in DOM / template. Otherwise errors will occur when nesting widgets inside each other.

editables: {
    header: 'h1',
    content: {
        selector: 'div.content',
        allowedContent: 'p strong em; a[!href]'
    }
}
The function used to obtain an accessibility label for the widget. ...

The function used to obtain an accessibility label for the widget. It might be used to make the widget labels as precise as possible, since it has access to the widget instance.

If not specified, the default implementation will use the pathName or the main element tag name.

The method executed while initializing a widget, after a widget instance is created, but before it is ready. ...

The method executed while initializing a widget, after a widget instance is created, but before it is ready. It is executed before the first CKEDITOR.plugins.widget.data is fired so it is common to use the init method to populate widget data with information loaded from the DOM, like for exmaple:

init: function() {
    this.setData( 'width', this.element.getStyle( 'width' ) );

    if ( this.parts.caption.getStyle( 'display' ) != 'none' )
        this.setData( 'showCaption', true );
}
If set to true/false, it will force the widget to be either an inline or a block widget. ...

If set to true/false, it will force the widget to be either an inline or a block widget. If not set, the widget type will be determined from the widget element.

Widget type influences whether a block (div) or an inline (span) element is used for the wrapper.

The method to be executed when the widget's command is executed in order to insert a new widget (widget of this type ...

The method to be executed when the widget's command is executed in order to insert a new widget (widget of this type is not focused). If not defined, then the default action will be performed which means that:

If set to true, the widget's element will be covered with a transparent mask. ...

If set to true, the widget's element will be covered with a transparent mask. This will prevent its content from being clickable, which matters in case of special elements like embedded Flash or iframes that generate a separate "context".

Widget definition name. ...

Widget definition name. It is automatically set when the definition is registered.

Overrides: CKEDITOR.feature.name

An object containing definitions of widget components (part name => CSS selector). ...

An object containing definitions of widget components (part name => CSS selector).

parts: {
    image: 'img',
    caption: 'div.caption'
}

The widget name displayed in the elements path.

The widget name displayed in the elements path.

CKEDITOR.plugins.embedBase.baseDefinition
view source
: CKEDITOR.template
The template used to generate the URL of the content provider. ...

The template used to generate the URL of the content provider. Content provider is a service which the embed widget will request in order to get an oEmbed response that can be transformed into content which can be embedded in the editor.

Example content providers are:

Both Iframely and Noembed are proxy services which support JSONP requests, hence they are not limited by the same-origin policy. Unfortunately, usually oEmbed services exposed by real content providers like YouTube or Twitter do not support XHR with CORS or do not support oEmbed at all which makes it impossible or hard to get such content to be embedded in the editor. This problem is solved by proxy content providers like Iframely and Noembed.

This property must be defined after creating an embed widget base definition.

By default two values are passed to the template:

  • {url} – The URL of the resource to be embedded.
  • {callback} – The JSONP callback to be executed.

Example value:

widgetDefinition.providerUrl = new CKEDITOR.template(
    '//ckeditor.iframe.ly/api/oembed?url={url}&callback={callback}'
);
Minimal HTML code that this feature must be allowed to generate in order to work. ...

Minimal HTML code that this feature must be allowed to generate in order to work.

For example a basic image feature (image button displaying the image dialog window) needs 'img[src,alt]' in order to be activated.

During the feature validation this value is passed to CKEDITOR.filter.check.

If this value is not provided, a feature will be always activated.

Defaults to: null

Function transforming custom widget's CKEDITOR.style instance into CKEDITOR.filter.allowedContentRules. ...

Function transforming custom widget's CKEDITOR.style instance into CKEDITOR.filter.allowedContentRules. It may be used when a static styleableElements property is not enough to inform the CKEDITOR.filter what HTML features should be enabled when allowing the given style.

In most cases, when style's classes just have to be added to element name(s) used by the widget element, it is recommended to use simpler styleableElements property.

In order to get parsed classes from the style definition you can use CKEDITOR.style.customHandlers.widget.getClassesArray.

For example, if you want to use the object format of allowed content rules, to specify match validator, your implementation could look like this:

editor.widgets.add( 'customWidget', {
    // ...

    styleToAllowedContentRules: funciton( style ) {
        // Retrieve classes defined in the style.
        var classes = style.getClassesArray();

        // Do something crazy - for example return allowed content rules in object format,
        // with custom match property and propertiesOnly flag.
        return {
            h1: {
                match: isWidgetElement,
                propertiesOnly: true,
                classes: classes
            }
        };
    }
} );

Available since: 4.4

Parameters

Returns

Names of element(s) (separated by spaces) for which the CKEDITOR.filter should allow classes defined in the widget st...

Names of element(s) (separated by spaces) for which the CKEDITOR.filter should allow classes defined in the widget styles. For example if your widget is upcasted from a simple <div> element, then in order to make it styleable you can set:

editor.widgets.add( 'customWidget', {
    upcast: function( element ) {
        return element.name == 'div';
    },

    // ...

    styleableElements: 'div'
} );

Then, when the following style is defined:

{
    name: 'Thick border', type: 'widget', widget: 'customWidget',
    attributes: { 'class': 'thickBorder' }
}

a rule allowing the thickBorder class for div elements will be registered in the CKEDITOR.filter.

If you need to have more freedom when transforming widget style to allowed content rules, you can use the styleToAllowedContentRules callback.

Available since: 4.4

The template which will be used to create a new widget element (when the widget's command is executed). ...

The template which will be used to create a new widget element (when the widget's command is executed). This string is populated with default values by using the CKEDITOR.template format. Therefore it has to be a valid CKEDITOR.template argument.

The function to be used to upcast an element to this widget or a comma-separated list of upcast methods from the upca...

The function to be used to upcast an element to this widget or a comma-separated list of upcast methods from the upcasts object.

The upcast function is not executed in the widget context (because the widget does not exist yet) and two arguments are passed:

  • element (CKEDITOR.htmlParser.element) – The element to be checked.
  • data (Object) – The object which can be extended with data which will then be passed to the widget.

An element will be upcasted if a function returned true or an instance of a CKEDITOR.htmlParser.element if upcasting meant DOM structure changes (in this case the widget will be initialized on the returned element).

The upcast method(s) priority. ...

The upcast method(s) priority. The upcast with a lower priority number will be called before the one with a higher number. The default priority is 10.

Defaults to: 10

Available since: 4.5

The object containing functions which can be used to upcast this widget. ...

The object containing functions which can be used to upcast this widget. Only those pointed by the upcast property will be used.

In most cases it is appropriate to use upcast directly, because majority of widgets need just one method. However, in some cases the widget author may want to expose more than one variant and then this property may be used.

upcasts: {
    // This function may upcast only figure elements.
    figure: function() {
        // ...
    },
    // This function may upcast only image elements.
    image: function() {
        // ...
    },
    // More variants...
}

// Then, widget user may choose which upcast methods will be enabled.
editor.on( 'widgetDefinition', function( evt ) {
    if ( evt.data.name == 'image' )
            evt.data.upcast = 'figure,image'; // Use both methods.
} );
CKEDITOR.plugins.embedBase.baseDefinition
view source
: RegExp
A regular expression to pre-validate URLs. ...

A regular expression to pre-validate URLs.

See:

  • [https://iframely.com/docs/providers],
  • isUrlValid.

Defaults to: /^((https?:)?\/\/|www\.)/i

Defined By

Methods

CKEDITOR.plugins.embedBase.baseDefinition
view source
( url, response )private
Caches the provider response. ...

Caches the provider response.

Parameters

  • url : String
  • response : Object
CKEDITOR.plugins.embedBase.baseDefinition
view source
( ) : CKEDITOR.plugins.notificationAggregator.taskprivate
Creates a notification aggregator task. ...

Creates a notification aggregator task.

Returns

CKEDITOR.plugins.embedBase.baseDefinition
view source
( url ) : Object/undefinedprivate
Returns the cached response. ...

Returns the cached response.

Parameters

  • url : String

Returns

  • Object/undefined

    Response or undefined if the cache was missed.

CKEDITOR.plugins.embedBase.baseDefinition
view source
( request, messageTypeOrMessage )private
Handles an error. ...

Handles an error. An error can be caused either by a request failure or an unsupported oEmbed response type.

Parameters

CKEDITOR.plugins.embedBase.baseDefinition
view source
( request ) : Booleanprivate
Handles the response of a successful request. ...

Handles the response of a successful request.

Fires the handleResponse event in order to convert the oEmbed response to HTML that can be embedded.

If the response can be handled, the content is set.

Parameters

Returns

  • Boolean

    Whether the response can be handled. Returns false if handleResponse was canceled or the default listener could not convert oEmbed response into embeddable HTML.

CKEDITOR.plugins.embedBase.baseDefinition
view source
( response ) : String/nullprivate
Returns embeddable HTML for an oEmbed response if it is of the photo, video or rich type. ...

Returns embeddable HTML for an oEmbed response if it is of the photo, video or rich type.

Parameters

  • response : Object

    The oEmbed response.

Returns

  • String/null

    HTML string to be embedded or null if this response type is not supported.

CKEDITOR.plugins.embedBase.baseDefinition
view source
( request )private
Sends the request to the provider using the JSONP technique. ...

Sends the request to the provider using the JSONP technique.

Parameters

CKEDITOR.plugins.embedBase.baseDefinition
view source
( url, content )private
The very final step of content loading. ...

The very final step of content loading. The url data property is changed and the content is embedded (CKEDITOR.plugins.widget.element's HTML is set).

Parameters

  • url : String

    The resource URL.

  • content : String

    HTML content to be embedded.

CKEDITOR.plugins.embedBase.baseDefinition
view source
( messageTypeOrMessage, [url], [suffix] ) : String
Generates an error message based on the message type (with a possible suffix) or the custom message template. ...

Generates an error message based on the message type (with a possible suffix) or the custom message template.

This method is used when showing a notification or an alert (in a dialog) about an error. Usually it is used with an error type which is a string from the editor.lang.embedbase object.

There are two error types available at the moment: 'unsupportedUrl' and 'fetchingFailed'. Additionally, both can be suffixed with 'Given'. See the language entries to see the difference. Inside the dialog this method is used with a suffix and to generate a notification message it is used without a suffix.

Additionally, a custom message may be passed and just like language entries, it can use the {url} placeholder.

While handling the response you can set an error message or its type. It will be passed to this method later.

widget.on( 'handleResponse', function( evt ) {
    if ( evt.data.response.type != 'rich' ) {
        evt.data.errorMessage = '{url} cannot be embedded. Only rich type is supported.';
        evt.cancel();

        // Or:
        evt.data.errorMessage = 'unsupportedUrl.';
        evt.cancel();
    }
} );

If you need to display your own error:

editor.showNotification(
    widget.getErrorMessage( '{url} cannot be embedded. Only rich type is supported.', wrongUrl )
);

Or with a message type:

editor.showNotification(
    widget.getErrorMessage( 'unsupportedUrl', wrongUrl )
);

Parameters

  • messageTypeOrMessage : String
  • url : String (optional)
  • suffix : String (optional)

Returns

  • String
CKEDITOR.plugins.embedBase.baseDefinition
view source
( url ) : Boolean
Checks whether the URL is valid. ...

Checks whether the URL is valid. Usually the content provider makes the final validation as only the provider knows what kind of URLs are accepted. However, to give the user some immediate feedback a synchronous validation is performed using the urlRegExp pattern and the validateUrl event.

Parameters

  • url : String

    The URL to check.

Returns

  • Boolean

    Whether the URL is valid (supported).

CKEDITOR.plugins.embedBase.baseDefinition
view source
( url, opts ) : CKEDITOR.plugins.embedBase.request
Loads content for a given resource URL by requesting the provider. ...

Loads content for a given resource URL by requesting the provider.

Usually widgets are controlled by the CKEDITOR.plugins.widget.setData method. However, loading content is an asynchronous operation due to client-server communication, and it would not be possible to pass callbacks to the CKEDITOR.plugins.widget.setData method so this new method is defined for embed widgets.

This method fires two events that allow to customize widget behavior without changing its code:

Note: This method is always asynchronous, even if the cache was hit.

Example usage:

var url = 'https://twitter.com/reinmarpl/status/573118615274315776';
widget.loadContent( url, {
    callback: function() {
        // Success. It is a good time to save a snapshot.
        editor.fire( 'saveSnapshot' );
        console.log( widget.data.url ); // The above URL. It is only changed
                                        // once the content is successfully loaded.
    },

    errorCallback: function( message ) {
        editor.showNotification( widget.getErrorMessage( message, url ), 'warning' );
    }
} );

Parameters

  • url : String

    Resource URL to be embedded.

  • opts : Object
    • callback : Function (optional)

      Callback called when content was successfully loaded into the editor.

    • errorCallback : Function (optional)

      Callback called when an error occurred.

      Parameters

    • noNotifications : Boolean (optional)

      Do not show notifications (useful when the dialog is open).

Returns

Returns a feature that this feature needs to register. ...

Returns a feature that this feature needs to register.

In some cases, during activation, one feature may need to register another feature. For example a CKEDITOR.ui.button often registers a related command. See CKEDITOR.ui.button.toFeature.

This method is executed when a feature is passed to the CKEDITOR.editor.addFeature.

Returns

Defined By

Events

CKEDITOR.plugins.embedBase.baseDefinition
view source
( evt )
Fired after receiving a response from the provider. ...

Fired after receiving a response from the provider. This event listener job is to turn the oEmbed response to embeddable HTML by setting evt.data.html.

widget.on( 'handleReaponse', function( evt ) {
    evt.data.html = customOembedToHtmlConverter( evt.data.response );
} );

This event can also be canceled to indicate that the response cannot be handled. In such case the evt.data.errorMessage must be set (see getErrorMessage).

widget.on( 'handleReaponse', function( evt ) {
    if ( evt.data.response.type == 'photo' ) {
        // Will display the editor.lang.embedbase.unsupportedUrl(Given) message.
        evt.data.errorMessage = 'unsupportedUrl';
        evt.cancel();
    }
} );

This event has a default late-listener (with a priority of 999) that, if evt.data.html has not been set yet, will try to handle the response by using the _responseToHtml method.

Parameters

  • evt : CKEDITOR.eventInfo
    • data : Object
      • url : String

        The resource URL.

      • response : Object

        The oEmbed response.

      • html : String (optional)

        The HTML which will be embedded.

        Defaults to: ''

      • errorMessage : String (optional)

        The error message or message type (see getErrorMessage) that must be set if this event is canceled to indicate an unsupported oEmbed response.

CKEDITOR.plugins.embedBase.baseDefinition
view source
( evt )
Fired by the loadContent method to dispatch a request to the provider. ...

Fired by the loadContent method to dispatch a request to the provider. You can cancel this event and send the request using a different technique. By default, if the event is not stopped or canceled a request will be sent using the JSONP technique.

widget.on( 'sendRequest', function( evt ) {
    var request = evt.data;

    // Send the request using a technique of your choice (XHR with CORS for instance).
    myApp.requestOembedProvider( request.url, function( err, response ) {
        if ( err ) {
            request.errorCallback( err );
        } else {
            request.callback( response );
        }
    } );

    // Do not call other listeners, so the default behavior (JSONP request)
    // will not be executed.
    evt.stop();
} );

Parameters

CKEDITOR.plugins.embedBase.baseDefinition
view source
( evt )
Fired by the isUrlValid method. ...

Fired by the isUrlValid method. Cancel the event to make the URL invalid.

Parameters