Projet

Général

Profil

Paste
Télécharger (4,94 ko) Statistiques
| Branche: | Révision:

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

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">' + 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]) {
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.getOptions = function () {
59
  // Get all the values
60
  var ret = {};
61
  // Keep track of multi-value fields.
62
  var fieldDelta = {};
63

    
64
  $.each($('#media-wysiwyg-format-form .fieldset-wrapper *').serializeArray(), function (i, field) {
65

    
66
    // Support multi-value select lists, which show up here with [] at the end.
67
    if ('[]' == field.name.slice(-2)) {
68
      if (typeof fieldDelta[field.name] === 'undefined') {
69
        fieldDelta[field.name] = 0;
70
      }
71
      else {
72
        fieldDelta[field.name] += 1;
73
      }
74
      field.name = field.name.replace('[]', '[' + fieldDelta[field.name] + ']');
75
    }
76

    
77
    ret[field.name] = field.value;
78

    
79
    // When a field uses a WYSIWYG format, the value needs to be extracted and encoded.
80
    if (field.name.match(/\[format\]/i)) {
81
      field.name = field.name.replace(/\[format\]/i, '[value]');
82
      field.key = 'edit-' + field.name.replace(/[_\[]/g, '-').replace(/[\]]/g, '');
83

    
84
      // Attempt to retrieve content for this field from any associated javascript rich-text editor.
85
      var editorContent = Drupal.media.formatForm.getEditorContent(field.key);
86
      if (editorContent) {
87
        // Replace the already-cached value with the value from the editor.
88
        ret[field.name] = editorContent;
89
      }
90
      else {
91
        // An editor was not used for this field - either because none was configured for the selected format,
92
        // or possibly because the user chose to revert to the plain-text editor (CKEditor allows that).
93
        // Replace the already-cached value with the raw value from the long-text field value.
94
        // (Replacment is needed because this function may be invoked multiple times on the same field,
95
        // and so the cached value may already have been encoded - we don't want to double-encode it!)
96
        ret[field.name] = $('#' + field.key).val();
97
      }
98

    
99
      // Encode the formatted value to play nicely within JSON.
100
      // (It could contain HTML and other quoted entities, no matter what sort of editor was used)
101
      ret[field.name] = encodeURIComponent(ret[field.name]);
102
    }
103
  });
104

    
105
  return ret;
106
};
107

    
108
Drupal.media.formatForm.getFormattedMedia = function () {
109
  var formatType = $("#edit-format").val();
110
  return { type: formatType, options: Drupal.media.formatForm.getOptions(), html: Drupal.settings.media.formatFormFormats[formatType] };
111
};
112

    
113
Drupal.media.formatForm.submit = function () {
114
  // @see Drupal.behaviors.mediaFormatForm.attach().
115
  var buttons = $(parent.window.document.body).find('#mediaStyleSelector').parent('.ui-dialog').find('.ui-dialog-buttonpane button');
116
  buttons[0].click();
117
}
118

    
119
})(jQuery);