Skip to content

Commit 2eb3bae

Browse files
authored
Merge pull request #3 from Muetze42/development
Add requirement 0.0.2
2 parents e8bcec0 + 9591a64 commit 2eb3bae

File tree

11 files changed

+441
-31
lines changed

11 files changed

+441
-31
lines changed

README.md

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
1-
[![Stand With Ukraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)](https://vshymanskyy.github.io/StandWithUkraine/)
2-
31
# Nova Textarea- & Text field with BBCodes
2+
Quickly developed. Short tutorial.
43

5-
Quickly developed. Short tutorial.
6-
7-
With this Nova field you have the _Textarea_ or _Text_ with _BBCodes_.
4+
With this Nova field you have the _Textarea_ or _Text_ with _BBCodes_.
85

96
(Tested with Nova 4)
107

11-
![form](https://raw.githubusercontent.com/Muetze42/asset-repo/main/nova-bbcode-textarea/images/form.png "form")
12-
138
## Install
14-
159
```
1610
composer require norman-huth/nova-bbcode-textarea
1711
```
1812

1913
## Usage
20-
2114
```php
2215

2316
use NormanHuth\BBCode\BBCode;
@@ -27,9 +20,7 @@ BBCode::make(__('Name'), 'name')->codes([
2720
'{date}' => __('This code insert the date'),
2821
]),
2922
```
30-
3123
Or Text Field:
32-
3324
```php
3425

3526
use NormanHuth\BBCode\BB;
@@ -41,7 +32,6 @@ BB::make(__('Name'), 'name')->codes([
4132
```
4233

4334
### Don't always show the content of textarea fields inside Nova
44-
4535
```php
4636
BBCode::make(__('Name'), 'name')->codes([
4737
'{author}' => __('This code insert the author'),
@@ -50,7 +40,6 @@ BBCode::make(__('Name'), 'name')->codes([
5040
```
5141

5242
### Change button class
53-
5443
```php
5544
BBCode::make(__('Name'), 'name')->codes([
5645
'{author}' => __('This code insert the author'),
@@ -59,13 +48,10 @@ BBCode::make(__('Name'), 'name')->codes([
5948
```
6049

6150
### Add inline style to the button
62-
6351
```php
6452
BBCode::make(__('Name'), 'name')->codes([
6553
'{author}' => __('This code insert the author'),
6654
'{date}' => __('This code insert the date'),
6755
])->btnStyle('margin-bottom: 1rem'),
6856
```
6957

70-
---
71-
[![More Laravel Nova Packages](https://raw.githubusercontent.com/Muetze42/asset-repo/main/svg/more-laravel-nova-packages.svg)](https://huth.it/nova-packages)

composer.json

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
{
22
"name": "norman-huth/nova-bbcode-textarea",
3-
"description": "With this Nova field you have the Textarea or Text with BBCodes.",
3+
"description": "Textarea with BBCodes. A Laravel Nova field.",
44
"keywords": [
55
"laravel",
6-
"nova",
7-
"larave-nova",
8-
"nova-fields",
9-
"nova-4"
6+
"nova"
107
],
118
"homepage": "https://github.com/Muetze42/nova-bbcode-textarea",
12-
"support": {
13-
"issues": "https://github.com/Muetze42/nova-bbcode-textarea/issues",
14-
"source": "https://github.com/Muetze42/nova-bbcode-textarea",
15-
"docs": "https://github.com/Muetze42/nova-bbcode-textarea#readme"
16-
},
179
"authors": [
1810
{
1911
"name": "Norman Huth",
12+
"email": "[email protected]",
2013
"homepage": "https://huth.it"
2114
}
2215
],
2316
"license": "MIT",
2417
"require": {
25-
"php": "^8.0"
18+
"php": "^8.0",
19+
"laravel/nova": "^4.5.2"
2620
},
2721
"autoload": {
2822
"psr-4": {

dist/js/field.js

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/components/bb/FormField.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
</template>
3434

3535
<script>
36-
import {DependentFormField, HandlesValidationErrors} from 'laravel-nova'
36+
import {DependentFormField, HandlesValidationErrors} from './../../mixins'
3737
3838
export default {
3939
mixins: [HandlesValidationErrors, DependentFormField],

resources/js/components/bbcode/FormField.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</template>
2323

2424
<script>
25-
import {DependentFormField, HandlesValidationErrors} from 'laravel-nova'
25+
import {DependentFormField, HandlesValidationErrors} from './../../mixins'
2626
2727
export default {
2828
mixins: [HandlesValidationErrors, DependentFormField],
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
import { CancelToken } from 'axios'
2+
import debounce from 'lodash/debounce'
3+
import forIn from 'lodash/forIn'
4+
import get from 'lodash/get'
5+
import identity from 'lodash/identity'
6+
import isNil from 'lodash/isNil'
7+
import pickBy from 'lodash/pickBy'
8+
import FormField from './FormField'
9+
import { mapProps } from './propTypes'
10+
11+
export default {
12+
extends: FormField,
13+
props: mapProps([
14+
'shownViaNewRelationModal',
15+
'field',
16+
'viaResource',
17+
'viaResourceId',
18+
'viaRelationship',
19+
'resourceName',
20+
'resourceId',
21+
'relatedResourceName',
22+
'relatedResourceId',
23+
]),
24+
25+
data: () => ({
26+
debouncer: null,
27+
canceller: null,
28+
watchedFields: {},
29+
watchedEvents: {},
30+
syncedField: null,
31+
pivot: false,
32+
editMode: 'create',
33+
}),
34+
35+
created() {
36+
this.debouncer = debounce(callback => callback(), 50)
37+
},
38+
39+
mounted() {
40+
if (this.relatedResourceName !== '' && !isNil(this.relatedResourceName)) {
41+
this.pivot = true
42+
43+
if (this.relatedResourceId !== '' && !isNil(this.relatedResourceId)) {
44+
this.editMode = 'update-attached'
45+
} else {
46+
this.editMode = 'attach'
47+
}
48+
} else {
49+
if (this.resourceId !== '' && !isNil(this.resourceId)) {
50+
this.editMode = 'update'
51+
}
52+
}
53+
54+
if (this.dependsOn.length > 0) {
55+
this.dependsOn.forEach(dependsOn => {
56+
this.watchedEvents[dependsOn] = value => {
57+
this.watchedFields[dependsOn] = value
58+
59+
this.debouncer(() => this.syncField())
60+
}
61+
62+
Nova.$on(
63+
this.getFieldAttributeChangeEventName(dependsOn),
64+
this.watchedEvents[dependsOn]
65+
)
66+
})
67+
}
68+
},
69+
70+
beforeUnmount() {
71+
if (this.dependsOn.length > 0) {
72+
forIn(this.watchedEvents, (event, dependsOn) => {
73+
Nova.$off(this.getFieldAttributeChangeEventName(event.dependsOn), event)
74+
})
75+
}
76+
},
77+
78+
methods: {
79+
/*
80+
* Set the initial value for the field
81+
*/
82+
setInitialValue() {
83+
this.value = !(
84+
this.currentField.value === undefined ||
85+
this.currentField.value === null
86+
)
87+
? this.currentField.value
88+
: this.value
89+
},
90+
91+
syncField() {
92+
if (this.canceller !== null) this.canceller()
93+
94+
Nova.request()
95+
.patch(this.syncFieldEndpoint, this.watchedFields, {
96+
params: pickBy(
97+
{
98+
editing: true,
99+
editMode: this.editMode,
100+
viaResource: this.viaResource,
101+
viaResourceId: this.viaResourceId,
102+
viaRelationship: this.viaRelationship,
103+
field: this.field.attribute,
104+
},
105+
identity
106+
),
107+
cancelToken: new CancelToken(canceller => {
108+
this.canceller = canceller
109+
}),
110+
})
111+
.then(response => {
112+
this.syncedField = response.data
113+
114+
if (isNil(this.syncedField.value)) {
115+
this.syncedField.value = this.field.value
116+
} else {
117+
this.setInitialValue()
118+
}
119+
120+
this.onSyncedField()
121+
})
122+
},
123+
124+
onSyncedField() {
125+
//
126+
},
127+
},
128+
129+
computed: {
130+
/**
131+
* Determine if the field is in readonly mode
132+
*/
133+
currentField() {
134+
return this.syncedField || this.field
135+
},
136+
137+
/**
138+
* Determine if the field is in readonly mode
139+
*/
140+
currentlyIsReadonly() {
141+
if (this.syncedField !== null) {
142+
return Boolean(
143+
this.syncedField.readonly ||
144+
get(this.syncedField, 'extraAttributes.readonly')
145+
)
146+
}
147+
148+
return Boolean(
149+
this.field.readonly || get(this.field, 'extraAttributes.readonly')
150+
)
151+
},
152+
153+
dependsOn() {
154+
return this.field.dependsOn || []
155+
},
156+
157+
syncFieldEndpoint() {
158+
if (this.editMode === 'update-attached') {
159+
return `/nova-api/${this.resourceName}/${this.resourceId}/update-pivot-fields/${this.relatedResourceName}/${this.relatedResourceId}`
160+
} else if (this.editMode == 'attach') {
161+
return `/nova-api/${this.resourceName}/${this.resourceId}/creation-pivot-fields/${this.relatedResourceName}`
162+
} else if (this.editMode === 'update') {
163+
return `/nova-api/${this.resourceName}/${this.resourceId}/update-fields`
164+
}
165+
166+
return `/nova-api/${this.resourceName}/creation-fields`
167+
},
168+
},
169+
}

resources/js/mixins/FormEvents.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import isNil from 'lodash/isNil'
2+
import { mapProps } from './propTypes'
3+
4+
export default {
5+
props: {
6+
formUniqueId: {
7+
type: String,
8+
},
9+
},
10+
11+
methods: {
12+
emitFieldValue(attribute, value) {
13+
Nova.$emit(`${attribute}-value`, value)
14+
15+
if (this.hasFormUniqueId === true) {
16+
Nova.$emit(`${this.formUniqueId}-${attribute}-value`, value)
17+
}
18+
},
19+
20+
emitFieldValueChange(attribute, value) {
21+
Nova.$emit(`${attribute}-change`, value)
22+
23+
if (this.hasFormUniqueId === true) {
24+
Nova.$emit(`${this.formUniqueId}-${attribute}-change`, value)
25+
}
26+
},
27+
28+
/**
29+
* Get field attribue value event name.
30+
*/
31+
getFieldAttributeValueEventName(attribute) {
32+
return this.hasFormUniqueId === true
33+
? `${this.formUniqueId}-${attribute}-value`
34+
: `${attribute}-value`
35+
},
36+
37+
/**
38+
* Get field attribue value event name.
39+
*/
40+
getFieldAttributeChangeEventName(attribute) {
41+
return this.hasFormUniqueId === true
42+
? `${this.formUniqueId}-${attribute}-change`
43+
: `${attribute}-change`
44+
},
45+
},
46+
47+
computed: {
48+
/**
49+
* Determine if the field has Form Unique ID.
50+
*/
51+
hasFormUniqueId() {
52+
return !isNil(this.formUniqueId) && this.formUniqueId !== ''
53+
},
54+
55+
/**
56+
* Get field attribue value event name.
57+
*/
58+
fieldAttributeValueEventName() {
59+
return this.getFieldAttributeValueEventName(this.field.attribute)
60+
},
61+
62+
/**
63+
* Get field attribue value event name.
64+
*/
65+
fieldAttributeChangeEventName() {
66+
return this.getFieldAttributeChangeEventName(this.field.attribute)
67+
},
68+
},
69+
}

0 commit comments

Comments
 (0)