root / htmltest / sites / all / modules / media / js / media.js @ a5572547
1 | 85ad3d82 | Assos Assos | |
---|---|---|---|
2 | /**
|
||
3 | * @file
|
||
4 | * This file handles the JS for Media Module functions.
|
||
5 | */
|
||
6 | |||
7 | (function ($) { |
||
8 | |||
9 | /**
|
||
10 | * Loads media browsers and callbacks, specifically for media as a field.
|
||
11 | */
|
||
12 | Drupal.behaviors.mediaElement = { |
||
13 | 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(); |
||
53 | }); |
||
54 | |||
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 | }); |
||
64 | |||
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 | }); |
||
94 | } |
||
95 | }; |
||
96 | |||
97 | })(jQuery); |