Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added options for generation of css and js directly in the form, inst…
…ead of capture (@content_for)
  • Loading branch information
bbenezech committed Mar 31, 2010
1 parent 6449a6b commit f5874f3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 32 deletions.
6 changes: 5 additions & 1 deletion generators/papermill_initializer/papermill_initializer.rb
Expand Up @@ -28,7 +28,11 @@ module Papermill
# Helper can generates inline css styling that adapt to your gallery/images placeholder. You can use it to scaffold, then copy the lines you need in your application css and set it to false. # Helper can generates inline css styling that adapt to your gallery/images placeholder. You can use it to scaffold, then copy the lines you need in your application css and set it to false.


# :inline_css => true, # :inline_css => true,


# If you don't want generated CSS and JS to be captured and available through @papermill_inline_css/@papermill_inline_js, and instead directly outputed in the form, set it to false

# :use_content_for => true,

# SwfUpload will only let the user upload images. # SwfUpload will only let the user upload images.


# :images_only => false, # :images_only => false,
Expand Down
79 changes: 48 additions & 31 deletions lib/papermill/form_builder.rb
Expand Up @@ -51,16 +51,18 @@ def papermill_upload_tag(method, options)
options = association_options.deep_merge(options) options = association_options.deep_merge(options)
field_name = "#{assetable_name}[papermill_#{method}_ids][]" field_name = "#{assetable_name}[papermill_#{method}_ids][]"


html = {}

if ot = options[:thumbnail] if ot = options[:thumbnail]
w = ot[:width] || ot[:height] && ot[:aspect_ratio] && (ot[:height] * ot[:aspect_ratio]).to_i || nil w = ot[:width] || ot[:height] && ot[:aspect_ratio] && (ot[:height] * ot[:aspect_ratio]).to_i || nil
h = ot[:height] || ot[:width] && ot[:aspect_ratio] && (ot[:width] / ot[:aspect_ratio]).to_i || nil h = ot[:height] || ot[:width] && ot[:aspect_ratio] && (ot[:width] / ot[:aspect_ratio]).to_i || nil
computed_style = ot[:style] || (w || h) && "#{w}x#{h}>" || "original" computed_style = ot[:style] || (w || h) && "#{w}x#{h}>" || "original"
set_papermill_inline_css(field_id, w, h, options) html[:css] = set_papermill_inline_css(field_id, w, h, options)
end end


set_papermill_inline_js(field_id, compute_papermill_url(:create, computed_style, field_name, field_id, options), options) html[:js] = set_papermill_inline_js(field_id, compute_papermill_url(:create, computed_style, field_name, field_id, options), options)


html = {}
html[:upload_button] = %{\ html[:upload_button] = %{\
<div id="#{field_id}-button-wrapper" class="papermill-button-wrapper" style="height: #{options[:swfupload][:button_height]}px;"> <div id="#{field_id}-button-wrapper" class="papermill-button-wrapper" style="height: #{options[:swfupload][:button_height]}px;">
<span id="browse_for_#{field_id}" class="swf_button"></span> <span id="browse_for_#{field_id}" class="swf_button"></span>
Expand Down Expand Up @@ -91,7 +93,7 @@ def papermill_upload_tag(method, options)
# hidden_field needed to empty a list. # hidden_field needed to empty a list.
%{<div class="papermill"> %{<div class="papermill">
#{@template.hidden_field("#{assetable_name}[papermill_#{method}_ids]", nil)} #{@template.hidden_field("#{assetable_name}[papermill_#{method}_ids]", nil)}
#{options[:form_helper_elements].map{|element| html[element] || ""}.join("\n")} #{html[:css] + html[:js] + options[:form_helper_elements].map{|element| html[element] || ""}.join("\n")}
</div>} </div>}
end end


Expand All @@ -106,36 +108,44 @@ def compute_papermill_url(action, computed_style, field_name, field_id, options)
end end


def set_papermill_inline_js(field_id, create_url, options) def set_papermill_inline_js(field_id, create_url, options)
return unless options[:inline_css] outputed_js = (options[:gallery] ? %{ jQuery("##{field_id}").sortable() } : "") +
@template.content_for :papermill_inline_js do %{
%{ jQuery("##{field_id}").sortable() } if options[:gallery] new SWFUpload({
end post_params: {
@template.content_for :papermill_inline_js do "#{ ActionController::Base.session_options[:key] }": "#{ @template.cookies[ActionController::Base.session_options[:key]] }"
%{ },
new SWFUpload({ upload_id: "#{ field_id }",
post_params: { upload_url: "#{ @template.escape_javascript create_url }",
"#{ ActionController::Base.session_options[:key] }": "#{ @template.cookies[ActionController::Base.session_options[:key]] }" file_types: "#{ options[:images_only] ? '*.jpg;*.jpeg;*.png;*.gif' : '' }",
}, file_queue_limit: "#{ !options[:gallery] ? '1' : '0' }",
upload_id: "#{ field_id }", file_queued_handler: Papermill.file_queued,
upload_url: "#{ @template.escape_javascript create_url }", file_dialog_complete_handler: Papermill.file_dialog_complete,
file_types: "#{ options[:images_only] ? '*.jpg;*.jpeg;*.png;*.gif' : '' }", upload_start_handler: Papermill.upload_start,
file_queue_limit: "#{ !options[:gallery] ? '1' : '0' }", upload_progress_handler: Papermill.upload_progress,
file_queued_handler: Papermill.file_queued, file_queue_error_handler: Papermill.file_queue_error,
file_dialog_complete_handler: Papermill.file_dialog_complete, upload_error_handler: Papermill.upload_error,
upload_start_handler: Papermill.upload_start, upload_success_handler: Papermill.upload_success,
upload_progress_handler: Papermill.upload_progress, upload_complete_handler: Papermill.upload_complete,
file_queue_error_handler: Papermill.file_queue_error, button_placeholder_id: "browse_for_#{field_id}",
upload_error_handler: Papermill.upload_error, #{ options[:swfupload].map { |key, value| "#{key}: #{value}" if value }.compact.join(",\n") }
upload_success_handler: Papermill.upload_success, });
upload_complete_handler: Papermill.upload_complete, }
button_placeholder_id: "browse_for_#{field_id}", if options[:use_content_for]
#{ options[:swfupload].map { |key, value| "#{key}: #{value}" if value }.compact.join(",\n") } @template.content_for :papermill_inline_js do
outputed_js
end
""
else
%{<script type="text/javascript">
$(function() {
#{outputed_js}
}); });
} </script>}
end end
end end


def set_papermill_inline_css(field_id, width, height, options) def set_papermill_inline_css(field_id, width, height, options)
return unless options[:inline_css]
html = ["\n"] html = ["\n"]
size = [width && "width:#{width}px", height && "height:#{height}px"].compact.join("; ") size = [width && "width:#{width}px", height && "height:#{height}px"].compact.join("; ")
if og = options[:gallery] if og = options[:gallery]
Expand All @@ -148,8 +158,15 @@ def set_papermill_inline_css(field_id, width, height, options)
html << %{##{field_id}, ##{field_id} .asset { #{size} }} html << %{##{field_id}, ##{field_id} .asset { #{size} }}
end end
html << %{##{field_id} .name { width:#{width || "100"}px; }} html << %{##{field_id} .name { width:#{width || "100"}px; }}
@template.content_for :papermill_inline_css do if options[:use_content_for]
html.join("\n") @template.content_for :papermill_inline_css do
html.join("\n")
end
""
else
%{<style type="text/css">
#{html.join("\n")}
</style>}
end end
end end
end end
1 change: 1 addition & 0 deletions lib/papermill/papermill_options.rb
Expand Up @@ -3,6 +3,7 @@ module Papermill
:class_name => "PapermillAsset", :class_name => "PapermillAsset",
:through => false, :through => false,
:inline_css => true, :inline_css => true,
:use_content_for => true,
:images_only => false, :images_only => false,
:form_helper_elements => [:upload_button, :container, :browser, :mass_edit], :form_helper_elements => [:upload_button, :container, :browser, :mass_edit],
:mass_edit => true, :mass_edit => true,
Expand Down

0 comments on commit f5874f3

Please sign in to comment.