Permalink
Browse files

Merge pull request #229 from funkedgeek/master

Uploader changes
  • Loading branch information...
2 parents 8c38bbe + dca9a7e commit 0bc64993992c24b0d497ee731e686b4d6472d339 @rilwis committed Feb 25, 2013
@@ -63,13 +63,10 @@ static function html( $html, $meta, $field )
$html = self::get_uploaded_files( $meta, $field );
// Show form upload
- $classes = array( 'button', 'rwmb-file-advanced-upload', 'hide-if-no-js' );
- if ( ! empty( $field['max_file_uploads'] ) )
- {
- $max_file_uploads = (int) $field['max_file_uploads'];
- if ( count( $meta ) >= $max_file_uploads )
- $classes[] = 'hidden';
- }
+ $classes = array( 'button', 'rwmb-file-advanced-upload', 'hide-if-no-js', 'new-files' );
+ if ( ! empty( $field['max_file_uploads'] ) && count( $meta ) >= (int) $field['max_file_uploads'] )
+ $classes[] = 'hidden';
+
$classes = implode( ' ', $classes );
$html .= "<a href='#' class='{$classes}' data-attach_file_nonce={$attach_nonce}>{$i18n_title}</a>";
View
@@ -82,14 +82,17 @@ static function html( $html, $meta, $field )
// Uploaded files
$html = self::get_uploaded_files( $meta, $field );
-
+ $new_file_classes = array('new-files');
+ if ( ! empty( $field['max_file_uploads'] ) && count( $meta ) >= (int) $field['max_file_uploads'] )
+ $new_file_classes[] = 'hidden';
// Show form upload
$html .= sprintf(
- '<h4>%s</h4>
- <div class="new-files">
+ '<div class="%s">
+ <h4>%s</h4>
<div class="file-input"><input type="file" name="%s[]" /></div>
<a class="rwmb-add-file" href="#"><strong>%s</strong></a>
</div>',
+ implode( ' ', $new_file_classes ),
$i18n_title,
$field['id'],
$i18n_more
@@ -101,9 +104,13 @@ static function html( $html, $meta, $field )
static function get_uploaded_files( $files, $field )
{
$delete_nonce = wp_create_nonce( "rwmb-delete-file_{$field['id']}" );
- $ol = '<ul class="rwmb-uploaded rwmb-file" data-field_id="%s" data-delete_nonce="%s" data-force_delete="%s" data-max_file_uploads="%s" data-mime_type="%s">';
+ $classes = array('rwmb-file', 'rwmb-uploaded');
+ if ( count( $files ) <= 0 )
+ $classes[] = 'hidden';
+ $ol = '<ul class="%s" data-field_id="%s" data-delete_nonce="%s" data-force_delete="%s" data-max_file_uploads="%s" data-mime_type="%s">';
$html = sprintf(
$ol,
+ implode( ' ', $classes ),
$field['id'],
$delete_nonce,
$field['force_delete'] ? 1 : 0,
@@ -67,13 +67,10 @@ static function html( $html, $meta, $field )
$html .= self::get_uploaded_images( $meta, $field );
// Show form upload
- $classes = array( 'button', 'rwmb-image-advanced-upload', 'hide-if-no-js' );
- if ( ! empty( $field['max_file_uploads'] ) )
- {
- $max_file_uploads = (int) $field['max_file_uploads'];
- if ( count( $meta ) >= $max_file_uploads )
- $classes[] = 'hidden';
- }
+ $classes = array( 'button', 'rwmb-image-advanced-upload', 'hide-if-no-js', 'new-files' );
+ if ( ! empty( $field['max_file_uploads'] ) && count( $meta ) >= (int) $field['max_file_uploads'] )
+ $classes[] = 'hidden';
+
$classes = implode( ' ', $classes );
$html .= "<a href='#' class='{$classes}' data-attach_media_nonce={$attach_nonce}>{$i18n_title}</a>";
View
@@ -79,8 +79,7 @@ static function html( $html, $meta, $field )
$i18n_more = _x( '+ Add new image', 'image upload', 'rwmb' );
// Uploaded images
- if ( ! empty( $meta ) )
- $html .= self::get_uploaded_images( $meta, $field );
+ $html .= self::get_uploaded_images( $meta, $field );
// Show form upload
$html .= sprintf(
@@ -109,10 +108,13 @@ static function get_uploaded_images( $images, $field )
{
$reorder_nonce = wp_create_nonce( "rwmb-reorder-images_{$field['id']}" );
$delete_nonce = wp_create_nonce( "rwmb-delete-file_{$field['id']}" );
-
- $ul = '<ul class="rwmb-images rwmb-uploaded" data-field_id="%s" data-delete_nonce="%s" data-reorder_nonce="%s" data-force_delete="%s" data-max_file_uploads="%s">';
+ $classes = array('rwmb-images', 'rwmb-uploaded');
+ if ( count( $images ) <= 0 )
+ $classes[] = 'hidden';
+ $ul = '<ul class="%s" data-field_id="%s" data-delete_nonce="%s" data-reorder_nonce="%s" data-force_delete="%s" data-max_file_uploads="%s">';
$html = sprintf(
$ul,
+ implode( ' ', $classes ),
$field['id'],
$delete_nonce,
$reorder_nonce,
@@ -93,7 +93,7 @@ static function html( $html, $meta, $field )
// Check for max_file_uploads
- $classes = array( 'rwmb-drag-drop', 'drag-drop', 'hide-if-no-js' );
+ $classes = array( 'rwmb-drag-drop', 'drag-drop', 'hide-if-no-js', 'new-files');
if ( ! empty( $field['max_file_uploads'] ) && count( $meta ) >= (int) $field['max_file_uploads'] )
$classes[] = 'hidden';
View
@@ -5,20 +5,19 @@ jQuery( document ).ready( function( $ )
$( '.rwmb-file-advanced-upload' ).on( 'click', function(e) {
e.preventDefault();
- var uploadButton = $(this),
- $fileList = uploadButton.siblings( '.rwmb-uploaded' ),
+ var $uploadButton = $(this),
+ $fileList = $uploadButton.siblings( '.rwmb-uploaded' ),
fieldID = $fileList.data('field_id'),
maxFileUploads = $fileList.data( 'max_file_uploads' ),
mimeType = $fileList.data( 'mime_type' );
- console.log($fileList);
// If the frame already exists, re-open it.
if ( rwmbFileFrames[fieldID] ) {
rwmbFileFrames[fieldID].open();
return;
}
//Setup media frame
- frame_options = {
+ frameOptions = {
className : 'media-frame rwmb-file-frame',
frame : 'select',
multiple : true,
@@ -29,70 +28,62 @@ jQuery( document ).ready( function( $ )
};
if( mimeType ) {
- frame_options.library = {
+ frameOptions.library = {
type : mimeType
};
}
- rwmbFileFrames[fieldID] = wp.media( frame_options );
+ rwmbFileFrames[fieldID] = wp.media( frameOptions );
//Handle selection
rwmbFileFrames[fieldID].on( 'select', function() {
//Get selections
- var selection = rwmbFileFrames[fieldID].state().get( 'selection' ),
+ var selection = rwmbFileFrames[fieldID].state().get( 'selection' ).toJSON(),
msg = 'You may only upload ' + maxFileUploads + ' file',
- uploaded = $fileList.children().length,
- total = uploaded + selection.length;
+ uploaded = $fileList.children().length;
if ( maxFileUploads > 1 )
msg += 's';
- if( maxFileUploads > 0 ) {
- if( total > maxFileUploads ) {
- alert( msg );
- }
- if( total >= maxFileUploads ) {
- uploadButton.addClass( 'hidden' );
+ if ( maxFileUploads > 0 && ( uploaded + selection.length ) > maxFileUploads )
+ {
+ if( uploaded < maxFileUploads ){
+ selection = selection.slice( 0, maxFileUploads - uploaded );
}
+ alert( msg );
}
+ for( i in selection) {
+ var attachment = selection[i];
- selection.map( function( attachment, index ) {
-
- //Convert attachment to JSON
- attachment = attachment.toJSON();
-
//Check if image already attached
- if( $fileList.children('li#item_' + attachment.id ).length > 0 || maxFileUploads > 0 && uploaded + 1 + index > maxFileUploads){
- return;
- }
+ if( $fileList.children('li#item_' + attachment.id ).length > 0 ){
+ continue;
+ }
- //Attach attachment to field and get HTML
var data = {
action : 'rwmb_attach_file',
post_id : $( '#post_ID' ).val(),
field_id : $fileList.data('field_id'),
attachment_id : attachment.id,
- _ajax_nonce : uploadButton.data('attach_file_nonce')
+ _ajax_nonce : $uploadButton.data('attach_file_nonce')
};
$.post( ajaxurl, data, function( r )
{
var res = wpAjax.parseAjaxResponse( r, 'ajax-response' );
if ( res.errors )
alert( res.responses[0].errors[0].message );
- else
- $fileList.append( res.responses[0].data );
- }, 'xml' );
- });
+ else
+ $fileList.removeClass('hidden').prepend( res.responses[0].data );
+
+ // Hide files button if reach max file uploads
+ if ( $fileList.children().length >= maxFileUploads ) $uploadButton.addClass( 'hidden' );
+ }, 'xml' );
+ }
});
//Open
rwmbFileFrames[fieldID].open();
} );
-
- $( '.rwmb-uploaded ' ).on( 'click', '.rwmb-delete-file', function()
- {
- $( this ).parents( '.rwmb-uploaded' ).siblings( '.rwmb-file-advanced-upload' ).removeClass( 'hidden' );
- } );
} );
View
@@ -3,9 +3,17 @@ jQuery( document ).ready( function( $ )
// Add more file
$( '.rwmb-add-file' ).click( function()
{
- var $this = $( this ), $first = $this.parent().find( '.file-input:first' );
-
- $first.clone().insertBefore( $this );
+ var $this = $( this ),
+ $fields = $this.siblings( '.file-input' ),
+ $first = $fields.first(),
+ $fileList = $this.closest('.rwmb-input').find( '.rwmb-uploaded' ),
+ fileCount = $fileList.children('li').length,
+ maxFileUploads = $fileList.data( 'max_file_uploads' );
+
+ console.log( $fileList )
+ if( ($fields.length + fileCount) < maxFileUploads || maxFileUploads <= 0) {
+ $first.clone().insertBefore( $this );
+ }
return false;
} );
@@ -31,8 +39,15 @@ jQuery( document ).ready( function( $ )
if ( res.errors )
alert( res.responses[0].errors[0].message );
- else
+ else
+ {
$parent.remove();
+ $container.siblings('.new-files').removeClass('hidden');
+ if( $container.children('li').length <=0 )
+ {
+ $container.addClass( 'hidden' );
+ }
+ }
}, 'xml' );
return false;
View
@@ -5,7 +5,7 @@ jQuery( document ).ready( function( $ )
$( '.rwmb-image-advanced-upload' ).on( 'click', function(e) {
e.preventDefault();
- var upload_button = $(this),
+ var $uploadButton = $(this),
$imageList = $(this).siblings( '.rwmb-images' ),
maxFileUploads = $imageList.data( 'max_file_uploads' );
@@ -34,39 +34,35 @@ jQuery( document ).ready( function( $ )
//Handle selection
rwmbMediaFrame.on( 'select', function() {
//Get selections
- var selection = rwmbMediaFrame.state().get( 'selection' ),
+ var selection = rwmbMediaFrame.state().get( 'selection' ).toJSON(),
msg = 'You may only upload ' + maxFileUploads + ' file',
- uploaded = $imageList.children().length,
- total = uploaded + selection.length;
+ uploaded = $imageList.children().length;
if ( maxFileUploads > 1 )
msg += 's';
-
- if( maxFileUploads > 0 ) {
- if( total > maxFileUploads ) {
- alert( msg );
- }
- if( total >= maxFileUploads ) {
- upload_button.addClass( 'hidden' );
+
+ if ( maxFileUploads > 0 && ( uploaded + selection.length ) > maxFileUploads )
+ {
+ if( uploaded < maxFileUploads ){
+ selection = selection.slice( 0, maxFileUploads - uploaded );
}
+ alert( msg );
}
-
- selection.map( function( attachment, index ) {
+
+ for( i in selection) {
+ var attachment = selection[i];
- //Convert attachment to JSON
- attachment = attachment.toJSON();
-
//Check if image already attached
- if( $imageList.children('li#item_' + attachment.id ).length > 0 || maxFileUploads > 0 && uploaded + 1 + index > maxFileUploads){
- return;
- }
+ if( $imageList.children('li#item_' + attachment.id ).length > 0 ){
+ continue;
+ }
//Attach attachment to field and get HTML
var data = {
action : 'rwmb_attach_media',
post_id : $( '#post_ID' ).val(),
field_id : $imageList.data('field_id'),
attachment_id : attachment.id,
- _ajax_nonce : upload_button.data('attach_media_nonce')
+ _ajax_nonce : $uploadButton.data('attach_media_nonce')
};
$.post( ajaxurl, data, function( r )
{
@@ -75,17 +71,17 @@ jQuery( document ).ready( function( $ )
if ( res.errors )
alert( res.responses[0].errors[0].message );
else
- $imageList.prepend( res.responses[0].data );
- }, 'xml' );
- });
+ $imageList.removeClass('hidden').prepend( res.responses[0].data );
+
+ // Hide files button if reach max file uploads
+ if ( $imageList.children().length >= maxFileUploads ) $uploadButton.addClass( 'hidden' );
+ }, 'xml' );
+ }
+
+
});
//Open
rwmbMediaFrame.open();
} );
-
- $( '.rwmb-images' ).on( 'click', '.rwmb-delete-file', function()
- {
- $( this ).parents( '.rwmb-images' ).siblings( '.rwmb-image-advanced-upload' ).removeClass( 'hidden' );
- } );
} );
Oops, something went wrong.

0 comments on commit 0bc6499

Please sign in to comment.