<%
# encoding: utf-8
#
# Redmine plugin for Document Management System "Features"
#
# Copyright (C) 2011    Vít Jonáš <vit.jonas@gmail.com>
# Copyright (C) 2012    Daniel Munn <dan.munn@munnster.co.uk>
# Copyright (C) 2011-16 Karel Pičman <karel.picman@kontron.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
%>
<div class="dmsf_upload_select">
  <select id="uploader_select">
   <option value="1">
      <%= l(:label_drag_drop) %>
    </option>
    <option value="2">
      <%= l(:label_classic) %>
    </option>
  </select>
</div>

<h3><%= l(:label_upload) %></h3>

<%= form_tag({:controller => 'dmsf_upload', :action => 'upload_files', :id => @project, :folder_id => @folder},
             :id => 'uploadform', :method => :post, :multipart => true) do %>
  <div id="uploader">
    <div class="box">
      <p>
        <label><%= l(:label_attachment_plural) %></label>
        <%= render :partial => 'attachments/form' %>
      </p>
    </div>
    <%= submit_tag l(:label_upload) %>
  </div>
<% end %>

<script type="text/javascript">
  var originalUploaderContent;
  var uploader = $('#uploader');

  originalUploaderContent = uploader.html();

  $('#uploader_select').change(function() {
    if($(this).val() === '2') {
      uploader.html(originalUploaderContent);
      setupFileDrop();
    } else {
      initPlUploader(uploader);
    }
  });

  initPlUploader(uploader);

  var dmsfFileFieldCount = 1; 
</script>

<% content_for :header_tags do %>
  <%= stylesheet_link_tag 'plupload/jquery.ui.plupload.css', :plugin => 'redmine_dmsf' %>  
  <%= javascript_include_tag 'plupload/js/plupload.full.min.js', :plugin => 'redmine_dmsf' %>
  <%= javascript_include_tag 'plupload/js/jquery.ui.plupload/jquery.ui.plupload.js', :plugin => 'redmine_dmsf' %>
  <%= javascript_include_tag(js_url, :plugin => 'redmine_dmsf') %>

  <script type="text/javascript">
    function initPlUploader(uploader) {
      uploader.html('<div></div>');
      uploader = $('div', uploader);
      uploader.plupload({
        runtimes : 'html5,flash,silverlight,html4',
        url : '<%= url_for({:controller => 'dmsf_upload', :action => 'upload_file', :id => @project}) %>',
        max_file_size : '<%= "#{@ajax_upload_size}mb" %>',
        max_file_count: '<%= Setting.plugin_redmine_dmsf['dmsf_max_file_upload'].to_i if Setting.plugin_redmine_dmsf['dmsf_max_file_upload'].to_i > 0 %>',
        multipart: true,
        // Enable ability to drag'n'drop files onto the widget (currently only HTML5 supports that)
        dragdrop: true,
        multipart_params : { authenticity_token : $('input[name=authenticity_token]').val() },

        // Rename files by clicking on their titles
        rename: true,
        
        // Views to activate
        views: {
            list: true,
            thumbs: true, // Show thumbs
            active: 'thumbs'
        },
        
        // Flash settings
        flash_swf_url : '<%= plugin_asset_path(:redmine_dmsf, 'javascripts', 'plupload/js/Moxie.swf') %>',

        // Silverlight settings
        silverlight_xap_url : '<%= plugin_asset_path(:redmine_dmsf, 'javascripts', 'plupload/js/Moxie.xap') %>'
      });
      $('.plupload_scroll',uploader).resizable({
        handles: 's'
      });
      var pluploader = uploader.plupload('getUploader');

      pluploader.bind('FileUploaded', function(pluploader, file, response) {
        var responseObject = $.parseJSON(response.response);
        if (responseObject == null) { //Bug: on Firefox folders entries act unexpectedly.
          file.status = plupload.FAILED;
          pluploader.trigger('UploadProgress', file);
          pluploader.trigger('QueueChanged');
        } else {
          if (responseObject.error == null) {
            var disk_filename_input = $('<input/>').attr('type', 'hidden')
                    .attr('name', 'uploaded[' + dmsfFileFieldCount + '][disk_filename]')
                    .val(responseObject.disk_filename);
            uploader.append(disk_filename_input);
            var content_type_input = $('<input/>').attr('type', 'hidden')
                    .attr('name', 'uploaded[' + dmsfFileFieldCount + '][content_type]')
                    .val(responseObject.content_type);
            uploader.append(content_type_input);
            var original_filename_input = $('<input/>').attr('type', 'hidden')
                    .attr('name', 'uploaded[' + dmsfFileFieldCount + '][original_filename]')
                    .val(responseObject.original_filename);
            uploader.append(original_filename_input);
          } else {
              file.status = plupload.FAILED;              
              pluploader.trigger('UploadProgress', file);
              pluploader.trigger('QueueChanged');             
          }
        }
        if(pluploader.total.uploaded == pluploader.files.length) $('#uploadform').submit();
        else if((pluploader.total.uploaded + pluploader.total.failed) == pluploader.files.length) setTimeout(function() {$('#uploadform').submit();}, 2000);
        else dmsfFileFieldCount++;
        return true;
      });
    }
  </script>
<% end %>
