balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

file.js (3894B)


      1 ( function ( $, rwmb ) {
      2 	'use strict';
      3 
      4 	var file = {};
      5 
      6 	/**
      7 	 * Handles a click on add new file.
      8 	 * Expects `this` to equal the clicked element.
      9 	 *
     10 	 * @param event Click event.
     11 	 */
     12 	file.addHandler = function ( event ) {
     13 		event.preventDefault();
     14 
     15 		var $this = $( this ),
     16 			$clone = $this.prev().clone();
     17 
     18 		$clone.insertBefore( this ).val( '' );
     19 
     20 		var $fieldInput = $this.closest( '.rwmb-input' );
     21 		file.updateVisibility.call( $fieldInput.find( '.rwmb-files' ) );
     22 		file.setRequired.call( $fieldInput );
     23 	};
     24 
     25 	/**
     26 	 * Handles a click on delete new file.
     27 	 * Expects `this` to equal the clicked element.
     28 	 *
     29 	 * @param event Click event.
     30 	 */
     31 	file.deleteHandler = function ( event ) {
     32 		event.preventDefault();
     33 
     34 		var $this = $( this ),
     35 			$item = $this.closest( 'li' ),
     36 			$uploaded = $this.closest( '.rwmb-files' ),
     37 			$metaBox = $uploaded.closest( '.rwmb-meta-box' );
     38 
     39 		$item.remove();
     40 		file.updateVisibility.call( $uploaded );
     41 
     42 		file.setRequired.call( $uploaded.parent() );
     43 
     44 		if ( 1 > $uploaded.data( 'force_delete' ) ) {
     45 			return;
     46 		}
     47 
     48 		$.post( ajaxurl, {
     49 			action: 'rwmb_delete_file',
     50 			_ajax_nonce: $uploaded.data( 'delete_nonce' ),
     51 			field_id: $uploaded.data( 'field_id' ),
     52 			field_name: $uploaded.data( 'field_name' ),
     53 			object_type: $metaBox.data( 'object-type' ),
     54 			object_id: $metaBox.data( 'object-id' ),
     55 			attachment_id: $this.data( 'attachment_id' )
     56 		}, function ( response ) {
     57 			if ( ! response.success ) {
     58 				alert( response.data );
     59 			}
     60 		}, 'json' );
     61 	};
     62 
     63 	/**
     64 	 * Sort uploaded files.
     65 	 * Expects `this` to equal the uploaded file list.
     66 	 */
     67 	file.sort = function () {
     68 		$( this ).sortable( {
     69 			items: 'li',
     70 			start: function ( event, ui ) {
     71 				ui.placeholder.height( ui.helper.outerHeight() );
     72 				ui.placeholder.width( ui.helper.outerWidth() );
     73 			},
     74 			update: function( event, ui ) {
     75 				ui.item.find( rwmb.inputSelectors ).first().trigger( 'mb_change' );
     76 			}
     77 		} );
     78 	};
     79 
     80 	/**
     81 	 * Update visibility of upload inputs and Add new file link.
     82 	 * Expect this equal to the uploaded file list.
     83 	 */
     84 	file.updateVisibility = function () {
     85 		var $uploaded = $( this ),
     86 			max = parseInt( $uploaded.data( 'max_file_uploads' ), 10 ),
     87 			$new = $uploaded.siblings( '.rwmb-file-new' ),
     88 			$add = $new.find( '.rwmb-file-add' ),
     89 			numFiles = $uploaded.children().length,
     90 			numInputs = $new.find( '.rwmb-file-input' ).length;
     91 
     92 		$uploaded.toggle( 0 < numFiles );
     93 		if ( 0 === max ) {
     94 			return;
     95 		}
     96 		$new.toggle( numFiles < max );
     97 		$add.toggle( numFiles + numInputs < max );
     98 	};
     99 
    100 	// Reset field when cloning.
    101 	file.resetClone = function() {
    102 		var $this = $( this ),
    103 			$clone = $this.closest( '.rwmb-clone' ),
    104 			$list = $clone.find( '.rwmb-files' ),
    105 			$key = $clone.find( '.rwmb-file-index' ),
    106 			inputName = '_file_' + rwmb.uniqid();
    107 
    108 		$list.empty();
    109 		$clone.find( '.rwmb-file-input' ).attr( 'name', inputName + '[]' ).not( ':first' ).remove();
    110 
    111 		$key.val( inputName );
    112 
    113 		file.updateVisibility.call( $list );
    114 	};
    115 
    116 	// Set 'required' attribute. 'this' is the wrapper field input.
    117 	file.setRequired = function() {
    118 		var $this = $( this ),
    119 			$uploaded = $this.find( '.rwmb-files' ),
    120 			$inputs = $this.find( '.rwmb-file-new input' );
    121 		$inputs.prop( 'required', false );
    122 
    123 		if ( $uploaded.children().length ) {
    124 			return;
    125 		}
    126 
    127 		var $firstInput = $inputs.first();
    128 		if ( 1 === $firstInput.data( 'required' ) ) {
    129 			$firstInput.prop( 'required', true );
    130 		}
    131 	};
    132 
    133 	function init( e ) {
    134 		var $el = $( e.target ),
    135 			$uploaded = $el.find( '.rwmb-files' );
    136 
    137 		$uploaded.each( file.sort );
    138 		$uploaded.each( file.updateVisibility );
    139 
    140 		$el.find( '.rwmb-file-wrapper, .rwmb-image-wrapper' ).each( file.setRequired );
    141 	}
    142 
    143 	rwmb.$document
    144 		.on( 'mb_ready', init )
    145 		.on( 'click', '.rwmb-file-add', file.addHandler )
    146 		.on( 'click', '.rwmb-file-delete', file.deleteHandler )
    147 		.on( 'clone', '.rwmb-file-input', file.resetClone );
    148 } )( jQuery, rwmb );