# wpdirectorykit/1.2.7/admin/js/jquery.wpmediaelement_file.js

WP Directory Kit, version 1.2.7. 109 lines.

- Page: https://pluginprobe.com/plugins/wpdirectorykit/1.2.7/code/admin/js/jquery.wpmediaelement_file.js
- Raw: https://pluginprobe.com/plugins/wpdirectorykit/1.2.7/raw/admin/js/jquery.wpmediaelement_file.js
- Modified: 2023-10-17T19:27:58+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/wpdirectorykit/1.2.7/code/admin/js/jquery.wpmediaelement_file.js#L10-L20`.

```javascript
"use strict";

jQuery.fn.wpMediaElementFile = function (options) 
{
    var defaults = {
        obj: null,
        mediaControl: null,
        addImgLinkSelector: '.upload-custom-img',
        delImgLinkSelector: '.delete-custom-img',
        imgContainerSelector: '.custom-img-container',
        imgIdInputSelector: '.logo_file_id',
        addImgLink: null,
        delImgLink: null,
        imgContainer: null,
        imgIdInput: null,
        frame: null
    };
    var options = jQuery.extend(defaults, options);
    
    /* Public API */
    this.getCurrent = function()
    {
        return options.obj;
    }
        
    return this.each (function () 
    {
        options.obj = jQuery(this);

        options.addImgLink = options.obj.find(options.addImgLinkSelector);
        options.delImgLink = options.obj.find(options.delImgLinkSelector);
        options.imgContainer = options.obj.find(options.imgContainerSelector);
        options.imgIdInput = options.obj.find(options.imgIdInputSelector);

        init_start();

        return this;
    });

    function init_start()
    {
		options.frame = wp.media({
			title: 'Select or Upload File Of Your Chosen Persuasion',
			library: {
				type: 'image'
			},
			button: {
				text: 'Use this media'
			},
			multiple: false
		});
        
        options.frame.on( 'open', updateFrame ).state('library').on( 'select', selectImg );
        
		options.addImgLink.on( 'click', function( e ) {
			e.preventDefault();
			
			options.frame.open();
		});
        
        // DELETE IMAGE LINK
        options.delImgLink.on( 'click', function( event ){
            
            event.preventDefault();
            
            // Clear out the preview image
            options.imgContainer.html( '' );
            
            // Un-hide the add image link
            options.addImgLink.removeClass( 'hidden' );
            
            // Hide the delete image link
            options.delImgLink.addClass( 'hidden' );
            
            // Delete the image id from the hidden input
            options.imgIdInput.val( '' );
            
            //trigger update widget
            options.imgIdInput.change();
        });

    }
    
    function updateFrame()
    {
        // Do something when the media frame is opened.
    }
    
    function selectImg()
    {
      // Get media attachment details from the frame state
      var attachment = options.frame.state().get('selection').first().toJSON();
      // Send the attachment URL to our custom image input field.
      options.imgContainer.append( attachment.name );
    
      // Send the attachment id to our hidden input
      options.imgIdInput.val( attachment.id );
    
      // Hide the add image link
      options.addImgLink.addClass( 'hidden' );
    
      // Unhide the remove image link
      options.delImgLink.removeClass( 'hidden' );
      
      //trigger update widget
      options.imgIdInput.change();
    }

}
```
