From 6951090c45a642f1af9234b2af322a654fc819a0 Mon Sep 17 00:00:00 2001
From: James Elliott <james.elliott@acquia.com>
Date: Tue, 12 Apr 2011 16:51:18 -0400
Subject: [PATCH 06/15] Refactor media browser display on the admin page

- Separated thumbnails JS and CSS to separate files that are included as needed

- Created a media_browser_build() function to be the main function for building the renderable array
---
 css/media.css              |  124 -----------------------
 css/media.thumbnails.css   |  134 +++++++++++++++++++++++++
 includes/media.admin.inc   |  189 +-----------------------------------
 includes/media.browser.inc |  233 +++++++++++++++++++++++++++++++++++++++++++-
 includes/media.theme.inc   |    1 -
 js/media.admin.js          |   65 +-----------
 js/media.thumbnails.js     |   47 +++++++++
 media.module               |   14 +--
 8 files changed, 425 insertions(+), 382 deletions(-)
 create mode 100644 css/media.thumbnails.css
 create mode 100644 js/media.thumbnails.js

diff --git a/css/media.css b/css/media.css
index eff4960..f4816a3 100644
--- a/css/media.css
+++ b/css/media.css
@@ -84,126 +84,6 @@
 
 /* @end */
 
-/* @group media item */
-
-.media-item {
-  background: #FFF;
-  border: 1px solid #CCCCCC;
-  margin: 10px;
-  padding: 6px 6px 2px;
-  width: 100px;
-}
-
-.media-item.selected {
-  background: #F4ECC7;
-}
-
-.media-item a {
-  display: block;
-}
-
-.media-item img {
-  border: 2px solid transparent;
-  display: inline-block;
-  margin-left: -2px;
-  height: auto;
-  width: 100%;
-}
-
-.media-item img:hover {
-  border-color: #058AC5;
-}
-
-
-.media-item-icons img{
-  height:25px;
-  width:25px;
-  float:left;
-}
-
-.media-item .label-wrapper {
-  overflow: hidden;
-  margin-left: 14px;
-}
-
-.media-item .label-wrapper:hover {
-  border-bottom: 2px solid #CCC;
-  border-right: 2px solid #CCC;
-  display: inline-block;
-  line-height: 16px;
-  margin-bottom: -1px;
-  overflow: visible;
-  position: relative;
-  z-index: 10;
-}
-
-.preview .media-item .label-wrapper {
-  margin-left: 0;
-}
-
-.preview {
-  display: inline-block;
-  vertical-align: middle;
-}
-
-
-.media-item .media-filename {
-  background: #FFF;
-  color: #058AC5;
-  font-size: 10px;
-  padding: 0 3px;
-  white-space: nowrap;
-}
-
-.media-item .label-wrapper:hover .media-filename {
-  border: 1px solid #888;
-  display: inline-block;
-  margin: 0 0 -2px -1px;
-}
-
-.media-item.selected .media-filename {
-  background: #F4ECC7;
-}
-
-.media-item .media-filename:hover {
-  text-decoration: underline;
-}
-
-.media-modal-frame {
-  overflow: hidden;
-}
-
-.media-display-thumbnails .media-list-thumbnails {
-  margin: 0 -10px;
-}
-
-.media-list-thumbnails > li {
-  float: left;
-  list-style: none;
-}
-
-.media-list-thumbnails .form-type-checkbox {
-  margin: -35px 0 0 16px;
-  position:  absolute;
-}
-
-.media-item .media-type-icon {
-  border: none;
-  margin: 0 0 -5px;
-  width: auto;
-}
-
-.ui-dialog.media-wrapper .ui-dialog-buttonpane {
-  display: none;
-}
-
-.ui-widget.ui-widget-content.media-wrapper {
-  background: none;
-  border: none;
-}
-
-/* @end */
-
 /* @group media browser */
 
 body.page-media-browser {
@@ -324,10 +204,6 @@ a.button {
   clear: both;
 }
 
-.media-thumbnails-select {
-  float: left;
-}
-
 #edit-options .form-item-format label {
   display: inline;
 }
diff --git a/css/media.thumbnails.css b/css/media.thumbnails.css
new file mode 100644
index 0000000..d3db5cb
--- /dev/null
+++ b/css/media.thumbnails.css
@@ -0,0 +1,134 @@
+.media-item {
+  background: #FFF;
+  border: 1px solid #CCCCCC;
+  margin: 10px;
+  padding: 6px 6px 2px;
+  width: 100px;
+}
+
+.media-item.selected {
+  background: #F4ECC7;
+}
+
+.media-item a {
+  display: block;
+}
+
+.media-item img {
+  border: 2px solid transparent;
+  display: inline-block;
+  margin-left: -2px;
+  height: auto;
+  width: 100%;
+}
+
+.media-item img:hover {
+  border-color: #058AC5;
+}
+
+
+.media-item-icons img{
+  height:25px;
+  width:25px;
+  float:left;
+}
+
+.media-item .label-wrapper {
+  overflow: hidden;
+  margin-left: 14px;
+}
+
+.media-item .label-wrapper:hover {
+  border-bottom: 2px solid #CCC;
+  border-right: 2px solid #CCC;
+  display: inline-block;
+  line-height: 16px;
+  margin-bottom: -1px;
+  overflow: visible;
+  position: relative;
+  z-index: 10;
+}
+
+.preview .media-item .label-wrapper {
+  margin-left: 0;
+}
+
+.preview {
+  display: inline-block;
+  vertical-align: middle;
+}
+
+
+.media-item .media-filename {
+  background: #FFF;
+  color: #058AC5;
+  font-size: 10px;
+  padding: 0 3px;
+  white-space: nowrap;
+}
+
+.media-item .label-wrapper:hover .media-filename {
+  border: 1px solid #888;
+  display: inline-block;
+  margin: 0 0 -2px -1px;
+}
+
+.media-item.selected .media-filename {
+  background: #F4ECC7;
+}
+
+.media-item .media-filename:hover {
+  text-decoration: underline;
+}
+
+.media-modal-frame {
+  overflow: hidden;
+}
+
+.media-display-thumbnails .media-list-thumbnails {
+  margin: 0 -10px;
+}
+
+.media-list-thumbnails > li {
+  float: left;
+  list-style: none;
+}
+
+.media-list-thumbnails .form-type-checkbox {
+  margin: -35px 0 0 16px;
+  position:  absolute;
+}
+
+.media-item .media-type-icon {
+  border: none;
+  margin: 0 0 -5px;
+  width: auto;
+}
+
+.ui-dialog.media-wrapper .ui-dialog-buttonpane {
+  display: none;
+}
+
+.ui-widget.ui-widget-content.media-wrapper {
+  background: none;
+  border: none;
+}
+
+.media-thumbnails-select {
+  display: none;
+}
+
+html.js .media-thumbnails-select {
+  display: block;
+  float: left;
+}
+
+html.js .media-thumbnails-select ul {
+  padding: 0;
+  margin: 0 0 0 5px;
+  display: inline-block;
+}
+
+.media-thumbnails-select li {
+  display: inline-block;
+}
\ No newline at end of file
diff --git a/includes/media.admin.inc b/includes/media.admin.inc
index 5708e39..48c89ac 100644
--- a/includes/media.admin.inc
+++ b/includes/media.admin.inc
@@ -15,8 +15,7 @@ require_once dirname(__FILE__) . '/media.pages.inc';
  * Display the list or thumbnails media admin display.
  */
 function media_admin($form, $form_state) {
-  global $user;
-  
+  require_once dirname(__FILE__) . '/media.browser.inc';
   $path = drupal_get_path('module', 'media');
 
   $form['#attached'] = array(
@@ -29,37 +28,6 @@ function media_admin($form, $form_state) {
     return media_multiple_delete_confirm($form, $form_state, array_filter($form_state['values']['files']), 'admin/content/media', 'admin/content/media');
   }
 
-  require_once dirname(__FILE__) . '/media.browser.inc';
-  media_attach_browser_js($form);
-  
-  $types = media_display_types();
-
-  if (arg(3)) {
-    $display = arg(3);
-    if (!in_array($display, array_keys($types))) {
-      exit(drupal_not_found());
-    }
-    // Save their preference.
-    db_merge('media_list_type')
-      ->key(array('uid' => $user->uid))
-      ->fields(array(
-          'type' => $display,
-      ))
-      ->execute();
-  }
-  else {
-    $display = db_query("SELECT type FROM {media_list_type} WHERE uid = :uid", array(':uid' => $user->uid))->fetch();
-    if (!$display) {
-      $display = 'list';
-    }
-    else {
-      $display = $display->type;
-    }
-  }
-
-  // Build the display switch.
-  $form['switch'] = media_admin_display_switch(array('active display' => $display));
-
   // Build the 'Media operations' form.
   $form['options'] = array(
     '#type' => 'fieldset',
@@ -84,164 +52,13 @@ function media_admin($form, $form_state) {
     '#validate' => array('media_admin_validate'),
   );
 
-  include_once $types[$display]['file'];
-
-  $form['admin'] = $types[$display]['callback']();
-
-  return $form;
-}
-
-
-/**
- * Form builder: Builds the media list administration overview.
- */
-function media_admin_list() {
-  // @todo Change to media_variable_get('admin_pager_limit') for consistency
-  //   with browser_pager_limit?
-  $limit = variable_get('media_admin_limit', 50);
-
-  // Build the sortable table header.
-  $header = array(
-   'title' => array('data' => t('Title'), 'field' => 'f.filename'),
-   'type' => array('data' => t('Type'), 'field' => 'f.filemime'),
-   'size' => array('data' => t('Size'), 'field' => 'f.filesize'),
-   'author' => array('data' => t('Author'), 'field' => 'u.name'),
-   'timestamp' => array('data' => t('Updated'), 'field' => 'f.timestamp', 'sort' => 'asc'),
-   'operations' => array('data' => t('Operations')),
-  );
-
-  $query = db_select('file_managed', 'f')->extend('PagerDefault')->extend('TableSort');
-  $query->join('users', 'u', 'f.uid = u.uid');
-
-  $result = $query
-   ->fields('f')
-   ->fields('u', array('name'))
-   ->condition('f.status', FILE_STATUS_PERMANENT)
-   ->limit($limit)
-   ->orderByHeader($header)
-   ->execute();
-
-  $destination = drupal_get_destination();
-  $files = array();
-  $options = array();
-
-  foreach ($result as $file) {
-    $options[$file->fid] = array(
-      'title' => theme('media_link', array('file' => $file)),
-      'type' =>  check_plain($file->filemime),
-      'size' => format_size($file->filesize),
-      'author' => theme('username', array('account' => $file)),
-      'timestamp' => format_date($file->timestamp, 'short'),
-    );
-    $options[$file->fid]['operations'] = l(t('edit'), 'media/' . $file->fid . '/edit', array('query' => $destination));
-  }
-
-  $form['files'] = array(
-   '#type' => 'tableselect',
-   '#header' => $header,
-   '#options' => $options,
-   '#empty' => t('No media available.'),
-   '#attributes' => array('class' => array('media-display-table', 'media-clear')),
-  );
-  $form['pager'] = array('#markup' => theme('pager', array('tags' => NULL)));
-
-  return $form;
-}
-
-/**
- * Form builder: Builds the media thumbnails administration overview.
- */
-function media_admin_thumbnails() {
-  // @todo Change to media_variable_get('admin_pager_limit') for consistency
-  //   with browser_pager_limit?
-  $limit = variable_get('media_admin_limit', 50);
-
-  $query = new EntityFieldQuery();
-
-  $query->pager($limit);
-  $query->entityCondition('entity_type', 'media');
-  $query->propertyCondition('status', FILE_STATUS_PERMANENT);
-
-  $result = $query->execute();
-  $ids = key_exists('media', $result) ? array_keys($result['media']) : FALSE;
-  if ($ids) {
-    $media_entities = entity_load('media', $ids);
-  }
-  else {
-    $media_entities = array();
-  }
-
-  $destination = drupal_get_destination();
-  $rows = array();
-  $options = array();
-  $form['files'] = array(
-   '#tree' => TRUE,
-   '#prefix' => '<div class="media-display-thumbnails media-clear clearfix"><ul class="media-list-thumbnails">',
-   '#suffix' => '</ul></div>',
-  );
-
-  if (count($media_entities)) {
-    foreach ($media_entities as $media) {
-     $preview = media_get_thumbnail_preview($media, TRUE);
-     $form['files'][$media->fid] = array(
-       '#type' => 'checkbox',
-       '#title' => '',
-       '#prefix' => '<li>' . drupal_render($preview),
-       '#suffix' => '</li>',
-     );
-    }
-  }
-
-  $form['pager'] = array('#markup' => theme('pager', array('tags' => NULL)));
+  
+  $form['browser'] = media_build_browser();
 
   return $form;
 }
 
 /**
- * Build the display switch portion of the file listings form.
- */
-function media_admin_display_switch($options = array()) {
-  $options += array(
-    'form location' => 'admin/content/media',
-    'active display' => 'list',
-  );
-  $display_types = media_display_types();
-
-  // Build the item list.
-  $display_items = array();
-  foreach ($display_types as $delta => $item) {
-    $attributes = array('title' => $item['description']);
-
-    // Set a seperate icon for the active item.
-    if ($delta == $options['active display']) {
-      $icon = $item['icon_active'];
-      $attributes['class'][] = 'active';
-    }
-    else {
-      $icon = $item['icon'];
-    }
-
-    $display_items[] = array(
-      'data' => l(theme('image', array('path' => $icon, 'alt' => $item['title'])),
-        $options['form location'] . '/' . $delta,
-        array(
-          'html' => TRUE,
-          'attributes' => $attributes,
-        )),
-    );
-  }
-
-  return array(
-    '#type' => 'markup',
-    '#markup' => theme('item_list', array(
-       'items' => $display_items,
-       'attributes' => array('class' => 'media-display-switch'),
-      )
-    ),
-  );
-}
-
-/**
  * Validate media_admin_list form submissions.
  *
  * Check if any files have been selected to perform the chosen
diff --git a/includes/media.browser.inc b/includes/media.browser.inc
index 108c32c..729bf77 100644
--- a/includes/media.browser.inc
+++ b/includes/media.browser.inc
@@ -98,9 +98,239 @@ EOS;
   );
 
   $output['tabset']['plugins'] = $plugins;
+  
+  return ajax_render($output);
+  
   return $output;
 }
 
+function media_build_browser() {
+  // Attach browser JS
+  media_attach_browser_js($form);
+  
+  // Gather the display information.
+  $display = media_browser_get_display();
+  
+  // Build the display switch.
+  $build['switch'] = media_browser_display_switch(array('active display' => $display['name']));
+  
+  // Build the display.
+  include_once $display['file'];
+  $build['display'] = $display['callback']();
+  
+  return $build;
+}
+
+function media_browser_get_display() {
+  global $user;
+  $types = media_display_types();
+
+  if (arg(3)) {
+    $display = arg(3);
+    if (!in_array($display, array_keys($types))) {
+      exit(drupal_not_found());
+    }
+    // Save their preference.
+    db_merge('media_list_type')
+      ->key(array('uid' => $user->uid))
+      ->fields(array(
+          'type' => $display,
+      ))
+      ->execute();
+  }
+  else {
+    $display = db_query("SELECT type FROM {media_list_type} WHERE uid = :uid", array(':uid' => $user->uid))->fetch();
+    if (!$display) {
+      $display = 'list';
+    }
+    else {
+      $display = $display->type;
+    }
+  }
+  
+  $result = array();
+  foreach($types[$display] as $key => $data) {
+    $result[$key] = $data;
+  }
+  $result['name'] = $display;
+  
+  return $result;
+}
+
+/**
+ * Form builder: Builds the media list administration overview.
+ */
+function media_browser_table() {
+  // @todo Change to media_variable_get('admin_pager_limit') for consistency
+  //   with browser_pager_limit?
+  $limit = isset($_GET['limit']) ? $_GET['limit'] : variable_get('media_admin_limit', 50);
+  $types = isset($_GET['types']) ? $_GET['types'] : media_type_get_types();
+
+  // Build the sortable table header.
+  $header = array(
+   'title' => array('data' => t('Title'), 'field' => 'f.filename'),
+   'type' => array('data' => t('Type'), 'field' => 'f.filemime'),
+   'size' => array('data' => t('Size'), 'field' => 'f.filesize'),
+   'author' => array('data' => t('Author'), 'field' => 'u.name'),
+   'timestamp' => array('data' => t('Updated'), 'field' => 'f.timestamp', 'sort' => 'asc'),
+   'operations' => array('data' => t('Operations')),
+  );
+
+  $query = db_select('file_managed', 'f')->extend('PagerDefault')->extend('TableSort');
+  $query->join('users', 'u', 'f.uid = u.uid');
+
+  $result = $query
+   ->fields('f')
+   ->fields('u', array('name'))
+   ->condition('f.status', FILE_STATUS_PERMANENT)
+   ->limit($limit)
+   ->orderByHeader($header)
+   ->execute();
+
+  $destination = drupal_get_destination();
+  $files = array();
+  $options = array();
+
+  foreach ($result as $file) {
+    $options[$file->fid] = array(
+      'title' => theme('media_link', array('file' => $file)),
+      'type' =>  check_plain($file->filemime),
+      'size' => format_size($file->filesize),
+      'author' => theme('username', array('account' => $file)),
+      'timestamp' => format_date($file->timestamp, 'short'),
+    );
+    $options[$file->fid]['operations'] = l(t('edit'), 'media/' . $file->fid . '/edit', array('query' => $destination));
+  }
+
+  $form['files'] = array(
+   '#type' => 'tableselect',
+   '#header' => $header,
+   '#options' => $options,
+   '#empty' => t('No media available.'),
+   '#attributes' => array('class' => array('media-display-table', 'media-clear')),
+  );
+
+  return $form;
+}
+
+/**
+ * Form builder: Builds the media thumbnails administration overview.
+ */
+function media_browser_thumbnails() {
+  // @todo Change to media_variable_get('admin_pager_limit') for consistency
+  //   with browser_pager_limit?
+  $limit = isset($_GET['limit']) ? $_GET['limit'] : variable_get('media_admin_limit', 50);
+  $types = isset($_GET['types']) ? $_GET['types'] : media_type_get_types();
+
+  $query = new EntityFieldQuery();
+
+  $query->pager($limit);
+  $query->entityCondition('entity_type', 'media');
+  $query->propertyCondition('status', FILE_STATUS_PERMANENT);
+
+  $result = $query->execute();
+  $ids = key_exists('media', $result) ? array_keys($result['media']) : FALSE;
+  if ($ids) {
+    $media_entities = entity_load('media', $ids);
+  }
+  else {
+    $media_entities = array();
+  }
+  
+  $path = drupal_get_path('module', 'media');
+  $form['#attached'] = array(
+    'css' => array($path . '/css/media.thumbnails.css'),
+    'js' => array($path . '/js/media.thumbnails.js'),
+  );
+  
+  $form['select_all'] = array(
+    '#theme' => 'links',
+    '#links' => array(
+      'all' => array(
+        'title' => 'all,',
+        'href' => '#',
+        'attributes' => array('class' => array('select')),
+      ),
+      'none' => array(
+        'title' => 'none',
+        'href' => '#',
+        'attributes' => array('class' => array('deselect')),
+      ),
+    ),
+    '#prefix' => '<div class="media-thumbnails-select"><strong>' . t('Select') . ':</strong>',
+    '#suffix' => '</div>',
+  );
+
+  $destination = drupal_get_destination();
+  $rows = array();
+  $options = array();
+  $form['files'] = array(
+   '#tree' => TRUE,
+   '#prefix' => '<div class="media-display-thumbnails media-clear clearfix"><ul class="media-list-thumbnails">',
+   '#suffix' => '</ul></div>',
+  );
+
+  if (count($media_entities)) {
+    foreach ($media_entities as $media) {
+     $preview = media_get_thumbnail_preview($media, TRUE);
+     $form['files'][$media->fid] = array(
+       '#type' => 'checkbox',
+       '#title' => '',
+       '#prefix' => '<li>' . drupal_render($preview),
+       '#suffix' => '</li>',
+     );
+    }
+  }
+
+  $form['pager'] = array('#markup' => theme('pager', array('tags' => NULL)));
+
+  return $form;
+}
+
+/**
+ * Build the display switch portion of the file listings form.
+ */
+function media_browser_display_switch($options = array()) {
+  $options += array(
+    'form location' => 'admin/content/media',
+    'active display' => 'list',
+  );
+  $display_types = media_display_types();
+
+  // Build the item list.
+  $display_items = array();
+  foreach ($display_types as $delta => $item) {
+    $attributes = array('title' => $item['description']);
+
+    // Set a seperate icon for the active item.
+    if ($delta == $options['active display']) {
+      $icon = $item['icon_active'];
+      $attributes['class'][] = 'active';
+    }
+    else {
+      $icon = $item['icon'];
+    }
+
+    $display_items[] = array(
+      'data' => l(theme('image', array('path' => $icon, 'alt' => $item['title'])),
+        $options['form location'] . '/' . $delta,
+        array(
+          'html' => TRUE,
+          'attributes' => $attributes,
+        )),
+    );
+  }
+
+  return array(
+    '#type' => 'markup',
+    '#markup' => theme('item_list', array(
+       'items' => $display_items,
+       'attributes' => array('class' => 'media-display-switch'),
+      )
+    ),
+  );
+}
+
 /**
  * Provides a singleton of the params passed to the media browser.
  *
@@ -272,7 +502,8 @@ function media_media_browser_plugin_view($plugin_name, $params) {
         // We should probably change this to load dynamically when requested
         // via the JS file.
         ),
-        '#markup' => '<div id="container"><div id="scrollbox"><ul id="media-browser-library-list" class="media-list-thumbnails"></ul><div id="status"></div></div></div>',
+        #'#markup' => '<div id="container"><div id="scrollbox"><ul id="media-browser-library-list" class="media-list-thumbnails"></ul><div id="status"></div></div></div>',
+        '#markup' => media_browser_thumbnails(),
       );
     break;
   }
diff --git a/includes/media.theme.inc b/includes/media.theme.inc
index 3098152..1b5f373 100644
--- a/includes/media.theme.inc
+++ b/includes/media.theme.inc
@@ -189,7 +189,6 @@ function theme_media_admin_thumbnail_operations($variables) {
  * Add messages to the page.
  */
 function template_preprocess_media_dialog_page(&$variables) {
-  kpr($variables);
   $variables['messages'] = theme('status_messages');
 }
 
diff --git a/js/media.admin.js b/js/media.admin.js
index 291d4b2..8140210 100644
--- a/js/media.admin.js
+++ b/js/media.admin.js
@@ -22,29 +22,14 @@ Drupal.behaviors.mediaAdmin = {
         return confirm(Drupal.t('If you switch views, you will lose your selection.'));
       }
     }
-    
-    $('.media-display-switch a').bind('click', show_confirm_if_existing_selections)
+    $('.media-display-switch a', context).bind('click', show_confirm_if_existing_selections)
 
-    if ($('form.media-list-operation', context).length != 0) {
-      return;  
-    }
-    $('a[href="/admin/content/media/add"]').bind('click', Drupal.media.admin.addMedia);
-
-    if ($('body.page-admin-content-media-thumbnails').length != 0) {
-      // Implements 'select all/none' for thumbnail view.
-      // @TODO: Support grabbing more than one page of thumbnails.
-      Drupal.media.admin.selectAllNoneLinks();
-    }
-    
-    // If the media item is clicked anywhere other than on the image itself
-    // check the checkbox. For the record, JS thinks this is wonky.
-    $('.media-item').bind('click', Drupal.media.admin.itemClick);
+    $('a[href="/admin/content/media/add"]', context).bind('click', Drupal.media.admin.addMedia);
 
     // When any checkboxes are clicked on this form check to see if any are checked.
     // If any checkboxes are checked, show the edit options (@todo rename to edit-actions).
-    var $checkboxes = $('#media-admin :checkbox');
+    var $checkboxes = $('#media-admin :checkbox', context);
     $checkboxes.bind('change', Drupal.media.admin.showOrHideEditOptions);
-    $checkboxes.bind('change', Drupal.media.admin.itemChange);
     $checkboxes.trigger('change');
   }
 };
@@ -57,7 +42,8 @@ Drupal.media.admin.addMedia = function (event) {
   // For how it gets passed.
   var options = {
     disabledPlugins: ['library'],
-    multiselect: true
+    multiselect: true,
+    limit: 10
   };
   Drupal.media.popups.mediaBrowser(function (mediaFiles) {
     // When the media browser succeeds, we refresh
@@ -77,47 +63,6 @@ Drupal.media.admin.showOrHideEditOptions = function() {
   }
 };
 
-Drupal.media.admin.selectAllNoneLinks = function () {
-  var allLink = $('<a href="#">' + Drupal.t('all') + '</a>').click(Drupal.media.admin.selectAll);
-  var noneLink = $('<a href="#">' + Drupal.t('none') + '</a>').click(Drupal.media.admin.selectNone);
-  
-  // Add the select options
-  $('<div class="media-thumbnails-select" />')
-    .append('<strong>' + Drupal.t('Select') + ':</strong> ')
-    .append(allLink)
-    .append(', ')
-    .append(noneLink)
-    .prependTo('#media-admin > div')
-};
-
-Drupal.media.admin.selectAll = function (event) {
-  $('.media-display-thumbnails', $(this).parents('form')).find(':checkbox').attr('checked', true).change();
-  return false;
-};
-
-Drupal.media.admin.selectNone = function (event) {
-  $('.media-display-thumbnails', $(this).parents('form')).find(':checkbox').attr('checked', false).change();
-  return false;
-};
-
-Drupal.media.admin.itemClick = function (event) {
-  if ($(event.target).is('img, a')) {
-    return;
-  }
-  var checkbox = $(this).parent().find(':checkbox');
-  checkbox.attr('checked', !checkbox.attr('checked')).change();
-};
-
-Drupal.media.admin.itemChange = function (event) {
-  // Add an extra class to selected thumbnails.
-  var $target = $(event.target);
-  if ($target.attr('checked')) {
-    $target.parents('li').find('.media-item').addClass('selected');
-  } else {
-    $target.parents('li').find('.media-item').removeClass('selected');
-  }
-};
-
 /**
  * JavaScript for the Media types administrative form.
  */
diff --git a/js/media.thumbnails.js b/js/media.thumbnails.js
new file mode 100644
index 0000000..3e0372a
--- /dev/null
+++ b/js/media.thumbnails.js
@@ -0,0 +1,47 @@
+(function ($) {
+
+Drupal.media = Drupal.media || {};
+Drupal.media.thumbnails = Drupal.media.thumbnails || {};
+
+Drupal.behaviors.mediaThumbnails = {
+  attach: function (context) {
+    $('.media-thumbnails-select .select').bind('click', Drupal.media.thumbnails.selectAll);
+    $('.media-thumbnails-select .deselect').bind('click', Drupal.media.thumbnails.selectNone);
+    
+    // If the media item is clicked anywhere other than on the image itself
+    // check the checkbox. For the record, JS thinks this is wonky.
+    $('.media-item').bind('click', Drupal.media.thumbnails.itemClick);
+    
+    $('.media-item + * :checkbox').bind('change', Drupal.media.thumbnails.itemChange).trigger('change');
+  }
+}
+
+Drupal.media.thumbnails.selectAll = function (event) {
+  $('.media-display-thumbnails', $(this).parents('form')).find(':checkbox').attr('checked', true).change();
+  return false;
+};
+
+Drupal.media.thumbnails.selectNone = function (event) {
+  $('.media-display-thumbnails', $(this).parents('form')).find(':checkbox').attr('checked', false).change();
+  return false;
+};
+
+Drupal.media.thumbnails.itemClick = function (event) {
+  if ($(event.target).is('img, a')) {
+    return;
+  }
+  var checkbox = $(this).parent().find(':checkbox');
+  checkbox.attr('checked', !checkbox.attr('checked')).trigger('change');
+};
+
+Drupal.media.thumbnails.itemChange = function (event) {
+  // Add an extra class to selected thumbnails.
+  var $target = $(event.target);
+  if ($target.attr('checked')) {
+    $target.parents('li').find('.media-item').addClass('selected');
+  } else {
+    $target.parents('li').find('.media-item').removeClass('selected');
+  }
+};
+
+})(jQuery);
\ No newline at end of file
diff --git a/media.module b/media.module
index e6e0168..b441b64 100644
--- a/media.module
+++ b/media.module
@@ -143,6 +143,7 @@ function media_menu() {
     'type' => MENU_CALLBACK,
     'file' => 'includes/media.browser.inc',
     'theme callback' => 'media_dialog_get_theme_name',
+    'delivery callback' => 'ajax_deliver',
   );
 
   // A testbed to try out the media browser with different launch commands.
@@ -334,13 +335,6 @@ function media_theme() {
       'file' => 'includes/media.theme.inc',
     ),
 
-    // Dialog page.
-    'media_dialog_page' => array(
-      'render element' => 'page',
-      'template' => 'templates/media-dialog-page',
-      'file' => 'includes/media.theme.inc',
-    ),
-
     //
     'media_element' => array(
       'render element' => 'element',
@@ -488,7 +482,7 @@ function media_media_display_types() {
     'description' => t('Display as a list.'),
     'icon' => $path . '/images/display-list.png',
     'icon_active' => $path . '/images/display-list-active.png',
-    'callback' => 'media_admin_list',
+    'callback' => 'media_browser_table',
     'file' => drupal_get_path('module', 'media') . '/includes/media.admin.inc',
   );
 
@@ -497,7 +491,7 @@ function media_media_display_types() {
     'description' => t('Display as thumbnails.'),
     'icon' => $path . '/images/display-thumbnails.png',
     'icon_active' => $path . '/images/display-thumbnails-active.png',
-    'callback' => 'media_admin_thumbnails',
+    'callback' => 'media_browser_thumbnails',
     'file' => drupal_get_path('module', 'media') . '/includes/media.admin.inc',
   );
 
@@ -776,7 +770,7 @@ function media_library() {
   $libraries['media_browser'] = array(
     'title' => 'Media Browser popup libraries',
     'js' => array(
-      $path . '/js/media.popups.js' => array('group' => JS_DEFAULT),
+      $path . '/js/media.dialog.js' => array('group' => JS_DEFAULT),
     ),
     'dependencies' => array(
       array('media', 'media_base'),
-- 
1.7.4.msysgit.0

