rails_com 1.2.1 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +165 -0
- data/README.md +18 -26
- data/Rakefile +6 -9
- data/app/assets/config/rails_com_manifest.js +2 -0
- data/app/assets/javascripts/controllers/active_storage_ext/videos/show.ready.js +1 -0
- data/app/assets/javascripts/rails_com/common.js +3 -0
- data/app/assets/javascripts/rails_com/fetch_xhr_script.js +3 -2
- data/app/assets/stylesheets/controllers/active_storage_ext/videos/show.css +7 -0
- data/app/assets/stylesheets/controllers/active_storage_ext/videos/transfer.css +4 -0
- data/app/controllers/active_storage/attachments_controller.rb +13 -0
- data/app/controllers/active_storage_ext/videos_controller.rb +25 -0
- data/app/controllers/concerns/the_common_api.rb +7 -1
- data/app/helpers/rails_com/active_helper.rb +14 -10
- data/app/helpers/rails_com/assets_helper.rb +40 -14
- data/app/helpers/rails_com/format_helper.rb +37 -0
- data/app/models/state_machine.rb +10 -6
- data/app/views/active_storage/attachments/_image_item.html.erb +6 -0
- data/app/views/active_storage/attachments/_list.html.erb +28 -0
- data/app/views/active_storage/attachments/_list_edit.html.erb +25 -0
- data/app/views/active_storage/attachments/_list_form.html.erb +28 -0
- data/app/views/active_storage/attachments/_video_item.html.erb +6 -0
- data/app/views/active_storage/attachments/destroy.js.erb +1 -0
- data/app/views/active_storage_ext/videos/show.html.erb +6 -0
- data/app/views/kaminari/_next_page.html.erb +1 -1
- data/app/views/kaminari/_prev_page.html.erb +1 -1
- data/app/views/shared/_error_messages.html.erb +1 -1
- data/config/locales/en.yml +5 -0
- data/config/locales/zh.yml +5 -0
- data/config/routes.rb +10 -5
- data/lib/assets/javascripts/input-attachment.js +83 -22
- data/lib/mina/git2.rb +59 -0
- data/lib/rails_com.rb +22 -12
- data/lib/rails_com/config.rb +3 -1
- data/lib/rails_com/core_ext/array.rb +35 -17
- data/lib/rails_com/core_ext/date.rb +24 -0
- data/lib/rails_com/core_ext/numeric.rb +11 -0
- data/lib/rails_com/core_ext/string.rb +15 -0
- data/lib/rails_com/engine.rb +23 -11
- data/lib/rails_com/{controller_helper.rb → helpers/controller_helper.rb} +0 -20
- data/lib/rails_com/{model_helper.rb → helpers/model_helper.rb} +3 -3
- data/lib/rails_com/{controllers.rb → meta/controllers.rb} +0 -0
- data/lib/rails_com/{models.rb → meta/models.rb} +1 -0
- data/lib/rails_com/{routes.rb → meta/routes.rb} +9 -0
- data/lib/rails_com/rails_ext/activestorage_attached.rb +5 -5
- data/lib/rails_com/rails_ext/attachment_transfer.rb +40 -0
- data/lib/rails_com/rails_ext/named_base.rb +9 -0
- data/lib/rails_com/rails_ext/template_renderer.rb +18 -1
- data/lib/rails_com/rails_ext/video_response.rb +28 -0
- data/lib/{utils → rails_com/utils}/ip_helper.rb +0 -0
- data/lib/rails_com/{helpers → utils}/jobber.rb +0 -0
- data/lib/rails_com/utils/num_helper.rb +45 -0
- data/lib/rails_com/{setting.rb → utils/setting.rb} +0 -0
- data/lib/rails_com/utils/time_helper.rb +58 -0
- data/lib/rails_com/{helpers → utils}/uid_helper.rb +0 -0
- data/lib/rails_com/version.rb +1 -1
- data/lib/templates/erb/scaffold/_form.html.erb.tt +2 -2
- data/lib/templates/erb/scaffold/index.html.erb.tt +21 -13
- data/lib/templates/erb/scaffold/show.html.erb.tt +14 -5
- data/lib/templates/rails/scaffold_controller/api_controller.rb.tt +48 -0
- data/lib/templates/rails/scaffold_controller/controller.rb.tt +16 -15
- metadata +45 -35
- data/MIT-LICENSE +0 -20
- data/app/controllers/common_controller.rb +0 -10
- data/app/controllers/the_guards_controller.rb +0 -18
- data/app/helpers/rails_com/common_helper.rb +0 -13
- data/app/views/the_guards/index.html.erb +0 -18
- data/lib/utils/time_helper.rb +0 -37
@@ -1,31 +1,57 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
module RailsCom::AssetsHelper
|
3
3
|
|
4
|
-
def js_load(filename = nil,
|
4
|
+
def js_load(filename = nil, **options)
|
5
5
|
filename ||= "controllers/#{controller_path}/#{action_name}"
|
6
|
-
|
7
|
-
|
8
|
-
if
|
6
|
+
paths = assets_load_path(filename)
|
7
|
+
|
8
|
+
if paths.any? { |path| File.exist?(path) }
|
9
9
|
javascript_include_tag filename, options
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
def css_load(filename = nil,
|
13
|
+
def css_load(filename = nil, **options)
|
14
14
|
filename ||= "controllers/#{controller_path}/#{action_name}"
|
15
|
-
|
16
|
-
|
17
|
-
if File.exist?(path
|
15
|
+
paths = assets_load_path(filename, relative_path: 'app/assets/stylesheets', ext: ['.css', '.css.erb'])
|
16
|
+
|
17
|
+
if paths.any? { |path| File.exist?(path) }
|
18
18
|
stylesheet_link_tag filename, options
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
23
|
-
filename ||= "controllers/#{controller_path}/#{action_name}
|
24
|
-
|
25
|
-
|
26
|
-
if
|
27
|
-
|
22
|
+
def js_pack(filename = nil, **options)
|
23
|
+
filename ||= "controllers/#{controller_path}/#{action_name}"
|
24
|
+
paths = assets_load_path(filename, relative_path: 'app/javascript/packs', **options)
|
25
|
+
|
26
|
+
if paths.any? { |path| File.exist?(path) }
|
27
|
+
javascript_pack_tag filename, options
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
+
def js_ready(**options)
|
32
|
+
js_load("controllers/#{controller_path}/#{action_name}.ready", **options)
|
33
|
+
end
|
34
|
+
|
35
|
+
def js_pack_ready(**options)
|
36
|
+
js_pack("controllers/#{controller_path}/#{action_name}-ready", **options)
|
37
|
+
end
|
38
|
+
|
39
|
+
def assets_load_path(filename, relative_path: 'app/assets/javascripts', ext: ['.js', '.js.erb'])
|
40
|
+
paths = []
|
41
|
+
|
42
|
+
file_path = Pathname.new(relative_path).join filename
|
43
|
+
rails_path = Rails.root.join file_path
|
44
|
+
ext.each do |name|
|
45
|
+
paths << rails_path.to_s + name
|
46
|
+
end
|
47
|
+
if @_rendered_from
|
48
|
+
engine_path = @_rendered_from.join file_path
|
49
|
+
ext.each do |name|
|
50
|
+
paths << engine_path.to_s + name
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
paths
|
55
|
+
end
|
56
|
+
|
31
57
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module RailsCom::FormatHelper
|
3
|
+
|
4
|
+
def simple_format_hash(hash_text, html_options = {}, options = {})
|
5
|
+
wrapper_tag = options.fetch(:wrapper_tag, :p)
|
6
|
+
|
7
|
+
hash_text.map do |k, v|
|
8
|
+
if k.to_s.rstrip.end_with?(':')
|
9
|
+
text = k.to_s + ' ' + v.to_s
|
10
|
+
else
|
11
|
+
text = k.to_s + ': ' + v.to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
content_tag(wrapper_tag, text, html_options)
|
15
|
+
end.join("\n\n").html_safe
|
16
|
+
end
|
17
|
+
|
18
|
+
def simple_format(text, html_options = {}, options = {})
|
19
|
+
if text.is_a?(Hash)
|
20
|
+
return simple_format_hash(text, html_options, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
if text.is_a?(Array)
|
24
|
+
begin
|
25
|
+
hash_text = text.to_h
|
26
|
+
return simple_format_hash(hash_text, html_options, options)
|
27
|
+
rescue TypeError
|
28
|
+
return content_tag(:span, text, html_options)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
text = text.to_s
|
33
|
+
|
34
|
+
super
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
data/app/models/state_machine.rb
CHANGED
@@ -3,12 +3,12 @@ module StateMachine
|
|
3
3
|
# to defined next_xxx_states in class
|
4
4
|
|
5
5
|
# obj.next_to state: 'xxx'
|
6
|
-
def next_to(options = {})
|
6
|
+
def next_to(options = {}, &block)
|
7
7
|
options.each do |column, value|
|
8
8
|
if self.class.method_defined? "next_#{column}_states"
|
9
9
|
_next_state = self.send("next_#{column}_states").first
|
10
10
|
else
|
11
|
-
_next_state = next_state(column)
|
11
|
+
_next_state = next_state(column, &block)
|
12
12
|
end
|
13
13
|
|
14
14
|
if _next_state == value.to_s
|
@@ -21,17 +21,17 @@ module StateMachine
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
def next_to!(options = {})
|
24
|
+
def next_to!(options = {}, &block)
|
25
25
|
self.next_to(options, &block)
|
26
26
|
self.save!
|
27
27
|
end
|
28
28
|
|
29
|
-
def trigger_to(options = {})
|
29
|
+
def trigger_to(options = {}, &block)
|
30
30
|
options.each do |column, value|
|
31
31
|
if self.class.method_defined? "next_#{column}_states"
|
32
32
|
_next_states = self.send "next_#{column}_states"
|
33
33
|
else
|
34
|
-
_next_states = next_states(column)
|
34
|
+
_next_states = next_states(column, &block)
|
35
35
|
end
|
36
36
|
|
37
37
|
if _next_states.include?(value.to_s)
|
@@ -44,11 +44,15 @@ module StateMachine
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
def trigger_to!(options = {})
|
47
|
+
def trigger_to!(options = {}, &block)
|
48
48
|
self.trigger_to(options, &block)
|
49
49
|
self.save!
|
50
50
|
end
|
51
51
|
|
52
|
+
def next_state(state_name, &block)
|
53
|
+
next_states(state_name, &block).first
|
54
|
+
end
|
55
|
+
|
52
56
|
# obj.next_states(:state) do |result|
|
53
57
|
# result.reject
|
54
58
|
# end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
<div class="ui small image">
|
2
|
+
<%= link_to rails_attachment_path(image.id), method: :delete, data: {confirm: 'Are you sure?'}, remote: true, class: 'ui grey right corner mini label' do %>
|
3
|
+
<i class="times icon"></i>
|
4
|
+
<% end %>
|
5
|
+
<%= image_tag image, id: "attachment_#{image.id}", data: { target: 'attachment.src' }, style: 'max-width: 100%' %>
|
6
|
+
</div>
|
@@ -0,0 +1,28 @@
|
|
1
|
+
<% if target.is_a?(ActiveStorage::Attached::One) && target.attached? %>
|
2
|
+
<% if target.content_type.to_s.start_with?('image') %>
|
3
|
+
<%= image_tag target, style: 'max-width: 100%', class: 'ui small image' %>
|
4
|
+
<% elsif target.content_type.to_s.start_with?('video') %>
|
5
|
+
<%= link_to target.filename, rails_video_path(video), target: '_blank' %>
|
6
|
+
<% else %>
|
7
|
+
<p id="attachment_<%= target.id %>">
|
8
|
+
<%= link_to target.filename, rails_blob_path(target) %>
|
9
|
+
</p>
|
10
|
+
<% end %>
|
11
|
+
<% elsif target.is_a?(ActiveStorage::Attached::Many) %>
|
12
|
+
<% target.each do |file| %>
|
13
|
+
<% if file.content_type.to_s.start_with?('image') %>
|
14
|
+
<%= image_tag file, style: 'max-width: 100%', class: 'ui small image' %>
|
15
|
+
<% elsif file.content_type.to_s.start_with?('video') %>
|
16
|
+
<p id="attachment_<%= file.id %>">
|
17
|
+
<%= link_to file.filename, rails_video_path(file), target: '_blank' %>
|
18
|
+
</p>
|
19
|
+
<% else %>
|
20
|
+
<p id="attachment_<%= file.id %>">
|
21
|
+
<%= link_to file.filename, rails_blob_path(file) %>
|
22
|
+
</p>
|
23
|
+
<% end %>
|
24
|
+
<% end %>
|
25
|
+
<% end %>
|
26
|
+
|
27
|
+
|
28
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
<% if target.is_a?(ActiveStorage::Attached::One) && target.attached? %>
|
2
|
+
<% if target.content_type.to_s.start_with?('image') %>
|
3
|
+
<%= render partial: 'active_storage/attachments/image_item', locals: { image: target } %>
|
4
|
+
<% elsif target.content_type.to_s.start_with?('video') %>
|
5
|
+
<%= render partial: 'active_storage/attachments/video_item', locals: { video: target } %>
|
6
|
+
<% else %>
|
7
|
+
<p id="attachment_<%= target.id %>">
|
8
|
+
<%= link_to target.filename, rails_blob_path(target) %>
|
9
|
+
<%= link_to 'x', rails_attachment_path(target.id), method: :delete, data: {confirm: 'Are you sure?'}, remote: true %>
|
10
|
+
</p>
|
11
|
+
<% end %>
|
12
|
+
<% elsif target.is_a?(ActiveStorage::Attached::Many) %>
|
13
|
+
<% target.each do |file| %>
|
14
|
+
<% if file.content_type.to_s.start_with?('image') %>
|
15
|
+
<%= render partial: 'active_storage/attachments/image_item', locals: { image: file } %>
|
16
|
+
<% elsif file.content_type.to_s.start_with?('video') %>
|
17
|
+
<%= render partial: 'active_storage/attachments/video_item', locals: { video: file } %>
|
18
|
+
<% else %>
|
19
|
+
<p id="attachment_<%= file.id %>">
|
20
|
+
<%= link_to file.filename, rails_blob_path(file) %>
|
21
|
+
<%= link_to 'x', rails_attachment_path(file.id), method: :delete, data: {confirm: 'Are you sure?'}, remote: true %>
|
22
|
+
</p>
|
23
|
+
<% end %>
|
24
|
+
<% end %>
|
25
|
+
<% end %>
|
@@ -0,0 +1,28 @@
|
|
1
|
+
<div class="top attached inline fields" id="<%= "#{target.record.class.name.underscore}_attachments_#{target.record.id}" %>">
|
2
|
+
<div class="six wide field"></div>
|
3
|
+
<div class="field" id="file_preview" data-controller="attachment">
|
4
|
+
<% if target.is_a?(ActiveStorage::Attached::One) && target.attached? %>
|
5
|
+
<% if target.content_type.to_s.start_with?('image') %>
|
6
|
+
<%= render partial: 'active_storage/attachments/image_item', locals: { image: target } %>
|
7
|
+
<% else %>
|
8
|
+
<p id="attachment_<%= target.id %>">
|
9
|
+
<%= link_to target.filename, rails_blob_path(target) %>
|
10
|
+
<%= link_to 'x', rails_attachment_path(target.id), method: :delete, data: {confirm: 'Are you sure?'}, remote: true %>
|
11
|
+
</p>
|
12
|
+
<% end %>
|
13
|
+
<% elsif target.is_a?(ActiveStorage::Attached::Many) %>
|
14
|
+
<% target.each do |file| %>
|
15
|
+
<% if file.content_type.to_s.start_with?('image') %>
|
16
|
+
<%= render partial: 'active_storage/attachments/image_item', locals: { image: file } %>
|
17
|
+
<% else %>
|
18
|
+
<p id="attachment_<%= file.id %>">
|
19
|
+
<%= link_to file.filename, rails_blob_path(file) %>
|
20
|
+
<%= link_to 'x', rails_attachment_path(file.id), method: :delete, data: {confirm: 'Are you sure?'}, remote: true %>
|
21
|
+
</p>
|
22
|
+
<% end %>
|
23
|
+
<% end %>
|
24
|
+
<% end %>
|
25
|
+
</div>
|
26
|
+
</div>
|
27
|
+
|
28
|
+
|
@@ -0,0 +1,6 @@
|
|
1
|
+
<p id="attachment_<%= video.id %>">
|
2
|
+
<%= link_to video.filename, rails_video_path(video), target: '_blank' %>
|
3
|
+
<%= link_to 'transfer', transfer_rails_video_path(video.id), method: :put, data: { confirm: 'are you sure?' }, class: 'ui label' %>
|
4
|
+
<%= link_to 'x', rails_attachment_path(video.id), method: :delete, data: {confirm: 'Are you sure?'}, remote: true %>
|
5
|
+
</p>
|
6
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
$('#attachment_<%= @attachment.id %>').remove();
|
@@ -1,7 +1,7 @@
|
|
1
1
|
<% if target.errors.any? %>
|
2
2
|
<div class="ui negative message">
|
3
3
|
<i class="close icon"></i>
|
4
|
-
<div class="header"
|
4
|
+
<div class="header"><%= target.errors.count %> <% if target.errors.count == 1 %> error <% else %> errors <% end %>: </div>
|
5
5
|
<ul class="list">
|
6
6
|
<% target.errors.full_messages.each do |msg| %>
|
7
7
|
<li><%= msg %></li>
|
data/config/locales/en.yml
CHANGED
data/config/locales/zh.yml
CHANGED
data/config/routes.rb
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
Rails.application.routes.draw do
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
|
3
|
+
scope :rails, as: 'rails', module: 'active_storage' do
|
4
|
+
resources :attachments, only: [:destroy]
|
5
|
+
end
|
6
|
+
|
7
|
+
scope :rails, as: 'rails', module: 'active_storage_ext' do
|
8
|
+
resources :videos, only: [:show] do
|
9
|
+
put :transfer, on: :member
|
10
|
+
end
|
6
11
|
end
|
7
12
|
|
8
|
-
end
|
13
|
+
end
|
@@ -4,7 +4,7 @@
|
|
4
4
|
/*
|
5
5
|
* Input Field with Attachment
|
6
6
|
* Fork and Inspired by inlineAttachment(https://github.com/Rovak/InlineAttachment)
|
7
|
-
* Author:
|
7
|
+
* Author: mingyuan.qin
|
8
8
|
* Contact: mingyuan0715@foxmail.com
|
9
9
|
*/
|
10
10
|
|
@@ -14,7 +14,6 @@
|
|
14
14
|
var InputAttachment = function(options) {
|
15
15
|
this.settings = InputAttachment.util.merge(options, InputAttachment.defaults);
|
16
16
|
this.editor = InputAttachment.editors.initEditor(this.settings['editor']);
|
17
|
-
this.fileInput = InputAttachment.editors.initEditor(this.settings['fileInput']);
|
18
17
|
this.filenameTag = '{filename}';
|
19
18
|
this.lastValue = null;
|
20
19
|
};
|
@@ -83,8 +82,8 @@
|
|
83
82
|
/**
|
84
83
|
* Inserts the given value at the current cursor position of the textarea element
|
85
84
|
*
|
86
|
-
* @param {
|
87
|
-
* @param {String}
|
85
|
+
* @param {HTMLElement} el
|
86
|
+
* @param {String} text Text which will be inserted at the cursor position
|
88
87
|
*/
|
89
88
|
insertTextAtCursor: function(el, text) {
|
90
89
|
var scrollPos = el.scrollTop,
|
@@ -93,17 +92,17 @@
|
|
93
92
|
range;
|
94
93
|
|
95
94
|
if ((el.selectionStart || el.selectionStart === '0')) {
|
96
|
-
browser =
|
95
|
+
browser = 'ff';
|
97
96
|
} else if (document.selection) {
|
98
|
-
browser =
|
97
|
+
browser = 'ie';
|
99
98
|
}
|
100
99
|
|
101
|
-
if (browser ===
|
100
|
+
if (browser === 'ie') {
|
102
101
|
el.focus();
|
103
102
|
range = document.selection.createRange();
|
104
103
|
range.moveStart('character', -el.value.length);
|
105
104
|
strPos = range.text.length;
|
106
|
-
} else if (browser ===
|
105
|
+
} else if (browser === 'ff') {
|
107
106
|
strPos = el.selectionStart;
|
108
107
|
}
|
109
108
|
|
@@ -111,7 +110,7 @@
|
|
111
110
|
var back = (el.value).substring(strPos, el.value.length);
|
112
111
|
el.value = front + text + back;
|
113
112
|
strPos = strPos + text.length;
|
114
|
-
if (browser ===
|
113
|
+
if (browser === 'ie') {
|
115
114
|
el.focus();
|
116
115
|
range = document.selection.createRange();
|
117
116
|
range.moveStart('character', -el.value.length);
|
@@ -150,6 +149,8 @@
|
|
150
149
|
|
151
150
|
uploadFileInput: '',
|
152
151
|
|
152
|
+
previewDiv: 'file_preview',
|
153
|
+
|
153
154
|
/**
|
154
155
|
* Extension which will be used when a file extension could not
|
155
156
|
* be detected
|
@@ -187,7 +188,7 @@
|
|
187
188
|
/**
|
188
189
|
* Text which will be used when uploading has failed
|
189
190
|
*/
|
190
|
-
errorText:
|
191
|
+
errorText: 'Error uploading file',
|
191
192
|
|
192
193
|
/**
|
193
194
|
* Extra parameters which will be send when uploading a file
|
@@ -231,7 +232,7 @@
|
|
231
232
|
},
|
232
233
|
|
233
234
|
/**
|
234
|
-
* When a file has
|
235
|
+
* When a file has successfully been uploaded
|
235
236
|
*/
|
236
237
|
onFileUploaded: function() {}
|
237
238
|
};
|
@@ -262,14 +263,14 @@
|
|
262
263
|
}
|
263
264
|
}
|
264
265
|
|
265
|
-
var remoteFilename =
|
266
|
+
var remoteFilename = 'image-' + Date.now() + '.' + extension;
|
266
267
|
if (typeof settings.remoteFilename === 'function') {
|
267
268
|
remoteFilename = settings.remoteFilename(file);
|
268
269
|
}
|
269
270
|
|
270
271
|
formData.append(settings.uploadFieldName, file, remoteFilename);
|
271
272
|
|
272
|
-
// Append the extra parameters to the
|
273
|
+
// Append the extra parameters to the form data
|
273
274
|
if (typeof settings.extraParams === "object") {
|
274
275
|
for (var key in settings.extraParams) {
|
275
276
|
if (settings.extraParams.hasOwnProperty(key)) {
|
@@ -281,7 +282,7 @@
|
|
281
282
|
xhr.open('POST', settings.uploadUrl);
|
282
283
|
|
283
284
|
// Add any available extra headers
|
284
|
-
if (typeof settings.extraHeaders ===
|
285
|
+
if (typeof settings.extraHeaders === 'object') {
|
285
286
|
for (var header in settings.extraHeaders) {
|
286
287
|
if (settings.extraHeaders.hasOwnProperty(header)) {
|
287
288
|
xhr.setRequestHeader(header, settings.extraHeaders[header]);
|
@@ -303,10 +304,43 @@
|
|
303
304
|
return xhr;
|
304
305
|
};
|
305
306
|
|
307
|
+
InputAttachment.prototype.previewFile = function(file, obj) {
|
308
|
+
var fileList = document.getElementById(this.settings.previewDiv),
|
309
|
+
img = new Image();
|
310
|
+
img.classList.add('ui', 'small', 'image');
|
311
|
+
|
312
|
+
if(window.URL){
|
313
|
+
//File API
|
314
|
+
img.src = window.URL.createObjectURL(file); //创建一个object URL,并不是你的本地路径
|
315
|
+
img.onload = function(e) {
|
316
|
+
window.URL.revokeObjectURL(this.src); //图片加载后,释放object URL
|
317
|
+
}
|
318
|
+
} else if(window.FileReader){
|
319
|
+
//opera不支持createObjectURL/revokeObjectURL方法。我们用FileReader对象来处理
|
320
|
+
var reader = new FileReader();
|
321
|
+
reader.readAsDataURL(file);
|
322
|
+
reader.onload = function(e){
|
323
|
+
img.src = this.result;
|
324
|
+
}
|
325
|
+
}else{
|
326
|
+
//ie
|
327
|
+
obj.select();
|
328
|
+
obj.blur();
|
329
|
+
var nfile = document.selection.createRange().text;
|
330
|
+
document.selection.empty();
|
331
|
+
img.src = nfile;
|
332
|
+
img.onload=function(e){
|
333
|
+
|
334
|
+
}
|
335
|
+
}
|
336
|
+
|
337
|
+
fileList.appendChild(img);
|
338
|
+
};
|
339
|
+
|
306
340
|
/**
|
307
341
|
* Returns if the given file is allowed to handle
|
308
342
|
*
|
309
|
-
* @param {File} clipboard data file
|
343
|
+
* @param {File} file clipboard data file
|
310
344
|
*/
|
311
345
|
InputAttachment.prototype.isFileAllowed = function(file) {
|
312
346
|
if (file.kind === 'string') { return false; }
|
@@ -321,7 +355,7 @@
|
|
321
355
|
* Handles upload response
|
322
356
|
*
|
323
357
|
* @param {XMLHttpRequest} xhr
|
324
|
-
* @return {
|
358
|
+
* @return {void}
|
325
359
|
*/
|
326
360
|
InputAttachment.prototype.onFileUploadResponse = function(xhr) {
|
327
361
|
if (this.settings.onFileUploadResponse.call(this, xhr) !== false) {
|
@@ -337,7 +371,7 @@
|
|
337
371
|
}
|
338
372
|
var text = this.editor.getValue().replace(this.lastValue, newValue);
|
339
373
|
this.editor.setValue(text);
|
340
|
-
this.settings.onFileUploaded.call(this, filename);
|
374
|
+
this.settings.onFileUploaded.call(this, filename, result);
|
341
375
|
}
|
342
376
|
}
|
343
377
|
};
|
@@ -347,7 +381,7 @@
|
|
347
381
|
* Called when a file has failed to upload
|
348
382
|
*
|
349
383
|
* @param {XMLHttpRequest} xhr
|
350
|
-
* @return {
|
384
|
+
* @return {void}
|
351
385
|
*/
|
352
386
|
InputAttachment.prototype.onFileUploadError = function(xhr) {
|
353
387
|
if (this.settings.onFileUploadError.call(this, xhr) !== false) {
|
@@ -360,7 +394,7 @@
|
|
360
394
|
* Called when a file has been inserted, either by drop or paste
|
361
395
|
*
|
362
396
|
* @param {File} file
|
363
|
-
* @return {
|
397
|
+
* @return {void}
|
364
398
|
*/
|
365
399
|
InputAttachment.prototype.onFileInserted = function(file) {
|
366
400
|
if (this.settings.onFileReceived.call(this, file) !== false) {
|
@@ -369,9 +403,8 @@
|
|
369
403
|
}
|
370
404
|
};
|
371
405
|
|
372
|
-
|
373
406
|
/**
|
374
|
-
* Called when a paste event
|
407
|
+
* Called when a paste event occurred
|
375
408
|
* @param {Event} e
|
376
409
|
* @return {Boolean} if the event was handled
|
377
410
|
*/
|
@@ -380,7 +413,7 @@
|
|
380
413
|
clipboardData = e.clipboardData,
|
381
414
|
items;
|
382
415
|
|
383
|
-
if (typeof clipboardData ===
|
416
|
+
if (typeof clipboardData === 'object') {
|
384
417
|
items = clipboardData.items || clipboardData.files || [];
|
385
418
|
|
386
419
|
for (var i = 0; i < items.length; i++) {
|
@@ -430,6 +463,20 @@
|
|
430
463
|
return result;
|
431
464
|
};
|
432
465
|
|
466
|
+
InputAttachment.prototype.imagePreview = function(e){
|
467
|
+
var result = false;
|
468
|
+
|
469
|
+
for (var i = 0; i < e.target.files.length; i++) {
|
470
|
+
var file = e.target.files[i];
|
471
|
+
if (this.isFileAllowed(file)) {
|
472
|
+
result = true;
|
473
|
+
this.previewFile(file, e.target);
|
474
|
+
}
|
475
|
+
}
|
476
|
+
|
477
|
+
return result;
|
478
|
+
};
|
479
|
+
|
433
480
|
InputAttachment.prototype.onFileInputClick = function(e) {
|
434
481
|
console.log('fileInputClick', e)
|
435
482
|
};
|
@@ -476,6 +523,20 @@
|
|
476
523
|
}
|
477
524
|
}
|
478
525
|
|
526
|
+
function attachToPreview(options) {
|
527
|
+
options = options || {};
|
528
|
+
var fileInput = document.getElementById(options['fileInput']);
|
529
|
+
options['fileInput'] = fileInput;
|
530
|
+
var inlineattach = new InputAttachment(options);
|
531
|
+
|
532
|
+
if (fileInput) {
|
533
|
+
fileInput.addEventListener('change', function(e) {
|
534
|
+
inlineattach.imagePreview(e)
|
535
|
+
}, false);
|
536
|
+
}
|
537
|
+
}
|
538
|
+
|
479
539
|
window.attachToInput = attachToInput;
|
540
|
+
window.attachToPreview = attachToPreview;
|
480
541
|
|
481
542
|
})(document, window);
|