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"
initialize: ->
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)
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()

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

View File

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

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 accounts
- theme assets
- snippets
x snippets
- polish the page
- upload many files at once
- delete in ajax

View File

@ -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
/**
* 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) {
settings = $.extend({
sep: '-'
}, settings);
var target = $(settings.target);
target.data('touched', (target.val() != ''));
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);
};
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) {
$r.text($w.val());
if (show) {
$r.show();
$w.hide();
}
else {
$r.hide();
$w.show().focus();
}
}
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);
}
$t.keyup(makeSlug);
return $t;
});
};
})(jQuery);