Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Choose existing list not loading, fix #33

Open
RVXD opened this issue May 31, 2012 · 0 comments
Open

Choose existing list not loading, fix #33

RVXD opened this issue May 31, 2012 · 0 comments

Comments

@RVXD
Copy link
Contributor

RVXD commented May 31, 2012

If using multiple instances of uploadify within silverstripe cms, the import list is not populated.
If the uploadify field was loaded with ajax the import list also did not work.

I changed uploadify_init to fix this.
Now on tabchange the list is updated. I also added 'var' to all variables defined within functions (line 129).

For us this works better.

if( $(this).parent().attr('class')=='first' ){
$t.find('.import_dropdown select').trigger("change");
}

Altered code for uploadify_init.js:

jQuery.noConflict();
(function($) {
$(function() {
$('.UploadifyField').livequery(function() {
$(this).each(function() {
var $t = $(this);
if(!$t.hasClass('backend')) {
var $input = $('input.uploadify',$t);
var name = $input.attr('name');
var id = $input.attr('id');
var klass = $input.attr('class');
var $uploader = $('');
$input.replaceWith($uploader);
}
else {
$uploader = $('input.uploadify', $t);
}
/**
Build a set of options to pass to the uploadify object
**/
var opts = $uploader.metadata();
$.extend(opts, {
onComplete: function(event, queueID, fileObj, response, data) {
var $e = $(event.currentTarget);
var $container = $e.parents('.UploadifyField:first');
if(isNaN(response)) {
alert(response);
}
$e = $(event.currentTarget);
if($e.metadata().refreshlink) {
var $preview = $('#upload_preview_'+$e.attr('id'));
var $inputs = $('.inputs input', $preview);
if($preview.length) {
var ids = new Array();
$inputs.each(function() {
if($(this).val().length) {
ids.push($(this).val());
}
});
ids.push(response);

                        $.ajax({
                            url: $e.metadata().refreshlink,
                            data: {'FileIDs' : ids.join(",")},
                            async: false,
                            dataType: "json",
                            success: function(data) {
                                $preview.html(data.html);
                            }
                        });
                    }
                }
            },
            onAllComplete : function(event) {
                var $e = $(event.currentTarget);
                $e.data('active',false);
                $e.parents('form').find(':submit').attr('disabled',false).removeClass('disabled');
                $container = $e.parents('.UploadifyField:first');
                $('.preview',$container).show();
                if($e.metadata().upload_on_submit) {
                    $e.parents('form').submit();
                }

            },
            onSelectOnce: function(event, queueID, fileObj) {
                var $e = $(event.currentTarget);
                if($('#folder_select_'+$e.attr('id')).length) {
                    folder_id = $('#folder_select_'+$e.attr('id')).find('select:first').val();
                }
                else if($('#folder_hidden_'+$e.attr('id')).length) {
                    folder_id = $('#folder_hidden_'+$e.attr('id')).val();
                }
                var data = $e.uploadifySettings('scriptData');
                $.extend(data, {
                    FolderID : folder_id
                });
                $e.uploadifySettings('scriptData', data, true);
                $e.data('active',true);
                if(!$e.metadata().upload_on_submit) {
                    $e.parents('form').find(':submit').attr('disabled',true).addClass('disabled');
                }
            },
            onCancel: function(event, queueID, fileObj, data) {
                var $e = $(event.currentTarget);
                if (data.fileCount == 0) {
                    $e.closest('.UploadifyField').find('.preview').show().html('<div class="no_files"></div>');
                    if (!$e.metadata().auto && !$e.metadata().upload_on_submit) {
                        $('.uploadifyfield_queue_actions').show();
                    }
                }
            }
        });

        // Handle form submission if the upload happens on submit
        if($uploader.metadata().upload_on_submit) {
            $(this).parents('form:first').submit(function(e) {
                cansubmit = true;
                $('input.uploadify').each(function() {
                    if($(this).data('active')) {
                        cansubmit = false;
                        $(this).uploadifyUpload();
                    }
                });
                return cansubmit;

            });
        }
        $uploader.uploadify(opts);

        // Build the "fake" CSS button
        var $buttonWrapper = $('.button_wrapper', $t);
        var $fakeButton = $(".button_wrapper a",$t);
        var width = $fakeButton.outerWidth();
        var height = $fakeButton.outerHeight();
        opts.width = width;
        opts.height = height;
        $buttonWrapper.css("width", width + "px").css("height", height + "px")

        // Activate uploadify
        // Tabs for the backend
        if($t.find('.horizontal_tab_wrap').length) {
          $tabSet = $t.find('.horizontal_tab_wrap');
          var tabContainers = $('div.horizontal_tabs > div', $tabSet);
          tabContainers.hide().filter(':last').show();

          $('div.tabNavigation ul.navigation a', $tabSet).live("click",function () {

            // trigger reload on tab change added XD
            if( $(this).parent().attr('class')=='first' ){
                $t.find('.import_dropdown select').trigger("change");
            }

              tabContainers.hide();
              tabContainers.filter(this.hash).show();
              $(this).parents('ul:first').find('.selected').removeClass('selected');
              $(this).addClass('selected');
              return false;
          });

          $('div.tabNavigation ul.navigation a:last', $tabSet).click();
        }



    });
});

/**
 Attach behaviours external to the uploader, e.g. queue functions
                                                                    **/

// Delete buttons for the queue items
$('.upload_previews li .delete a').live("click", function() {
    var $t = $(this);
    $.post(
        $t.attr('href'),
        {'FileID' : $t.attr('rel')},
        function() {
            $t.parents("li:first").fadeOut(function() {
                $(this).remove();
                $('.inputs input[value='+$t.attr('rel')+']').remove();
            });
        }
    );
    return false;
});


// Change folder ajax post
$('.folder_select').find(':submit').live("click", function() {
    var $t = $(this);
    var $target = $(this).parents('.UploadifyField').find('.uploadify');
    var $folderSelect = $('#folder_select_'+$target.attr('id'));
    var folder_id = $('select:first', $folderSelect).val();
    var new_folder = $('input:first', $folderSelect).val();
    $folderSelect.parents('.folder_select_wrap').load(
        $t.metadata().url,
        { FolderID : folder_id, NewFolder : new_folder}
    );
    return false;
});
$('.folder_select :submit').livequery(function() {
    $(this).siblings('label').hide();
});

// Attach sorting, if multiple uploads
$('.upload_previews ul.sortable').livequery(function() {
    var $list = $(this);
    var meta = $list.metadata();
    $list.sortable({
        update: function(e) {
            $.post(meta.url, $list.sortable("serialize"));
        },
        containment : 'document',
        tolerance : 'intersect'
    });
});


$('.import_dropdown select').livequery("change", function() {

    var uplField = $(this).parents('.UploadifyField').find('.uploadify').metadata();
    var fileExt = uplField.fileExt;
    var $t = $(this);
    var $target = $t.parents('.import_dropdown').find('.import_list');
    $t.parents('.import_dropdown').find('button').hide();
    $target.html('').addClass('loading').show().css('height','50px');

    var data = (fileExt != "*.*") ? { FolderID : $t.val(), 'FileExt' : fileExt } : {FolderID : $t.val()};

    $.ajax({
        url : $t.metadata().url,
        data : data,
        success : function(data) {
            $target.slideUp(function() {
                $(this).removeClass('loading').css({'height' : 'auto', 'max-height' : '150px','overflow' : 'auto'});
                $(this).html(data);
                if($('input', $(this)).length) {
                    $t.parents('.import_dropdown').find('button').show();
                }
                $(this).slideDown();
            });
        }
    });
}).change();

$('.import_dropdown button').live("click", function() {
    var url = $(this).metadata().url;
    var $target = $(this).parents('.UploadifyField').find('.preview');
    var $uploader = $(this).parents('.UploadifyField').find('.uploadify');
    var $list = $(this).parents('.import_dropdown');
    var ids = new Array();
    $target.find('input').each(function() {
        if($(this).val().length) {
            ids.push($(this).val());
        }
    });
    $list.find(':checked').each(function() {
        ids.push($(this).val());
    });
    $.ajax({
        url: $uploader.metadata().refreshlink,
        data: {'FileIDs' : ids.join(",")},
        dataType : "json",
        success: function(data) {
            $target.html(data.html);
            $msg = $list.find('.import_message');
            $msg.html(data.success).fadeIn();
            setTimeout(function() {
                $msg.fadeOut()
            },5000);
            $list.find('select').val('');
            $list.find('button').hide();
            $list.find('.import_list').slideUp();

        }
    });
    return false;
});

$('.import_list_search_field').live('keyup',function(){
    var searchTxt = $(this).val().toLowerCase();

    $(this).parent().find('.import_list').find('li').each(function(x){
        var v = $(this).find('label').text().toLowerCase();

        if(v.indexOf(searchTxt) != -1){
            $(this).removeClass().addClass(function(){return (x%2==0) ? 'odd' : 'even';}).show();
        } else {
            $(this).hide();
        }
    });
});

});
})(jQuery);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant