JAVASCRIPT   26
Custom Media Library modal
Guest on 18th September 2023 08:07:06 AM


  1. /**
  2.  * Custom Media Library modal window interaction
  3.  *
  4.  * @package SlideDeck
  5.  * @author Hummingbird Web Solutions Pvt. Ltd.
  6.  * @version 1.0.0
  7.  */
  8.  
  9. var SlideDeckMediaLibrary = function(){
  10.     // Class for single add buttons
  11.     this.singleAddClass = "add-to-slidedeck-button";
  12.     // ID for multi add button
  13.     this.addAllId = "slidedeck-add-all-images";
  14.     // ID for add-selected button
  15.     this.addSelectedId = "slidedeck-add-selected-images";
  16.     // Class for add multiple checkboxes
  17.     this.addMultipleCheckboxClass = "slidedeck-add-multiple";
  18.     // Buttons used by this Class
  19.     this.buttons = {};
  20.     // Current Add Media tab
  21.     this.tab = "upload";
  22.    
  23.     // The current slide ID
  24.     this.slideId = -1;
  25.    
  26.     // Image container on parent document
  27.     this.imageContainer;
  28.     // Content Source Flyout
  29.     this.contentSource;
  30.    
  31.     // Initiate Class
  32.     this.__construct();
  33. };
  34.  
  35. (function($, window, undefined){
  36.     // Class construct routine
  37.     SlideDeckMediaLibrary.prototype.__construct = function(){
  38.         var self = this;
  39.        
  40.         // This isn't a SlideDeck media upload, do NOT process further
  41.         if(!parent.document.location.search.match(/page\=slidedeck\.php/))
  42.             return false;
  43.        
  44.         if(parent.jQuery('input[name="source[]"]').val() != "custom")
  45.             return false;
  46.        
  47.         this.isBulkUpload = (document.location.search.match(/slidedeck_bulkupload\=1/) != undefined);
  48.        
  49.         $(document).ready(function(){
  50.             self.initialize();
  51.         });
  52.     };
  53.    
  54.     // Add images to the SlideDeck - accepts a single ID or an array of IDs
  55.     SlideDeckMediaLibrary.prototype.addImage = function(mediaId){
  56.         var self = this;
  57.        
  58.         var queryString = 'action=slidedeck_slide_add_from_medialibrary';
  59.             queryString += '&slide_id=' + this.slideId;
  60.             queryString += '&media_id=' + mediaId;
  61.             queryString += '&_wpnonce=' + _medialibrary_nonce;
  62.        
  63.         $.ajax({
  64.             url: ajaxurl,
  65.             data: queryString,
  66.             dataType: "json",
  67.             success: function(data){
  68.                 if(data.valid === true){
  69.                     var $thumbnail = parent.jQuery('#slidedeck-custom-slide-editor-form').find('.sd-flyout-thumbnail');
  70.                     var label = data.filename.length > 50 ? data.filename.substr(0,50) + "…" : data.filename;
  71.                     $thumbnail.find('img').attr('src', data.media_meta.src[0]);
  72.                     $thumbnail.find('.label').html(label);
  73.                     $thumbnail.slideDown(500);
  74.                     parent.jQuery('#sd-image-upload-container, #sd-image-upload, #slidedeck-custom-slide-editor-form .select-source').slideUp(500);
  75.  
  76.                     self.fetchImageMeta(mediaId);
  77.                 }
  78.             }
  79.         });
  80.     };
  81.    
  82.     SlideDeckMediaLibrary.prototype.fetchImageMeta = function(mediaId){
  83.         var self = this;
  84.         var $form = parent.jQuery('#slidedeck-custom-slide-editor-form');
  85.        
  86.         var queryString = 'action=slidedeck_query_image_from_medialibrary';
  87.             queryString += '&media_id=' + mediaId;
  88.             queryString += '&_wpnonce=' + _medialibrary_nonce;
  89.        
  90.         parent.jQuery.ajax({
  91.             url: ajaxurl,
  92.             data: queryString,
  93.             dataType: "json",
  94.             success: function(response){
  95.                 $form.find('input[name="post_title"]').val(response.data.title);
  96.                 var mceInstance = null;
  97.                 if( typeof(parent.tinyMCE) !== 'undefined' ) {
  98.                     mceInstance = parent.tinyMCE;
  99.                 } else {
  100.                     mceInstance = tinyMCE;
  101.                 }
  102.                 mceInstance.activeEditor.setContent(response.data.excerpt);
  103.  
  104.                 // Close the Thickbox
  105.                 parent.tb_remove();
  106.             }
  107.         });
  108.     };
  109.  
  110.     SlideDeckMediaLibrary.prototype.addImages = function(mediaIds){
  111.         var self = this;
  112.        
  113.         var queryString = 'action=slidedeck_slide_bulk_upload';
  114.             queryString += '&slidedeck=' + this.slidedeckId;
  115.             for (var i=0; i < mediaIds.length; i++) {
  116.                 queryString += '&media[]=' + mediaIds[i];
  117.             };
  118.             queryString += '&_wpnonce=' + _medialibrary_nonce;
  119.        
  120.         $.ajax({
  121.             url: ajaxurl,
  122.             type: 'post',
  123.             data: queryString,
  124.             dataType: "json",
  125.             success: function(data){
  126.                 if(data.valid === true){
  127.                     parent.SlideDeckSourceCustom.updateContentControl(data.html);
  128.                     parent.SlideDeckSourceCustom.close();
  129.                     parent.tb_remove();
  130.                 }
  131.             }
  132.         });
  133.     };
  134.    
  135.     // Bind all submission events to appropriate buttons
  136.     SlideDeckMediaLibrary.prototype.bind = function(){
  137.         var self = this;
  138.        
  139.         $('body').delegate('.' + this.singleAddClass, 'click', function(event){
  140.             event.preventDefault();
  141.            
  142.             var mediaId = $(this).val();
  143.             // Bulk upload single image insertion choice
  144.             if(isNaN(mediaId)){
  145.                 mediaId = $.data(this, 'mediaId');
  146.                 self.addImages([mediaId]);
  147.             }
  148.             // Single slide "From Media Library" image source choice
  149.             else {
  150.                 self.addImage(mediaId);
  151.             }
  152.         });
  153.        
  154.         $('#' + this.addAllId).bind('click', function(event){
  155.             event.preventDefault();
  156.            
  157.             var mediaIds = [];
  158.             $('.' + self.singleAddClass).each(function(ind){
  159.                 var mediaId = $.data(this, 'mediaId');
  160.                 mediaIds.push(mediaId);
  161.             });
  162.            
  163.             self.addImages(mediaIds);
  164.         });
  165.        
  166.         $('#' + this.addSelectedId).bind('click', function(event){
  167.             event.preventDefault();
  168.            
  169.             var mediaIds = [];
  170.             $('.' + self.addMultipleCheckboxClass).each(function(ind){
  171.                 if(this.checked)
  172.                     mediaIds.push(this.value);
  173.             });
  174.            
  175.             self.addImages(mediaIds);
  176.         });
  177.     };
  178.    
  179.     // Route which tab initialize routine to run
  180.     SlideDeckMediaLibrary.prototype.initialize = function(){
  181.         // Get the current tab
  182.         var location = document.location.search.match(/tab\=([a-zA-Z0-9\-_]+)/),
  183.             styles = '';
  184.         if(location)
  185.             this.tab = location[1];
  186.        
  187.         // Do actions for regular single file choice
  188.         if(!this.isBulkUpload){
  189.             this.initializeSingleChoice();
  190.         }
  191.         // Process as regular bulk upload
  192.         else {
  193.             this.initializeBulkUpload();
  194.         }
  195.  
  196.         styles += '<style type="text/css">';
  197.         styles += '#gallery-settings,#save-all,#gallery-form table.widefat,#sort-buttons,#save,#filter>.subsubsub,.menu_order,.media-item table.describe > tbody tr[class] {display:none !important;}';
  198.         styles += '.media-upload-form {background: #fff;}';
  199.         styles += '#media-upload #filter {position: relative;}';
  200.         styles += '.slidedeck-add-multiple, .add-to-slidedeck-button {margin-top: 6px;}';
  201.         styles += '.tablenav .tablenav-pages {height: auto; width: auto; margin-bottom: 0;}';
  202.         styles += 'p.search-box {display: none;}';
  203.         styles += 'form {padding-top: 1em; margin-top: 0;}';
  204.         styles += '</style>';
  205.  
  206.         $('head').append(styles);
  207.        
  208.         switch(this.tab){
  209.             case "upload":
  210.             case "type":
  211.                 this.tabUpload();
  212.             break;
  213.            
  214.             case "gallery":
  215.             case "library":
  216.                 this.tabLibrary();
  217.             break;
  218.         }
  219.     };
  220.    
  221.     SlideDeckMediaLibrary.prototype.initializeSingleChoice = function(){
  222.         this.imageContainer = parent.jQuery('#slidedeck-medialibrary-images');
  223.         this.contentSource = parent.jQuery('#slidedeck-content-source');
  224.        
  225.         // This slide's ID
  226.         this.slideId = document.location.search.match(/slide_id=([0-9]+)/)[1];
  227.         $('#filter').append('<input id="slide_id" type="hidden" name="slide_id" value="' + this.slideId + '" />');
  228.        
  229.         // The parent post's ID
  230.         this.slidedeckId = document.location.search.match(/post_id=([0-9]+)/)[1];
  231.  
  232.         // Add the SlideDeck UI type
  233.         this.addSlideDeckUIField( 'slidedeck_custom' );
  234.        
  235.         // Hide the navigation tabs to prevent confusion
  236.         $('#media-upload-header').remove();
  237.     };
  238.    
  239.     SlideDeckMediaLibrary.prototype.initializeBulkUpload = function(){
  240.         this.slidedeckId = document.location.search.match(/post_id=([0-9]+)/)[1];
  241.        
  242.         // Add the SlideDeck UI type
  243.         this.addSlideDeckUIField( 'slidedeck_bulkupload' );
  244.        
  245.         // Remove the single URL tab
  246.         $('#media-upload-header').find('#tab-type_url').remove();
  247.     };
  248.    
  249.     // Adds the hidden field to keep track of the SlideDeck UI
  250.     SlideDeckMediaLibrary.prototype.addSlideDeckUIField = function( keyName ){
  251.         $('#slidedeck_ui').remove();
  252.         $('#filter').append('<input id="slidedeck_ui" type="hidden" name="' + keyName + '" value="1" />');
  253.     };
  254.    
  255.     // Method for replacing "Insert into Post" buttons with "Add to SlideDeck" buttons
  256.     SlideDeckMediaLibrary.prototype.replaceButton = function(el){
  257.         var $button = $(el);
  258.         var buttonId = $button.attr('id');
  259.         var mediaId = buttonId.match(/\[(\d+)\]/)[1];
  260.        
  261.         $button.replaceWith('<input type="hidden" id="' + buttonId + '" class="add-to-slidedeck-button" value="Add to SlideDeck" />');
  262.        
  263.         // Map the mediaId for the image as a data property for access later
  264.         $.data(document.getElementById(buttonId), 'mediaId', mediaId);
  265.     };
  266.    
  267.     // Media Library tab
  268.     SlideDeckMediaLibrary.prototype.tabLibrary = function(){
  269.         var self = this;
  270.         var $mediaItems = $('#media-items');
  271.         var $buttons = $mediaItems.find('input[type="submit"]');
  272.        
  273.         $buttons.each(function(ind){
  274.             self.replaceButton(this);
  275.         });
  276.        
  277.         $mediaItems.find('.toggle.describe-toggle-on').each(function(){
  278.             var $this = $(this);
  279.             var mediaId = $this.closest('.media-item').attr('id').split('-')[2];
  280.            
  281.             if(self.isBulkUpload){
  282.                 $this.before('<input type="checkbox" value="' + mediaId + '" class="' + self.addMultipleCheckboxClass + '" style="float:right;margin:12px 15px 0 5px;" />');
  283.             } else {
  284.                 $this.before('<button value="' + mediaId + '" class="' + self.singleAddClass + '" style="float:right;margin:7px 15px 0 5px;">Add to SlideDeck</button>');
  285.             }
  286.         });
  287.        
  288.         if(this.isBulkUpload){
  289.             $mediaItems.find('.media-item:first-child').before('<p style="margin:5px;text-align:right;"><label style="margin-right:8px;font-weight:bold;font-style:italic;">Select All to add to SlideDeck <input type="checkbox" id="slidedeck-add-multiple-select-all" style="margin-left:5px;" /></label></p>');
  290.             $('#slidedeck-add-multiple-select-all').bind('click', function(event){
  291.                 var selectAll = this;
  292.                
  293.                 $mediaItems.find('.' + self.addMultipleCheckboxClass).each(function(){
  294.                     this.checked = selectAll.checked;
  295.                 });
  296.             });
  297.            
  298.             $('.ml-submit').append('<a href="#" id="' + this.addSelectedId + '" class="button">Add Selected to SlideDeck</a>');
  299.         }
  300.        
  301.         this.bind();
  302.     };
  303.    
  304.     // Upload tab
  305.     SlideDeckMediaLibrary.prototype.tabUpload = function(){
  306.         $('.savebutton.ml-submit').append('<a href="#" id="' + this.addAllId + '" class="button" style="margin-left: 10px;">Add all to SlideDeck</a>');
  307.        
  308.         new this.Watcher('image-form');
  309.        
  310.         this.bind();
  311.     };
  312.    
  313.     // Watcher Class for Upload tab - watches for addition of "Insert into Post" buttons to replace them
  314.     SlideDeckMediaLibrary.prototype.Watcher = function(el){
  315.         var self = this;
  316.         this.el = document.getElementById(el);
  317.        
  318.         this.getButtons = function(){
  319.             var inputs = self.el.getElementsByTagName('input'),
  320.                 count = 0,
  321.                 buttons = [];
  322.                
  323.             for(var i in inputs){
  324.                 if(inputs[i].type == "submit" && inputs[i].id.match(/send\[(\d+)\]/)){
  325.                     buttons.push(inputs[i]);
  326.                 }
  327.             }
  328.            
  329.             return buttons;
  330.         };
  331.        
  332.         this.checker = function(){
  333.             var buttons = self.getButtons();
  334.            
  335.             for(var b in buttons){
  336.                 SlideDeckMediaLibrary.prototype.replaceButton(buttons[b]);
  337.             }
  338.         };
  339.        
  340.         this.interval = setInterval(this.checker, 100);
  341.     };
  342.    
  343.     new SlideDeckMediaLibrary();
  344. })(jQuery, window, null);

Raw Paste

Login or Register to edit or fork this paste. It's free.