Révision ca0757b9
Ajouté par Assos Assos il y a plus de 9 ans
drupal7/sites/all/modules/media/js/media.js | ||
---|---|---|
1 |
|
|
2 | 1 |
/** |
3 |
* @file |
|
4 |
* This file handles the JS for Media Module functions. |
|
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). |
|
5 | 8 |
*/ |
6 | 9 |
|
7 | 10 |
(function ($) { |
8 | 11 |
|
9 | 12 |
/** |
10 |
* Loads media browsers and callbacks, specifically for media as a field.
|
|
13 |
* Attach behaviors to media element browse fields.
|
|
11 | 14 |
*/ |
12 | 15 |
Drupal.behaviors.mediaElement = { |
13 | 16 |
attach: function (context, settings) { |
14 |
// Options set from media.fields.inc for the types, etc to show in the browser. |
|
15 |
|
|
16 |
// For each widget (in case of multi-entry) |
|
17 |
$('.media-widget', context).once('mediaBrowserLaunch', function () { |
|
18 |
var options = settings.media.elements[this.id]; |
|
19 |
globalOptions = {}; |
|
20 |
if (options.global != undefined) { |
|
21 |
var globalOptions = options.global; |
|
22 |
} |
|
23 |
//options = Drupal.settings.media.fields[this.id]; |
|
24 |
var fidField = $('.fid', this); |
|
25 |
var previewField = $('.preview', this); |
|
26 |
var editButton = $('.edit', this); |
|
27 |
var removeButton = $('.remove', this); |
|
28 |
|
|
29 |
// Hide the edit and remove buttons if there is no file data yet. |
|
30 |
if (fidField.val() == 0) { |
|
31 |
if (editButton.length) { |
|
32 |
editButton.hide(); |
|
33 |
} |
|
34 |
removeButton.hide(); |
|
35 |
} |
|
36 |
|
|
37 |
// When someone clicks the link to pick media (or clicks on an existing thumbnail) |
|
38 |
$('.launcher', this).bind('click', function (e) { |
|
39 |
// Launch the browser, providing the following callback function |
|
40 |
// @TODO: This should not be an anomyous function. |
|
41 |
Drupal.media.popups.mediaBrowser(function (mediaFiles) { |
|
42 |
if (mediaFiles.length < 0) { |
|
43 |
return; |
|
44 |
} |
|
45 |
var mediaFile = mediaFiles[0]; |
|
46 |
// Set the value of the filefield fid (hidden) and trigger a change. |
|
47 |
fidField.val(mediaFile.fid); |
|
48 |
fidField.trigger('change'); |
|
49 |
// Set the preview field HTML. |
|
50 |
previewField.html(mediaFile.preview); |
|
51 |
}, globalOptions); |
|
52 |
e.preventDefault(); |
|
17 |
if (settings.media && settings.media.elements) { |
|
18 |
$.each(settings.media.elements, function(selector) { |
|
19 |
$(selector, context).once('media-browser-launch', function () { |
|
20 |
var configuration = settings.media.elements[selector]; |
|
21 |
// The user has JavaScript enabled, so display the browse field and hide |
|
22 |
// the upload and attach fields which are only used as a fallback in |
|
23 |
// case the user is unable to use the media browser. |
|
24 |
$(selector, context).children('.browse').show(); |
|
25 |
$(selector, context).children('.upload').hide(); |
|
26 |
$(selector, context).children('.attach').hide(); |
|
27 |
$(selector, context).children('.browse').bind('click', {configuration: configuration}, Drupal.media.openBrowser); |
|
28 |
}); |
|
53 | 29 |
}); |
30 |
} |
|
31 |
} |
|
32 |
}; |
|
54 | 33 |
|
55 |
// When someone clicks the Remove button. |
|
56 |
$('.remove', this).bind('click', function (e) { |
|
57 |
// Set the value of the filefield fid (hidden) and trigger change. |
|
58 |
fidField.val(0); |
|
59 |
fidField.trigger('change'); |
|
60 |
// Set the preview field HTML. |
|
61 |
previewField.html(''); |
|
62 |
e.preventDefault(); |
|
63 |
}); |
|
34 |
/** |
|
35 |
* Attach behaviors to the media attach and remove buttons. |
|
36 |
*/ |
|
37 |
Drupal.behaviors.mediaButtons = { |
|
38 |
attach: function (context) { |
|
39 |
$('input.form-submit', context).bind('mousedown', Drupal.media.disableFields); |
|
40 |
}, |
|
41 |
detach: function (context) { |
|
42 |
$('input.form-submit', context).unbind('mousedown', Drupal.media.disableFields); |
|
43 |
} |
|
44 |
}; |
|
64 | 45 |
|
65 |
// Show or hide the edit/remove buttons if the field has a file or not. |
|
66 |
$('.fid', this).bind('change', function() { |
|
67 |
if (fidField.val() == 0) { |
|
68 |
if (editButton.length) { |
|
69 |
editButton.hide(); |
|
70 |
} |
|
71 |
removeButton.hide(); |
|
72 |
} |
|
73 |
else { |
|
74 |
if (editButton.length) { |
|
75 |
var url = Drupal.settings.basePath + 'file/' + fidField.val() + '/edit'; |
|
76 |
$.ajax({ |
|
77 |
url: location.protocol + '//' + location.host + url, |
|
78 |
type: 'HEAD', |
|
79 |
success: function(data) { |
|
80 |
editButton.attr('href', editButton.attr('href').replace(/media\/\d+\/edit/, 'media/' + fidField.val() + '/edit')); |
|
81 |
// Re-process the edit link through CTools modal behaviors. |
|
82 |
editButton.unbind('click'); |
|
83 |
editButton.removeClass('ctools-use-modal-processed'); |
|
84 |
// @todo Maybe add support for Drupal.detachBehaviors in Drupal.behaviors.ZZCToolsModal? |
|
85 |
Drupal.attachBehaviors(editButton.parent(), Drupal.settings); |
|
86 |
editButton.show(); |
|
87 |
} |
|
88 |
}); |
|
89 |
} |
|
90 |
removeButton.show(); |
|
91 |
} |
|
92 |
}); |
|
93 |
}); |
|
46 |
/** |
|
47 |
* Media attach utility functions. |
|
48 |
*/ |
|
49 |
Drupal.media = Drupal.media || {}; |
|
50 |
|
|
51 |
/** |
|
52 |
* Opens the media browser with the element's configuration settings. |
|
53 |
*/ |
|
54 |
Drupal.media.openBrowser = function (event) { |
|
55 |
var clickedButton = this; |
|
56 |
var configuration = event.data.configuration.global; |
|
57 |
|
|
58 |
// Find the file ID, preview and upload fields. |
|
59 |
var fidField = $(this).siblings('.fid'); |
|
60 |
var previewField = $(this).siblings('.preview'); |
|
61 |
var uploadField = $(this).siblings('.upload'); |
|
62 |
|
|
63 |
// Find the edit and remove buttons. |
|
64 |
var editButton = $(this).siblings('.edit'); |
|
65 |
var removeButton = $(this).siblings('.remove'); |
|
66 |
|
|
67 |
// Launch the media browser. |
|
68 |
Drupal.media.popups.mediaBrowser(function (mediaFiles) { |
|
69 |
// Ensure that there was at least one media file selected. |
|
70 |
if (mediaFiles.length < 0) { |
|
71 |
return; |
|
72 |
} |
|
73 |
|
|
74 |
// Grab the first of the selected media files. |
|
75 |
var mediaFile = mediaFiles[0]; |
|
76 |
|
|
77 |
// Set the value of the hidden file ID field and trigger a change. |
|
78 |
uploadField.val(mediaFile.fid); |
|
79 |
uploadField.trigger('change'); |
|
80 |
|
|
81 |
// Find the attach button and automatically trigger it. |
|
82 |
var attachButton = uploadField.siblings('.attach'); |
|
83 |
attachButton.trigger('mousedown'); |
|
84 |
|
|
85 |
// Display a preview of the file using the selected media file's display. |
|
86 |
previewField.html(mediaFile.preview); |
|
87 |
}, configuration); |
|
88 |
|
|
89 |
return false; |
|
90 |
}; |
|
91 |
|
|
92 |
/** |
|
93 |
* Prevent media browsing when using buttons not intended to browse. |
|
94 |
*/ |
|
95 |
Drupal.media.disableFields = function (event) { |
|
96 |
var clickedButton = this; |
|
97 |
|
|
98 |
// Only disable browse fields for Ajax buttons. |
|
99 |
if (!$(clickedButton).hasClass('ajax-processed')) { |
|
100 |
return; |
|
94 | 101 |
} |
102 |
|
|
103 |
// Check if we're working with an "Attach" button. |
|
104 |
var $enabledFields = []; |
|
105 |
if ($(this).closest('div.media-widget').length > 0) { |
|
106 |
$enabledFields = $(this).closest('div.media-widget').find('input.attach'); |
|
107 |
} |
|
108 |
|
|
109 |
// Temporarily disable attach fields other than the one we're currently |
|
110 |
// working with. Filter out fields that are already disabled so that they |
|
111 |
// do not get enabled when we re-enable these fields at the end of behavior |
|
112 |
// processing. Re-enable in a setTimeout set to a relatively short amount |
|
113 |
// of time (1 second). All the other mousedown handlers (like Drupal's Ajax |
|
114 |
// behaviors) are excuted before any timeout functions are called, so we |
|
115 |
// don't have to worry about the fields being re-enabled too soon. |
|
116 |
// @todo If the previous sentence is true, why not set the timeout to 0? |
|
117 |
var $fieldsToTemporarilyDisable = $('div.media-widget input.attach').not($enabledFields).not(':disabled'); |
|
118 |
$fieldsToTemporarilyDisable.attr('disabled', 'disabled'); |
|
119 |
setTimeout(function (){ |
|
120 |
$fieldsToTemporarilyDisable.attr('disabled', false); |
|
121 |
}, 1000); |
|
95 | 122 |
}; |
96 | 123 |
|
97 | 124 |
})(jQuery); |
125 |
|
Formats disponibles : Unified diff
Weekly update of contrib modules