Module: JCropper::Helpers

Included in:
ActionView::Base
Defined in:
lib/jcropper/helpers.rb

Instance Method Summary collapse

Instance Method Details

#croppable_image(object_name, attachment, style, options = {}) ⇒ Object



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
# File 'lib/jcropper/helpers.rb', line 3

def croppable_image(object_name, attachment, style, options = {})
  object = eval("@#{object_name.to_s}") unless object_name.is_a? ActiveRecord::Base
  options[:view_size] = {:width => options.delete(:width), :height => options.delete(:height)}
  return unless options = parse_options(object, options)

  ###CRZ - duplicated in JS!
  view_scale = find_bounding_scale([options[:view_size][:width], options[:view_size][:height]],
                [options[:original_geometry][:width], options[:original_geometry][:height]])

  scaled_img_dims = {:width => options[:original_geometry][:width]*view_scale, :height => options[:original_geometry][:height]*view_scale}
  s = "<div class='#{options[:css_prefix]}'>#{image_tag(object.send(attachment).url, scaled_img_dims.merge(:id => options[:id]))}</div>" + "\n"
  s += hidden_field_tag("#{object_name}[#{@js_cropper_c_i.coord_names[:x]}]", @js_cropper_c_i.starting_crop[:x], :id => @js_cropper_c_i.coord_names[:x]) + "\n"
  s += hidden_field_tag("#{object_name}[#{@js_cropper_c_i.coord_names[:y]}]", @js_cropper_c_i.starting_crop[:y], :id => @js_cropper_c_i.coord_names[:y]) + "\n"
  s += hidden_field_tag("#{object_name}[#{@js_cropper_c_i.coord_names[:w]}]", @js_cropper_c_i.starting_crop[:w], :id => @js_cropper_c_i.coord_names[:w]) + "\n"
  s += hidden_field_tag("#{object_name}[#{@js_cropper_c_i.coord_names[:h]}]", @js_cropper_c_i.starting_crop[:h], :id => @js_cropper_c_i.coord_names[:h]) + "\n"
  s += <<-JS
    <script type="text/javascript">
      #{options[:js_object]} = new CroppedImage(jQuery('.#{options[:css_prefix]} img'),
        $.extend(
          #{camelize_keys(options).to_json},
          {
            originalGeometry: #{{:width => @js_cropper_c_i.original_geometry.width, :height => @js_cropper_c_i.original_geometry.height}.to_json},
            targetGeometry: #{{:width => @js_cropper_c_i.target_geometry.width, :height => @js_cropper_c_i.target_geometry.height}.to_json},
            coordNames: #{@js_cropper_c_i.coord_names.to_json}
          })
      );
    </script>
  JS
  s
end

#croppable_image_preview(object_name, attachment, style, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jcropper/helpers.rb', line 34

def croppable_image_preview(object_name, attachment, style, options = {})
  object = eval("@#{object_name.to_s}") unless object_name.is_a? ActiveRecord::Base      
  options[:preview_size] = {:width => options.delete(:width), :height => options.delete(:height)}
  return unless options = parse_options(object, options)
  
  s = <<-HTML
  <div style="overflow:hidden;height:#{options[:preview_size][:height]}px;width:#{options[:preview_size][:width]}px;" class="#{options[:css_prefix]}-preview-mask">
    #{image_tag(@js_cropper_c_i.attachment.url(:original), :class => "#{options[:css_prefix]}-preview")}
  </div>
  HTML
  s
end