From 515dad66e2d986b618d1e68d05673d8271c3657f Mon Sep 17 00:00:00 2001 From: huang Date: Tue, 20 Oct 2015 10:30:09 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BE=E7=A8=8B=E8=B5=84=E6=BA=90=E5=BA=93?= =?UTF-8?q?=E9=99=84=E4=BB=B6=E5=8D=95=E7=8B=ACJS=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/files/_attachement_list.html.erb | 2 +- public/javascripts/attachments.js | 287 ++++++++++++++------- 2 files changed, 201 insertions(+), 88 deletions(-) diff --git a/app/views/files/_attachement_list.html.erb b/app/views/files/_attachement_list.html.erb index c2c8d2d10..eb730640c 100644 --- a/app/views/files/_attachement_list.html.erb +++ b/app/views/files/_attachement_list.html.erb @@ -27,7 +27,7 @@ :id => '_file', :class => ie8? ? '':'file_selector', :multiple => true, - :onchange => 'addInputFiles(this,"'+ checkBox.to_s+'");', + :onchange => 'addInputFilesCourseSource(this,"'+ checkBox.to_s+'");', :style => ie8? ? '': 'display:none', :data => { :max_file_size => Setting.attachment_max_size.to_i.kilobytes, diff --git a/public/javascripts/attachments.js b/public/javascripts/attachments.js index 12384f451..329d0db5a 100644 --- a/public/javascripts/attachments.js +++ b/public/javascripts/attachments.js @@ -88,7 +88,7 @@ function addFile_board(inputEl, file, eagerUpload, id) { return null; } -function addFile(inputEl, file, eagerUpload,checkBox) { +function addFile(inputEl, file, eagerUpload) { var attachments_frame = '#attachments_fields'; if ($(attachments_frame).children().length < 30) { @@ -99,72 +99,50 @@ function addFile(inputEl, file, eagerUpload,checkBox) { 'id': 'attachments_' + attachmentId, 'class': 'attachment' }); - //alert(checkBox); - if(checkBox){ - fileSpan.append( - $('', { - 'type': 'text', - 'class': 'filename readonly', - 'name': 'attachments[' + attachmentId + '][filename]', - 'readonly': 'readonly' - }).val(file.name), - $('', { - 'type': 'text', - 'class': 'description', - 'name': 'attachments[' + attachmentId + '][description]', - 'maxlength': 254, - 'placeholder': $(inputEl).data('descriptionPlaceholder') - }).toggle(!eagerUpload), - $('
', { - 'class': 'div_attachments', - 'name': 'div_' + 'attachments_' + attachmentId - }) - ).appendTo('#attachments_fields'); - }else { - fileSpan.append( - $('', { - 'type': 'text', - 'class': 'filename readonly', - 'name': 'attachments[' + attachmentId + '][filename]', - 'readonly': 'readonly' - }).val(file.name), - $('', { - 'type': 'text', - 'class': 'description', - 'name': 'attachments[' + attachmentId + '][description]', - 'maxlength': 254, - 'placeholder': $(inputEl).data('descriptionPlaceholder') - }).toggle(!eagerUpload), - $('' + $(inputEl).data('fieldIsPublic') + ':').attr({ - 'class': 'ispublic-label' - }), - $('', { - 'type': 'checkbox', - 'class': 'is_public_checkbox', - 'value': 1, - 'name': 'attachments[' + attachmentId + '][is_public_checkbox]', - checked: 'checked' - }).toggle(!eagerUpload), - $(' ').attr({ - 'href': "#", - 'class': 'remove-upload' - }).click(function () { - if (confirm($(inputEl).data('areYouSure'))) { - removeFile(); - if (!eagerUpload) { - (function (e) { - reload(e); - })(fileSpan); - } - } - }).toggle(!eagerUpload), - $('
', { - 'class': 'div_attachments', - 'name': 'div_' + 'attachments_' + attachmentId - }) - ).appendTo('#attachments_fields'); - } + fileSpan.append( + $('', { + 'type': 'text', + 'class': 'filename readonly', + 'name': 'attachments[' + attachmentId + '][filename]', + 'readonly': 'readonly' + }).val(file.name), + $('', { + 'type': 'text', + 'class': 'description', + 'name': 'attachments[' + attachmentId + '][description]', + 'maxlength': 254, + 'placeholder': $(inputEl).data('descriptionPlaceholder') + }).toggle(!eagerUpload), + $('' + $(inputEl).data('fieldIsPublic') + ':').attr({ + 'class': 'ispublic-label' + }), + $('', { + 'type': 'checkbox', + 'class': 'is_public_checkbox', + 'value': 1, + 'name': 'attachments[' + attachmentId + '][is_public_checkbox]', + checked: 'checked' + }).toggle(!eagerUpload), + $(' ').attr({ + 'href': "#", + 'class': 'remove-upload' + }).click(function() { + if (confirm($(inputEl).data('areYouSure'))) { + removeFile(); + if (!eagerUpload) { + (function(e) { + reload(e); + })(fileSpan); + } + } + + }).toggle(!eagerUpload), + $('
', { + 'class': 'div_attachments', + 'name': 'div_' + 'attachments_' + attachmentId + }) + ).appendTo('#attachments_fields'); if (eagerUpload) { ajaxUpload(file, attachmentId, fileSpan, inputEl); @@ -195,9 +173,9 @@ function ajaxUpload(file, attachmentId, fileSpan, inputEl) { ajaxUpload.uploading++; uploadBlob(file, $(inputEl).data('upload-path'), attachmentId, { - loadstartEventHandler: onLoadstart.bind(progressSpan), - progressEventHandler: onProgress.bind(progressSpan) - }) + loadstartEventHandler: onLoadstart.bind(progressSpan), + progressEventHandler: onProgress.bind(progressSpan) + }) .done(function(result) { progressSpan.progressbar('value', 100).remove(); fileSpan.find('input.description, a').css('display', 'inline-block'); @@ -284,22 +262,22 @@ function removeFile() { //gcm delete all file //modify by yutao 2015-5-14 ��1��ҳ����ڶ���ϴ��ؼ�ʱ�˿�������bug �ʸ�֮ start function removeAll(containerid) { - if (confirm(deleteallfiles)) { - if (containerid == undefined) { - $(".remove-upload").removeAttr("data-confirm"); - $(".remove-upload").click(); - } else { - var arr = $(".remove-upload").filter(function() { - return $(this).data('containerid') == containerid; - }); - arr.removeAttr("data-confirm"); - arr.click(); - } + if (confirm(deleteallfiles)) { + if (containerid == undefined) { + $(".remove-upload").removeAttr("data-confirm"); + $(".remove-upload").click(); + } else { + var arr = $(".remove-upload").filter(function() { + return $(this).data('containerid') == containerid; + }); + arr.removeAttr("data-confirm"); + arr.click(); } - // return false; } - //modify by yutao 2015-5-14 ��1��ҳ����ڶ���ϴ��ؼ�ʱ�˿�������bug �ʸ�֮ end - //gcm + // return false; +} +//modify by yutao 2015-5-14 ��1��ҳ����ڶ���ϴ��ؼ�ʱ�˿�������bug �ʸ�֮ end +//gcm function uploadBlob(blob, uploadUrl, attachmentId, options) { @@ -335,11 +313,10 @@ function uploadBlob(blob, uploadUrl, attachmentId, options) { } function addInputFiles(inputEl) { - checkBox = arguments[1] == 'public' ? false : true; // var clearedFileInput = $(inputEl).clone().val(''); if (inputEl.files) { // upload files using ajax - uploadAndAttachFiles(inputEl.files, inputEl,checkBox); + uploadAndAttachFiles(inputEl.files, inputEl); // $(inputEl).remove(); } else { // browser not supporting the file API, upload on form submission @@ -386,7 +363,7 @@ function addInputFiles_board(inputEl, id) { //clearedFileInput.insertAfter('#attachments_fields'); } -function uploadAndAttachFiles(files, inputEl,checkBox) { +function uploadAndAttachFiles(files, inputEl) { var maxFileSize = $(inputEl).data('max-file-size'); var maxFileSizeExceeded = $(inputEl).data('max-file-size-message'); @@ -401,7 +378,7 @@ function uploadAndAttachFiles(files, inputEl,checkBox) { window.alert(maxFileSizeExceeded); } else { $.each(files, function() { - addFile(inputEl, this, true,checkBox); + addFile(inputEl, this, true); }); } } @@ -490,3 +467,139 @@ $(function() { } }); }); + +//课程课件 +function addInputFilesCourseSource(inputEl) { + checkBox = arguments[1] == 'public' ? false : true; + // var clearedFileInput = $(inputEl).clone().val(''); + if (inputEl.files) { + // upload files using ajax + uploadAndAttachFilesCourseSource(inputEl.files, inputEl,checkBox); + // $(inputEl).remove(); + } else { + // browser not supporting the file API, upload on form submission + var attachmentId; + var aFilename = inputEl.value.split(/\/|\\/); + var count = $('#attachments_fields>span').length; + attachmentId = addFile(inputEl, { + name: aFilename[aFilename.length - 1] + }, false); + if (attachmentId) { + $(inputEl).attr({ + name: 'attachments[' + attachmentId + '][file]' + }).hide(); + if (count <= 0) count = 1; + $('#upload_file_count').html("" + count + "" + $(inputEl).data('fileCount')); + } + } + + //clearedFileInput.insertAfter('#attachments_fields'); +} + +function uploadAndAttachFilesCourseSource(files, inputEl,checkBox) { + + var maxFileSize = $(inputEl).data('max-file-size'); + var maxFileSizeExceeded = $(inputEl).data('max-file-size-message'); + + var sizeExceeded = false; + $.each(files, function() { + if (this.size && maxFileSize != null && this.size > parseInt(maxFileSize)) { + sizeExceeded = true; + } + }); + if (sizeExceeded) { + window.alert(maxFileSizeExceeded); + } else { + $.each(files, function() { + addFileCourseSource(inputEl, this, true,checkBox); + }); + } +} + +function addFileCourseSource(inputEl, file, eagerUpload,checkBox) { + + var attachments_frame = '#attachments_fields'; + if ($(attachments_frame).children().length < 30) { + deleteallfiles = $(inputEl).data('deleteAllFiles'); + var attachmentId = addFile.nextAttachmentId++; + + var fileSpan = $('', { + 'id': 'attachments_' + attachmentId, + 'class': 'attachment' + }); + //alert(checkBox); + if(checkBox){ + fileSpan.append( + $('', { + 'type': 'text', + 'class': 'filename readonly', + 'name': 'attachments[' + attachmentId + '][filename]', + 'readonly': 'readonly' + }).val(file.name), + $('', { + 'type': 'text', + 'class': 'description', + 'name': 'attachments[' + attachmentId + '][description]', + 'maxlength': 254, + 'placeholder': $(inputEl).data('descriptionPlaceholder') + }).toggle(!eagerUpload), + $('
', { + 'class': 'div_attachments', + 'name': 'div_' + 'attachments_' + attachmentId + }) + ).appendTo('#attachments_fields'); + }else { + fileSpan.append( + $('', { + 'type': 'text', + 'class': 'filename readonly', + 'name': 'attachments[' + attachmentId + '][filename]', + 'readonly': 'readonly' + }).val(file.name), + $('', { + 'type': 'text', + 'class': 'description', + 'name': 'attachments[' + attachmentId + '][description]', + 'maxlength': 254, + 'placeholder': $(inputEl).data('descriptionPlaceholder') + }).toggle(!eagerUpload), + $('' + $(inputEl).data('fieldIsPublic') + ':').attr({ + 'class': 'ispublic-label' + }), + $('', { + 'type': 'checkbox', + 'class': 'is_public_checkbox', + 'value': 1, + 'name': 'attachments[' + attachmentId + '][is_public_checkbox]', + checked: 'checked' + }).toggle(!eagerUpload), + $(' ').attr({ + 'href': "#", + 'class': 'remove-upload' + }).click(function () { + if (confirm($(inputEl).data('areYouSure'))) { + removeFile(); + if (!eagerUpload) { + (function (e) { + reload(e); + })(fileSpan); + } + } + + }).toggle(!eagerUpload), + $('
', { + 'class': 'div_attachments', + 'name': 'div_' + 'attachments_' + attachmentId + }) + ).appendTo('#attachments_fields'); + } + + if (eagerUpload) { + ajaxUpload(file, attachmentId, fileSpan, inputEl); + + } + return attachmentId; + } + return null; +} +addFileCourseSource.nextAttachmentId = 1; \ No newline at end of file