Skip to content

Commit

Permalink
Initial build.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisThompsonTLDR committed Nov 9, 2018
0 parents commit 53805cb
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 0 deletions.
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "christhompsontldr/collective-input",
"description": "A Laravel Collective form component for building Bootstrap 4 friendly form elements.",
"authors": [
{
"name": "Chris Thompson",
"email": "[email protected]",
"homepage": "https://christhompsontldr.com"
}
],
"keywords": ["laravel" , "collective", "forms", "Bootstrap"],
"support": {
"issues": "https://github.com/christhompsontldr/collective-input/issues",
"source": "https://github.com/christhompsontldr/collective-input"
},
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"Christhompsontldr\\CollectiveInput\\": "src/"
}
},
"license": "MIT",
"type": "project",
"extra": {
"laravel": {
"providers": [
"Christhompsontldr\\CollectiveInput\\ServiceProvider"
]
}
}
}
Empty file added readme.md
Empty file.
33 changes: 33 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Christhompsontldr\CollectiveInput;

use Form;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{

public function boot() {
Form::component('bs', 'collective::input', ['name', 'type', 'options']);

// https://stackoverflow.com/questions/38135455/how-to-have-one-time-push-in-laravel-blade
Blade::directive('pushonce', function ($expression) {
$domain = explode(':', trim(substr($expression, 1, -1)));
$push_name = $domain[0];
$push_sub = isset($domain[1]) ? $domain[1] : '';
$isDisplayed = '__pushonce_' . md5($push_name.'_'.$push_sub);
return "<?php if(!isset(\$__env->{$isDisplayed})): \$__env->{$isDisplayed} = true; \$__env->startPush('{$push_name}'); ?>";
});

Blade::directive('endpushonce', function ($expression) {
return '<?php $__env->stopPush(); endif; ?>';
});

// hintpath views
$this->loadViewsFrom(realpath(__DIR__ . '/resources/views'), 'collective');

$this->publishes([
realpath(__DIR__ . '/resources/views') => resource_path('views/vendor/collective'),
]);
}
}
208 changes: 208 additions & 0 deletions src/resources/views/input.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
<?php
$dotName = str_replace(['[', ']'], ['.', ''], $name);

if (!isset($type)) {
$type = 'text';
}

// add the invalid class
if (!isset($options['class'])) {
if (in_array($type, ['checkbox', 'radio'])) {
$options['class'] = ' form-check-input';
}
elseif ($type == 'file') {
$options['class'] = 'form-control-file';
}
else {
$options['class'] = 'form-control';
}
}
$options['class'] .= (($errors->has($dotName)) ? ' is-invalid' : '');


if (in_array('required', $options)) {
$options['required'] = true;
} else {
$options['required'] = false;
}


$selected = null;
if (isset($options['selected'])) {
$selected = $options['selected'];
unset($options['selected']);
}
$selectOptions = [];
if (isset($options['options'])) {
$selectOptions = $options['options'];
unset($options['options']);
}
$after = '';
if (isset($options['after'])) {
$after = $options['after'];
unset($options['after']);
}


$label = false;
if (isset($options['label']) && $options['label'] !== false) {
$label = $options['label'];
} elseif (!isset($options['label'])) {
$label = ucwords(str_replace('_', ' ', $name));
}
unset($options['label']);


if (!isset($options['id'])) {
$options['id'] = $name;
}

$formGroupClass = '';
if (isset($options['form-group'])) {
if (isset($options['form-group']['class'])) {
$formGroupClass = ' ' . $options['form-group']['class'];
}

unset($options['form-group']);
}

if (in_array($type, ['checkbox', 'radio'])) {
$options['id'] = str_replace(['[', ']'], '', $options['id']) . $options['value'];
}

$value = null;
if (isset($options['value'])) {
$value = old($name, $options['value']);
}


// switch field type to textarea for type=html
switch ($type) {
case 'html':
$fieldType = 'html';
break;
case 'datetime':
$fieldType = 'text';
$options['class'] .= ' datetime';
break;
default:
$fieldType = $type;
}
?>
<div class="form-group{{ $formGroupClass }}">
@if ($label !== false && !in_array($type, ['checkbox', 'radio']))
{{ Form::label($name, $label) }}
@endif


@if ($fieldType == 'select')
{{ Form::{$fieldType}($name, $selectOptions, $selected, $options) }}

@elseif (in_array($type, ['checkbox', 'radio']))
<div class="form-check">
{{ Form::{$fieldType}($name, $options['value'], false, $options) }}
{{ Form::label($options['id'], $label, ['class' => 'form-check-label']) }}
</div>

@elseif ($fieldType == 'address')
@php
$tmpOptions = $options;
$tmpOptions['class'] .= ' mb-1';
@endphp
{{ Form::text('address', $value, $tmpOptions) }}
{{ Form::text('address_2', $value, $options) }}
</div>
<div class="form-group{{ $formGroupClass }}">

<div class="row">
<div class="col">
@if ($label !== false)
{{ Form::label('city', 'City') }}
@endif
{{ Form::text('city', $value, $options) }}
</div>
<div class="col">
@if ($label !== false)
{{ Form::label('state', 'State') }}
@endif
{{ Form::text('state', $value, $options) }}
</div>
<div class="col">
@if ($label !== false)
{{ Form::label('zip', 'Zip') }}
@endif
{{ Form::text('Zip', $value, $options) }}
</div>
</div>

@elseif($type == 'datetime')
<div class="input-group date" id="{{ $options['id'] }}" data-target-input="nearest">
<input type="text" name="{{ $name }}" class="{{ $options['class'] }}" data-target="#{{ $options['id'] }}"/>
<div class="input-group-append" data-target="#{{ $options['id'] }}" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fal fa-calendar-alt"></i></div>
</div>
</div>

@elseif(in_array($type, ['password', 'file']))
{{ Form::{$fieldType}($name, $options) }}

@else
{{ Form::{$fieldType}($name, $value, $options) }}
@endif

@if ($after)
<small class="form-text text-muted">{{ $after }}</small>
@endif


{!! $errors->first($dotName, '<small class="invalid-feedback">:message</small>') !!}
</div>

@if ($type == 'html')
@pushonce('after-styles:summernote')
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.10/summernote-bs4.css" integrity="sha256-fPUAOwSYkVTnL8xdLidCEi5IxW+ZVfcmNJ4m/+EGVI8=" crossorigin="anonymous" />
@endpushonce

@pushonce('after-scripts:summernote')
<script src="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.10/summernote-bs4.min.js" integrity="sha256-o5hEO6rl7yksLT3gTjQcYYDt03Lx9VwNu81FrO82Ofw=" crossorigin="anonymous"></script>
@endpushonce

@push('after-scripts')
<script>
$(function() {
$('#{{ $name }}').summernote({
height: {{ isset($options['height']) ? $options['height'] : 400 }},
toolbar: [
// [groupName, [list of button]]
['style', ['bold', 'italic', 'underline', 'clear']],
['para', ['ul', 'ol']],
['link', ['link']]
]
});
});
</script>
@endpush
@endif

@if ($type == 'datetime')
@pushonce('after-styles:datetime')
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tempusdominus-bootstrap-4/5.0.0-alpha14/css/tempusdominus-bootstrap-4.min.css">
@endpushonce

@pushonce('after-scripts:datetime')
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js" integrity="sha256-CutOzxCRucUsn6C6TcEYsauvvYilEniTXldPa6/wu0k=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tempusdominus-bootstrap-4/5.0.0-alpha14/js/tempusdominus-bootstrap-4.min.js"></script>
<script>
$(function () {
$('.datetime').datetimepicker({
icons: {
time: "fal fa-clock",
date: "fal fa-calendar-alt",
up: "fal fa-arrow-up",
down: "fal fa-arrow-down"
}
});
});
</script>
@endpushonce
@endif

0 comments on commit 53805cb

Please sign in to comment.