Skip to content

Commit

Permalink
Merge changes from attachments en photo pickers
Browse files Browse the repository at this point in the history
  • Loading branch information
Bramjetten committed Apr 22, 2016
2 parents 890cf0c + 92c24d6 commit 37108e9
Show file tree
Hide file tree
Showing 33 changed files with 271 additions and 82 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ group :development, :test do
gem 'codeclimate-test-reporter', require: nil
gem 'figaro'
gem 'appraisal'
end
end
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
class Spina.InfiniteScroll
@init: (link) ->
$(window).off('scroll.infiniteScroll')
$('#main, #overlay section').off('scroll.infiniteScroll')

$link = $(link)
if (url = $link.find('a').attr('href'))
$(window).on 'scroll.infiniteScroll', => @loadNextPage($link)
$(window).scroll()
$('#main, #overlay section').on('scroll.infiniteScroll', -> $(window).trigger 'scroll.infiniteScroll')
$(window).trigger('scroll.infiniteScroll')

@loadNextPage: ($link) ->
if ($(window).scrollTop() > $link.offset().top - $(window).height() - 500)
$(window).off('scroll.infiniteScroll')
$('#main, #overlay section').off('scroll.infiniteScroll')
$.rails.disableElement($link.find('a'))
$.getScript($link.find('a').attr('href'))

Expand All @@ -18,3 +21,4 @@ $.fn.infiniteScroll = () ->

$(document).on 'page:change', ->
$(window).off('scroll.infiniteScroll')
$('#main, #overlay section').off('scroll.infiniteScroll')
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Trix.config.blockAttributes = $.extend Trix.config.blockAttributes, {
class Spina.TrixAttachment
@photoSelect: (e) ->
editor_id = $(this).closest('trix-toolbar').data('editor-id')
$.get("/admin/photos/wysihtml5_select/#{editor_id}")
$.get("<%= Spina::Engine.routes.url_helpers.wysihtml5_select_admin_photos_path('') %>/#{editor_id}")

@photoInsert: (e, url) ->
length = this.editor.getDocument().toString().length
Expand Down
3 changes: 3 additions & 0 deletions app/assets/stylesheets/spina/admin/application.sass
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
@import spina

.infinite-scroll
@include clearfix
8 changes: 5 additions & 3 deletions app/controllers/spina/admin/attachments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def destroy
end

def select
@attachments = Attachment.file_attached.sorted
@selected_attachment_id = Attachment.find_by(id: params[:selected_attachment_id]).try(:id)
@attachments = Attachment.order_by_ids(@selected_attachment_id).file_attached.sorted
@attachment = Attachment.new
end

Expand All @@ -33,12 +34,13 @@ def insert
end

def select_collection
@attachments = Attachment.file_attached.sorted
@selected_attachment_ids = Attachment.where(id: params[:selected_attachment_ids]).ids
@attachments = Attachment.order_by_ids(@selected_attachment_ids).file_attached.sorted
@attachment = Attachment.new
end

def insert_collection
@attachments = Attachment.find(params[:attachment_ids])
@attachments = Attachment.where(id: params[:attachment_ids])
end

private
Expand Down
10 changes: 4 additions & 6 deletions app/controllers/spina/admin/photos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ def link
end

def photo_select
selected = params[:selected_photo_id] || Array.new
@selected_photo_id = Photo.find_by(id: params[:selected_photo_id]).try(:id)
hidden_field_id = params[:hidden_field_id]
@photos = Photo.order_by_ids(selected).sorted.page(params[:page])
@selected_photo = Photo.find(selected)
@photos = Photo.order_by_ids(@selected_photo_id).sorted.page(params[:page])
@photo = Photo.new

if params[:page].present?
Expand All @@ -56,9 +55,8 @@ def photo_select
end

def photo_collection_select
selected = params[:selected_photo_ids] || Array.new
@photos = Photo.order_by_ids(selected).sorted.page(params[:page])
@selected_photos = Photo.where(id: selected)
@selected_photo_ids = Photo.where(id: params[:selected_photo_ids]).ids
@photos = Photo.order_by_ids(@selected_photo_ids).sorted.page(params[:page])
@photo = Photo.new

if params[:page].present?
Expand Down
15 changes: 15 additions & 0 deletions app/helpers/spina/admin/pages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ def partable_type_partial_namespace(partable_type)
partable_type.tableize.sub(/\Aspina\//, '')
end

def flatten_nested_hash(hash)
hash.flat_map{|k, v| [k, *flatten_nested_hash(v)]}
end

def page_ancestry_options(page)
pages = Spina::Page.active
pages = pages.where.not(id: page.subtree.ids) unless page.new_record?

flatten_nested_hash(pages.arrange(order: :position)).map do |page|
next if page.depth >= Spina.config.max_page_depth - 1
page_menu_title = page.depth.zero? ? page.menu_title : " #{page.menu_title}".indent(page.depth, '-')
[page_menu_title, page.ancestry]
end.compact
end

end
end
end
5 changes: 5 additions & 0 deletions app/models/spina/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ def update_attributes(attributes)
end
end

def self.order_by_ids(ids)
sql = sanitize_sql_for_conditions({id: ids})
order("CASE WHEN #{sql} THEN 0 ELSE 1 END")
end

end
end
2 changes: 1 addition & 1 deletion app/models/spina/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def generate_materialized_path
name == 'homepage' ? '' : "#{url_title}"
else
ancestors.collect(&:url_title).append(url_title).join('/')
end
end
end

def ancestry_is_nil
Expand Down
2 changes: 1 addition & 1 deletion app/views/spina/admin/attachments/_select.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
%tbody
- @attachments.each do |attachment|
%tr
%td= radio_button_tag :attachment_id, attachment.id
%td= radio_button_tag :attachment_id, attachment.id, attachment.id == @selected_attachment_id
%td= attachment.name

%footer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
%tbody
- @attachments.each do |attachment|
%tr
%td= check_box_tag 'attachment_ids[]', attachment.id
%td= check_box_tag 'attachment_ids[]', attachment.id, attachment.id.in?(@selected_attachment_ids)
%td= attachment.name

%footer
Expand Down
5 changes: 5 additions & 0 deletions app/views/spina/admin/attachments/insert.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
hidden_input = $("input[name='<%= j params[:page_part_id] %>[page_partable_id]']")
hidden_input.parents('.media_picker').find('.attachment').remove()
hidden_input.parents('.media_picker').append("<div class='attachment'><%= @attachment.name %></div>")
hidden_input.val("<%= @attachment.id %>")
$.hideModal()
5 changes: 0 additions & 5 deletions app/views/spina/admin/attachments/insert.js.erb

This file was deleted.

7 changes: 7 additions & 0 deletions app/views/spina/admin/attachments/insert_collection.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
hidden_input = $("input[name='<%= j params[:page_part_id] %>[attachment_tokens]']")
attachment_collection = $("<%= j render partial: 'attachment_collection', locals: {attachments: @attachments} %>")

hidden_input.parents('.media_picker').find('.attachment').remove()
hidden_input.parents('.media_picker').append(attachment_collection)
hidden_input.val("<%= @attachments.map(&:id).join(',') %>")
$.hideModal()
7 changes: 0 additions & 7 deletions app/views/spina/admin/attachments/insert_collection.js.erb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
= f.object.title
.horizontal-form-content
= f.fields_for :page_partable, f.object.page_partable do |ff|
= link_to select_collection_admin_attachments_path(ff.object_name), remote: true, class: "media_picker" do
.clearfix
.placeholder.pull-right
%span.button.button-small.button-round
%i.icon.icon-dots
= t('spina.choose_from_library')
- if ff.object.attachments.any?
- ff.object.attachments.each do |attachment|
.attachment= attachment.name
= link_to select_collection_admin_attachments_path(ff.object_name, selected_attachment_ids: ff.object.attachment_ids), remote: true, class: 'media_picker clearfix' do
.placeholder.pull-right
%span.button.button-small.button-round
%i.icon.icon-dots
= t('spina.choose_from_library')
- if ff.object.attachments.any?
- ff.object.attachments.each do |attachment|
.attachment= attachment.name

= ff.hidden_field :attachment_tokens, value: ff.object.attachment_ids.join(",")
15 changes: 7 additions & 8 deletions app/views/spina/admin/page_partables/attachments/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
.horizontal-form-label
= f.object.title
.horizontal-form-content
= link_to select_admin_attachments_path(f.object_name), remote: true, class: "media_picker" do
.clearfix
.placeholder.pull-right
%span.button.button-small.button-round
%i.icon.icon-dots
= t('spina.choose_from_library')
= link_to select_admin_attachments_path(f.object_name, selected_attachment_id: f.object.page_partable.id), remote: true, class: 'media_picker clearfix' do
.placeholder.pull-right
%span.button.button-small.button-round
%i.icon.icon-dots
= t('spina.choose_from_library')

- if f.object.page_partable && f.object.page_partable.file.present?
.attachment= f.object.page_partable.name
- if f.object.page_partable && f.object.page_partable.file.present?
.attachment= f.object.page_partable.name

= f.hidden_field :page_partable_id
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
= f.object.title
.horizontal-form-content
= f.fields_for :page_partable, f.object.page_partable do |ff|
= link_to spina.photo_collection_select_admin_photos_path(ff.object_name, selected_photo_ids: (ff.object.photo_ids if ff.object.photos.any?)), remote: true, class: "media_picker" do
.placeholder{class: ('pull-right' if ff.object.photos.any?)}
= link_to spina.photo_collection_select_admin_photos_path(ff.object_name, selected_photo_ids: ff.object.photo_ids), remote: true, class: 'media_picker clearfix' do
.placeholder.pull-right
%span.button.button-small.button-round
%i.icon.icon-dots
=t 'spina.pages.photos_picker'
Expand Down
16 changes: 6 additions & 10 deletions app/views/spina/admin/page_partables/photos/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
.horizontal-form-label
= f.object.title
.horizontal-form-content
= link_to spina.photo_select_admin_photos_path(f.object_name, selected_photo_id: (f.object.page_partable.id if f.object.page_partable.present?)), remote: true, class: "media_picker" do
= link_to spina.photo_select_admin_photos_path(f.object_name, selected_photo_id: f.object.page_partable.id), remote: true, class: 'media_picker clearfix' do
.placeholder.pull-right
%span.button.button-small.button-round
%i.icon.icon-dots
=t 'spina.pages.photo_picker'

- if f.object.page_partable && f.object.page_partable.file.present?
.placeholder.pull-right
%span.button.button-small.button-round
%i.icon.icon-dots
=t 'spina.pages.photo_picker'
.image
= image_tag f.object.page_partable.file.thumb
- else
.placeholder
%span.button.button-small.button-round
%i.icon.icon-dots
=t 'spina.pages.photo_picker'

= f.hidden_field :page_partable_id
2 changes: 1 addition & 1 deletion app/views/spina/admin/pages/_form_advanced.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@
= Spina::Page.human_attribute_name :ancestry
%td
.select-dropdown.ancestry
= f.select :ancestry, options_from_collection_for_select(Spina::Page.active.sorted.roots, 'id', 'menu_title', @page.ancestry.to_i), include_blank: Spina::Page.human_attribute_name(:no_parent)
= f.select :ancestry, page_ancestry_options(f.object), include_blank: Spina::Page.human_attribute_name(:no_parent)
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
= form_for [spina, :admin, @photo], html: {multipart: true} do |f|
= f.label :file, t('spina.photos.upload_image')
= f.file_field :file, data: {customfileinput: true}

= form_tag spina.insert_photo_collection_admin_photos_path(params[:page_part_id]), remote: true, class: 'gallery-prepend-image' do
.infinite-scroll
= render partial: 'spina/admin/photos/photo_multi_picker', collection: @photos

.infinite-pagination= link_to_next_page @photos, 'Next', params: {selected_photo_ids: @selected_photos.ids}, remote: true
.infinite-pagination= link_to_next_page @photos, 'Next', params: params.slice(:selected_photo_ids), remote: true

.gallery-select-action-bar
.pull-right
Expand All @@ -19,5 +19,5 @@
= icon('plus')
= t('spina.photos.choose_images')
%span.gallery-select-counter
- if @selected_photos.any?
(#{@selected_photos.count})
- if @selected_photo_ids.any?
(#{ @selected_photo_ids.count })
4 changes: 2 additions & 2 deletions app/views/spina/admin/photos/_photo_multi_picker.html.haml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.item{class: ('selected' if @selected_photos.include?(photo_multi_picker))}
= check_box_tag 'photo_ids[]', photo_multi_picker.id, @selected_photos.include?(photo_multi_picker)
.item{class: ('selected' if photo_multi_picker.id.in?(@selected_photo_ids)) }
= check_box_tag 'photo_ids[]', photo_multi_picker.id, photo_multi_picker.id.in?(@selected_photo_ids)
= image_tag photo_multi_picker.file.thumb.url, id: "photo_#{photo_multi_picker.id}"
3 changes: 2 additions & 1 deletion app/views/spina/admin/photos/_photo_select.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
.infinite-scroll
= render partial: 'spina/admin/photos/photo_single_picker', collection: @photos

.infinite-pagination= link_to_next_page @photos, 'Next', params: {selected_photo_id: @selected_photo.try(:id)}, remote: true
.infinite-pagination= link_to_next_page @photos, 'Next', params: params.slice(:selected_photo_id), remote: true

4 changes: 2 additions & 2 deletions app/views/spina/admin/photos/_photo_single_picker.html.haml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.item{class: ('selected' if @selected_photo == photo_single_picker)}
= radio_button_tag :photo_id, photo_single_picker.id, photo_single_picker == @selected_photo
.item{ class: ('selected' if photo_single_picker.id == @selected_photo_id) }
= radio_button_tag :photo_id, photo_single_picker.id, photo_single_picker.id == @selected_photo_id
= image_tag photo_single_picker.file.thumb.url, id: "photo_#{photo_single_picker.id}"
5 changes: 3 additions & 2 deletions app/views/spina/admin/photos/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
= f.file_field :file, data: {customfileinput: true}

.gallery-prepend-image
.infinite-scroll= render partial: 'spina/admin/photos/photo', collection: @photos
.infinite-scroll
= render partial: 'spina/admin/photos/photo', collection: @photos

.infinite-pagination= link_to_next_page @photos, 'Next', remote: true
.infinite-pagination= link_to_next_page @photos, 'Next', remote: true
6 changes: 3 additions & 3 deletions app/views/spina/admin/photos/insert_photo.js.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var hidden_input = $("input[name='<%= j params[:page_part_id] %>[page_partable_id]'], input[name='<%= j params[:page_part_id] %>[structure_partable_id]'], input[data-hidden-field='<%= j params[:hidden_field_id] %>']");
hidden_input.parents('.media_picker').find('.image, .placeholder').remove();
hidden_input.parents('.media_picker').find('.image').remove();

<% if @photo.present? %>
hidden_input.parents('.media_picker').prepend("<div class='placeholder pull-right'><span class='button button-small button-round' data-icon='U'><%= I18n.t('spina.pages.photo_picker') %></span></div><div class='image'><img src='<%= @photo.file.thumb %>' /></div>");
hidden_input.parents('.media_picker').append("<div class='image'><img src='<%= @photo.file.thumb %>' /></div>");
hidden_input.val("<%= @photo.id %>");
<% else %>
hidden_input.parents('.media_picker').prepend("<div class='placeholder'><span class='button button-small button-round' data-icon='U'><%= I18n.t('spina.pages.photo_picker') %></span></div>");
hidden_input.parents('.media_picker').append("<input type='hidden' type='checkbox' name='<%= j params[:page_part_id] %>[_destroy]' value='1' />")
<% end %>
$.hideModal();
3 changes: 1 addition & 2 deletions app/views/spina/admin/photos/insert_photo_collection.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ var hidden_input = $("input[name='<%= j params[:page_part_id] %>[photo_tokens]']
<% if @photos.present? %>
var photo_collection = $("<%= j render partial: 'photo_collection', locals: {photos: @photos} %>");
hidden_input.parents('.horizontal-form-content').find('.photo-collection').html(photo_collection);
hidden_input.parents('.horizontal-form-content').find('a .placeholder').addClass('pull-right');
hidden_input.val("<%= @photos.map(&:id).join(',') %>");
<% else %>
hidden_input.parents('.horizontal-form-content').find('a .placeholder').removeClass('pull-right');
hidden_input.val("");
hidden_input.parents('.horizontal-form-content').find('.photo-collection').empty()
<% end %>

$('.sortable-grid').sortable().bind('sortupdate', function(e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
$("<%=j render partial: 'spina/admin/photos/photo_multi_picker', collection: @photos %>").appendTo($(".infinite-scroll"));

// Update pagination link
$('.infinite-pagination').html("<%= j link_to_next_page @photos, 'Next', params: {selected_photo_ids: @selected_photos.ids}, remote: true %>").infiniteScroll()
$('.infinite-pagination').html("<%= j link_to_next_page @photos, 'Next', params: params.slice(:selected_photo_ids), remote: true %>").infiniteScroll()

Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
$("<%=j render partial: 'spina/admin/photos/photo_single_picker', collection: @photos %>").appendTo($(".infinite-scroll"));

// Update pagination link
$('.infinite-pagination').html("<%= j link_to_next_page @photos, 'Next', params: {selected_photo_id: @selected_photo.try(:id)}, remote: true %>").infiniteScroll()
$('.infinite-pagination').html("<%= j link_to_next_page @photos, 'Next', params: params.slice(:selected_photo_id), remote: true %>").infiniteScroll()

Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
= link_to spina.photo_select_admin_photos_path(f.object_name, selected_photo_id: (f.object.structure_partable.id if f.object.structure_partable.present?)), remote: true, class: "media_picker" do
= link_to spina.photo_select_admin_photos_path(f.object_name, selected_photo_id: f.object.structure_partable.id), remote: true, class: "media_picker" do
.placeholder.pull-right
%span.button.button-small.button-round{data: {icon: 'U'}}=t 'spina.pages.photo_picker'
- if f.object.structure_partable && f.object.structure_partable.file.present?
.placeholder.pull-right
%span.button.button-small.button-round{data: {icon: 'U'}}=t 'spina.pages.photo_picker'
.image
= image_tag f.object.structure_partable.file.thumb
- else
.placeholder
%span.button.button-small.button-round{data: {icon: 'U'}}=t 'spina.pages.photo_picker'

= f.hidden_field :structure_partable_id
Loading

0 comments on commit 37108e9

Please sign in to comment.