From e37c1b2e8d6b65fde04d82471a0d3466a24f7fd0 Mon Sep 17 00:00:00 2001
From: James Elliott <james.elliott@acquia.com>
Date: Tue, 12 Apr 2011 08:20:36 -0400
Subject: [PATCH 05/15] #1124682 UnWTFing media.admin.js

---
 js/media.admin.js |  160 ++++++++++++++++++++++++++---------------------------
 media.module      |   13 +++-
 2 files changed, 88 insertions(+), 85 deletions(-)

diff --git a/js/media.admin.js b/js/media.admin.js
index dd8f6b6..291d4b2 100644
--- a/js/media.admin.js
+++ b/js/media.admin.js
@@ -10,6 +10,7 @@
 (function ($) {
 
 Drupal.media = Drupal.media || {};
+Drupal.media.admin = Drupal.media.admin || {};
 
 /**
  * Functionality for the thumbnail display
@@ -23,104 +24,99 @@ Drupal.behaviors.mediaAdmin = {
     }
     
     $('.media-display-switch a').bind('click', show_confirm_if_existing_selections)
-    // Configure the "Add file" link to fire the media browser popup.
-    $('ul.action-links li', context).remove();
+
     if ($('form.media-list-operation', context).length != 0) {
       return;  
     }
-    var $launcherLink = $('<a class="media-launcher" href="#"></a>').html('Add file');
-    $launcherLink.bind('click', function () {
-      // This option format needs *serious* work.
-      // Not even bothering documenting it because it needs to be thrown.
-      // See media.browser.js and media.browser.inc - media_browser()
-      // For how it gets passed.
-      var options = {
-        disabledPlugins: ['library'],
-        multiselect: true
-      };
-      Drupal.media.popups.mediaBrowser(function (mediaFiles) {
-        // When the media browser succeeds, we refresh
-        // @TODO: Should jump to the new media file and perhaps highlight it.
-        parent.window.location.reload();
-        return false;
-      }, options);
-    });
-    $('ul.action-links', context).append($('<li></li>').append($launcherLink));
-
+    $('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.
-      var allLink = $('<a href="#">' + Drupal.t('all') + '</a>')
-        .click(function () {
-          $('.media-display-thumbnails', $(this).parents('form')).find(':checkbox').attr('checked', true).change();
-          return false;
-        });
-      var noneLink = $('<a href="#">' + Drupal.t('none') + '</a>')
-        .click(function () {
-          $('.media-display-thumbnails', $(this).parents('form')).find(':checkbox').attr('checked', false).change();
-          return false;
-        });
-      $('<div class="media-thumbnails-select" />')
-        .append('<strong>' + Drupal.t('Select') + ':</strong> ')
-        .append(allLink)
-        .append(', ')
-        .append(noneLink)
-        .prependTo('#media-admin > div')
-      // 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', function (e) {
-        if ($(e.target).is('img, a')) {
-          return;
-        }
-        var checkbox = $(this).parent().find(':checkbox');
-        if (checkbox.is(':checked')) {
-          checkbox.attr('checked', false).change();
-        } else {
-          checkbox.attr('checked', true).change();
-        }
-      });
-  
-      // Add an extra class to selected thumbnails.
-      $('.media-display-thumbnails :checkbox').each(function () {
-        var checkbox = $(this);
-        if (checkbox.is(':checked')) {
-          $(checkbox.parents('li').find('.media-item')).addClass('selected');
-        }
-  
-        checkbox.bind('change.media', function () {
-          if (checkbox.is(':checked')) {
-            $(checkbox.parents('li').find('.media-item')).addClass('selected');
-          }
-          else {
-            $(checkbox.parents('li').find('.media-item')).removeClass('selected');
-          }
-        });
-      });
+      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);
 
     // 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).
-    $('#media-admin :checkbox').bind('change', function () {
-      Drupal.behaviors.mediaAdmin.showOrHideEditOptions();
-    });
+    var $checkboxes = $('#media-admin :checkbox');
+    $checkboxes.bind('change', Drupal.media.admin.showOrHideEditOptions);
+    $checkboxes.bind('change', Drupal.media.admin.itemChange);
+    $checkboxes.trigger('change');
+  }
+};
 
-    Drupal.behaviors.mediaAdmin.showOrHideEditOptions();
-  },
+Drupal.media.admin.addMedia = function (event) {
+  event.preventDefault();
+  // This option format needs *serious* work.
+  // Not even bothering documenting it because it needs to be thrown.
+  // See media.browser.js and media.browser.inc - media_browser()
+  // For how it gets passed.
+  var options = {
+    disabledPlugins: ['library'],
+    multiselect: true
+  };
+  Drupal.media.popups.mediaBrowser(function (mediaFiles) {
+    // When the media browser succeeds, we refresh
+    // @TODO: Should jump to the new media file and perhaps highlight it.
+    parent.window.location.reload();
+    return false;
+  }, options);
+};
 
-  // Checks if any checkboxes on the form are checked, if so it will show the
-  // edit-options panel.
-  showOrHideEditOptions: function() {
-    var fieldset = $('#edit-options');
-    if (!$('#media-admin input[type=checkbox]:checked').size()) {
-      fieldset.slideUp('fast');
-    }
-    else {
-      fieldset.slideDown('fast');
-    }
+Drupal.media.admin.showOrHideEditOptions = function() {
+  var fieldset = $('#edit-options');
+  if (!$('#media-admin input[type=checkbox]:checked').length) {
+    fieldset.slideUp('fast');
+  }
+  else {
+    fieldset.slideDown('fast');
   }
 };
 
+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/media.module b/media.module
index ff1506c..e6e0168 100644
--- a/media.module
+++ b/media.module
@@ -123,6 +123,16 @@ function media_menu() {
     'access arguments' => array('import media'),
     'type' => MENU_LOCAL_ACTION,
   );
+  
+  $items['admin/content/media/add'] = array(
+    'title' => 'Add media',
+    'description' => 'Import files into your media library.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('media_add_upload'),
+    'file' => 'includes/media.pages.inc',
+    'access arguments' => array('import media'),
+    'type' => MENU_LOCAL_ACTION,
+  );
 
   $items['media/browser'] = array(
     'title' => 'Media browser',
@@ -235,9 +245,6 @@ function media_menu() {
      // Use the same setting as node module
     'theme callback' => '_node_custom_theme',
   );
-
-  $items['admin/content/media/browser'] = $items['media/browser'];
-  $items['admin/content/media/browser']['type'] = MENU_LOCAL_ACTION;
   
   $items['media/js'] = array(
     'page callback' => 'media_preview_ajax',
-- 
1.7.4.msysgit.0

