Skip to content

Commit cda02bd

Browse files
author
Alex Tharp
authored
Merge pull request #32 from cortex-cms/content-item-field-type
COR-525, COR-527: ContentItemFieldType Association And Selection Implementation
2 parents b527ec5 + 912909c commit cda02bd

File tree

8 files changed

+87
-11
lines changed

8 files changed

+87
-11
lines changed

app/assets/javascripts/ckeditor/plugins/cortex_media_insert/plugin.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
(function (global) {
1+
(function (global) {
22
'use strict';
33

44
global.CKEDITOR.plugins.add('cortex_media_insert', {
55
icons: 'media',
66
init: function (editor) {
77
editor.addCommand('insertMedia', {
88
exec: function (editor) {
9-
window.MODALS.featured.open();
9+
window.MODALS.wysiwyg.open();
1010

1111
global.media_select = {};
1212
global.media_select_defer = $.Deferred();
1313
global.media_select_defer.promise(global.media_select);
1414

1515
global.media_select.done(function (media) {
16-
window.MODALS.featured.close();
16+
window.MODALS.wysiwyg.close();
1717

1818
var mediaTag = editor.document.createElement('media');
1919
mediaTag.setAttribute('id', media.id);
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
//= require ckeditor/init
22
//= require ../ckeditor/config
3+
4+
//= require_tree ./cells
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
$('.media-select--featured').click(function (elem) {
2+
var id = $(this).data().id;
3+
var title = $(this).data().title;
4+
var thumb_url = $(this).data().thumb;
5+
6+
$(".association_content_item_id").val(id);
7+
8+
$('.content-item-button__selection').remove();
9+
$('.content-item-button').append(
10+
'<div class="content-item-button__selection">' +
11+
'<img src="' + thumb_url + '" height="50px">' +
12+
'<div class="content-item-button__selection__text">' +
13+
'Selected Media: ' +
14+
title +
15+
'</div></div>'
16+
);
17+
18+
window.MODALS.featured.close();
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
= render_associated_content_item_thumb
2+
.content-item-button__selection__text
3+
Selected Media:
4+
= associated_content_item_title

app/cells/plugins/core/asset_cell.rb

+13
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ module Core
33
class AssetCell < Plugins::Core::Cell
44
include ActionView::Helpers::NumberHelper
55
include UtilityHelper
6+
include Cells::AssociationHelper
67

78
def input
89
render
910
end
1011

12+
def association
13+
render
14+
end
15+
1116
private
1217

1318
def render_allowed_asset_extensions
@@ -33,6 +38,14 @@ def render_label
3338
def render_input
3439
@options[:form].file_field 'data[asset]'
3540
end
41+
42+
def associated_content_item_thumb_url
43+
data['asset']['style_urls']['mini']
44+
end
45+
46+
def render_associated_content_item_thumb
47+
image_tag(associated_content_item_thumb_url, height: '50px')
48+
end
3649
end
3750
end
3851
end

app/cells/plugins/core/content_item/popup.haml

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33

44
= render_label
55
%br
6-
%button.content_item_button.text-center.popup--open
7-
.button_content
6+
%button.content-item-button.popup--open
7+
.content-item-button__select.content-item-button__select--selected
88
%i{ class: "material-icons icon" }
99
cloud_upload
1010
%br
11-
%span.content_item_button-text
12-
Click to add a
11+
%span.content-item-button__select__text
12+
Click to select a
1313
= field.name
14-
from the media library
14+
from the Media library
15+
- if associated_content_item && associated_primary_field_type_class == AssetFieldType
16+
.content-item-button__selection
17+
= render_association_cell
1518
%small
1619
Recommended size: 1452px x 530px with a live area of 930px x 530px

app/cells/plugins/core/content_item_cell.rb

+37-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,47 @@ def value
1111
data&.[]('content_item_id')
1212
end
1313

14+
def associated_content_item
15+
ContentItem.find_by_id(value)
16+
end
17+
18+
def associated_primary_field
19+
associated_content_item.content_type.fields.find_by_name(field.metadata['field_name'])
20+
end
21+
22+
def associated_primary_field_type_class
23+
associated_primary_field.field_type_instance.class
24+
end
25+
26+
def associated_primary_field_item
27+
associated_content_item.field_items.find_by_field_id associated_primary_field
28+
end
29+
30+
def associated_content_item_title
31+
# Gross hack, this should rely on 'primary title field' config feature in future, and should use a scope
32+
title_field_item = associated_content_item.field_items.find do |field_item|
33+
field_item.field.name == 'Title'
34+
end
35+
36+
title_field_item.data['text']
37+
end
38+
1439
def render_label
15-
"Add #{field.name}"
40+
"Select #{field.name}"
1641
end
1742

1843
def render_content_item_id
19-
@options[:form].hidden_field 'data[content_item_id]', value: value
44+
@options[:form].hidden_field 'data[content_item_id]', value: value, class: 'association_content_item_id'
45+
end
46+
47+
def render_association_cell
48+
cell(Plugins::Core::AssetCell, associated_primary_field_item,
49+
associated_content_item: associated_content_item,
50+
associated_primary_field: associated_primary_field,
51+
associated_primary_field_type_class: associated_primary_field_type_class,
52+
associated_primary_field_item: associated_primary_field_item,
53+
associated_content_item_title: associated_content_item_title)
54+
.(:association)
2055
end
2156
end
2257
end

cortex-plugins-core.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Gem::Specification.new do |s|
2222
s.add_dependency "cells-haml", "~> 0.0.10"
2323
s.add_dependency "mimemagic", "~> 0.3.2"
2424
s.add_dependency "ckeditor", "= 4.2.0"
25-
s.add_dependency "jsonb_accessor", "~> 1.0.0.beta.2"
25+
s.add_dependency "jsonb_accessor", "~> 1.0.0.beta"
2626

2727
s.add_development_dependency "sqlite3"
2828
end

0 commit comments

Comments
 (0)