Permalink
Browse files

add support for ajax saving for assets and components

  • Loading branch information...
1 parent 0cec054 commit 828707cfadd27ab90f1a7244b37af42c694485e4 @akaspick akaspick committed Sep 27, 2011
@@ -30,10 +30,21 @@ def update
@asset.assign_ordered_attributes params[:cms_asset]
if @asset.save
- flash[:notice] = t('assets.flash.updated')
- redirect_to cms_root_path
+ respond_to do |format|
+ format.html {
+ redirect_to edit_cms_asset_path(@asset), :notice => t('assets.flash.updated')
+ }
+ format.js {
+ flash.now[:notice] = t('assets.flash.updated')
+ }
+ end
else
- render :action => 'edit'
+ respond_to do |format|
+ format.html {
+ render :action => 'edit'
+ }
+ format.js
+ end
end
end
@@ -42,7 +53,7 @@ def destroy
@asset.destroy
flash[:notice] = t('assets.flash.deleted')
-
+
respond_to do |format|
format.html { redirect_to cms_root_path }
end
@@ -13,15 +13,23 @@ def edit
end
def update
+ edit_url = {:controller => 'cms/components', :action => 'edit', :url => @path}
+
if Cms::Component.editable?(@path)
@component = Cms::Component.new(@context, @path)
@component.write params[:file_content]
- flash[:notice] = "The component file has been updated."
- redirect_to cms_root_path
+ respond_to do |format|
+ format.html {
+ redirect_to edit_url, :notice => t('components.flash.updated')
+ }
+ format.js {
+ flash.now[:notice] = t('components.flash.updated')
+ }
+ end
else
flash[:error] = "Not an editable component."
- redirect_to :controller => 'cms/components', :action => 'edit', :url => @path
+ redirect_to edit_url
end
end
@@ -25,7 +25,15 @@ def cms_row_class
cycle 'dark', 'light'
end
- def codemirror_edit(content_type, form, content_id)
+ def cms_ajax_update_form(page, object, path)
+ if object.errors.empty?
+ page << cms_flash_message
+ else
+ page.replace_html 'content', :file => "cms/#{path}/edit.html.erb"
+ end
+ end
+
+ def codemirror_edit(content_type, form, content_id, use_ajax = true)
mode = nil
case content_type
@@ -46,7 +54,7 @@ def codemirror_edit(content_type, form, content_id)
end
end
- javascript_tag %(initCodemirror('#{mode}', $$('#{form}').first(), $('#{content_id}')))
+ javascript_tag %(initCodemirror('#{mode}', $$('#{form}').first(), $('#{content_id}'), #{use_ajax}))
end
def file_type_icon(file_name)
@@ -26,7 +26,7 @@
<span class="hint"><%= t('simple_form.hints.cms_asset.file_content').html_safe %></span>
</div>
- <%= codemirror_edit Cms::Editable::content_type(@asset.asset_file_name), 'form.simple_form', 'cms_asset_file_content' %>
+ <%= codemirror_edit Cms::Editable::content_type(@asset.asset_file_name), 'form.simple_form', 'cms_asset_file_content', !@asset.new_record? %>
<% end %>
<div class="dimensions">
@@ -0,0 +1 @@
+cms_ajax_update_form page, @asset, 'assets'
@@ -0,0 +1 @@
+page << cms_flash_message
@@ -9,4 +9,4 @@
<%= f.commit_button_or_cancel %>
<% end %>
-<%= codemirror_edit @page.content_type, 'form.simple_form.cms_page', 'cms_page_content' %>
+<%= codemirror_edit @page.content_type, 'form.simple_form.cms_page', 'cms_page_content', !@page.new_record? %>
@@ -1,5 +1 @@
-if @page.errors.empty?
- page << cms_flash_message
-else
- page.replace_html 'content', :file => 'cms/pages/edit.html.erb'
-end
+cms_ajax_update_form page, @page, 'pages'
@@ -39,6 +39,8 @@ en:
index:
title: 'Components'
none: 'There are currently no components.'
+ flash:
+ updated: 'The component file has been updated.'
simple_form:
"yes": 'Yes'
@@ -47,9 +47,14 @@ function codemirrorToggleFullscreenEditing()
}
}
-function codemirrorSave(editor, form) {
+function codemirrorSave(editor, form, use_ajax) {
editor.save();
+ if (!use_ajax) {
+ form.submit();
+ return;
+ }
+
var has_indicator = $('indicator');
if (has_indicator)
@@ -68,14 +73,14 @@ function codemirrorSave(editor, form) {
form.disable();
}
-function initCodemirror(mode, form, textarea) {
+function initCodemirror(mode, form, textarea, use_ajax) {
var codeMirrorOptions = {
mode : mode,
onKeyEvent : function(instance, event){
if (event.type == 'keydown') {
if (event.ctrlKey && (event.which == 83 || event.keyCode == 83)) {
event.stop();
- codemirrorSave(instance, form);
+ codemirrorSave(instance, form, use_ajax);
}
// Hook into F11
//else if (event.keyCode == 122 || event.keyCode == 27) {

0 comments on commit 828707c

Please sign in to comment.