Projet

Général

Profil

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

root / drupal7 / sites / all / modules / media / js / media.js @ 0ccfec7f

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
    // Grab the first of the selected media files.
95
    var mediaFile = mediaFiles[0];
96

    
97
    // Set the value of the hidden file ID field and trigger a change.
98
    uploadField.val(mediaFile.fid);
99
    uploadField.trigger('change');
100

    
101
    // Find the attach button and automatically trigger it.
102
    var attachButton = uploadField.siblings('.attach');
103
    attachButton.trigger('mousedown');
104

    
105
    // Display a preview of the file using the selected media file's display.
106
    previewField.html(mediaFile.preview);
107
  }, configuration);
108

    
109
  return false;
110
};
111

    
112
/**
113
 * Prevent media browsing when using buttons not intended to browse.
114
 */
115
Drupal.media.disableFields = function (event) {
116
  var clickedButton = this;
117

    
118
  // Only disable browse fields for Ajax buttons.
119
  if (!$(clickedButton).hasClass('ajax-processed')) {
120
    return;
121
  }
122

    
123
  // Check if we're working with an "Attach" button.
124
  var $enabledFields = [];
125
  if ($(this).closest('div.media-widget').length > 0) {
126
    $enabledFields = $(this).closest('div.media-widget').find('input.attach');
127
  }
128

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

    
144
})(jQuery);