diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb
index b0a7be31..e41e2a43 100644
--- a/app/controllers/attachments_controller.rb
+++ b/app/controllers/attachments_controller.rb
@@ -359,6 +359,7 @@ class AttachmentsController < ApplicationController
saved = @attachment.save
respond_to do |format|
format.js
+ format.json { render json: { attachment_id: @attachment.id, url: download_attachment_path(@attachment.id) } }
format.api {
if saved
render :action => 'upload', :status => :created
diff --git a/app/controllers/libraries_controller.rb b/app/controllers/libraries_controller.rb
index db5d20b0..27e55975 100644
--- a/app/controllers/libraries_controller.rb
+++ b/app/controllers/libraries_controller.rb
@@ -49,7 +49,7 @@ class LibrariesController < ApplicationController
rescue ActiveRecord::RecordInvalid => e
flash[:message] = e.record.errors.full_messages.join(',')
render 'new'
- rescue Libraries::SubmitService::Error => ex
+ rescue Libraries::SubmitService::Error, Libraries::SaveService::Error => ex
flash[:message] = ex.message
render 'new'
end
@@ -73,7 +73,7 @@ class LibrariesController < ApplicationController
rescue ActiveRecord::RecordInvalid => e
flash[:message] = e.record.errors.full_messages.join(',')
render 'edit'
- rescue Libraries::SubmitService::Error => ex
+ rescue Libraries::SubmitService::Error, Libraries::SaveService::Error => ex
flash[:message] = ex.message
render 'edit'
end
@@ -117,6 +117,7 @@ class LibrariesController < ApplicationController
hash = params[:library].presence || {}
hash[:tag_ids] = params[:tag_ids].to_s.split(',')
hash[:attachment_ids] = (params[:attachments].presence || []).values.map{|h| h[:attachment_id]}
+ hash[:cover_id] = save_cover.try(:id) || hash[:cover_id]
hash
end
end
@@ -132,4 +133,14 @@ class LibrariesController < ApplicationController
def increment_visit_count
@library.increment_visited_count! if @library && @library.id
end
+
+ def save_cover
+ return if params[:cover_file].blank?
+
+ attachment = Attachment.new(file: params[:cover_file])
+ attachment.author = User.current
+ attachment.filename = Redmine::Utils.random_hex(16)
+ attachment.save
+ attachment
+ end
end
\ No newline at end of file
diff --git a/app/models/library.rb b/app/models/library.rb
index c0fe35ee..6029f984 100644
--- a/app/models/library.rb
+++ b/app/models/library.rb
@@ -17,7 +17,7 @@ class Library < ActiveRecord::Base
validates :content, presence: true
validates :uuid, presence: true, uniqueness: true
validates :author_name, presence: true, length: { maximum: 10 }
- validates :author_school_name, length: { maximum: 50 }, allow_blank: true
+ validates :author_school_name, presence: true, length: { maximum: 50 }
acts_as_attachable
diff --git a/app/views/libraries/_form.html.erb b/app/views/libraries/_form.html.erb
index 36d09bed..6628a3ea 100644
--- a/app/views/libraries/_form.html.erb
+++ b/app/views/libraries/_form.html.erb
@@ -1,6 +1,6 @@
上传教学案例
-
- <% cover_exists = @library.cover_id.present? %>
+ <% cover_exists = @library.cover_id.present? && !@library.cover_id_changed? %>
封面图 (上传尺寸:120*90 px)
-
-
-
- <% if cover_exists %>
- <%= image_tag(named_attachment_path(@library.cover.id, @library.cover.filename), :class => "w120 h90 ml5 shixun_image_show") %>
- <% else %>
-
- <% end %>
- <%= file_field_tag 'library[cover_id]',
- :id => "upload_img_#{@library.id}",
- :style => 'display:none;',
- :size => "1",
- :multiple => false,
- :onchange => 'addInputAvatar(this);',
- :data => {
- :max_file_size => Setting.authentication_img_max_size.to_i.kilobytes,
- :max_file_size_message => l(:error_user_auth_too_big, :max_size => number_to_human_size(Setting.authentication_img_max_size.to_i.kilobytes)),
- :max_concurrent_uploads => Redmine::Configuration['max_concurrent_ajax_uploads'].to_i,
- :file_type => Redmine::Configuration['pic_types'].to_s,
- :type_support_message => l(:error_pic_type),
- :upload_path => uploads_path(:format => 'js',:project =>nil),
- :description_placeholder => nil ,# l(:label_optional_description)
- } %>
-
@@ -155,6 +139,9 @@
var submitForm = function(){
var title = $("input[name='library[title]']").val();
var content = $("textarea[name='library[content]']").val();
+ var author_name = $("input[name='library[author_name]']").val();
+ var author_school_name = $("input[name='library[author_school_name]']").val();
+ var cover_file = $("input[name='cover_file").val();
if (!title || title.length == 0) {
$("#title_notice").removeClass("none");
@@ -168,6 +155,25 @@
}else{
$("#des_notice").addClass("none");
}
+ if (!author_name || author_name.length == 0) {
+ $("#author_name_notice").removeClass("none");
+ return
+ }else{
+ $("#author_name_notice").addClass("none");
+ }
+ if (!author_school_name || author_school_name.length == 0) {
+ $("#author_school_name_notice").removeClass("none");
+ return
+ }else{
+ $("#author_school_name_notice").addClass("none");
+ }
+ var coverId = $("input[name='library[cover_id]']").val();
+ if ((!coverId || coverId.length == 0) && (!cover_file || cover_file.length == 0)) {
+ $("#cover_file_notice").removeClass("none");
+ return
+ }else{
+ $("#cover_file_notice").addClass("none");
+ }
if($('.attachments_fields .attachment').length == 0){
$("#file_notice").removeClass("none");
@@ -199,5 +205,26 @@
console.log('ids', ids)
$('#tag_ids').val(ids);
})
+
+ $("#library-cover-file").change(function (e) {
+ var file = e.target.files[0] || e.dataTransfer.files[0];
+ $('input.cover-file-name').val(document.getElementById("library-cover-file").files[0].name);
+ if (file) {
+ var reader = new FileReader();
+ reader.onload = function () {
+ $(".library-cover-perview img").attr("src", this.result);
+ $('.marginuploading').addClass('hidden').hide();
+ $('.library-cover-perview').show();
+ }
+
+ reader.readAsDataURL(file);
+ }
+ });
+
+ $('.library-cover-select').hover(function(){
+ $('.library-cover-select .marginuploading.hidden').show();
+ },function(){
+ $('.library-cover-select .marginuploading.hidden').hide();
+ });
});
\ No newline at end of file
diff --git a/public/stylesheets/educoder/edu-main.css b/public/stylesheets/educoder/edu-main.css
index a716e275..db0bbd45 100644
--- a/public/stylesheets/educoder/edu-main.css
+++ b/public/stylesheets/educoder/edu-main.css
@@ -1211,18 +1211,18 @@ html>body #ajax-indicator { position: fixed; }
}
.surfacePlot{
- width:120px;
- height:90px;
+ cursor: pointer;
+ width:122px;
+ height:92px;
background:rgba(250,250,250,1);
border:1px solid rgba(221,221,221,1);
}
.marginuploading{
- margin: 27px 42px;
-}
-
-.roundedRectangles{
+ padding: 28px 43px;
position: absolute;
- top: 10px;
- right: -22px;
+ z-index: 10;
+}
+.surfacePlot:hover .marginuploading.hidden{
+ background: rgba(0,0,0, 0.1);
}
\ No newline at end of file