Permalink
Browse files

add preview page feature

  • Loading branch information...
1 parent 65ea969 commit fde3036753d6f3f8b8c4acb45189808e9c62cfc4 @saturnflyer saturnflyer committed Mar 29, 2011
@@ -22,6 +22,12 @@ def new
assign_page_attributes
response_for :new
end
+
+ def preview
+ render_preview
+ rescue => exception
+ render :text => exception.message unless @performed_render
+ end
private
def assign_page_attributes
@@ -40,6 +46,25 @@ def model_class
Page
end
end
+
+ def render_preview
+ Page.transaction do
+ PagePart.transaction do
+ page_class = Page.descendants.include?(model_class) ? model_class : Page
+ if request.referer =~ %r{/admin/pages/(\d+)/edit}
+ page = Page.find($1).becomes(page_class)
+ page.update_attributes(params[:page])
+ else
+ page = page_class.new(params[:page])
+ page.published_at = page.updated_at = page.created_at = Time.now
+ page.parent = Page.find($1) if request.referer =~ %r{/admin/pages/(\d+)/children/new}
+ end
+ page.process(request,response)
+ @performed_render = true
+ raise 'Changes not saved!'
+ end
+ end
+ end
def count_deleted_pages
@count = model.children.count + 1
@@ -1,3 +1,4 @@
+- include_javascript 'admin/page_preview'
= fields.hidden_field :lock_version
= fields.hidden_field :parent_id
= fields.hidden_field :class_name
@@ -55,7 +56,16 @@
%p.buttons{:style=>"clear: left"}
= save_model_button(@page)
= save_model_and_continue_editing_button(@page)
+ = submit_tag(t('preview', :default => 'Preview'), :class => 'button', :id => 'show-preview')
= t('or')
= link_to t('cancel'), admin_pages_url
+ #preview_panel.fullcover.grey_out{:style => 'display: none;'}
+ %iframe{:id => 'page-preview', :class => 'fullcover', :name => 'page-preview', :src => '/loading-iframe.html', :frameborder => 0, :scrolling => "auto"}
+ .preview_tools
+ =submit_tag(t('buttons.save_changes'), :class => 'big save_changes')
+ =submit_tag(t('buttons.save_and_continue'), :class => 'big save_and_continue', :name => 'continue')
+ %span.info
+ = t('or')
+ =link_to(t('edit_page'),{}, :class => 'cancel')
- form_bottom.edit_timestamp do
= updated_stamp @page
View
@@ -143,6 +143,7 @@ en:
preferences_controller:
error_updating: 'There was an error updating your preferences.'
updated: 'Your preferences have been updated.'
+ preview: 'Preview'
published: 'Published'
published_at: 'Published at'
published_on: 'Published On'
View
@@ -9,6 +9,7 @@
admin.resources :snippets
admin.resources :users
end
+ map.preview 'admin/preview', :controller => 'admin/pages', :action => 'preview', :conditions => {:method => [:post, :put]}
map.namespace :admin do |admin|
admin.resource :preferences
@@ -1,4 +1,23 @@
document.observe('dom:loaded', function() {
+
+ var previewer = $('preview_panel')
+ var preview_tools = previewer.down('.preview_tools')
+ var frame = $('page-preview')
+ var body = document.body
+
+ Event.addBehavior({
+ "div.preview_tools a.cancel:click" : function(event) {
+ previewer.hide()
+ body.removeClassName('clipped')
+ frame.src = ''
+ event.stop()
+ },
+ "iframe:load" : function(event) {
+ preview_tools.style['opacity'] = null
+ }
+ });
+
+
$('show-preview').observe('click', function(e) {
e.stop();
@@ -7,17 +26,10 @@ document.observe('dom:loaded', function() {
oldAction = form.action
try {
- var previewer = $('preview_panel').show()
- var frame = $('page-preview')
- $$('div.preview_tools a.cancel, div.preview_tools input').each(function(item, index){
- item.observe('click', function(e){
- if(item.hasClassName('cancel')) {
- previewer.hide()
- frame.src = ''
- e.stop();
- }
- })
- })
+ $(body).scrollTo()
+ previewer.show()
+ preview_tools.style['opacity'] = 1
+ body.addClassName('clipped')
form.target = frame.id
form.action = '/admin/preview'
form.submit()
View
@@ -0,0 +1,11 @@
+<html>
+ <head>
+ <meta http-Equiv="Cache-Control" Content="no-cache">
+ <meta http-Equiv="Pragma" Content="no-cache">
+ <meta http-Equiv="Expires" Content="0">
+ <title>Loading...</title>
+ </head>
+ <body>
+ <p>Loading Preview...</p>
+ </body>
+</html>

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -5,6 +5,7 @@
// Modules
@import compass
+@import modules/boxes
@import modules/links
+global-reset
@@ -0,0 +1,6 @@
+=alt-box
+ background: #d8d8d8
+ +linear-gradient(color-stops(#fff, #ccc))
+ +border-radius(8px)
+ +box-shadow(rgba(0,0,0,0.15), 2px, 2px, 3px)
+ padding: 12px 10px
@@ -24,4 +24,29 @@ body.reversed
font-size: 125%
margin: -20px -20px 20px
padding: 15px 20px
- text-shadow: 0px 0px 20px rgba(255, 255, 255, 0.1)
+ text-shadow: 0px 0px 20px rgba(255, 255, 255, 0.1)
+
+.fullcover
+ position: absolute
+ width: 100%
+ height: 100%
+ top: 0
+ right: 0
+ bottom: 0
+ left: 0
+.clipped
+ overflow: hidden
+.grey_out
+ background: #666
+ background: rgba(0, 0, 0, 0.7)
+.preview_tools
+ color: #000
+ position: fixed
+ top: 10px
+ right: 20px
+ +opacity(0.7)
+ +alt-box
+ &:hover
+ +opaque
+ a
+ +alt-link
@@ -230,11 +230,7 @@ body.reversed
color: #d0d0d0
text-shadow: none
.buttons
- background: #d8d8d8
- +linear-gradient(color-stops(#fff, #ccc))
- +border-radius(8px)
- +box-shadow(rgba(0,0,0,0.15), 2px, 2px, 3px)
- padding: 12px 10px
+ +alt-box
fieldset
background: #858585
padding: 20px 15px
@@ -42,7 +42,7 @@ input[type=image]
border: none
vertical-align: middle
-input.big
+.big
font-size: 120%
#change_password span, span.info, span.error
@@ -191,6 +191,39 @@
end
end
+ describe '#preview' do
+
+ let(:preview_page){ pages(:home) }
+ let(:body_id){ preview_page.part('body').id }
+ let(:preview_params){
+ {'page' => {
+ 'title' => 'BOGUS',
+ 'id' => preview_page.id.to_s,
+ 'parts_attributes' => [{'content' => 'TEST', 'id' => body_id.to_s}] } }
+ }
+ it 'should render the page with changes' do
+ request.stub!(:referer).and_return("/admin/pages/#{preview_page.id}/edit")
+ post :preview, preview_params
+ response.body.should eql('TEST')
+ end
+
+ describe 'new child' do
+ it 'should not save any changes' do
+ page_count = Page.count
+ request.stub!(:referer).and_return("/admin/pages/#{preview_page.id}/edit")
+ post :preview, preview_params
+ Page.count.should == page_count
+ end
+ end
+ describe 'edit existing page' do
+ it 'should not save any changes' do
+ request.stub!(:referer).and_return("/admin/pages/#{preview_page.id}/edit")
+ original_date = preview_page.updated_at
+ post :preview, preview_params
+ preview_page.reload.updated_at.should == original_date
+ end
+ end
+ end
describe "prompting page removal" do
integrate_views

3 comments on commit fde3036

Owner

avonderluft replied Mar 29, 2011

it appears that none of the rake radiant:update tasks move the loading-iframe.html page to RAILS_ROOT/public.

Owner

saturnflyer replied Mar 29, 2011

Thanks. i'll take a look at it.

Owner

avonderluft replied Mar 29, 2011

thanks. I just commented on the radiant issue on this, and another concern as well: https://github.com/radiant/radiant/issues/#issue/35/comment/932496

Please sign in to comment.