diff --git a/includes/media.browser.inc b/includes/media.browser.inc index fd91c17..8c2f17e 100644 --- a/includes/media.browser.inc +++ b/includes/media.browser.inc @@ -12,22 +12,7 @@ function media_browser($selected = NULL) { $output = array(); $output['#attached']['library'][] = array('media', 'media_browser_page'); - // Build out browser settings. Permissions- and security-related behaviors - // should not rely on these parameters, since they come from the HTTP query. - // @TODO make sure we treat parameters as user input. - $params = drupal_get_query_parameters() + array( - 'types' => array(), - 'multiselect' => FALSE, - ); - - // Transform text 'true' and 'false' to actual booleans. - foreach ($params as $k => $v) { - if ($v === 'true') { $params[$k] = TRUE; } - elseif ($v === 'false') { $params[$k] = FALSE; } - } - - array_walk_recursive($params, 'media_recursive_check_plain'); - $params = media_set_browser_params($params); + $params = media_set_browser_params(); // If one or more files have been selected, the browser interaction is now // complete. Return empty page content to the dialog which now needs to close, @@ -170,22 +155,36 @@ function media_browser($selected = NULL) { * * It also offers a chance for some meddler to meddle with them. * - * @param array $params - * An array of parameters provided when a media_browser is launched. - * * @see media_browser() */ -function media_set_browser_params(array $params = NULL) { - $stored_params = &drupal_static(__FUNCTION__, array()); - - if (isset($params)) { - $stored_params = $params; - // Allow modules to alter the parameters. - drupal_alter('media_browser_params', $stored_params); - } + function media_set_browser_params() { + $params = &drupal_static(__FUNCTION__, array()); + + if (empty($params)) { + // Build out browser settings. Permissions- and security-related behaviors + // should not rely on these parameters, since they come from the HTTP query. + // @TODO make sure we treat parameters as user input. + $params = drupal_get_query_parameters() + array( + 'types' => array(), + 'multiselect' => FALSE, + ); + + // Transform text 'true' and 'false' to actual booleans. + foreach ($params as $k => $v) { + if ($v === 'true') { $params[$k] = TRUE; } + elseif ($v === 'false') { $params[$k] = FALSE; } + } + + array_walk_recursive($params, 'media_recursive_check_plain'); + $params = media_set_browser_params($params); + + // Allow modules to alter the parameters. + drupal_alter('media_browser_params', $params); + } + + return $params; + } - return $stored_params; -} /** * For sanity in grammar. diff --git a/js/plugins/media.views.js b/js/plugins/media.views.js index f5f44d0..67137ff 100644 --- a/js/plugins/media.views.js +++ b/js/plugins/media.views.js @@ -65,6 +65,12 @@ Drupal.media.browser.views.select = function(view) { * Sets up event-handlers for selecting items in the view. */ Drupal.media.browser.views.setup = function(view) { + // Ensure we only setup each view once.. + if ($(view).hasClass('media-browser-views-processed')) return; + + // Reset the list of selected files + Drupal.media.browser.selectMedia([]); + // Catch the click on a media item $('.view-content .media-item', view).bind('click', function () { var fid = $(this).closest('a[data-fid]').data('fid'), @@ -120,6 +126,9 @@ Drupal.media.browser.views.setup = function(view) { } Drupal.media.browser.selectMedia(selectedFiles); }); + + // Add the processed class, so we dont accidentally process the same element twice.. + $(view).addClass('media-browser-views-processed'); } }(jQuery));