Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function ($) {
- // nabindujem si odobratie uz nahraneho suboru
- function plup_remove_item(selector) {
- $(selector).bind('click', function(event) {
- var parent = $(this).parent();
- var parentsParent = parent.parent();
- parent.remove();
- parentsParent.trigger('formUpdated');
- });
- }
- // nabindujem zmenu vlekosti pri kliknuti na input
- function plup_resize_input(selector) {
- $(selector).bind('focus', function(event) {
- $(this).css('z-index', '10').animate({'width': '300px', 'height': '15px'}, 300);
- });
- $(selector).bind('blur', function(event) {
- $(this).removeAttr('style');
- });
- }
- Drupal.behaviors.plup = {
- attach: function(context, settings) {
- // Skript spracujem, len raz. Pouzivam toto namiesto Drupalovskeho .once()
- if (!Drupal.settings.plup.once) {
- /**
- * Pociatocne akcie
- */
- $('#plup-list').sortable(); // Aktivujem sortable
- $('#plup-upload').hide(); // Skryjem tlacidlo pre upload
- var uploader = new plupload.Uploader(Drupal.settings.plup); // Vytvorim objekt pluploadu
- plup_remove_item('.plup-remove-item'); // Ak existuju nejake obrazky, nabindujem tlacidlo pre odobranie
- plup_resize_input('#plup-list > li > input.form-text'); // nabindujem zmenu velkosti inputu na existujuce prvky
- /**
- * Eventy
- */
- // Inicializacia plupload skriptu
- uploader.bind('Init', function(up, params) {
- });
- // Start uploadovania
- uploader.bind('Start', function(up, files) {
- });
- // Zmena stavu uploadovania(start/stop)
- uploader.bind('StateChanged', function(up) {
- if (up.state == 2) {
- $('#plup-progress').progressbar(); // aktivujem hlavny progressbar
- }
- if (up.state == 1) {
- $('#plup-progress').progressbar('destroy'); // znicim progressbar
- $('#plup-upload').hide(); // skryjem tlacidlo pre upload
- $('.plup-drag-info').show(); // zobrazim info o drag
- }
- });
- // Akcie pri pridani suborov do fronty
- // Files su aktualne pridavane subory do fronty, tzn. nie vsetky dokopy
- uploader.bind('FilesAdded', function(up, files) {
- $('.plup-drag-info').hide(); // skryjem spravu pre drag
- $('#plup-upload').show(); // zobrazim tlacidlo pre upload
- // kazdy subor si zaradim do zoznamu fornty
- $.each(files, function(i, file) {
- $('#plup-filelist table').append('<tr id="' + file.id + '">' +
- '<td class="plup-filelist-file">' + file.name + '</td>' +
- '<td class="plup-filelist-size">' + plupload.formatSize(file.size) + '</td>' +
- '<td class="plup-filelist-message"></td>' +
- '<td class="plup-filelist-remove"><a class="plup-remove-file"></a></td>' +
- '</tr>');
- // nabindujem si odobratie suboru z fronty
- $('#' + file.id + ' .plup-filelist-remove > a').bind('click', function(event) {
- $('#' + file.id).remove();
- up.removeFile(file);
- });
- });
- up.refresh(); // refresh kvoli flashu alebo silverlightu
- });
- // Ulohy pri odobrani suboru z fronty
- // Tu je chyba. Malo by to byt pole files ale je to objekt file
- // - zla dokumentacia
- uploader.bind('FilesRemoved', function(up, files) {
- // ak neostali ziadne subory vo fronte
- if (up.files.length == 0) {
- $('#plup-upload').hide(); // skryjem tlacidlo pre upload
- $('.plup-drag-info').show(); // zobrazim spravu pre drag
- }
- });
- // Akcie pri uploadovani suborov
- uploader.bind('UploadProgress', function(up, file) {
- // Aktualizacia progress baru
- $('#plup-progress').progressbar({value: uploader.total.percent});
- });
- // Akcie ked nastane chyba
- uploader.bind('Error', function(up, err) {
- console.log(err);
- $('#' + err.file.id + ' > .plup-filelist-message').append('<b>Error: ' + err.message + '</b>');
- up.refresh(); // refresh kvoli flashu alebo silverlightu
- });
- // Akcie po dokonceni uploadu 1 suboru z fronty
- uploader.bind('FileUploaded', function(up, file, response) {
- // response je objekt ktory ma response a status parametre, preto 2x repsonse
- console.log(response);
- var fileSaved = jQuery.parseJSON(response.response);
- var delta = $('#plup-list li').length;
- var name = Drupal.settings.plup.name +'[' + delta + ']';
- $('#plup-filelist #' + file.id).remove(); // odstranim nahraty subor zo zoznamu fronty
- var title_field = Drupal.settings.plup.title_field ? '<input type="text" class="form-text" name="'+ name + '[title]" value="" />' : '';
- var alt_field = Drupal.settings.plup.alt_field ? '<input type="text" class="form-text" name="'+ name + '[alt]" value="" />' : '';
- // pridam si thumbnail do sachovnice
- $('#plup-list').append(
- '<li class="ui-state-default">' +
- '<div class="plup-thumb-wrapper"><img src="'+ Drupal.settings.plup.image_style_path + file.target_name + '" /></div>' +
- '<a class="plup-remove-item"></a>' +
- title_field +
- alt_field +
- '<input type="hidden" name="'+ name + '[fid]" value="' + fileSaved.fid + '" />' +
- '<input type="hidden" name="'+ name + '[weight]" value="' + delta + '" />' +
- '</li>');
- // nabindujem odobratie uz nahrateho suboru
- var new_element = $('input[name="'+ name +'[fid]"]');
- var remove_element = $(new_element).siblings('.plup-remove-item');
- plup_remove_item(remove_element);
- // nabindujem zmenu velkosti inputu
- var text_element = $(new_element).siblings('input.form-text');
- plup_resize_input(text_element);
- // poviem drupalu ze sa zmenila forma
- new_element.trigger('formUpdated');
- });
- // Akcie po dokonceni uploadu vsetkych suborov
- uploader.bind('UploadComplete', function(up, files) {
- $('#plup-list').sortable('refresh'); // refreshnem sortable
- $('.plup-drag-info').show(); // zobrazim spravu pre drag
- });
- /**
- * Dodatocne ulohy
- */
- // Pri kliknuti na 'upload' sa zacnu nahravat subory
- $('#' + Drupal.settings.plup.upload).click(function(e) {
- // Zamedzi nahratie viac suborov ako je povolenych
- if (Drupal.settings.plup.max_files > 0) {
- var uploaded = $('#plup-list li').length;
- var selected = $('#plup-filelist td.plup-filelist-file').length;
- var allowed = Drupal.settings.plup.max_files - uploaded;
- if ((selected + uploaded) > Drupal.settings.plup.max_files) {
- alert('Mozete nahrat iba '+ allowed +' suborov.');
- return;
- }
- }
- uploader.start();
- e.preventDefault();
- });
- // Inicializujem plupload
- uploader.init();
- // Pri prehadzovani poradia poloziek sa bude menit ich 'weight' input
- // aby sa zmena ulozila do db
- $('#plup-list').bind('sortupdate', function(event, ui) {
- $(this).find('li').each(function(index) {
- $(this).find('input[name$="[weight]"]').val(index);
- });
- });
- // // Client side form validation
- // $('form').submit(function(e) {
- // var uploader = $('#uploader').plupload('getUploader');
- //
- // // Files in queue upload them first
- // if (uploader.files.length > 0) {
- // // When all files are uploaded submit form
- // uploader.bind('StateChanged', function() {
- // if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
- // $('form')[0].submit();
- // }
- // });
- //
- // uploader.start();
- // } else
- // alert('You must at least upload one file.');
- //
- // return false;
- // });
- // Informujem sa ze skript uz bol spracovany
- Drupal.settings.plup.once = 1;
- }
- }
- }
- })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement