Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
robmpreston committed Oct 25, 2018
0 parents commit 0a8514d
Show file tree
Hide file tree
Showing 22 changed files with 6,570 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.idea
/vendor
/node_modules
package-lock.json
composer.phar
composer.lock
phpunit.xml
.phpunit.result.cache
.DS_Store
Thumbs.db
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Nova Tags

29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "64robots/nova-tags",
"description": "A Laravel Nova field.",
"keywords": [
"laravel",
"nova"
],
"license": "MIT",
"require": {
"php": ">=7.1.0"
},
"autoload": {
"psr-4": {
"R64\\Tags\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"R64\\Tags\\FieldServiceProvider"
]
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
Empty file added dist/css/field.css
Empty file.
1 change: 1 addition & 0 deletions dist/js/field.js

Large diffs are not rendered by default.

Binary file added fonts/element-icons.ttf
Binary file not shown.
Binary file added fonts/element-icons.woff
Binary file not shown.
Binary file not shown.
Binary file not shown.
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"
}
12 changes: 12 additions & 0 deletions nova-imports/BelongsToFieldStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
fetchAvailableResources(resourceName, fieldAttribute, params) {
return Nova.request().get(
`/nova-api/${resourceName}/associatable/${fieldAttribute}`,
params
)
},

determineIfSoftDeletes(resourceName) {
return Nova.request().get(`/nova-api/${resourceName}/soft-deletes`)
},
}
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"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",
"element-ui": "^2.4.7",
"laravel-mix": "^2.1.14",
"laravel-nova": "^1.0",
"vue-multiselect": "^2.1.0"
},
"dependencies": {
"vue": "^2.5.0"
}
}
13 changes: 13 additions & 0 deletions resources/js/components/DetailField.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<template>
<panel-item :field="field" />
</template>

<script>
import DisplayCommaSeparated from './DisplayCommaSeparated';
export default {
mixins: [DisplayCommaSeparated],
props: ['resource', 'resourceName', 'resourceId', 'field']
};
</script>
8 changes: 8 additions & 0 deletions resources/js/components/DisplayCommaSeparated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
created() {
const values = this.field.value.map(
field => (this.field.labelKey ? field[this.field.labelKey] : field)
);
this.$set(this.field, 'value', values.join(', '));
}
};
123 changes: 123 additions & 0 deletions resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<template>
<default-field :field="field">
<template slot="field">
<Multiselect
v-model="value"
:tag-placeholder="tagPlaceholder"
:placeholder="placeholder"
:label="label"
:track-by="key"
:options="availableResources"
:multiple="true"
:taggable="true"
@tag="addTag"
/>

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

<script>
import 'vue-multiselect/dist/vue-multiselect.min.css';
import Multiselect from 'vue-multiselect';
import FormField from 'laravel-nova/src/mixins/FormField';
import HandlesValidationErrors from 'laravel-nova/src/mixins/HandlesValidationErrors';
import storage from '../../../nova-imports/BelongsToFieldStorage';
export default {
components: { Multiselect },
mixins: [FormField, HandlesValidationErrors],
props: ['resourceName', 'resourceId', 'field'],
data() {
return {
availableResources: []
};
},
computed: {
key() {
return this.field.valueKey || 'id';
},
label() {
return this.field.labelKey || 'name';
},
placeholder() {
return this.field.placeholder === undefined
? this.__('Search or add a tag')
: this.field.placeholder;
},
tagPlaceholder() {
return this.field.tagPlaceholder === undefined
? this.__('Add this as new tag')
: this.field.tagPlaceholder;
}
},
created() {
this.getAvailableResources();
},
methods: {
/*
* Add a new tag with random id
*/
addTag(newTag) {
const tag = {
[this.label]: newTag,
[this.key]:
newTag.substring(0, 2) + Math.floor(Math.random() * 10000000)
};
this.availableResources.push(tag);
this.value.push(tag);
},
/*
* Set the initial, internal value for the field.
*/
setInitialValue() {
let resources = this.field.value || [];
resources = resources.map(resource => ({
[this.key]: resource[this.key],
[this.label]: resource[this.label]
}));
this.value = resources;
},
/**
* Get the resources that may be related to this resource.
*/
getAvailableResources() {
return storage
.fetchAvailableResources(
this.resourceName,
this.field.attribute,
this.queryParams
)
.then(({ data: { resources } }) => {
this.availableResources = resources.map(resource => ({
[this.key]: resource.value,
[this.label]: resource.display
}));
});
},
/**
* Fill the given FormData object with the field's internal value.
*/
fill(formData) {
formData.append(this.field.attribute, JSON.stringify(this.value) || '');
}
}
};
</script>
13 changes: 13 additions & 0 deletions resources/js/components/IndexField.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<template>
<span>{{ field.value }}</span>
</template>

<script>
import DisplayCommaSeparated from './DisplayCommaSeparated';
export default {
mixins: [DisplayCommaSeparated],
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-fields-tags', require('./components/IndexField'));
Vue.component('detail-nova-fields-tags', require('./components/DetailField'));
Vue.component('form-nova-fields-tags', 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
33 changes: 33 additions & 0 deletions src/FieldServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace R64\Tags;

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

class FieldServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Nova::serving(function (ServingNova $event) {
Nova::script('nova-fields-tags', __DIR__.'/../dist/js/field.js');
Nova::style('nova-fields-tags', __DIR__.'/../dist/css/field.css');
});
}

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

0 comments on commit 0a8514d

Please sign in to comment.