Projet

Général

Profil

Paste
Télécharger (2,92 ko) Statistiques
| Branche: | Révision:

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

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.getOptions = function () {
34
  // Get all the values
35
  var ret = {};
36
  // Keep track of multi-value fields.
37
  var fieldDelta = {};
38

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

    
41
    // Support multi-value select lists, which show up here with [] at the end.
42
    if ('[]' == field.name.slice(-2)) {
43
      if (typeof fieldDelta[field.name] === 'undefined') {
44
        fieldDelta[field.name] = 0;
45
      }
46
      else {
47
        fieldDelta[field.name] += 1;
48
      }
49
      field.name = field.name.replace('[]', '[' + fieldDelta[field.name] + ']');
50
    }
51

    
52
    ret[field.name] = field.value;
53

    
54
    // When a field uses a WYSIWYG format, the value needs to be extracted.
55
    if (field.name.match(/\[format\]/i)) {
56
      field.name = field.name.replace(/\[format\]/i, '[value]');
57
      field.key  = 'edit-' + field.name.replace(/[_\[]/g, '-').replace(/[\]]/g, '');
58

    
59
      if (Drupal.wysiwyg && Drupal.wysiwyg.instances[field.key]) {
60
        // Retrieve the content from the WYSIWYG instance.
61
        ret[field.name] = Drupal.wysiwyg.instances[field.key].getContent();
62

    
63
        // Encode the content to play nicely within JSON.
64
        ret[field.name] = encodeURIComponent(ret[field.name]);
65
      }
66
    }
67
  });
68

    
69
  return ret;
70
};
71

    
72
Drupal.media.formatForm.getFormattedMedia = function () {
73
  var formatType = $("#edit-format").val();
74
  return { type: formatType, options: Drupal.media.formatForm.getOptions(), html: Drupal.settings.media.formatFormFormats[formatType] };
75
};
76

    
77
Drupal.media.formatForm.submit = function () {
78
  // @see Drupal.behaviors.mediaFormatForm.attach().
79
  var buttons = $(parent.window.document.body).find('#mediaStyleSelector').parent('.ui-dialog').find('.ui-dialog-buttonpane button');
80
  buttons[0].click();
81
}
82

    
83
})(jQuery);