3 * The JavaScript file for the wysiwyg integration.
9 * A CKEditor plugin for vido embeds.
11 CKEDITOR.plugins.add('video_embed', {
14 * Set the plugin modes.
21 * Define the plugin requirements.
33 init: function (editor) {
34 this.registerWidget(editor);
35 this.addCommand(editor);
40 * Add the command to the editor.
42 addCommand: function (editor) {
44 var modalSaveWrapper = function (values) {
45 editor.fire('saveSnapshot');
46 self.modalSave(editor, values);
47 editor.fire('saveSnapshot');
49 editor.addCommand('video_embed', {
50 exec: function (editor, data) {
51 // If the selected element while we click the button is an instance
52 // of the video_embed widget, extract it's values so they can be
53 // sent to the server to prime the configuration form.
54 var existingValues = {};
55 if (editor.widgets.focused && editor.widgets.focused.name == 'video_embed') {
56 existingValues = editor.widgets.focused.data.json;
58 Drupal.ckeditor.openDialog(editor, Drupal.url('video-embed-wysiwyg/dialog/' + editor.config.drupal.format), existingValues, modalSaveWrapper, {
59 title: Drupal.t('Video Embed'),
60 dialogClass: 'video-embed-dialog'
67 * A callback that is triggered when the modal is saved.
69 modalSave: function (editor, values) {
70 // Insert a video widget that understands how to manage a JSON encoded
71 // object, provided the video_embed property is set.
72 var widget = editor.document.createElement('p');
73 widget.setHtml(JSON.stringify(values));
74 editor.insertHtml(widget.getOuterHtml());
78 * Register the widget.
80 registerWidget: function (editor) {
82 editor.widgets.add('video_embed', {
83 downcast: self.downcast,
90 * Check if the element is an instance of the video widget.
92 upcast: function (element, data) {
93 // Upcast check must be sensitive to both HTML encoded and plain text.
94 if (!element.getHtml().match(/^({(?=.*preview_thumbnail\b)(?=.*settings\b)(?=.*video_url\b)(?=.*settings_summary)(.*)})$/)) {
97 data.json = JSON.parse(element.getHtml());
98 element.setHtml(Drupal.theme('videoEmbedWidget', data.json));
103 * Turns a transformed widget into the downcasted representation.
105 downcast: function (element) {
106 element.setHtml(JSON.stringify(this.data.json));
110 * Add the icon to the toolbar.
112 addIcon: function (editor) {
113 if (!editor.ui.addButton) {
116 editor.ui.addButton('video_embed', {
117 label: Drupal.t('Video Embed'),
118 command: 'video_embed',
119 icon: this.path + '/icon.png'
125 * The widget template viewable in the WYSIWYG after creating a video.
127 Drupal.theme.videoEmbedWidget = function (settings) {
129 '<span class="video-embed-widget">',
130 '<img class="video-embed-widget__image" src="' + Drupal.checkPlain(settings.preview_thumbnail) + '">',
131 '<span class="video-embed-widget__summary">',
132 Drupal.checkPlain(settings.settings_summary),