Projet

Général

Profil

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

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

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
      $context.find(selector)
22
        .once('media-browser-launch')
23
        .siblings('.browse').show()
24
        .siblings('.upload').hide()
25
        .siblings('.attach').hide()
26
        .siblings('.browse').bind('click', {configuration: settings.media.elements[selector]}, Drupal.media.openBrowser);
27
    }
28

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

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

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

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

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

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

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

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

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

    
94
    var mediaFileValue;
95
    // Process the value based on multiselect.
96
    if (mediaFiles.length > 1) {
97
      // Reverse array to have files in correct order
98
      mediaFiles.reverse();
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);