root / drupal7 / sites / all / modules / media / modules / media_wysiwyg / js / media_wysiwyg.format_form.js @ d808fa20
1 |
|
---|---|
2 |
/**
|
3 |
* @file
|
4 |
* Attach behaviors to formatter radio select when selecting a media's display
|
5 |
* formatter.
|
6 |
*/
|
7 |
|
8 |
(function ($) { |
9 |
namespace('Drupal.media.formatForm');
|
10 |
|
11 |
Drupal.media.mediaFormatSelected = {}; |
12 |
|
13 |
Drupal.behaviors.mediaFormatForm = { |
14 |
attach: function (context, settings) { |
15 |
// Add the "Submit" button inside the IFRAME that trigger the behavior of
|
16 |
// the hidden "OK" button that is outside the IFRAME.
|
17 |
// @see Drupal.media.browser.validateButtons() for more details.
|
18 |
|
19 |
// @note I think this should be handled in media.browser.js in
|
20 |
// Drupal.media.browser.validateButtons but I'm not sure how crufty this
|
21 |
// particular functionality is. We should evaluate if it is still needed.
|
22 |
|
23 |
// @TODO can these be added to the content being displayed via form_alter?
|
24 |
|
25 |
// Adding the buttons should only be done once in order to prevent multiple
|
26 |
// buttons from being added if part of the form is updated via AJAX
|
27 |
$('#media-wysiwyg-format-form').once('format', function() { |
28 |
$('<a class="button fake-ok" href="javascript:void(0)">' + Drupal.t('Submit') + '</a>').appendTo($('#media-wysiwyg-format-form')).bind('click', Drupal.media.formatForm.submit); |
29 |
}); |
30 |
} |
31 |
}; |
32 |
|
33 |
Drupal.media.formatForm.getEditorContent = function(fieldKey) { |
34 |
// This is the default implementation of an overridable function:
|
35 |
// Each javascript rich-text editor module should provide an override implementation
|
36 |
// of this function which fetches content from the appropriate editor-specific variable.
|
37 |
|
38 |
// This current implementation explicitly handles the editors from the
|
39 |
// WYSIWYG and Media CKEditor modules: it should be modified to remove that
|
40 |
// as soon as each module has been confirmed to provide their own implementation.
|
41 |
|
42 |
if (Drupal.wysiwyg && Drupal.wysiwyg.instances[fieldKey] && Drupal.wysiwyg.instances[fieldKey].status) {
|
43 |
// Retrieve the content from the editor provided by the WYSIWYG Module.
|
44 |
// Remove this case once the WYSIWYG module provides an override for this function.
|
45 |
return Drupal.wysiwyg.instances[fieldKey].getContent();
|
46 |
} |
47 |
else if (typeof CKEDITOR !== 'undefined' && CKEDITOR.instances[fieldKey]) { |
48 |
// Retrieve the content from the editor provided by the Media CKEditor Module.
|
49 |
// Remove this case once the Media CKEditor module provides an override for this function.
|
50 |
return CKEDITOR.instances[fieldKey].getData();
|
51 |
} |
52 |
else {
|
53 |
// Default case => no known WYSIWYG editor.
|
54 |
return null; |
55 |
} |
56 |
} |
57 |
|
58 |
Drupal.media.formatForm.escapeFieldInput = function(input) { |
59 |
// This is the default implementation of an overridable function: It is
|
60 |
// intended to allow for the escaping of the user input from the format form.
|
61 |
// No escaping is done here, but this allows other modules to escape the input
|
62 |
// by overriding this function.
|
63 |
return input;
|
64 |
} |
65 |
|
66 |
Drupal.media.formatForm.getOptions = function () { |
67 |
// Get all the values
|
68 |
var ret = {};
|
69 |
// Keep track of multi-value fields.
|
70 |
var fieldDelta = {};
|
71 |
|
72 |
$.each($('#media-wysiwyg-format-form .fieldset-wrapper *').serializeArray(), function (i, field) { |
73 |
|
74 |
// Support multi-value fields, which show up here with [] at the end.
|
75 |
if ('[]' == field.name.slice(-2)) { |
76 |
if (typeof fieldDelta[field.name] === 'undefined') { |
77 |
fieldDelta[field.name] = 0;
|
78 |
} |
79 |
else {
|
80 |
fieldDelta[field.name] += 1;
|
81 |
} |
82 |
field.name = field.name.replace('[]', '[' + fieldDelta[field.name] + ']'); |
83 |
} |
84 |
|
85 |
ret[field.name] = Drupal.media.formatForm.escapeFieldInput(field.value); |
86 |
|
87 |
// When a field uses a WYSIWYG format, the value needs to be extracted and encoded.
|
88 |
if (field.name.match(/\[format\]/i)) { |
89 |
field.name = field.name.replace(/\[format\]/i, '[value]'); |
90 |
field.key = 'edit-' + field.name.replace(/[_\[]/g, '-').replace(/[\]]/g, ''); |
91 |
|
92 |
// Attempt to retrieve content for this field from any associated javascript rich-text editor.
|
93 |
var editorContent = Drupal.media.formatForm.getEditorContent(field.key);
|
94 |
// Find content or an empty string (in case existing content was removed).
|
95 |
if (editorContent || editorContent === '') { |
96 |
// Replace the already-cached value with the value from the editor.
|
97 |
ret[field.name] = editorContent; |
98 |
} |
99 |
else {
|
100 |
// An editor was not used for this field - either because none was configured for the selected format,
|
101 |
// or possibly because the user chose to revert to the plain-text editor (CKEditor allows that).
|
102 |
// Replace the already-cached value with the raw value from the long-text field value.
|
103 |
// (Replacment is needed because this function may be invoked multiple times on the same field,
|
104 |
// and so the cached value may already have been encoded - we don't want to double-encode it!)
|
105 |
ret[field.name] = $('#' + field.key).val(); |
106 |
} |
107 |
|
108 |
// Encode the formatted value to play nicely within JSON.
|
109 |
// (It could contain HTML and other quoted entities, no matter what sort of editor was used)
|
110 |
ret[field.name] = encodeURIComponent(ret[field.name]); |
111 |
} |
112 |
}); |
113 |
|
114 |
return ret;
|
115 |
}; |
116 |
|
117 |
Drupal.media.formatForm.getFormattedMedia = function () { |
118 |
var formatType = $("#edit-format").val(); |
119 |
return { type: formatType, options: Drupal.media.formatForm.getOptions(), html: Drupal.settings.media.formatFormFormats[formatType] }; |
120 |
}; |
121 |
|
122 |
Drupal.media.formatForm.submit = function () { |
123 |
// @see Drupal.behaviors.mediaFormatForm.attach().
|
124 |
var buttons = $(parent.window.document.body).find('#mediaStyleSelector').parent('.ui-dialog').find('.ui-dialog-buttonpane button'); |
125 |
buttons[0].click();
|
126 |
} |
127 |
|
128 |
})(jQuery); |