/
rich.rb
199 lines (156 loc) · 5.66 KB
/
rich.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
if Object.const_defined?("RailsAdmin")
require "rich/rails_admin/config/fields/types/rich_picker"
require "rich/rails_admin/config/fields/types/rich_editor"
end
require "rich/engine"
module Rich
# configure image styles
def self.image_styles
@@image_styles.merge({ :rich_thumb => "100x100#" })
end
def self.image_styles=(image_styles)
@@image_styles = image_styles
end
@@image_styles = {
:thumb => "100x100#"
}
mattr_accessor :convert_options
@@convert_options = {}
mattr_accessor :allowed_styles
@@allowed_styles = :all
mattr_accessor :default_style
@@default_style = :thumb
mattr_accessor :authentication_method
@@authentication_method = :none
mattr_accessor :insert_many
@@insert_many = false
mattr_accessor :allow_document_uploads
@@allow_document_uploads = false
mattr_accessor :allow_embeds
@@allow_embeds = false
mattr_accessor :allowed_image_types
@@allowed_image_types = ['image/jpeg', 'image/png', 'image/gif', 'image/jpg']
mattr_accessor :allowed_document_types
@@allowed_document_types = :all
mattr_accessor :file_path
@@file_path
mattr_accessor :backend
@@backend = :paperclip
# configuration for picker
mattr_accessor :placeholder_image
@@placeholder_image = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" # a transparent pixel
mattr_accessor :preview_size
@@preview_size = "100px"
mattr_accessor :hidden_input
@@hidden_input = false
# end configuration for picker
# Configuration defaults (these map directly to ckeditor settings)
mattr_accessor :editor
@@editor = {
:height => 400,
:stylesSet => [],
:extraPlugins => 'stylesheetparser,richfile,MediaEmbed,showblocks',
:removePlugins => 'scayt,menubutton,image,forms',
:contentsCss => :default,
:removeDialogTabs => 'link:advanced;link:target',
:startupOutlineBlocks => true,
:forcePasteAsPlainText => true,
:format_tags => 'h3;p;pre',
:toolbar => [['Format','Styles'],['Bold', 'Italic', '-','NumberedList', 'BulletedList', 'Blockquote', '-', 'richImage', 'richFile','MediaEmbed', '-', 'Link', 'Unlink'],['Source', 'ShowBlocks']],
:language => I18n.default_locale,
:richBrowserUrl => '/rich/files/',
:uiColor => '#f4f4f4'
}
# End configuration defaults
mattr_accessor :paginates_per
@@paginates_per = 34
def self.options(overrides={}, scope_type=nil, scope_id=nil)
# merge in editor settings configured elsewhere
if(self.allowed_styles == :all)
# replace :all with a list of the actual styles that are present
all_styles = Rich.image_styles.keys
all_styles.push(:original)
self.allowed_styles = all_styles
end
base = {
:allowed_styles => self.allowed_styles,
:default_style => self.default_style,
:insert_many => self.insert_many,
:allow_document_uploads => self.allow_document_uploads,
:allow_embeds => self.allow_embeds,
:placeholder_image => self.placeholder_image,
:preview_size => self.preview_size,
:hidden_input => self.hidden_input,
:paginates_per => self.paginates_per
}
editor_options = self.editor.merge(base)
# merge in local overrides
editor_options.merge!(overrides) if overrides
# if the contentcss is set to :default, use the asset pipeline
editor_options[:contentsCss] = ActionController::Base.helpers.stylesheet_path('rich/editor.css') if editor_options[:contentsCss] == :default
# update the language to the currently selected locale
editor_options[:language] = I18n.locale
# remove the filebrowser if allow_document_uploads is false (the default)
unless editor_options[:allow_document_uploads]
editor_options[:toolbar].map{|a| a.delete 'richFile'; a}
end
unless editor_options[:allow_embeds]
editor_options[:toolbar].map{|a| a.delete 'MediaEmbed'; a}
end
# object scoping
# todo: support scoped=string to scope to collections, set id to 0
unless editor_options[:scoped] == nil
# true signifies object level scoping
if editor_options[:scoped] == true
if(scope_type != nil && scope_id != nil)
editor_options[:scope_type] = scope_type
editor_options[:scope_id] = scope_id
else
# cannot scope new objects
editor_options[:scoped] = false
end
else
# not true (but also not nil) signifies scoping to a collection
if(scope_type != nil)
editor_options[:scope_type] = editor_options[:scoped]
editor_options[:scope_id] = 0
editor_options[:scoped] = true
else
editor_options[:scoped] = false
end
end
end
editor_options
end
def self.validate_mime_type(mime, simplified_type)
# does the mimetype match the given simplified type?
#puts "matching:" + mime + " TO " + simplified_type
false # assume the worst
if simplified_type == "image"
if allowed_image_types.include?(mime)
true
end
elsif simplified_type == "file"
if allowed_document_types == :all || allowed_document_types.include?(mime)
true
end
end
end
def self.setup
yield self
end
def self.insert
# manually inject into Formtastic 1. V2 is extended autmatically.
if Object.const_defined?("Formtastic")
if(Gem.loaded_specs["formtastic"].version.version[0,1] == "1")
require 'rich/integrations/legacy_formtastic'
::Formtastic::SemanticFormBuilder.send :include, Rich::Integrations::FormtasticBuilder
end
end
if self.backend == :paperclip
require 'rich/backends/paperclip'
elsif self.backend == :carrierwave
require 'rich/backends/carrierwave'
end
end
end