Skip to content

Commit

Permalink
video aspect ratio and new bulk image gallery element
Browse files Browse the repository at this point in the history
  • Loading branch information
jellygnite committed Dec 22, 2020
1 parent 42b3d70 commit 4c3a5c4
Show file tree
Hide file tree
Showing 16 changed files with 291 additions and 20 deletions.
1 change: 1 addition & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Jellygnite\Elements\Model\BaseElementObject:
extensions:
- Jellygnite\ElementalStyle\Extensions\DynamicStyleExtension


2 changes: 2 additions & 0 deletions client/dist/css/custom.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

.textarea.textfield { resize: none; overflow: auto; white-space:nowrap }
2 changes: 1 addition & 1 deletion client/dist/javascript/jsVideoUrlParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ function Vimeo() {

Vimeo.prototype.parseUrl = function (url, result) {
var match = url.match(/(vimeo(?:cdn|pro)?)\.com\/(?:(?:channels\/[\w]+|(?:(?:album\/\d+|groups\/[\w]+|staff\/frame)\/)?videos?)\/)?(\d+)(?:_(\d+)(?:x(\d+))?)?(\.\w+)?/i);
console.log(match);
// console.log(match);
if (!match) {
return result;
}
Expand Down
20 changes: 20 additions & 0 deletions client/dist/javascript/parsevideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
var $videoCode = $('#Form_ItemEditForm_Code');
var $videoProvider = $('#Form_ItemEditForm_Provider');
var $videoImageURL = $('#Form_ItemEditForm_ImageURL');
var $videoMetadata = $('#Form_ItemEditForm_Metadata');
if($videoCode.attr('readonly') && $videoProvider.attr('readonly')){
$('#Form_ItemEditForm_VideoURL_message').remove();
if ($videoURL.val()) {
try {
var urlParserObject = urlParser.parse($videoURL.val());
if(typeof urlParser !=="undefined"){
var metadata = '';
var video_id = urlParserObject.id;
var video_provider = urlParserObject.provider;
$videoCode.val(video_id);
Expand All @@ -25,6 +27,7 @@
async: false, // so we can alter the variable thumb_url
success: function(data){
thumb_url = data[0].thumbnail_large;
metadata = JSON.stringify(data);
},
error:function (xhr, ajaxOptions, thrownError){
if(xhr.status==404) {
Expand All @@ -34,22 +37,39 @@
});
}
if(video_provider == 'youtube') {
$.ajax({
type:'GET',
url: 'https://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=' + video_id + '&format=json',
dataType: 'json',
async: false,
success: function(data){
metadata = JSON.stringify(data);
},
error:function (xhr, ajaxOptions, thrownError){
if(xhr.status==404) {
console.log(thrownError);
}
}
});
thumb_url = 'https://img.youtube.com/vi/' + video_id + '/mqdefault.jpg';
}
$videoImageURL.val(thumb_url);
$videoMetadata.val(metadata);
}
}
catch(err) {
$videoURL.after('<div id="Form_ItemEditForm_VideoURL_message" class="warning">Please enter valid video URL.</div>');
$videoCode.val('');
$videoProvider.val('');
$videoImageURL.val('');
$videoMetadata.val('');
//console.log(err.message);
}
} else {
$videoCode.val('');
$videoProvider.val('');
$videoImageURL.val('');
$videoMetadata.val('');
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Model/ElementGallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,6 @@ protected function provideBlockSchema()
*/
public function getType()
{
return _t(__CLASS__.'.BlockType', 'Gallery');
return _t(__CLASS__.'.BlockType', 'Media Gallery');
}
}
169 changes: 169 additions & 0 deletions src/Model/ElementImageGallery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

namespace Jellygnite\Elements\Model;

use DNADesign\Elemental\Models\BaseElement;
use SilverStripe\Assets\Image;
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBHTMLText;
use Bummzack\SortableFile\Forms\SortableUploadField;

use SilverStripe\Dev\Debug;

/**
* Class ElementGallery
*
* @property string $Content
*
* @method \SilverStripe\ORM\ManyManyList Gallery()
*
* does not need ShowTitle
*/
class ElementImageGallery extends BaseElement
{


/**
* @var string
*/
private static $icon = 'font-icon-block-layout-5';

/**
* @return string
*/
private static $singular_name = 'Image Gallery Element';

/**
* @return string
*/
private static $plural_name = 'Image Gallery Elements';

/**
* @var string
*/
private static $table_name = 'ElementImageGallery';

/**
* @var array
*/
private static $styles = [];

/**
* @var array
*/
private static $db = [
'Content' => 'HTMLText'
];

/**
* @var array
*/
private static $many_many = [
'Images' => Image::class
];

/**
* @var array
*/
private static $many_many_extraFields = array(
'Images' => array(
'SortOrder' => 'Int',
),
);

/**
* @var array
*/
private static $owns = [
'Images',
];

/**
* Set to false to prevent an in-line edit form from showing in an elemental area. Instead the element will be
* clickable and a GridFieldDetailForm will be used.
*
* @config
* @var bool
*/
private static $inline_editable = false;

/**
* @param bool $includerelations
* @return array
*/
public function fieldLabels($includerelations = true)
{
$labels = parent::fieldLabels($includerelations);

// $labels['Content'] = _t(__CLASS__.'.ContentLabel', 'Intro');
$labels['Gallery'] = _t(__CLASS__ . '.ImageGalleryLabel', 'Image Gallery');

return $labels;
}

/**
* @return FieldList
*/
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {

if ($this->ID) {
$itemField = $fields->dataFieldByName('Images');
$fields->removeByName('Images');
$fields->dataFieldByName('Content')
->setRows(8);


$fields->addFieldToTab('Root.Main',

SortableUploadField::create(
'Images', $this->fieldLabel('Images')
)->setRightTitle('Supports bulk image uploads')
);
}
});

return parent::getCMSFields();
}

/**
* @return mixed
*/
public function getImagesList()
{
return $this->Images()->sort('SortOrder');
}

/**
* @return DBHTMLText
*/
public function getSummary()
{
if ($this->Images()->count() == 1) {
$label = ' item';
} else {
$label = ' items';
}
return DBField::create_field('HTMLText', $this->Images()->count() . $label)->Summary(20);
}

/**
* @return array
*/
protected function provideBlockSchema()
{
$blockSchema = parent::provideBlockSchema();
$blockSchema['content'] = $this->getSummary();
return $blockSchema;
}

/**
* @return string
*/
public function getType()
{
return _t(__CLASS__.'.BlockType', 'Image Gallery');
}
}
60 changes: 58 additions & 2 deletions src/Model/GalleryObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ class GalleryObject extends BaseElementObject


private static $valid_providers = ['youtube','vimeo'];
private static $default_aspect_ratio = 1.78; // 16:9

private static $db = array(
'Type' => 'Enum("Image,HTML5Video,EmbedVideo","Image")',
'VideoURL' => 'Varchar(255)',
'Code' => 'Varchar(100)',
'Provider' => 'Varchar(50)',
'ImageURL' => 'Varchar(255)'
'ImageURL' => 'Varchar(255)',
'Metadata' => 'Text', // saves a json object containing complete oembed metadata

);

private static $has_one = [
Expand Down Expand Up @@ -92,12 +95,14 @@ public function getCMSFields()

Requirements::javascript('jellygnite/silverstripe-elements:client/dist/javascript/jsVideoUrlParser.js');
Requirements::javascript('jellygnite/silverstripe-elements:client/dist/javascript/parsevideo.js');
Requirements::css('jellygnite/silverstripe-elements:client/dist/css/custom.css');

$fldType = $fields->dataFieldByName('Type');
$fldVideoURL = $fields->dataFieldByName('VideoURL')->setRightTitle('Enter a Youtube or Vimeo URL.');
$fldVideoCode = $fields->dataFieldByName('Code')->setAttribute("readonly",true);
$fldVideoProvider = $fields->dataFieldByName('Provider')->setAttribute("readonly",true);
$fldVideoImageURL = $fields->dataFieldByName('ImageURL')->setAttribute("readonly",true);
$fldVideoMetadata = $fields->dataFieldByName('Metadata')->setAttribute("readonly",true)->setRows(1)->addExtraClass('textfield');
$fldImage = $fields->dataFieldByName('Image')
->setFolderName('images/gallery')
->setDescription(null);
Expand All @@ -114,6 +119,7 @@ public function getCMSFields()
'Code',
'Provider',
'ImageURL',
'Metadata',
'Image',
'HTML5Video',
'Content',
Expand All @@ -127,7 +133,8 @@ public function getCMSFields()
$fldVideoCode,
$fldVideoProvider,
$fldVideoImageURL
)->setTitle("Video Meta")->addExtraClass("fieldgroup-horizontal")
)->setTitle("Video Meta")->addExtraClass("fieldgroup-horizontal"),
$fldVideoMetadata
])
->displayIf('Type')->isEqualTo('EmbedVideo')
->end(),
Expand Down Expand Up @@ -207,6 +214,55 @@ public function getEmbedURL($params = null) {
return false;
}
}

public function getMetadataAsArray(){
if($this->Metadata){
$metadata = json_decode($this->Metadata, true);
switch ($this->Provider) {
case 'youtube':
return $metadata;
break;
case 'vimeo':
return $metadata[0];
break;
}
}
return null;
}

public function getVideoWidth() {
$metadata = $this->getMetadataAsArray();
if($metadata){
return $metadata['width'];
}
return null;
}
public function getVideoHeight() {
$metadata = $this->getMetadataAsArray();
if($metadata){
return $metadata['height'];
}
return null;
}

// need to pass this in a way that can be converted to css or style
public function getVideoAspectRatio() {
$default_aspect_ratio = $this->config()->get('default_aspect_ratio');
$width = (int) $this->getVideoWidth();
$height = (int) $this->getVideoHeight();
if($width && $height){
$aspect_ratio = $width / $height;
return round($aspect_ratio, 2);
}
return $default_aspect_ratio;
}

// get container padding
public function getVideoContainerPadding() {
$padding = 1 / $this->getVideoAspectRatio() * 100;
return round($padding,2);
}

public function getMediaType($params = null) {
switch ($this->Type) {
case 'Image':
Expand Down
4 changes: 2 additions & 2 deletions templates/Jellygnite/Elements/Includes/GalleryGrid.ss
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="uk-grid uk-flex-top $StyleByLocation(grid) mt-5" data-uk-grid data-uk-grid-margin>
<div class="uk-grid uk-flex-top $StyleByLocation(grid) mt-5" data-uk-grid>
<% loop $MediaItemsList %>
<div class="item uk-text-center<% if $StyleVariant %> $StyleVariant<% end_if %>">
<% if $MediaType == 'image' %>
Expand All @@ -9,7 +9,7 @@
</div>
<% else_if $MediaType == 'video' %>
<div class="item-image-holder item-video-holder" data-mh="mh-image{$Up.ID}">
<div class="videowrap">
<div class="videowrap" style="padding-bottom:{$VideoContainerPadding}%;">
<div class="$Provider">
<iframe width="100%" height="100%" src="$EmbedURL" frameborder="0" allowfullscreen></iframe>
</div>
Expand Down
10 changes: 10 additions & 0 deletions templates/Jellygnite/Elements/Includes/ImageGalleryGrid.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class="uk-grid uk-flex-top $StyleByLocation(grid) mt-5" data-uk-grid>
<% loop $ImagesList %>
<% if $Me %>
<div class="item uk-text-center<% if $StyleVariant %> $StyleVariant<% end_if %>">
<div class="item-image-holder" data-mh="mh-image{$Up.ID}"><% if $Me %><img src="$Me.ScaleMaxWidth(640).URL" class="img-fluid" alt="<% if $Me.Title %>$Me.Title.ATT<% else %>$Title.ATT<% end_if %>"><% end_if %></div>
</div>
<% end_if %>
<% end_loop %>
</div>

Loading

0 comments on commit 4c3a5c4

Please sign in to comment.