From afb11a2fa242ff433da12df5a07f0bed2f251bb0 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Tue, 28 Jun 2016 16:40:23 +0800 Subject: [PATCH] now new albums and images are sorted by created date and are on the top. also modified loading new images --- app/assets/javascripts/gallery.js | 19 +++++++++--- app/controllers/admin/galleries_controller.rb | 31 ++++++++++++------- app/models/album.rb | 2 +- app/models/album_image.rb | 2 +- config/routes.rb | 15 +++++---- 5 files changed, 45 insertions(+), 24 deletions(-) diff --git a/app/assets/javascripts/gallery.js b/app/assets/javascripts/gallery.js index 11563d3..0dabb60 100644 --- a/app/assets/javascripts/gallery.js +++ b/app/assets/javascripts/gallery.js @@ -243,6 +243,8 @@ $(function() { }) // + + var last_image_id = null; $('.add-imgs').on({ click: function() { $('#fileupload').slideToggle(300, function() { @@ -250,6 +252,14 @@ $(function() { $('.add-imgs').html(' Close panel'); $('.rgbody').stop(true, false).animate({'padding-bottom': 280}, 300); $("#edit-order-btn").hide(); + $.ajax({ + url : "/admin/galleries/last_image_id", + data : {"albumid" : $("#fileupload_aid").val()}, + dataType : "json", + type : "get" + }).done(function(d){ + last_image_id = d.last_image_id; + }) } else { $('.add-imgs').html(' Add Image'); $('.files').empty() @@ -263,20 +273,19 @@ $(function() { } }); var fetchNewImages = function(){ - var lastid = ( $("li.rgalbum:last").length ? $("li.rgalbum:last").data("image-id") : null), - albumid = $("#fileupload_aid").val(); + var albumid = $("#fileupload_aid").val(); $.getJSON('/admin/galleries/get_photoData_json?id='+albumid, function(json, textStatus) { photosData = json; }); $.ajax({ - url : "/admin/new_images", - data : {"last_image_id" : lastid, "album_id" : albumid}, + url : "/admin/galleries/new_images", + data : {"last_image_id" : last_image_id, "album_id" : albumid}, success : function(data){ var $e = $(data); $imgs = $e.find("img"); - $("#imgholder").append($e); + $("#imgholder").prepend($e); $os = $e.find("a.open"); $os.pageslide({ loadComplete: function(instance,elem) { diff --git a/app/controllers/admin/galleries_controller.rb b/app/controllers/admin/galleries_controller.rb index 0587076..f6008ef 100644 --- a/app/controllers/admin/galleries_controller.rb +++ b/app/controllers/admin/galleries_controller.rb @@ -4,13 +4,17 @@ class Admin::GalleriesController < OrbitAdminController before_action :log_user_action layout "back_end" def index - @albums = Album.all.asc(:order) + @albums = Album.where(:order => -1).desc(:created_at) + albums = Album.where(:order.gt => -1).asc(:order) + @albums = @albums.concat(albums) @tags = @module_app.tags end def show @album = Album.find(params[:id]) - @images = @album.album_images.asc(:order) + @images = @album.album_images.where(:order => -1).desc(:created_at) + images = @album.album_images.where(:order.gt => -1).asc(:order) + @images = @images.concat(images) 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}} @@ -61,7 +65,9 @@ class Admin::GalleriesController < OrbitAdminController def get_photoData_json @album = Album.find(params[:id]) - @images = @album.album_images + @images = @album.album_images.where(:order => -1).desc(:created_at) + images = @album.album_images.where(:order.gt => -1).asc(:order) + @images = @images.concat(images) 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}} @@ -158,17 +164,20 @@ class Admin::GalleriesController < OrbitAdminController render :json=>{"files"=>a}.to_json end + def last_image_id + album = Album.find(params[:albumid]) + lastimage = album.album_images.last + render :json => {"last_image_id" => lastimage.id.to_s}.to_json + end + def new_images - - if params[:last_image_id] != "" - @lastimage = AlbumImage.find(params[:last_image_id]) - @album = Album.find(params[:album_id]) - @newimages = @album.album_images.where(:created_at.gt => @lastimage.created_at) + @album = Album.find(params[:album_id]) + if params[:last_image_id].present? + lastimage = AlbumImage.find(params[:last_image_id]) + @newimages = @album.album_images.where(:created_at.gt => lastimage.created_at,:order => -1).desc(:created_at) else - @album = Album.find(params[:album_id]) - @newimages = @album.album_images + @newimages = @album.album_images.where(:order => -1).desc(:created_at) end - render :layout=>false end diff --git a/app/models/album.rb b/app/models/album.rb index aea0e8a..b235329 100644 --- a/app/models/album.rb +++ b/app/models/album.rb @@ -13,7 +13,7 @@ class Album field :tag_names field :uid, type: String field :rss2_id, type: String - field :order, type: Integer, default: 0 + field :order, type: Integer, default: -1 # has_and_belongs_to_many :tags, :class_name => "GalleryTag" has_many :album_images, :autosave => true, :dependent => :destroy diff --git a/app/models/album_image.rb b/app/models/album_image.rb index d40ac10..447dd9d 100644 --- a/app/models/album_image.rb +++ b/app/models/album_image.rb @@ -9,7 +9,7 @@ class AlbumImage field :title field :description, localize: true field :rss2_id, type: String - field :order, type: Integer, default: 0 + field :order, type: Integer, default: -1 # has_and_belongs_to_many :tags, :class_name => "GalleryTag" diff --git a/config/routes.rb b/config/routes.rb index 6436413..533c672 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,18 +7,21 @@ Rails.application.routes.draw do namespace :admin do get "galleries/get_photoData_json" => "galleries#get_photoData_json" + get "galleries/new_images" => "galleries#new_images" + get "galleries/last_image_id" => "galleries#last_image_id" + post "galleries/set_cover" => "galleries#set_cover" + 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" + resources :galleries do get "imgs" => "galleries#imgs" end resources :images post "galleries/upload_image" => "galleries#upload_image" - get "new_images" => "galleries#new_images" - post "galleries/set_cover" => "galleries#set_cover" - 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