Advertisement
Guest User

Untitled

a guest
Jan 12th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function ($) {
  2.  
  3.   // nabindujem si odobratie uz nahraneho suboru
  4.   function plup_remove_item(selector) {
  5.     $(selector).bind('click', function(event) {
  6.       var parent = $(this).parent();
  7.       var parentsParent = parent.parent();
  8.       parent.remove();
  9.       parentsParent.trigger('formUpdated');
  10.     });
  11.   }
  12.  
  13.   // nabindujem zmenu vlekosti pri kliknuti na input
  14.   function plup_resize_input(selector) {
  15.     $(selector).bind('focus', function(event) {
  16.       $(this).css('z-index', '10').animate({'width': '300px', 'height': '15px'}, 300);
  17.     });
  18.     $(selector).bind('blur', function(event) {
  19.       $(this).removeAttr('style');
  20.     });
  21.   }
  22.  
  23.   Drupal.behaviors.plup = {
  24.     attach: function(context, settings) {
  25.       // Skript spracujem, len raz. Pouzivam toto namiesto Drupalovskeho .once()
  26.       if (!Drupal.settings.plup.once) {
  27.         /**
  28.          * Pociatocne akcie
  29.          */
  30.         $('#plup-list').sortable(); // Aktivujem sortable
  31.         $('#plup-upload').hide(); // Skryjem tlacidlo pre upload
  32.         var uploader = new plupload.Uploader(Drupal.settings.plup); // Vytvorim objekt pluploadu
  33.         plup_remove_item('.plup-remove-item'); // Ak existuju nejake obrazky, nabindujem tlacidlo pre odobranie
  34.         plup_resize_input('#plup-list > li > input.form-text'); // nabindujem zmenu velkosti inputu na existujuce prvky
  35.  
  36.         /**
  37.          * Eventy
  38.          */
  39.         // Inicializacia plupload skriptu
  40.         uploader.bind('Init', function(up, params) {
  41.         });
  42.  
  43.         // Start uploadovania
  44.         uploader.bind('Start', function(up, files) {
  45.         });
  46.  
  47.         // Zmena stavu uploadovania(start/stop)
  48.         uploader.bind('StateChanged', function(up) {
  49.           if (up.state == 2) {
  50.             $('#plup-progress').progressbar(); // aktivujem hlavny progressbar
  51.           }
  52.           if (up.state == 1) {
  53.             $('#plup-progress').progressbar('destroy'); // znicim progressbar
  54.             $('#plup-upload').hide(); // skryjem tlacidlo pre upload
  55.             $('.plup-drag-info').show(); // zobrazim info o drag
  56.           }
  57.         });
  58.  
  59.         // Akcie pri pridani suborov do fronty
  60.         // Files su aktualne pridavane subory do fronty, tzn. nie vsetky dokopy
  61.         uploader.bind('FilesAdded', function(up, files) {
  62.           $('.plup-drag-info').hide(); // skryjem spravu pre drag
  63.           $('#plup-upload').show(); // zobrazim tlacidlo pre upload
  64.  
  65.           // kazdy subor si zaradim do zoznamu fornty
  66.           $.each(files, function(i, file) {
  67.             $('#plup-filelist table').append('<tr id="' + file.id + '">' +
  68.               '<td class="plup-filelist-file">' +  file.name + '</td>' +
  69.               '<td class="plup-filelist-size">' + plupload.formatSize(file.size) + '</td>' +
  70.               '<td class="plup-filelist-message"></td>' +
  71.               '<td class="plup-filelist-remove"><a class="plup-remove-file"></a></td>' +
  72.               '</tr>');
  73.             // nabindujem si odobratie suboru z fronty
  74.             $('#' + file.id + ' .plup-filelist-remove > a').bind('click', function(event) {
  75.               $('#' + file.id).remove();
  76.               up.removeFile(file);
  77.             });
  78.           });
  79.  
  80.           up.refresh(); // refresh kvoli flashu alebo silverlightu
  81.         });
  82.  
  83.         // Ulohy pri odobrani suboru z fronty
  84.         // Tu je chyba. Malo by to byt pole files ale je to objekt file
  85.         // - zla dokumentacia
  86.         uploader.bind('FilesRemoved', function(up, files) {
  87.           // ak neostali ziadne subory vo fronte
  88.           if (up.files.length == 0) {
  89.             $('#plup-upload').hide(); // skryjem tlacidlo pre upload
  90.             $('.plup-drag-info').show(); // zobrazim spravu pre drag
  91.           }
  92.         });
  93.  
  94.         // Akcie pri uploadovani suborov
  95.         uploader.bind('UploadProgress', function(up, file) {
  96.           // Aktualizacia progress baru
  97.           $('#plup-progress').progressbar({value: uploader.total.percent});
  98.         });
  99.  
  100.         // Akcie ked nastane chyba
  101.         uploader.bind('Error', function(up, err) {
  102. console.log(err);
  103.           $('#' + err.file.id + ' > .plup-filelist-message').append('<b>Error: ' + err.message + '</b>');
  104.           up.refresh(); // refresh kvoli flashu alebo silverlightu
  105.         });
  106.  
  107.         // Akcie po dokonceni uploadu 1 suboru z fronty
  108.         uploader.bind('FileUploaded', function(up, file, response) {
  109.           // response je objekt ktory ma response a status parametre, preto 2x repsonse
  110. console.log(response);
  111.           var fileSaved = jQuery.parseJSON(response.response);
  112.           var delta = $('#plup-list li').length;
  113.           var name = Drupal.settings.plup.name +'[' + delta + ']';
  114.  
  115.           $('#plup-filelist #' + file.id).remove(); // odstranim nahraty subor zo zoznamu fronty
  116.           var title_field = Drupal.settings.plup.title_field ? '<input type="text" class="form-text" name="'+ name + '[title]" value="" />' : '';
  117.           var alt_field = Drupal.settings.plup.alt_field ? '<input type="text" class="form-text" name="'+ name + '[alt]" value="" />' : '';
  118.           // pridam si thumbnail do sachovnice
  119.           $('#plup-list').append(
  120.             '<li class="ui-state-default">' +
  121.             '<div class="plup-thumb-wrapper"><img src="'+ Drupal.settings.plup.image_style_path + file.target_name + '" /></div>' +
  122.             '<a class="plup-remove-item"></a>' +
  123.             title_field +
  124.             alt_field +
  125.             '<input type="hidden" name="'+ name + '[fid]" value="' + fileSaved.fid + '" />' +
  126.             '<input type="hidden" name="'+ name + '[weight]" value="' + delta + '" />' +
  127.             '</li>');
  128.           // nabindujem odobratie uz nahrateho suboru
  129.           var new_element = $('input[name="'+ name +'[fid]"]');
  130.           var remove_element = $(new_element).siblings('.plup-remove-item');
  131.           plup_remove_item(remove_element);
  132.           // nabindujem zmenu velkosti inputu
  133.           var text_element = $(new_element).siblings('input.form-text');
  134.           plup_resize_input(text_element);
  135.           // poviem drupalu ze sa zmenila forma
  136.           new_element.trigger('formUpdated');
  137.         });
  138.  
  139.         // Akcie po dokonceni uploadu vsetkych suborov
  140.         uploader.bind('UploadComplete', function(up, files) {
  141.           $('#plup-list').sortable('refresh'); // refreshnem sortable
  142.           $('.plup-drag-info').show(); // zobrazim spravu pre drag
  143.         });
  144.  
  145.  
  146.         /**
  147.          * Dodatocne ulohy
  148.          */
  149.         // Pri kliknuti na 'upload' sa zacnu nahravat subory
  150.         $('#' + Drupal.settings.plup.upload).click(function(e) {
  151.           // Zamedzi nahratie viac suborov ako je povolenych
  152.           if (Drupal.settings.plup.max_files > 0) {
  153.             var uploaded = $('#plup-list li').length;
  154.             var selected = $('#plup-filelist td.plup-filelist-file').length;
  155.             var allowed = Drupal.settings.plup.max_files - uploaded;
  156.             if ((selected + uploaded) > Drupal.settings.plup.max_files) {
  157.               alert('Mozete nahrat iba '+ allowed +' suborov.');
  158.               return;
  159.             }
  160.           }
  161.           uploader.start();
  162.           e.preventDefault();
  163.         });
  164.  
  165.         // Inicializujem plupload
  166.         uploader.init();
  167.  
  168.         // Pri prehadzovani poradia poloziek sa bude menit ich 'weight' input
  169.         // aby sa zmena ulozila do db
  170.         $('#plup-list').bind('sortupdate', function(event, ui) {
  171.           $(this).find('li').each(function(index) {
  172.             $(this).find('input[name$="[weight]"]').val(index);
  173.           });
  174.         });
  175.  
  176.  
  177. //        // Client side form validation
  178. //  $('form').submit(function(e) {
  179. //        var uploader = $('#uploader').plupload('getUploader');
  180. //
  181. //        // Files in queue upload them first
  182. //        if (uploader.files.length > 0) {
  183. //            // When all files are uploaded submit form
  184. //            uploader.bind('StateChanged', function() {
  185. //                if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
  186. //                    $('form')[0].submit();
  187. //                }
  188. //            });
  189. //
  190. //            uploader.start();
  191. //        } else
  192. //            alert('You must at least upload one file.');
  193. //
  194. //        return false;
  195. //    });
  196.  
  197.  
  198.         // Informujem sa ze skript uz bol spracovany
  199.         Drupal.settings.plup.once = 1;
  200.       }
  201.     }
  202.   }
  203.  
  204. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement