This repository was archived by the owner on Jan 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathengine.js
More file actions
91 lines (81 loc) · 3.05 KB
/
engine.js
File metadata and controls
91 lines (81 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* @file
* Drupal Entity embed plugin.
*/
(function ($, Drupal, CKEDITOR) {
"use strict";
CKEDITOR.plugins.add('drupalentity', {
// This plugin requires the Widgets System defined in the 'widget' plugin.
requires: 'widget',
// The plugin initialization logic goes inside this method.
beforeInit: function (editor) {
// Configure CKEditor DTD for custom drupal-entity element.
// @see https://www.drupal.org/node/2448449#comment-9717735
var dtd = CKEDITOR.dtd, tagName;
dtd['drupal-entity'] = {'#': 1};
// Register drupal-entity element as allowed child, in each tag that can
// contain a div element.
for (tagName in dtd) {
if (dtd[tagName].div) {
dtd[tagName]['drupal-entity'] = 1;
}
}
// Register the entity embed widget.
editor.widgets.add('drupalentity', {
// Minimum HTML which is required by this widget to work.
allowedContent: 'drupal-entity[*]',
requiredContent: 'drupal-entity[*]',
// Simply recognize the element as our own. The inner markup if fetched
// and inserted the init() callback, since it requires the actual DOM
// element.
upcast: function (element) {
var attributes = element.attributes;
if (attributes['data-entity-type'] === undefined || (attributes['data-entity-id'] === undefined && attributes['data-entity-uuid'] === undefined) || (attributes['data-view-mode'] === undefined && attributes['data-entity-embed-display'] === undefined)) {
return;
}
// Generate an ID for the element, so that we can use the Ajax
// framework.
element.attributes.id = generateEmbedId();
return element;
},
// Fetch the rendered entity.
init: function () {
var element = this.element;
var $element = $(element.$);
// Use the Ajax framework to fetch the HTML, so that we can retrieve
// out-of-band assets (JS, CSS...).
var entityEmbedPreview = new Drupal.ajax({
base: $element.attr('id'),
element: $element,
url: Drupal.url('embed/preview/' + editor.config.drupal.format + '?' + $.param({
value: element.getOuterHtml()
})),
progress: {type: 'none'},
// Use a custom event to trigger the call.
event: 'entity_embed_dummy_event'
});
entityEmbedPreview.execute();
},
// Downcast the element.
downcast: function (element) {
// Only keep the wrapping element.
element.setHtml('');
// Remove the auto-generated ID.
delete element.attributes.id;
return element;
}
});
}
});
/**
* Generates unique HTML IDs for the widgets.
*
* @returns {string}
*/
function generateEmbedId() {
if (typeof generateEmbedId.counter == 'undefined') {
generateEmbedId.counter = 0;
}
return 'entity-embed-' + generateEmbedId.counter++;
}
})(jQuery, Drupal, CKEDITOR);