Projet

Général

Profil

Paste
Télécharger (5,48 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media / modules / media_wysiwyg / js / media_wysiwyg.format_form.js @ d808fa20

1 ca0757b9 Assos Assos
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 d808fa20 Assos Assos
      $('<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 ca0757b9 Assos Assos
    });
30
  }
31
};
32
33 98df731a Assos Assos
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 0ece262b Assos Assos
  if (Drupal.wysiwyg && Drupal.wysiwyg.instances[fieldKey] && Drupal.wysiwyg.instances[fieldKey].status) {
43 98df731a Assos Assos
    // 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 0ece262b Assos Assos
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 ca0757b9 Assos Assos
Drupal.media.formatForm.getOptions = function () {
67
  // Get all the values
68
  var ret = {};
69 da542b7b Assos Assos
  // Keep track of multi-value fields.
70
  var fieldDelta = {};
71 ca0757b9 Assos Assos
72
  $.each($('#media-wysiwyg-format-form .fieldset-wrapper *').serializeArray(), function (i, field) {
73 da542b7b Assos Assos
74 0ece262b Assos Assos
    // Support multi-value fields, which show up here with [] at the end.
75 da542b7b Assos Assos
    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 0ece262b Assos Assos
    ret[field.name] = Drupal.media.formatForm.escapeFieldInput(field.value);
86 ca0757b9 Assos Assos
87 98df731a Assos Assos
    // When a field uses a WYSIWYG format, the value needs to be extracted and encoded.
88 ca0757b9 Assos Assos
    if (field.name.match(/\[format\]/i)) {
89
      field.name = field.name.replace(/\[format\]/i, '[value]');
90 98df731a Assos Assos
      field.key = 'edit-' + field.name.replace(/[_\[]/g, '-').replace(/[\]]/g, '');
91 ca0757b9 Assos Assos
92 98df731a Assos Assos
      // Attempt to retrieve content for this field from any associated javascript rich-text editor.
93
      var editorContent = Drupal.media.formatForm.getEditorContent(field.key);
94 0ece262b Assos Assos
      // Find content or an empty string (in case existing content was removed).
95
      if (editorContent || editorContent === '') {
96 98df731a Assos Assos
        // 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 ca0757b9 Assos Assos
      }
107 98df731a Assos Assos
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 ca0757b9 Assos Assos
    }
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);