From 8af344504537c4f4e14c39bfdd898d3099f58b2f Mon Sep 17 00:00:00 2001 From: did Date: Mon, 5 Dec 2011 04:29:58 -0800 Subject: [PATCH] snippets done --- .../locomotive/models/snippet.js.coffee | 2 - .../views/snippets/_form_view.js.coffee | 10 ++- .../views/snippets/edit_view.js.coffee | 6 ++ .../locomotive/snippets_controller.rb | 3 +- app/models/locomotive/snippet.rb | 8 ++ .../locomotive/snippet_presenter.rb | 11 +++ doc/TODO | 2 +- vendor/assets/javascripts/slugify.js | 77 ++++++------------- 8 files changed, 61 insertions(+), 58 deletions(-) create mode 100644 app/assets/javascripts/locomotive/views/snippets/edit_view.js.coffee create mode 100644 app/presenters/locomotive/snippet_presenter.rb diff --git a/app/assets/javascripts/locomotive/models/snippet.js.coffee b/app/assets/javascripts/locomotive/models/snippet.js.coffee index 32044315..10a6467a 100644 --- a/app/assets/javascripts/locomotive/models/snippet.js.coffee +++ b/app/assets/javascripts/locomotive/models/snippet.js.coffee @@ -4,6 +4,4 @@ class Locomotive.Models.Snippet extends Backbone.Model urlRoot: "#{Locomotive.mount_on}/snippets" - initialize: -> - class Locomotive.Models.SnippetsCollection extends Backbone.Collection \ No newline at end of file diff --git a/app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee b/app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee index 0b40bc2e..f2cd2d5b 100644 --- a/app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee +++ b/app/assets/javascripts/locomotive/views/snippets/_form_view.js.coffee @@ -15,6 +15,8 @@ class Locomotive.Views.Snippets.FormView extends Locomotive.Views.Shared.FormVie @model = new Locomotive.Models.Snippet(@options.snippet) + window.foo = @model + @image_picker_view = new Locomotive.Views.ThemeAssets.ImagePickerView collection: new Locomotive.Models.ThemeAssetsCollection() on_select: @insert_image @@ -24,11 +26,17 @@ class Locomotive.Views.Snippets.FormView extends Locomotive.Views.Shared.FormVie render: -> super() + # slugify the slug field from name + @slugify_name() + # liquid code textarea @enable_liquid_editing() return @ + slugify_name: -> + @$('#snippet_name').slugify(target: @$('#snippet_slug')) + open_image_picker: (event) -> event.stopPropagation() & event.preventDefault() @image_picker_view.editor = @editor @@ -48,7 +56,7 @@ class Locomotive.Views.Snippets.FormView extends Locomotive.Views.Shared.FormVie passDelay: 50 tabMode: 'shift' theme: 'default' - onChange: (editor) => @model.set(raw_template: editor.getValue()) + onChange: (editor) => @model.set(template: editor.getValue()) after_inputs_fold: -> @editor.refresh() \ No newline at end of file diff --git a/app/assets/javascripts/locomotive/views/snippets/edit_view.js.coffee b/app/assets/javascripts/locomotive/views/snippets/edit_view.js.coffee new file mode 100644 index 00000000..01952a19 --- /dev/null +++ b/app/assets/javascripts/locomotive/views/snippets/edit_view.js.coffee @@ -0,0 +1,6 @@ +Locomotive.Views.Snippets ||= {} + +class Locomotive.Views.Snippets.EditView extends Locomotive.Views.Snippets.FormView + + save: (event) -> + @save_in_ajax event \ No newline at end of file diff --git a/app/controllers/locomotive/snippets_controller.rb b/app/controllers/locomotive/snippets_controller.rb index 081f5611..03c83b0b 100644 --- a/app/controllers/locomotive/snippets_controller.rb +++ b/app/controllers/locomotive/snippets_controller.rb @@ -12,7 +12,6 @@ module Locomotive def create @snippet = current_site.snippets.create(params[:snippet]) - Rails.logger.debug "[SNIPPET] creating..." respond_with @snippet, :location => edit_snippet_url(@snippet._id) end @@ -23,7 +22,7 @@ module Locomotive def update @snippet = current_site.snippets.find(params[:id]) - @snippet.update_attributes(params[:id]) + @snippet.update_attributes(params[:snippet]) respond_with @snippet, :location => edit_snippet_url(@snippet._id) end diff --git a/app/models/locomotive/snippet.rb b/app/models/locomotive/snippet.rb index 75003f23..d5835721 100644 --- a/app/models/locomotive/snippet.rb +++ b/app/models/locomotive/snippet.rb @@ -20,8 +20,16 @@ module Locomotive validates_presence_of :site, :name, :slug, :template validates_uniqueness_of :slug, :scope => :site_id + ## behaviours ## + attr_protected :id + attr_accessible :name, :slug, :template + ## methods ## + def as_json(options = {}) + Locomotive::SnippetPresenter.new(self).as_json + end + protected def normalize_slug diff --git a/app/presenters/locomotive/snippet_presenter.rb b/app/presenters/locomotive/snippet_presenter.rb new file mode 100644 index 00000000..16354772 --- /dev/null +++ b/app/presenters/locomotive/snippet_presenter.rb @@ -0,0 +1,11 @@ +module Locomotive + class SnippetPresenter < BasePresenter + + delegate :name, :slug, :template, :to => :source + + def included_methods + super + %w(name slug template) + end + + end +end \ No newline at end of file diff --git a/doc/TODO b/doc/TODO index a5338e6c..c0ba52ff 100644 --- a/doc/TODO +++ b/doc/TODO @@ -25,7 +25,7 @@ x edit my site x create a new site x create a new accounts - theme assets - - snippets + x snippets - polish the page - upload many files at once - delete in ajax diff --git a/vendor/assets/javascripts/slugify.js b/vendor/assets/javascripts/slugify.js index d6f5f6a8..e175bca1 100644 --- a/vendor/assets/javascripts/slugify.js +++ b/vendor/assets/javascripts/slugify.js @@ -1,55 +1,28 @@ -(function($) { - $.fn.slug = function(o) { - var d = { - slug: 'slug', // Class used for slug destination input and span. The span must exists on the page - hide: false, // Boolean - By default the slug input field is hidden, set to false to show the input field and hide the span. - customizable: true, // Boolean - customizable - separate: '-', // Character - defult value set to '-' - write: 'input', // String - the tag name for wrinting personalized slug default is set to input - read: 'span' // String - the tag name for reading the slug default is set to span - }; - var o = $.extend(d, o); - return this.each(function() { - $t = $(this); - $w = $(o.write + "." + o.slug); - $r = $(o.read + "." + o.slug) - $().ready(function() { - if (o.hide) { - inputSlug(true); - } - }); - if (o.customizable) { - $w.live('blur', function() { - inputSlug(true) - }); - $r.live('click', function() { - inputSlug(false); - }); - } +/** + * Version 0.0.1 + * Fill in an input field from another one (source) + * and apply a filter on the string (slugify) + * Didier Lafforgue + */ +$.fn.slugify = function(settings) { - function inputSlug(show) { - $r.text($w.val()); - if (show) { - $r.show(); - $w.hide(); - } - else { - $r.hide(); - $w.show().focus(); - } - } + settings = $.extend({ + sep: '-' + }, settings); - makeSlug = function() { - s = s.replace(/\s/g, o.separate); - s = s.replace(eval("/[^a-z0-9" + o.separate + "]/g"), ''); - s = s.replace(eval("/" + o.separate + "{2,}/g"), o.separate); - s = s.replace(eval("/(^" + o.separate + ")|(" + o.separate + "$)/g"), ''); - $w.val(s); - $r.text(s); - } + var target = $(settings.target); + target.data('touched', (target.val() != '')); - $t.keyup(makeSlug); - return $t; - }); - }; -})(jQuery); \ No newline at end of file + var makeSlug = function(event) { + var source = $(this); + var newVal = source.val().slugify(settings.sep); + + if (!target.data('touched')) target.val(newVal); + } + + target.bind('keyup', function(event) { + $(this).data('touched', ($(this).val() != '')); + }); + + return $(this).bind('keyup', makeSlug); +}; \ No newline at end of file