Merge remote-tracking branch 'origin/rails_3_1' into rails_3_1

This commit is contained in:
Mario Visic 2011-12-05 22:31:14 +08:00
commit aa68d59979
12 changed files with 143 additions and 42 deletions

View File

@ -0,0 +1,7 @@
class Locomotive.Models.Snippet extends Backbone.Model
paramRoot: 'snippet'
urlRoot: "#{Locomotive.mount_on}/snippets"
class Locomotive.Models.SnippetsCollection extends Backbone.Collection

View File

@ -0,0 +1,62 @@
#= require ../shared/form_view
Locomotive.Views.Snippets ||= {}
class Locomotive.Views.Snippets.FormView extends Locomotive.Views.Shared.FormView
el: '#content'
events:
'click a#image-picker-link': 'open_image_picker'
'submit': 'save'
initialize: ->
_.bindAll(@, 'insert_image')
@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
Backbone.ModelBinding.bind @
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
@image_picker_view.render()
insert_image: (path) ->
text = "{{ '#{path}' | theme_image_url }}"
@editor.replaceSelection(text)
@image_picker_view.close()
enable_liquid_editing: ->
input = @$('#snippet_template')
@editor = CodeMirror.fromTextArea input.get()[0],
mode: 'liquid'
autoMatchParens: false
lineNumbers: false
passDelay: 50
tabMode: 'shift'
theme: 'default'
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

@ -0,0 +1,8 @@
Locomotive.Views.Snippets ||= {}
class Locomotive.Views.Snippets.NewView extends Locomotive.Views.Snippets.FormView
save: (event) ->
@save_in_ajax event,
on_success: (response, xhr) ->
window.location.href = xhr.getResponseHeader('location')

View File

@ -3,7 +3,7 @@ module Locomotive
sections 'settings', 'theme_assets'
respond_to :json, :only => :update
respond_to :json, :only => [:create, :update]
def new
@snippet = current_site.snippets.new
@ -12,7 +12,7 @@ module Locomotive
def create
@snippet = current_site.snippets.create(params[:snippet])
respond_with @snippet, :location => edit_snippet_url(@snippet)
respond_with @snippet, :location => edit_snippet_url(@snippet._id)
end
def edit
@ -22,8 +22,8 @@ module Locomotive
def update
@snippet = current_site.snippets.find(params[:id])
@snippet.update_attributes(params[:id])
respond_with @snippet, :location => edit_snippet_url(@snippet)
@snippet.update_attributes(params[:snippet])
respond_with @snippet, :location => edit_snippet_url(@snippet._id)
end
def destroy

View File

@ -22,7 +22,7 @@ module Locomotive
return '' if options.delete(:picker) == false
template.content_tag(:div,
template.link_to(template.t('locomotive.image_picker.link'), template.theme_assets_path, :id => 'image-picker-link', :class => 'picture'),
:class => 'more')
:class => 'more error-anchor')
end
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

@ -9,37 +9,4 @@
= f.input :subdomain, :as => :'Locomotive::Subdomain', :domain => application_domain, :input_html => { :readonly => !manage_subdomain? }
- if manage_domains?
= f.input :domains, :as => :'Locomotive::Empty'
/ - if manage_subdomain_or_domains?
/ = f.foldable_inputs :name => :access_points, :class => 'editable-list off' do
/
/ = f.custom_input :subdomain, :css => 'path' do
/ %em
/ http://
/ = f.text_field :subdomain
/ \.
/ %em
/ = application_domain
/
/ - if manage_domains?
/ - @site.domains_without_subdomain.each_with_index do |name, index|
/ %li{ :class => "item added #{'last' if index == @site.domains.size - 1}" }
/ %em
/ http://
/ = text_field_tag 'site[domains][]', name, :class => 'string label void domain'
/ &nbsp;
/ = error_on_domain(@site, name)
/ %span.actions
/ = link_to image_tag('admin/form/icons/trash.png'), '#', :class => 'remove first', :confirm => t('locomotive.messages.confirm')
/
/ %li.item.template
/ %em
/ http://
/ = text_field_tag 'label', t('formtastic.hints.site.domain_name'), :class => 'string label void domain'
/ &nbsp;
/ %span.actions
/ = link_to image_tag('admin/form/icons/trash.png'), '#', :class => 'remove first', :confirm => t('locomotive.messages.confirm')
/ %button{ :class => 'button light add', :type => 'button' }
/ %span!= t('locomotive.buttons.new_item')
= f.input :domains, :as => :'Locomotive::Empty'

View File

@ -1,6 +1,9 @@
- content_for :head do
/ = include_javascripts :image_picker, :snippets
/ = include_stylesheets :fancybox
= render '/locomotive/theme_assets/picker'
- content_for :backbone_view_data do
:plain
{ snippet: #{@snippet.persisted? ? @snippet.to_json : 'null'} }
= f.inputs :name => :information do
= f.input :name, :wrapper_html => { :class => 'highlighted' }

View File

@ -23,8 +23,9 @@ x edit my site
- fix other sections
x edit my account
x create a new site
x create a new account
x create a new accounts
- theme assets
x snippets
- polish the page
- upload many files at once
- delete in ajax

28
vendor/assets/javascripts/slugify.js vendored Normal file
View File

@ -0,0 +1,28 @@
/**
* 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);
};