( function ( $, rwmb ) { 'use strict'; var file = {}; /** * Handles a click on add new file. * Expects `this` to equal the clicked element. * * @param event Click event. */ file.addHandler = function ( event ) { event.preventDefault(); var $this = $( this ), $clone = $this.prev().clone(); $clone.insertBefore( this ).val( '' ); var $fieldInput = $this.closest( '.rwmb-input' ); file.updateVisibility.call( $fieldInput.find( '.rwmb-files' ) ); file.setRequired.call( $fieldInput ); }; /** * Handles a click on delete new file. * Expects `this` to equal the clicked element. * * @param event Click event. */ file.deleteHandler = function ( event ) { event.preventDefault(); var $this = $( this ), $item = $this.closest( 'li' ), $uploaded = $this.closest( '.rwmb-files' ), $metaBox = $uploaded.closest( '.rwmb-meta-box' ); $item.remove(); file.updateVisibility.call( $uploaded ); file.setRequired.call( $uploaded.parent() ); if ( 1 > $uploaded.data( 'force_delete' ) ) { return; } $.post( ajaxurl, { action: 'rwmb_delete_file', _ajax_nonce: $uploaded.data( 'delete_nonce' ), field_id: $uploaded.data( 'field_id' ), field_name: $uploaded.data( 'field_name' ), object_type: $metaBox.data( 'object-type' ), object_id: $metaBox.data( 'object-id' ), attachment_id: $this.data( 'attachment_id' ) }, function ( response ) { if ( !response.success ) { alert( response.data ); } }, 'json' ); }; /** * Sort uploaded files. * Expects `this` to equal the uploaded file list. */ file.sort = function () { $( this ).sortable( { items: 'li', start: function ( event, ui ) { ui.placeholder.height( ui.helper.outerHeight() ); ui.placeholder.width( ui.helper.outerWidth() ); }, update: function ( event, ui ) { ui.item.find( rwmb.inputSelectors ).first().trigger( 'mb_change' ); } } ); }; /** * Update visibility of upload inputs and Add new file link. * Expect this equal to the uploaded file list. */ file.updateVisibility = function () { var $uploaded = $( this ), max = parseInt( $uploaded.data( 'max_file_uploads' ), 10 ), $new = $uploaded.siblings( '.rwmb-file-new' ), $add = $new.find( '.rwmb-file-add' ), numFiles = $uploaded.children().length, numInputs = $new.find( '.rwmb-file-input' ).length; $uploaded.toggle( 0 < numFiles ); if ( 0 === max ) { return; } $new.toggle( numFiles < max ); $add.toggle( numFiles + numInputs < max ); }; // Reset field when cloning. file.resetClone = function () { var $this = $( this ), $clone = $this.closest( '.rwmb-clone' ), $list = $clone.find( '.rwmb-files' ); $list.empty(); $clone.find( '.rwmb-file-new' ).each( function () { var inputName = '_file_' + rwmb.uniqid(), $key = $( this ).siblings( '.rwmb-file-index' ); $( this ).find( '.rwmb-file-input' ).attr( 'name', inputName + '[]' ).not( ':first' ).remove(); $key.val( inputName ); } ); file.updateVisibility.call( $list ); }; // Set 'required' attribute. 'this' is the wrapper field input. file.setRequired = function () { var $this = $( this ), $uploaded = $this.find( '.rwmb-files' ), $inputs = $this.find( '.rwmb-file-new input' ); $inputs.prop( 'required', false ); if ( $uploaded.children().length ) { return; } var $firstInput = $inputs.first(); if ( 1 === $firstInput.data( 'required' ) ) { $firstInput.prop( 'required', true ); } }; function init( e ) { var $el = $( e.target ), $uploaded = $el.find( '.rwmb-files' ); $uploaded.each( file.sort ); $uploaded.each( file.updateVisibility ); $el.find( '.rwmb-file-wrapper, .rwmb-image-wrapper' ).each( file.setRequired ); } rwmb.$document .on( 'mb_ready', init ) .on( 'click', '.rwmb-file-add', file.addHandler ) .on( 'click', '.rwmb-file-delete', file.deleteHandler ) .on( 'clone', '.rwmb-file-input', file.resetClone ); } )( jQuery, rwmb );
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
jqueryui | Folder | 0755 |
|
|
leaflet | Folder | 0755 |
|
|
select2 | Folder | 0755 |
|
|
validation | Folder | 0755 |
|
|
wp-color-picker-alpha | Folder | 0755 |
|
|
autocomplete.js | File | 1.62 KB | 0644 |
|
autosave.js | File | 595 B | 0644 |
|
button-group.js | File | 1018 B | 0644 |
|
clone.js | File | 9.04 KB | 0644 |
|
color.js | File | 1.33 KB | 0644 |
|
date.js | File | 2.47 KB | 0644 |
|
datetime.js | File | 2.87 KB | 0644 |
|
file-input.js | File | 1.87 KB | 0644 |
|
file-upload.js | File | 5.1 KB | 0644 |
|
file.js | File | 3.88 KB | 0644 |
|
icon.js | File | 769 B | 0644 |
|
image-advanced.js | File | 1015 B | 0644 |
|
image-upload.js | File | 1007 B | 0644 |
|
input-list.js | File | 1.07 KB | 0644 |
|
map-frontend.js | File | 2.01 KB | 0644 |
|
map.js | File | 8.6 KB | 0644 |
|
media.js | File | 15.45 KB | 0644 |
|
modal.js | File | 4.12 KB | 0644 |
|
oembed.js | File | 880 B | 0644 |
|
osm-frontend.js | File | 1.2 KB | 0644 |
|
osm.js | File | 7.79 KB | 0644 |
|
post.js | File | 1.23 KB | 0644 |
|
range.js | File | 475 B | 0644 |
|
script.js | File | 751 B | 0644 |
|
select-advanced.js | File | 3.67 KB | 0644 |
|
select-tree.js | File | 1.62 KB | 0644 |
|
select.js | File | 536 B | 0644 |
|
slider.js | File | 922 B | 0644 |
|
taxonomy.js | File | 1.21 KB | 0644 |
|
time.js | File | 1.78 KB | 0644 |
|
user.js | File | 1.43 KB | 0644 |
|
video.js | File | 1.12 KB | 0644 |
|
wysiwyg.js | File | 5.36 KB | 0644 |
|