Repository URL to install this package:
|
Version:
0.5.0 ▾
|
class RichGalleryInput < ::Formtastic::Inputs::StringInput
include Formtastic::Helpers::InputHelper
attr_reader :editor_options
def to_html
@editor_options = Rich.options(options[:config], object_name, object.id)
local_input_options = {
:class => 'rich-picker',
:style => editor_options[:style]
}
input_wrapping do
label_html <<
input_field(local_input_options) <<
preview <<
javascript
end
end
private
def input_field(local_input_options)
builder.hidden_field(method, local_input_options.merge(input_html_options))
end
def preview_image_path
method_value = object.send(method)
# return placeholder image if this is a non-image picker OR if there is no value set
return editor_options[:placeholder_image] if editor_options[:type].to_s == 'file'
return editor_options[:placeholder_image] unless method_value.present?
column_type = column_for(method).type
if column_type == :integer
file = Rich::RichFile.find(method_value)
file.rich_file.url(:rich_thumb) #we ask paperclip directly for the file, so asset paths should not be an issue
else # should be :string
method_value
end
end
def button
%Q{
<a href='#{Rich.editor[:richBrowserUrl]}' class='button'>
#{I18n.t('picker_browse')}
</a>
}.html_safe
end
def javascript
%Q{
<script>
$(function(){
$('##{input_html_options[:id]}_input img').click(function(e){
e.preventDefault(); assetPicker.showFinder('##{input_html_options[:id]}', #{editor_options.to_json})
});
});
</script>
}.html_safe
end
def preview
return unless editor_options[:type] != 'file'
path = preview_image_path
klass = "class='rich-image-preview'"
style = "style='max-width:#{editor_options[:preview_size]}; max-height:#{editor_options[:preview_size]}; cursor:pointer;'"
if path
%Q{
<img src='#{preview_image_path}' #{klass} #{style}>
}.html_safe
else
%Q{
<div #{klass} #{style}>
<a href='#{Rich.editor[:richBrowserUrl]}'>
<i class="fa fa-file-o fa-5x"></i>
</a>
</div>
}.html_safe
end
end
end