Projet

Général

Profil

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

root / drupal7 / sites / all / modules / media / js / media.js @ d1c64ea8

1
/**
2
 * @file
3
 * Provides JavaScript additions to the media field widget.
4
 *
5
 * This file provides support for launching the media browser to select existing
6
 * files and disabling of other media fields during Ajax uploads (which prevents
7
 * separate media fields from accidentally attaching files).
8
 */
9

    
10
(function ($) {
11

    
12
/**
13
 * Attach behaviors to media element upload fields.
14
 */
15
Drupal.behaviors.mediaElement = {
16
  attach: function (context, settings) {
17
    var $context = $(context);
18
    var elements;
19

    
20
    function initMediaBrowser(selector) {
21
      var widget=$context.find(selector).once('media-browser-launch');
22
      var browse=widget.siblings('.browse').add(widget.find('.browse'));
23
      var upload=browse.siblings('.upload').add(widget.find('.upload'));
24
      var attach=upload.siblings('.attach').add(widget.find('.attach'));
25
      browse.show();
26
      upload.hide();
27
      attach.hide();
28
      browse.bind('click', {configuration: settings.media.elements[selector]}, Drupal.media.openBrowser);
29
    }
30

    
31
    if (settings.media && settings.media.elements) {
32
      elements = settings.media.elements;
33
      Object.keys(elements).forEach(initMediaBrowser);
34
    }
35
  },
36
  detach: function (context, settings, trigger) {
37
    var $context = $(context);
38
    var elements;
39

    
40
    function removeMediaBrowser(selector) {
41
      $context.find(selector)
42
        .removeOnce('media-browser-launch')
43
        .siblings('.browse').hide()
44
        .siblings('.upload').show()
45
        .siblings('.attach').show()
46
        .siblings('.browse').unbind('click', Drupal.media.openBrowser);
47
    }
48

    
49
    if (trigger === 'unload' && settings.media && settings.media.elements) {
50
      elements = settings.media.elements;
51
      Object.keys(elements).forEach(removeMediaBrowser);
52
    }
53
  }
54
};
55

    
56
/**
57
 * Attach behaviors to the media attach and remove buttons.
58
 */
59
Drupal.behaviors.mediaButtons = {
60
  attach: function (context) {
61
    $('input.form-submit', context).bind('mousedown', Drupal.media.disableFields);
62
  },
63
  detach: function (context) {
64
    $('input.form-submit', context).unbind('mousedown', Drupal.media.disableFields);
65
  }
66
};
67

    
68
/**
69
 * Media attach utility functions.
70
 */
71
Drupal.media = Drupal.media || {};
72

    
73
/**
74
 * Opens the media browser with the element's configuration settings.
75
 */
76
Drupal.media.openBrowser = function (event) {
77
  var clickedButton = this;
78
  var configuration = event.data.configuration.global;
79

    
80
  // Find the file ID, preview and upload fields.
81
  var fidField = $(this).siblings('.fid');
82
  var previewField = $(this).siblings('.preview');
83
  var uploadField = $(this).siblings('.upload');
84

    
85
  // Find the edit and remove buttons.
86
  var editButton = $(this).siblings('.edit');
87
  var removeButton = $(this).siblings('.remove');
88

    
89
  // Launch the media browser.
90
  Drupal.media.popups.mediaBrowser(function (mediaFiles) {
91
    // Ensure that there was at least one media file selected.
92
    if (mediaFiles.length < 0) {
93
      return;
94
    }
95

    
96
    var mediaFileValue;
97
    // Process the value based on multiselect.
98
    if (mediaFiles.length > 1) {
99
      // Concatenate the array into a comma separated string.
100
      mediaFileValue = mediaFiles.map(function(file) {
101
        return file.fid;
102
      }).join(',');
103
    }
104
    else {
105
      // Grab the first of the selected media files.
106
      mediaFileValue = mediaFiles[0].fid;
107

    
108
      // Display a preview of the file using the selected media file's display.
109
      previewField.html(mediaFileValue.preview);
110
    }
111

    
112
    // Set the value of the hidden file ID field and trigger a change.
113
    uploadField.val(mediaFileValue);
114
    uploadField.trigger('change');
115

    
116
    // Find the attach button and automatically trigger it.
117
    var attachButton = uploadField.siblings('.attach');
118
    attachButton.trigger('mousedown');
119
  }, configuration);
120

    
121
  return false;
122
};
123

    
124
/**
125
 * Prevent media browsing when using buttons not intended to browse.
126
 */
127
Drupal.media.disableFields = function (event) {
128
  var clickedButton = this;
129

    
130
  // Only disable browse fields for Ajax buttons.
131
  if (!$(clickedButton).hasClass('ajax-processed')) {
132
    return;
133
  }
134

    
135
  // Check if we're working with an "Attach" button.
136
  var $enabledFields = [];
137
  if ($(this).closest('div.media-widget').length > 0) {
138
    $enabledFields = $(this).closest('div.media-widget').find('input.attach');
139
  }
140

    
141
  // Temporarily disable attach fields other than the one we're currently
142
  // working with. Filter out fields that are already disabled so that they
143
  // do not get enabled when we re-enable these fields at the end of behavior
144
  // processing. Re-enable in a setTimeout set to a relatively short amount
145
  // of time (1 second). All the other mousedown handlers (like Drupal's Ajax
146
  // behaviors) are excuted before any timeout functions are called, so we
147
  // don't have to worry about the fields being re-enabled too soon.
148
  // @todo If the previous sentence is true, why not set the timeout to 0?
149
  var $fieldsToTemporarilyDisable = $('div.media-widget input.attach').not($enabledFields).not(':disabled');
150
  $fieldsToTemporarilyDisable.attr('disabled', 'disabled');
151
  setTimeout(function (){
152
    $fieldsToTemporarilyDisable.attr('disabled', false);
153
  }, 1000);
154
};
155

    
156
})(jQuery);