snippets done

This commit is contained in:
did 2011-12-05 04:29:58 -08:00
parent 04d9e90492
commit 8af3445045
8 changed files with 61 additions and 58 deletions

View File

@ -4,6 +4,4 @@ class Locomotive.Models.Snippet extends Backbone.Model
urlRoot: "#{Locomotive.mount_on}/snippets" urlRoot: "#{Locomotive.mount_on}/snippets"
initialize: ->
class Locomotive.Models.SnippetsCollection extends Backbone.Collection class Locomotive.Models.SnippetsCollection extends Backbone.Collection

View File

@ -15,6 +15,8 @@ class Locomotive.Views.Snippets.FormView extends Locomotive.Views.Shared.FormVie
@model = new Locomotive.Models.Snippet(@options.snippet) @model = new Locomotive.Models.Snippet(@options.snippet)
window.foo = @model
@image_picker_view = new Locomotive.Views.ThemeAssets.ImagePickerView @image_picker_view = new Locomotive.Views.ThemeAssets.ImagePickerView
collection: new Locomotive.Models.ThemeAssetsCollection() collection: new Locomotive.Models.ThemeAssetsCollection()
on_select: @insert_image on_select: @insert_image
@ -24,11 +26,17 @@ class Locomotive.Views.Snippets.FormView extends Locomotive.Views.Shared.FormVie
render: -> render: ->
super() super()
# slugify the slug field from name
@slugify_name()
# liquid code textarea # liquid code textarea
@enable_liquid_editing() @enable_liquid_editing()
return @ return @
slugify_name: ->
@$('#snippet_name').slugify(target: @$('#snippet_slug'))
open_image_picker: (event) -> open_image_picker: (event) ->
event.stopPropagation() & event.preventDefault() event.stopPropagation() & event.preventDefault()
@image_picker_view.editor = @editor @image_picker_view.editor = @editor
@ -48,7 +56,7 @@ class Locomotive.Views.Snippets.FormView extends Locomotive.Views.Shared.FormVie
passDelay: 50 passDelay: 50
tabMode: 'shift' tabMode: 'shift'
theme: 'default' theme: 'default'
onChange: (editor) => @model.set(raw_template: editor.getValue()) onChange: (editor) => @model.set(template: editor.getValue())
after_inputs_fold: -> after_inputs_fold: ->
@editor.refresh() @editor.refresh()

View File

@ -0,0 +1,6 @@
Locomotive.Views.Snippets ||= {}
class Locomotive.Views.Snippets.EditView extends Locomotive.Views.Snippets.FormView
save: (event) ->
@save_in_ajax event

View File

@ -12,7 +12,6 @@ module Locomotive
def create def create
@snippet = current_site.snippets.create(params[:snippet]) @snippet = current_site.snippets.create(params[:snippet])
Rails.logger.debug "[SNIPPET] creating..."
respond_with @snippet, :location => edit_snippet_url(@snippet._id) respond_with @snippet, :location => edit_snippet_url(@snippet._id)
end end
@ -23,7 +22,7 @@ module Locomotive
def update def update
@snippet = current_site.snippets.find(params[:id]) @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) respond_with @snippet, :location => edit_snippet_url(@snippet._id)
end end

View File

@ -20,8 +20,16 @@ module Locomotive
validates_presence_of :site, :name, :slug, :template validates_presence_of :site, :name, :slug, :template
validates_uniqueness_of :slug, :scope => :site_id validates_uniqueness_of :slug, :scope => :site_id
## behaviours ##
attr_protected :id
attr_accessible :name, :slug, :template
## methods ## ## methods ##
def as_json(options = {})
Locomotive::SnippetPresenter.new(self).as_json
end
protected protected
def normalize_slug def normalize_slug

View File

@ -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

View File

@ -25,7 +25,7 @@ x edit my site
x create a new site x create a new site
x create a new accounts x create a new accounts
- theme assets - theme assets
- snippets x snippets
- polish the page - polish the page
- upload many files at once - upload many files at once
- delete in ajax - delete in ajax

View File

@ -1,55 +1,28 @@
(function($) { /**
$.fn.slug = function(o) { * Version 0.0.1
var d = { * Fill in an input field from another one (source)
slug: 'slug', // Class used for slug destination input and span. The span must exists on the page * and apply a filter on the string (slugify)
hide: false, // Boolean - By default the slug input field is hidden, set to false to show the input field and hide the span. * Didier Lafforgue
customizable: true, // Boolean - customizable */
separate: '-', // Character - defult value set to '-' $.fn.slugify = function(settings) {
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);
});
}
function inputSlug(show) { settings = $.extend({
$r.text($w.val()); sep: '-'
if (show) { }, settings);
$r.show();
$w.hide();
}
else {
$r.hide();
$w.show().focus();
}
}
makeSlug = function() { var target = $(settings.target);
s = s.replace(/\s/g, o.separate); target.data('touched', (target.val() != ''));
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);
}
$t.keyup(makeSlug); var makeSlug = function(event) {
return $t; var source = $(this);
}); var newVal = source.val().slugify(settings.sep);
};
})(jQuery); if (!target.data('touched')) target.val(newVal);
}
target.bind('keyup', function(event) {
$(this).data('touched', ($(this).val() != ''));
});
return $(this).bind('keyup', makeSlug);
};