diff --git a/css/media.css b/css/media.css index 4686e7c..57ad7c3 100644 --- a/css/media.css +++ b/css/media.css @@ -79,7 +79,6 @@ .media-widget a.button + a.button { margin-left: 15px; - display: none; } /* @end */ diff --git a/includes/media.browser.inc b/includes/media.browser.inc index 8d79e20..33db133 100644 --- a/includes/media.browser.inc +++ b/includes/media.browser.inc @@ -39,7 +39,7 @@ function media_browser($selected = NULL) { } $files = file_load_multiple($fids); foreach ($files as $file) { - media_browser_build_media_item($file); + media_build_media_item($file); } $setting = array('media' => array('selectedMedia' => array_values($files))); drupal_add_js($setting, 'setting'); @@ -247,7 +247,7 @@ function media_browser_list() { $fids = $query->execute()->fetchCol(); $files = file_load_multiple($fids); foreach ($files as $file) { - media_browser_build_media_item($file); + media_build_media_item($file); } drupal_json_output(array('media' => array_values($files))); @@ -390,11 +390,11 @@ EOF; /** * Adds properties to the passed in file that are needed by the media browser JS code. + * + * @deprecated deprecated since version 7.x-2.x */ function media_browser_build_media_item($file) { - $preview = media_get_thumbnail_preview($file); - $file->preview = drupal_render($preview); - $file->url = file_create_url($file->uri); + media_build_media_item($file); } /** diff --git a/includes/media.variables.inc b/includes/media.variables.inc index 3a89055..ac1c18e 100644 --- a/includes/media.variables.inc +++ b/includes/media.variables.inc @@ -123,6 +123,7 @@ function media_variable_default($name = NULL) { // Attributes which can be modified via the wysiwyg and persist. 'wysiwyg_allowed_attributes' => array('height', 'width', 'hspace', 'vspace', 'border', 'align', 'style', 'alt', 'title', 'class', 'id'), + 'field_edit_media_text' => 'Edit media', 'field_select_media_text' => 'Select media', 'field_remove_media_text' => 'Remove media', diff --git a/js/media.js b/js/media.js index aa445b7..b1f218a 100644 --- a/js/media.js +++ b/js/media.js @@ -24,10 +24,12 @@ Drupal.behaviors.mediaElement = { var fidField = $('.fid', this); var previewField = $('.preview', this); var removeButton = $('.remove', this); // Actually a link, but looks like a button. + var editButton = $('.edit', this); - // Show the Remove button if there's an already selected media. - if (fidField.val() != 0) { - removeButton.css('display', 'inline-block'); + // hide the Remove button and the Edit if there isn't an selected media. + if (fidField.val() == 0) { + removeButton.hide(); + editButton.hide(); } // When someone clicks the link to pick media (or clicks on an existing thumbnail) @@ -45,6 +47,10 @@ Drupal.behaviors.mediaElement = { previewField.html(mediaFile.preview); // Show the Remove button. removeButton.show(); + // Show the Edit button. + if (settings.media.userCanEditFile) { + editButton.show(); + } }, globalOptions); return false; }); @@ -57,13 +63,26 @@ Drupal.behaviors.mediaElement = { previewField.html(''); // Hide the Remove button. removeButton.hide(); + // Hide the Edit button. + editButton.hide(); return false; }); $('.media-edit-link', this).bind('click', function () { var fid = fidField.val(); if (fid) { - Drupal.media.popups.mediaFieldEditor(fid, function (r) { alert(r); }); + Drupal.media.popups.mediaFieldEditor(fid, function (mediaFile) { + // Set the value of the filefield fid (hidden). + fidField.val(mediaFile.fid); + // Set the preview field HTML. + previewField.html(mediaFile.preview); + // Show the Remove button. + removeButton.show(); + // Show the Edit button. + if (settings.media.userCanEditFile) { + editButton.show(); + } + }); } return false; }); diff --git a/js/media.popups.js b/js/media.popups.js index f0b5333..05aa94e 100644 --- a/js/media.popups.js +++ b/js/media.popups.js @@ -226,6 +226,7 @@ Drupal.media.popups.mediaFieldEditor = function (fid, onSelect, options) { // @todo: remove this awful hack :( defaults.src = defaults.src.replace('-media_id-', fid); options = $.extend({}, defaults, options); + options.onSelect = onSelect; // Create it as a modal window. var mediaIframe = Drupal.media.popups.getPopupIframe(options.src, 'mediaFieldEditor'); // Attach the onLoad event @@ -238,29 +239,16 @@ Drupal.media.popups.mediaFieldEditor = function (fid, onSelect, options) { */ var ok = 'OK'; var cancel = 'Cancel'; - var notSelected = 'Very sorry, there was an unknown error embedding media.'; if (Drupal && Drupal.t) { ok = Drupal.t(ok); cancel = Drupal.t(cancel); - notSelected = Drupal.t(notSelected); } // @todo: let some options come through here. Currently can't be changed. var dialogOptions = Drupal.media.popups.getDialogOptions(); dialogOptions.buttons[ok] = function () { - alert('hell yeah'); - return "poo"; - - var formattedMedia = this.contentWindow.Drupal.media.formatForm.getFormattedMedia(); - if (!formattedMedia) { - alert(notSelected); - return; - } - onSelect(formattedMedia); - $(this).dialog("destroy"); - $(this).remove(); }; dialogOptions.buttons[cancel] = function () { @@ -276,13 +264,43 @@ Drupal.media.popups.mediaFieldEditor = function (fid, onSelect, options) { }; Drupal.media.popups.mediaFieldEditor.mediaBrowserOnLoad = function (e) { - + var buttons = $(this).dialog('option', 'buttons'); + var context = this.contentWindow.document; + var popup = this; + $(popup).ready(function() { + if (context.URL.indexOf('render=media-popup') != -1) { + $(popup).show(); + } + else { + $(popup).dialog("destroy"); + $(popup).remove(); + } + }); + $('#edit-ok', context).click(function() { + e.data.onSelect(popup.contentWindow.Drupal.settings.media.file); + $(popup).hide(); + var ok = 'OK'; + if (Drupal && Drupal.t) { + ok = Drupal.t(ok); + } + var action = buttons[ok]; + action.call(popup); + }); + $('#edit-cancel', context).click(function() { + var cancel = 'Cancel'; + if (Drupal && Drupal.t) { + cancel = Drupal.t(cancel); + } + var action = buttons[cancel]; + action.call(popup); + return false; + }); }; Drupal.media.popups.mediaFieldEditor.getDefaults = function () { return { // @todo: do this for real - src: '/media/-media_id-/edit?render=media-popup', + src: '/file/-media_id-/edit?render=media-popup', onLoad: Drupal.media.popups.mediaFieldEditor.mediaBrowserOnLoad }; }; diff --git a/media.module b/media.module index c0bd252..4ed1448 100644 --- a/media.module +++ b/media.module @@ -785,11 +785,15 @@ function media_element_process(&$element, &$form_state, $form) { $preview = media_get_thumbnail_preview($file); $markup = drupal_render($preview); } + $element['#attached']['js'][] = array( + 'data' => array('media' => array('userCanEditFile' => file_entity_access('edit'))), + 'type' => 'setting' + ); $element['preview'] = array( '#type' => 'item', '#markup' => $markup, '#prefix' => '
', - '#suffix' => '
' . t(media_variable_get('field_select_media_text')) . '' . t(media_variable_get('field_remove_media_text')) . '', + '#suffix' => '' . t(media_variable_get('field_edit_media_text')) . '' . t(media_variable_get('field_select_media_text')) . '' . t(media_variable_get('field_remove_media_text')) . '', ); /** @@ -1179,3 +1183,40 @@ function media_load_all_exports($module, $directory, $extension, $name = NULL) { return $return; } + +/** + * Implements hook_form_FORM_ID_alter(). + */ +function media_form_file_entity_edit_alter(&$form, &$form_state, $form_id) { + if (isset($_GET['render']) && $_GET['render'] == 'media-popup') { + unset($form['actions']); + $form['actions'] = array( + '#type' => 'actions', + ); + $form['actions']['ok'] = array( + '#type' => 'submit', + '#value' => t('OK'), + ); + $form['actions']['cancel'] = array( + '#type' => 'submit', + '#value' => t('Cancel'), + ); + $file = $form_state['file']; + media_build_media_item($file); + $form['#attached']['js'][] = array( + 'data' => array( + 'media' => array('file' => $file,), + ), + 'type' => 'setting', + ); + } +} + +/** + * Adds properties to the passed in file that are needed by the media browser JS code and media field editor. + */ +function media_build_media_item(&$file) { + $preview = media_get_thumbnail_preview($file); + $file->preview = drupal_render($preview); + $file->url = file_create_url($file->uri); +} \ No newline at end of file diff --git a/media.views.inc b/media.views.inc index 08100c3..92eadfb 100644 --- a/media.views.inc +++ b/media.views.inc @@ -56,7 +56,7 @@ function template_preprocess_media_views_view_media_browser(&$vars) { foreach ($vars['rows'] as $index => $row) { $file = $files[$row->fid]; // Add url/preview to the file object - media_browser_build_media_item($file); + media_build_media_item($file); $vars['rows'][$index] = $file; $vars['rows'][$index]->preview = l($file->preview, 'media/browser', array( 'html' => TRUE,