some revert stuff

This commit is contained in:
John Bintz 2012-11-07 18:46:54 -05:00
parent a604a76e02
commit 3ac7775d99
5 changed files with 36 additions and 20 deletions

View File

@ -0,0 +1,4 @@
en:
sisyphus:
autosaved_content: You are viewing autosaved content.
revert: Revert to original content

View File

@ -7,8 +7,25 @@ module Sisyphus
mattr_accessor :process
mattr_accessor :app_root
mattr_accessor :config
mattr_accessor :form_builder
def self.setup
yield self
end
end
class FormBuilder
def initialize(template)
@template = template
end
def status
@template.content_tag(:div, message.html_safe, :class => 'sisyphus_status', :style => 'display: none')
end
def message
@template.t('sisyphus.autosaved_content') << @template.content_tag(:button, @template.t('sisyphus.revert'))
end
end
Sisyphus.form_builder = Sisyphus::FormBuilder
end

View File

@ -30,25 +30,9 @@ module ActionView
alias_method_chain :form_for, :sisyphus
def sisyphus_status
buf = ActiveSupport::SafeBuffer.new
buf.safe_concat <<-JS
<div class="sisyphus_status" style="display:none">#{t('sisyphus.autosaved_content')}</div>
<script type="text/javascript">
(function() {
var original = $.sisyphus().options.onRestore;
$.sisyphus().options.onRestore = function() {
original();
$('.sisyphus_status').show();
};
})(this);
</script>
JS
buf
Sisyphus.form_builder.new(self).status
end
end
end
end

View File

@ -6,7 +6,7 @@ module ActionView
buf = ActiveSupport::SafeBuffer.new
if options.has_key?(:id) && Sisyphus::process
buf.safe_concat("<script type=\"text/javascript\">$(document).ready(function() {$('##{options[:id]}').sisyphus({excludeFields: $('input[name=utf8], input[name=_method], input[name=authenticity_token]')});});</script>")
buf.safe_concat("<script type=\"text/javascript\">$(document).ready(function() {$('##{options[:id]}').sisyphus({onRestore: $.sisyphus.railsRestore, excludeFields: $('input[name=utf8], input[name=_method], input[name=authenticity_token]')});});</script>")
end
buf << form_tag_without_sisyphus(url_for_options, options, &block)

View File

@ -0,0 +1,11 @@
//= require sisyphus
//= require_self
$.sisyphus.railsRestore = function() {
$('.sisyphus_status').show().find('button').unbind().click(function() {
$.sisyphus().manuallyReleaseData();
window.location.href = window.location.href;
return false;
});
};