Skip to content

Commit

Permalink
added drag and drop.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Husch committed Aug 20, 2010
1 parent 236e345 commit 47f4fe3
Show file tree
Hide file tree
Showing 20 changed files with 562 additions and 182 deletions.
32 changes: 0 additions & 32 deletions README

This file was deleted.

72 changes: 72 additions & 0 deletions README.textile
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
h1. Asset Gallery

This extension allows you to create galleries of Paperclipped Assets. Provides an admin interface to add, sort and remove assets from a Gallery with drag and drop, and radius tags to easily include the Galleries in your site.

("sbmsuite":http://github.com/sbmsuite) modified the views from zapnap/radiant-flash-gallery-extension to use in the new extension. However THIS IS NOT A FORK. It is a newly created extension. "jfqd":http://github.com/jfqd modified the extension to support ujs drag and drop and globalize2-paperclipped compatibility.

Tested with radiant 0.9.1

h2. Installation

Install the "Paperclipped Extension":http://github.com/kbingman/paperclipped/ before the Asset Gallery Extension:

<pre>
git clone http://github.com/kbingman/paperclipped.git vendor/extensions/paperclipped

rake production db:migrate:extensions
rake production radiant:extensions:paperclipped:update
</pre>

Now install the Asset Gallery Extension:

<pre>
git clone http://github.com/jfqd/radiant-asset_gallery-extension vendor/extensions/asset_gallery

rake production db:migrate:extensions
rake production radiant:extensions:asset_gallery:update
</pre>

Be careful about the extensions load order:

config.extensions = [ :paperclipped, :all ]

h2. Tag Examples

h3. List of galleries

<pre><code>
<r:gallery:each>
<div>
<div><r:gallery:first_image size="thumbnail" />
<div><r:gallery:name /></div>
</div>
</r:gallery:each>
</code></pre>

h3. Images of a gallery with caption

<pre><code>
<r:gallery:items name="my-gallery">
<div class="slide">
<r:gallery_item:image size="normal" />
<p><r:gallery_item:caption /></p>
</div>
</r:gallery:items>
</code></pre>

h2. Compatibility

This extension is compatible with the "Globalize2-Paperclipped Extension":http://github.com/Aissac/radiant-globalize2-paperclipped-extension. You need to install the Globalize2-Paperclipped Extension before the Asset Gallery Extension.

<pre>
git clone http://github.com/Aissac/radiant-globalize2-paperclipped-extension.git vendor/extensions/globalize2_paperclipped
</pre>

Be careful about the extensions load order:

config.extensions = [ :paperclipped, :globalize2, :globalize2_paperclipped, :all ]

h2. Contributors

"sbmsuite":http://github.com/sbmsuite
"jfqd":http://github.com/jfqd
46 changes: 25 additions & 21 deletions app/controllers/admin/galleries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,25 @@ def index

def new
@gallery = Gallery.new
render(:action => 'edit')
end

def edit
@gallery = Gallery.find(params[:id])
render(:action => 'edit')
if @gallery
@items = @gallery.gallery_items
@assets = Asset.quick_search("",@items)
render(:action => 'edit')
else
redirect_to(admin_gallery_path)
end
end

def create
params[:gallery][:user_id] = current_user.id
@gallery = Gallery.new(params[:gallery])
@gallery = Gallery.new(params[:gallery])
if @gallery.save
checked = params[:checked]
checked.each do |id, selected|
if selected == 1.to_s
@gallery.gallery_items.create(:asset_id => id)
end
end
flash[:notice] = "Successfully added a new gallery."
redirect_to(admin_galleries_path)
redirect_to(edit_admin_gallery_path(@gallery.id))
else
flash[:error] = "Validation errors occurred while processing this form. Please take a moment to review the form and correct any input errors before continuing."
render(:action => 'edit')
Expand All @@ -35,17 +34,6 @@ def create
def update
@gallery = Gallery.find(params[:id])
if @gallery.update_attributes(params[:gallery])
@gallery.gallery_items.clear
checked = params[:checked]
checked.each do |id, selected|
if selected == 1.to_s
if @gallery.gallery_items.exists?(:asset_id => id)
next
else
@gallery.gallery_items.create(:asset_id => id)
end
end
end
flash[:notice] = "Successfully updated the gallery details."
redirect_to(admin_galleries_path)
else
Expand All @@ -62,4 +50,20 @@ def destroy
redirect_to(admin_galleries_path)
end

# search assets
def search
gallery = Gallery.find(params[:id])
items = gallery.gallery_items # remove items from assets view!
@assets = Asset.quick_search(params[:search], items)
respond_to do |format|
format.html { render :layout => false }
format.js {
render :update do |page|
page.replace_html "asset_items", :partial => 'admin/galleries/assets.html.haml', :locals => { :assets => @assets }
page << 'Asset.MakeDraggables();'
end
}
end
end

end
52 changes: 52 additions & 0 deletions app/controllers/admin/gallery_items_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class Admin::GalleryItemsController < ApplicationController

# add an item to the gallery
def create
asset_id = (params[:gallery_item][:asset_id]).gsub("gallery_item_", "")
gallery = Gallery.find(params[:gallery_id])
unless gallery.gallery_items.exists?(:asset_id => asset_id)
gallery.gallery_items.create(:asset_id => asset_id)
end
respond_to do |format|
format.html { render :nothing => true }
format.js {
render :update, :status => 200 do |page|
page << 'Asset.SortItems();'
end
}
end
end

# remove an item from the gallery
def destroy
asset_id = params[:id].gsub("gallery_item_", "")
item = GalleryItem.find_by_gallery_id_and_asset_id(params[:gallery_id], asset_id)
item.destroy
respond_to do |format|
format.html { render :nothing => true }
format.js {
gallery = Gallery.find(params[:gallery_id])
items = gallery.gallery_items # remove items from assets view!
@assets = Asset.quick_search('', items)
render :update, :status => 200 do |page|
page.replace_html "asset_items", :partial => 'admin/galleries/assets.html.haml', :locals => { :assets => @assets }
page << 'Asset.MakeDraggables();'
end
}
end
end

# sort gallery items
def sort
gallery = Gallery.find(params[:gallery_id])
items = CGI::parse(params[:items])['gallery_items[]']
items.each_with_index do |id, index|
gallery.gallery_items.update_all(['position=?', index+1], ['asset_id=?', id])
end
respond_to do |format|
format.html { render :nothing => true }
format.js { render :nothing => true, :status => 200 }
end
end

end
2 changes: 1 addition & 1 deletion app/models/gallery.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Gallery < ActiveRecord::Base
belongs_to :user
has_many :gallery_items, :dependent => :destroy
has_many :gallery_items, :order => :position, :dependent => :destroy

validates_uniqueness_of :name
end
5 changes: 5 additions & 0 deletions app/views/admin/galleries/_assets.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- assets.each do |asset|
%li.asset-list-item{:id => "gallery_item_#{asset.id}"}
= image_tag(asset.thumbnail(:thumbnail), :alt => asset.asset_file_name, :title => asset.title)
%span= h asset.title[0..75]
%span.delete
5 changes: 5 additions & 0 deletions app/views/admin/galleries/_items.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- items.each do |item|
%li.asset-list-item{:id => "gallery_item_#{item.asset.id}"}
= image_tag(item.asset.thumbnail(:thumbnail), :alt => item.asset.asset_file_name, :title => item.asset.title)
%span= h item.asset.title[0..75]
%span.delete
106 changes: 42 additions & 64 deletions app/views/admin/galleries/edit.html.haml
Original file line number Diff line number Diff line change
@@ -1,71 +1,49 @@
- content_for 'page_css' do
:sass
p.title
margin: 10px 0 !important
- @stylesheets << 'admin/gallery'
- include_javascript 'admin/dragdrop'
- include_javascript 'admin/gallery'

#content .form-area div.error-with-field
input.textbox
font-family: Georgia,Palatino,"Times New Roman",Times,serif
font-size: 200%
width: 100%

textarea.textarea
height: 150px !important
width: 100%
display: block

input.file
margin: 50px 0

img.asset
margin: 5px 0
border: 2px solid #fff

a.asset
display: block
margin: 5px 0

div.asset-list-container
overflow: auto
width: 100%
height: 300px
border: 2px solid #cccccc

div.asset-list-item-container
display: inline-block

div.asset-list-item
border: 1px solid #cccccc
text-align: center
padding: 5px
margin: 5px

%h1= @gallery.new_record? ? 'New Gallery' : 'Edit Gallery'
%h1= 'Edit Gallery'

- form_for [:admin, @gallery], :html => {:multipart => true} do |f|
.form-area
%p.title
%label Title
= f.text_field :name, :class => 'textbox'
%p.title
%label Description
= f.text_area :caption, :class => 'textarea'
%p.title
%label Select Assets For Gallery
.asset-list-container
- Asset.all.each do |asset|
.asset-list-item-container
.asset-list-item
= image_tag(asset.thumbnail(:thumbnail))
%h5= asset.title
- if @gallery.gallery_items.exists?(:asset_id => asset.id)
= check_box "checked", asset.id, {:checked => true}
- else
= check_box "checked", asset.id


%p.buttons
#gallery_panel
.form-area

%p.title
%label Title
= f.text_field :name, :class => 'textbox'

.drawer
.drawer_contents#attributes
%table.fieldset
%tr
%th.label
%label Description
%td.field= f.text_area :caption, :class => 'textarea'
.drawer_handle
%a.toggle{:href=>'#attributes', :rel=>"toggle[attributes]", :class=>"#{(meta_errors? ? 'less' : 'more')}"}= meta_label

%p.title
%label Selected Assets <small>(Drag around for ordering)</small>
%ul.asset-list-container#gallery_items= render :partial => 'items', :locals => { :items => @items }

#assets_panel
%p.title#asset-selection
%label Assets Overview
%span.asset_filters
- form_tag(admin_assets_path,:id => 'filesearchform', :method => 'get') do
%input{ :type => "search", :id => "search", :name => "search" , :value => params[:search] }
= observe_field 'search', :frequency => 1, :url => { :controller => 'galleries', :action=> 'search' }, :method => 'get', :with => "'search=' + escape(value) + '&id=' + #{@gallery.id}"
%ul.asset-list-container#asset_items= render :partial => 'assets', :locals => { :assets => @assets }

%p.note Drag your images to the Selected Assets area

%p.buttons#submit_buttons
- button_text = @gallery.new_record? ? 'Create' : 'Save Changes'
= submit_tag button_text, :class => 'button'
or
= link_to 'Cancel', admin_galleries_url

#routes{:style => 'display:none'}
= hidden_field_tag 'admin_gallery_items_path', admin_gallery_items_path(@gallery)
= hidden_field_tag 'admin_gallery_items_sort_path', admin_gallery_items_sort_path(@gallery)

Loading

0 comments on commit 47f4fe3

Please sign in to comment.