Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Uploader changes #229

Merged
merged 1 commit into from

2 participants

@funkedgeek

No description provided.

@rilwis
Owner

I see you did a lot of changes in name conversion. I think we should keep using this_name format for name, to make everything compatible with WP.

@funkedgeek
@rilwis
Owner

OK, no problem. I'll accept the pull request now.

@rilwis rilwis closed this
@rilwis rilwis reopened this
@rilwis rilwis merged commit 0bc6499 into rilwis:master
@funkedgeek

This solves #39

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 25, 2013
  1. @funkedgeek
This page is out of date. Refresh to see the latest.
View
11 inc/fields/file-advanced.php
@@ -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
15 inc/fields/file.php
@@ -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,
View
11 inc/fields/image-advanced.php
@@ -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
10 inc/fields/image.php
@@ -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,
View
2  inc/fields/plupload-image.php
@@ -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
59 js/file-advanced.js
@@ -5,12 +5,11 @@ 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] ) {
@@ -18,7 +17,7 @@ jQuery( document ).ready( function( $ )
return;
}
//Setup media frame
- frame_options = {
+ frameOptions = {
className : 'media-frame rwmb-file-frame',
frame : 'select',
multiple : true,
@@ -29,49 +28,43 @@ 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 )
{
@@ -79,10 +72,13 @@ jQuery( document ).ready( function( $ )
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' );
+ }
});
@@ -90,9 +86,4 @@ jQuery( document ).ready( function( $ )
rwmbFileFrames[fieldID].open();
} );
-
- $( '.rwmb-uploaded ' ).on( 'click', '.rwmb-delete-file', function()
- {
- $( this ).parents( '.rwmb-uploaded' ).siblings( '.rwmb-file-advanced-upload' ).removeClass( 'hidden' );
- } );
} );
View
23 js/file.js
@@ -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
52 js/image-advanced.js
@@ -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,31 +34,27 @@ 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 = {
@@ -66,7 +62,7 @@ jQuery( document ).ready( function( $ )
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' );
- } );
} );
View
34 js/plupload-image.js
@@ -34,18 +34,18 @@ jQuery( document ).ready( function( $ )
//Add files
rwmbUploader.bind( 'FilesAdded', function( up, files )
{
- var max_file_uploads = $imageList.data('max_file_uploads'),
+ var maxFileUploads = $imageList.data('max_file_uploads'),
uploaded = $imageList.children().length,
- msg = 'You may only upload ' + max_file_uploads + ' file';
+ msg = 'You may only upload ' + maxFileUploads + ' file';
- if ( max_file_uploads > 1 )
+ if ( maxFileUploads > 1 )
msg += 's';
// Remove files from queue if exceed max file uploads
- if ( max_file_uploads > 0 && ( uploaded + files.length ) > max_file_uploads )
+ if ( maxFileUploads > 0 && ( uploaded + files.length ) > maxFileUploads )
{
- if( uploaded < max_file_uploads ){
- var diff = max_file_uploads - uploaded;
+ if( uploaded < maxFileUploads ){
+ var diff = maxFileUploads - uploaded;
up.splice( diff - 1, files.length - diff );
files = up.files;
}
@@ -53,17 +53,17 @@ jQuery( document ).ready( function( $ )
}
// Hide drag & drop section if reach max file uploads
- if ( ( uploaded + files.length ) >= max_file_uploads ) $dropArea.addClass( 'hidden' );
+ if ( ( uploaded + files.length ) >= maxFileUploads ) $dropArea.addClass( 'hidden' );
max = parseInt( up.settings.max_file_size, 10 );
// Upload files
plupload.each( files, function( file )
{
- add_loading( up, file, $imageList );
- add_throbber( file );
+ addLoading( up, file, $imageList );
+ addThrobber( file );
if ( file.size >= max )
- remove_error( file );
+ removeError( file );
} );
up.refresh();
up.start();
@@ -72,15 +72,15 @@ jQuery( document ).ready( function( $ )
rwmbUploader.bind( 'Error', function( up, e )
{
- add_loading( up, e.file, $imageList );
- remove_error( e.file );
+ addLoading( up, e.file, $imageList );
+ removeError( e.file );
up.removeFile( e.file );
} );
rwmbUploader.bind( 'FileUploaded', function( up, file, response )
{
var res = wpAjax.parseAjaxResponse( $.parseXML( response.response ), 'ajax-response' );
- false === res.errors ? $( 'li#' + file.id ).replaceWith( res.responses[0].data ) : remove_error( file );
+ false === res.errors ? $( 'li#' + file.id ).replaceWith( res.responses[0].data ) : removeError( file );
} );
});
@@ -93,7 +93,7 @@ jQuery( document ).ready( function( $ )
*
* @return void
*/
- function remove_error( file )
+ function removeError( file )
{
$( 'li#' + file.id )
.addClass( 'rwmb-image-error' )
@@ -110,9 +110,9 @@ jQuery( document ).ready( function( $ )
*
* @return void
*/
- function add_loading( up, file, $ul )
+ function addLoading( up, file, $ul )
{
- $ul.append( "<li id='" + file.id + "'><div class='rwmb-image-uploading-bar'></div><div id='" + file.id + "-throbber' class='rwmb-image-uploading-status'></div></li>" );
+ $ul.removeClass('hidden').append( "<li id='" + file.id + "'><div class='rwmb-image-uploading-bar'></div><div id='" + file.id + "-throbber' class='rwmb-image-uploading-status'></div></li>" );
}
/**
@@ -120,7 +120,7 @@ jQuery( document ).ready( function( $ )
*
* @return void
*/
- function add_throbber( file )
+ function addThrobber( file )
{
$( '#' + file.id + '-throbber' ).html( "<img class='rwmb-loader' height='64' width='64' src='" + RWMB.url + "img/loader.gif'/>" );
}
Something went wrong with that request. Please try again.