From a64ce9f2e9c13b8f644bd75800ee6b1cd9942066 Mon Sep 17 00:00:00 2001 From: mr Date: Mon, 31 Jan 2011 11:45:53 +0100 Subject: edit model name --- application.rb | 32 +++++++++++++++++++++++ public/javascripts/toxcreate.js | 57 +++++++++++++++++++++++++++++++++++++++++ views/model.haml | 8 +++--- views/model_name.haml | 12 +++++++++ views/model_name_edit.haml | 14 ++++++++++ views/style.sass | 9 +++++++ 6 files changed, 128 insertions(+), 4 deletions(-) create mode 100644 views/model_name.haml create mode 100644 views/model_name_edit.haml diff --git a/application.rb b/application.rb index 31bab62..2126a48 100644 --- a/application.rb +++ b/application.rb @@ -81,6 +81,38 @@ get '/model/:id/status/?' do end end +get '/model/:id/name/?' do + response['Content-Type'] = 'text/plain' + model = ToxCreateModel.get(params[:id]) + begin + case params[:mode] + when 'edit' + haml :model_name_edit, :locals=>{:model=>model}, :layout => false + when 'show' + haml :model_name, :locals=>{:model=>model}, :layout => false + else + params.inspect + end + rescue + return "unavailable" + end +end + +put '/model/:id/?' do + response['Content-Type'] = 'text/plain' + model = ToxCreateModel.get(params[:id]) + begin + if params[:name] && model.name != params[:name] + model.name = params[:name] + model.save + end + redirect url_for("/model/#{model.id}/name?mode=show") + rescue + return "unavailable" + end +end + + get '/model/:id/:view/?' do response['Content-Type'] = 'text/plain' model = ToxCreateModel.get(params[:id]) diff --git a/public/javascripts/toxcreate.js b/public/javascripts/toxcreate.js index aef0455..493168f 100755 --- a/public/javascripts/toxcreate.js +++ b/public/javascripts/toxcreate.js @@ -95,6 +95,63 @@ $(function() { } }); +jQuery.fn.editModel = function(type, options) { + var defaults = { + method: 'get', + action: this.attr('href'), + trigger_on: 'click' + }; + var opts = $.extend(defaults, options); + this.bind(opts.trigger_on, function() { + $.ajax({ + type: opts.method, + url: opts.action, + dataType: 'html', + data: { + '_method': 'get' + }, + success: function(data) { + $("div#model_" + opts.id + "_name").html(data); + $("input#model_" + opts.id + "_name").focus(); + }, + error: function(data) { + alert("model edit error!"); + } + }); + return false; + }); +}; + +jQuery.fn.saveModel = function(type, options) { + var defaults = { + method: 'put', + action: 'model/' + options.id, + trigger_on: 'click' + }; + var opts = $.extend(defaults, options); + + this.bind(opts.trigger_on, function() { + var name = $("input#model_" + opts.id + "_name").val(); + $.ajax({ + type: opts.method, + url: opts.action, + dataType: 'html', + data: { + '_method': 'put', + 'name': name + }, + success: function(data) { + $("div#model_" + opts.id + "_name").html(data); + }, + error: function(data) { + alert("model save error!"); + } + }); + return false; + }); +}; + + jQuery.fn.deleteModel = function(type, options) { var defaults = { method: 'post', diff --git a/views/model.haml b/views/model.haml index 6374bd8..2125283 100644 --- a/views/model.haml +++ b/views/model.haml @@ -1,13 +1,13 @@ - uri = url_for("/model/#{model.id}", :full) -- js = "$('#delete_#{model.id}').deleteModel('DELETE', {id: '#{model.id}'});\n " + "$('#show_model_#{model.id}_warnings').toggleWarnings('#{model.id}');" +- js = "$('#delete_#{model.id}').deleteModel('DELETE', {id: '#{model.id}'});\n " + "$('#show_model_#{model.id}_warnings').toggleWarnings('#{model.id}');\n" :javascript $(function() { #{js} }); -%div{:id => "model_#{model.id}"} - %h2 - = model.name +%div{:id => "model_#{model.id}"} + %div{:id => "model_#{model.id}_name"} + = haml :model_name, :locals=>{:model=>model}, :layout => false .model %dl %dt Status: diff --git a/views/model_name.haml b/views/model_name.haml new file mode 100644 index 0000000..b1f6f10 --- /dev/null +++ b/views/model_name.haml @@ -0,0 +1,12 @@ +- js = "$('#edit_#{model.id}').editModel('PUT', {id: '#{model.id}', mode: 'edit'});\n " +:javascript + $(function() { + #{js} + }); +%h2 + = model.name + - if is_authorized(model.web_uri, "PUT") + %span{:class => "edit_button"} + ( + %a{:href => url_for("/model/#{model.id}/name?mode=edit"), :id => "edit_#{model.id}"} edit + ) \ No newline at end of file diff --git a/views/model_name_edit.haml b/views/model_name_edit.haml new file mode 100644 index 0000000..aec59fc --- /dev/null +++ b/views/model_name_edit.haml @@ -0,0 +1,14 @@ +- js = "$('#cancel_#{model.id}').editModel('GET', {id: '#{model.id}', mode: 'show'});\n " +- js = "$('#save_#{model.id}').saveModel('POST', {id: '#{model.id}', mode: 'show'});\n " +:javascript + $(function() { + #{js} + }); +%form{:name => "form", :action => url_for("/model/#{model.id}"), :method => "post", :enctype => "multipart/form-data" } + %input{:type => 'text', :name => 'name', :id => "model_#{model.id}_name", :class => 'input_model_name', :size => '40', :value => model.name} + %input{:type => 'hidden', :name => 'id', :id => 'id', :value => model.id} + %input{:type => 'hidden', :name => 'subjectid', :id => 'subjectid', :value => session[:subjectid]} + %input{ :type => "submit", :value => "Save", :id => "save_#{model.id}", :class => "edit_button"} + %span{:class => "edit_button"} + %a{:href => url_for("/model/#{model.id}/name?mode=show"), :id => "cancel_#{model.id}"} Cancel + diff --git a/views/style.sass b/views/style.sass index 5f362a3..1fbd459 100644 --- a/views/style.sass +++ b/views/style.sass @@ -237,3 +237,12 @@ dl padding: 0.3em table border-collapse: collapse +.input_model_name + margin: 20px 3px 2px 3px + font-size: 1.2em + font-weight: bold + border: 0 + background-color: #FEFEFF + +.edit_button + font-size: 0.5em \ No newline at end of file -- cgit v1.2.3