Skip to content

Commit

Permalink
feat: add new field 'audio' ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
agoalofalife committed Jan 29, 2023
1 parent 3605d9e commit 2af3cdf
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion public/js/orchid_fields.js

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions resources/js/controllers/audio_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export default class extends window.Controller {
connect() {
const audio = this.element.querySelector('audio')

$(audio).on('ended', (event) => {
const controlPlayBtn = $(this.element.querySelector('.control-play'))
const controlPauseBtn = $(this.element.querySelector('.control-pause'))

controlPlayBtn.removeClass('d-none')
controlPauseBtn.addClass('d-none')
})
}

audio_play_pause() {
const audio = this.element.querySelector('audio')
const button = $(this.element.querySelector('button'))
const controlPlayBtn = button.find('.control-play')
const controlPauseBtn = button.find('.control-pause')

if (audio.paused) {
controlPlayBtn.addClass('d-none')
controlPauseBtn.removeClass('d-none')
audio.play()
} else if (audio.played) {
controlPlayBtn.removeClass('d-none')
controlPauseBtn.addClass('d-none')

audio.pause()
}
}
}
2 changes: 2 additions & 0 deletions resources/js/orchid_fields.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import RangeController from './controllers/range_controller';
import RateController from './controllers/rate_controller';
import ImageZoomController from './controllers/image_zoom_controller';
import AudioController from './controllers/audio_controller';

application.register('range', RangeController)
application.register('rate', RateController)
application.register('image-zoom', ImageZoomController)
application.register('audio', AudioController)
13 changes: 13 additions & 0 deletions resources/views/fields/audio.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@component($typeForm, get_defined_vars())
<div data-controller="audio">
@if($attributes['value'])
<div class="form-group mb-0">
<audio src='{{$attributes['value']}}' preload='{{$attributes['preload']}}'></audio>
<button type="button" class='btn btn-default' data-action="click->audio#audio_play_pause">
<x-orchid-icon path="{{$attributes['playIcon']}}" class="{{$attributes['playIcon']}}"/>
<x-orchid-icon path="{{$attributes['pauseIcon']}}" class="{{$attributes['pauseIcon']}} d-none"/>
</button>
</div>
@endif
</div>
@endcomponent
46 changes: 46 additions & 0 deletions src/Fields/Audio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace agoalofalife\Orchid\Fields;

use Orchid\Screen\Field;

/**
* Class Audio
*
* @method Audio source($value)
* @method Audio preload($value = true)
* @method Audio playIcon($value = null)
* @method Audio pauseIcon($value = null)
*/
class Audio extends Field
{
/**
* @var string
*/
protected $view = 'platform-fields::fields.audio';
/**
* Default attributes value.
*
* @var array
*/
protected $attributes = [
'preload' => 'none',
'playIcon' => 'control-play',
'pauseIcon' => 'control-pause',
];

/**
* Attributes available for a particular tag.
*
* @var array
*/
protected $inlineAttributes = [
'source',
'preload',
'playIcon',
'pauseIcon',
'value',
];
}

0 comments on commit 2af3cdf

Please sign in to comment.