From 7e432bd1edddf54dc33cc776342b7c34a40e9c76 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Thu, 16 Apr 2015 20:07:54 +0800 Subject: [PATCH] added reordering of images --- app/assets/javascripts/gallery.js | 42 ++++++++++++++++--- app/assets/javascripts/galleryAPI.js.erb | 5 ++- app/assets/stylesheets/gallery.css | 13 ++++++ app/controllers/admin/galleries_controller.rb | 5 ++- app/controllers/admin/images_controller.rb | 14 ++++++- app/controllers/galleries_controller.rb | 6 +-- app/models/album_image.rb | 1 + app/views/admin/galleries/index.html.erb | 1 + app/views/admin/galleries/show.html.erb | 10 +++-- config/routes.rb | 3 +- 10 files changed, 83 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/gallery.js b/app/assets/javascripts/gallery.js index 81ea973..25e12d7 100644 --- a/app/assets/javascripts/gallery.js +++ b/app/assets/javascripts/gallery.js @@ -34,6 +34,10 @@ $(function () { $(function() { var $container = $('.gallery'), $containerData = $container.data(); + $container.data("order-edit","0"); + $container.sortable({ + disabled: true + }); $container.imagesLoaded(function(){ var $albumname = $('.albumname'), $img = $('.rgalbum img'); @@ -146,11 +150,11 @@ $(function() { } if($("#imgholder").length){ - $('.open').pageslide({ - loadComplete: function(instance,elem) { - bindEvent(instance,elem); - } - }) + $('.open').pageslide({ + loadComplete: function(instance,elem) { + bindEvent(instance,elem); + } + }) } @@ -215,6 +219,32 @@ $(function() { $('.rgalbum').removeClass('active'); $('.deletephoto, .deselect, .addtags').removeClass('hide') }); + var images_order = []; + $("#edit-order-btn").on("click",function(){ + var el = $(this); + if($container.data("order-edit") == "0"){ + $container.sortable("enable"); + $(".order-edit-notification").slideDown(); + images_order = $container.sortable( "toArray", { attribute: "data-image-id" }); + $container.data("order-edit","1"); + el.text("Save Order"); + }else{ + var temp = $container.sortable( "toArray", { attribute: "data-image-id" }); + if(images_order.toString() != temp.toString()){ + $.ajax({ + url : "/admin/galleries/order", + data : {"imageids" : temp }, + type : "post", + dataType : "json" + }) + } + $(".order-edit-notification").slideUp(); + $container.sortable("disable"); + $container.data("order-edit","0"); + el.text("Edit Order"); + } + return false; + }) // $('.add-imgs').on({ @@ -223,11 +253,13 @@ $(function() { if(!$(this).is(':hidden')) { $('.add-imgs').html(' Close panel'); $('.rgbody').stop(true, false).animate({'padding-bottom': 280}, 300); + $("#edit-order-btn").hide(); } else { $('.add-imgs').html(' Add Image'); $('.files').empty() $('#file-list').checkListLength(); $('.rgbody').stop(true, false).animate({'padding-bottom': 0}, 300); + $("#edit-order-btn").show(); fetchNewImages(); }; }); diff --git a/app/assets/javascripts/galleryAPI.js.erb b/app/assets/javascripts/galleryAPI.js.erb index 67d6b83..ce81a81 100644 --- a/app/assets/javascripts/galleryAPI.js.erb +++ b/app/assets/javascripts/galleryAPI.js.erb @@ -17,7 +17,7 @@ var galleryAPI = function(){ this.loadTheater = function(id){ g.loadstart(); - var imageArray; + var imageArray = []; var imagecount = 0; var picHeight = 0; var bindHandlers = function(){ @@ -255,8 +255,9 @@ var galleryAPI = function(){ var preparestage = function(albumid){ $.getJSON("../galleries/"+albumid+"/imgs",function(album){ - imageArray = eval(album.images); + // imageArray = eval(album.images); $.each(album.images,function(i,image){ + imageArray.push(image) if(image._id == id) imagecount = i; }) diff --git a/app/assets/stylesheets/gallery.css b/app/assets/stylesheets/gallery.css index 7be5327..53d8c79 100644 --- a/app/assets/stylesheets/gallery.css +++ b/app/assets/stylesheets/gallery.css @@ -518,4 +518,17 @@ } #fileupload #file-list .action-bnt { text-align: right; +} + +.order-edit-notification{ + background-color: #ffffd5; + display: none; + height: 25px; + margin-left: 40%; + position: fixed; + text-align: center; + margin-top: 5px; + top: 85px; + width: 250px; + font-size: 13px; } \ No newline at end of file diff --git a/app/controllers/admin/galleries_controller.rb b/app/controllers/admin/galleries_controller.rb index 57d7ba2..60627e2 100644 --- a/app/controllers/admin/galleries_controller.rb +++ b/app/controllers/admin/galleries_controller.rb @@ -10,7 +10,7 @@ class Admin::GalleriesController < OrbitAdminController def show @album = Album.find(params[:id]) - @images = @album.album_images + @images = @album.album_images.asc(:order) image_content = [] @images.each do |image| image_content << {"id" => image.id.to_s, "description" => image.description_translations,"tags" => image.tags.collect{|t| t.id.to_s}} @@ -127,7 +127,7 @@ class Admin::GalleriesController < OrbitAdminController def imgs @album = Album.find(params[:gallery_id]) @tag_names = Array.new - @images = @album.album_images.all + @images = @album.album_images.asc(:order) @output = Array.new @images.each do |values| @output << { _id: values.id.to_s, @@ -136,6 +136,7 @@ class Admin::GalleriesController < OrbitAdminController title: values.title, file: values.file.as_json[:file], gallery_album_id: values.album_id, + order: values.order, tags: values.tags} end render :json=>{"images" => @output, "tags" => @album.tags}.to_json diff --git a/app/controllers/admin/images_controller.rb b/app/controllers/admin/images_controller.rb index 8b0936d..e71d03b 100644 --- a/app/controllers/admin/images_controller.rb +++ b/app/controllers/admin/images_controller.rb @@ -4,9 +4,21 @@ class Admin::ImagesController < OrbitAdminController @image = AlbumImage.find(params[:id]) @albumid = @image.album_id @album = Album.find(@albumid) - @images = @album.album_images.all + @images = @album.album_images.asc(:order) end + def changeorder + images = params[:imageids] + images.each_with_index do |image, i| + img = AlbumImage.find(image) rescue nil + if !img.nil? + img.order = i + img.save + end + end + render :json => {"success" => true}.to_json + end + def delete_photos images = params['images'] images.each do |image| diff --git a/app/controllers/galleries_controller.rb b/app/controllers/galleries_controller.rb index 1840757..5494361 100644 --- a/app/controllers/galleries_controller.rb +++ b/app/controllers/galleries_controller.rb @@ -20,7 +20,7 @@ class GalleriesController < ApplicationController def show params = OrbitHelper.params album = Album.find_by_param(params[:uid]) - images = album.album_images.collect do |a| + images = album.album_images.asc(:order).collect do |a| { "image-description" => a.description, "link_to_show" => "/" + I18n.locale.to_s + params[:url] + "/-" + a.id.to_s + "?method=theater", @@ -69,7 +69,7 @@ class GalleriesController < ApplicationController def imgs(album_id) album = Album.find(album_id) tag_names = Array.new - images = album.album_images.all + images = album.album_images.asc(:order) output = Array.new images.each do |values| output << { _id: values.id.to_s, @@ -87,7 +87,7 @@ class GalleriesController < ApplicationController image = AlbumImage.find(params[:uid]) albumid = image.album_id album = Album.find(albumid) - images = album.album_images.all + images = album.album_images.asc(:order) { "album" => album, "images" => images, diff --git a/app/models/album_image.rb b/app/models/album_image.rb index 1cbf382..b038b8b 100644 --- a/app/models/album_image.rb +++ b/app/models/album_image.rb @@ -8,6 +8,7 @@ class AlbumImage field :title field :description, localize: true field :rss2_id, type: String + field :order, type: Integer, default: 0 # has_and_belongs_to_many :tags, :class_name => "GalleryTag" diff --git a/app/views/admin/galleries/index.html.erb b/app/views/admin/galleries/index.html.erb index 1b96dc4..3599464 100644 --- a/app/views/admin/galleries/index.html.erb +++ b/app/views/admin/galleries/index.html.erb @@ -9,6 +9,7 @@ <% end %> <% content_for :page_specific_javascript do %> + <%= javascript_include_tag "lib/jquery-ui-1.10.0.custom.min" %> <%= javascript_include_tag "jquery.masonry.min.js" %> <%= javascript_include_tag "jquery.lite.image.resize.js" %> <%= javascript_include_tag "gallery" %> diff --git a/app/views/admin/galleries/show.html.erb b/app/views/admin/galleries/show.html.erb index 028b5ea..6904ce9 100644 --- a/app/views/admin/galleries/show.html.erb +++ b/app/views/admin/galleries/show.html.erb @@ -1,6 +1,7 @@ - -<%= stylesheet_link_tag "gallery" %> -<%= stylesheet_link_tag "lib/tags-groups" %> +<% content_for :page_specific_css do %> + <%= stylesheet_link_tag "gallery" %> + <%= stylesheet_link_tag "lib/tags-groups" %> +<% end %> +
Images re-ordering enabled.
Back @@ -19,6 +21,7 @@ Delete Photo Add Tags Edit + Edit Order Add Image @@ -169,6 +172,7 @@ <% content_for :page_specific_javascript do %> + <%= javascript_include_tag "lib/jquery-ui-1.10.0.custom.min" %> <%= javascript_include_tag "jquery.masonry.min.js" %> <%= javascript_include_tag "jquery.lite.image.resize.js" %> <%= javascript_include_tag "lib/checkbox.card" %> diff --git a/config/routes.rb b/config/routes.rb index b4eb17d..5160fb0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,8 +17,9 @@ Rails.application.routes.draw do post "galleries/delete_photos" => "images#delete_photos" post "galleries/update_image" => "images#update_image" post "galleries/image_tagging" => "images#image_tagging" + post "galleries/order" => "images#changeorder" # match "image_tagging" => "album_images#image_tagging" end - end + end