media.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * @file
  3. * This file handles the JS for Media Module functions.
  4. */
  5. (function ($) {
  6. /**
  7. * Loads media browsers and callbacks, specifically for media as a field.
  8. */
  9. Drupal.behaviors.mediaElement = {
  10. attach: function (context, settings) {
  11. // Options set from media.fields.inc for the types, etc to show in the browser.
  12. // For each widget (in case of multi-entry)
  13. $('.media-widget', context).once('mediaBrowserLaunch', function () {
  14. var options = settings.media.elements[this.id];
  15. globalOptions = {};
  16. if (options.global != undefined) {
  17. var globalOptions = options.global;
  18. }
  19. //options = Drupal.settings.media.fields[this.id];
  20. var fidField = $('.fid', this);
  21. var previewField = $('.preview', this);
  22. var removeButton = $('.remove', this); // Actually a link, but looks like a button.
  23. // Show the Remove button if there's an already selected media.
  24. if (fidField.val() != 0) {
  25. removeButton.css('display', 'inline-block');
  26. }
  27. // When someone clicks the link to pick media (or clicks on an existing thumbnail)
  28. $('.launcher', this).bind('click', function () {
  29. // Launch the browser, providing the following callback function
  30. // @TODO: This should not be an anomyous function.
  31. Drupal.media.popups.mediaBrowser(function (mediaFiles) {
  32. if (mediaFiles.length < 0) {
  33. return;
  34. }
  35. var mediaFile = mediaFiles[0];
  36. // Set the value of the filefield fid (hidden).
  37. fidField.val(mediaFile.fid);
  38. // Set the preview field HTML.
  39. previewField.html(mediaFile.preview);
  40. // Show the Remove button.
  41. removeButton.show();
  42. }, globalOptions);
  43. return false;
  44. });
  45. // When someone clicks the Remove button.
  46. $('.remove', this).bind('click', function () {
  47. // Set the value of the filefield fid (hidden).
  48. fidField.val(0);
  49. // Set the preview field HTML.
  50. previewField.html('');
  51. // Hide the Remove button.
  52. removeButton.hide();
  53. return false;
  54. });
  55. $('.media-edit-link', this).bind('click', function () {
  56. var fid = fidField.val();
  57. if (fid) {
  58. Drupal.media.popups.mediaFieldEditor(fid, function (r) { alert(r); });
  59. }
  60. return false;
  61. });
  62. });
  63. }
  64. };
  65. })(jQuery);