Skip to content
This repository was archived by the owner on Mar 7, 2024. It is now read-only.

Commit 9216d02

Browse files
authored
Merge pull request #3 from RingierIMU/master
Send through the ResourceId from Listener Field
2 parents d58db8b + 1e4e879 commit 9216d02

File tree

8 files changed

+38756
-135
lines changed

8 files changed

+38756
-135
lines changed

dist/js/field.js

+29,794-1
Large diffs are not rendered by default.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"laravel-nova": "^1.0"
1616
},
1717
"dependencies": {
18+
"numeral": "^2.0.6",
1819
"vue": "^2.5.0"
1920
}
2021
}
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,73 @@
11
<template>
2-
<default-field :field="field" :errors="errors">
3-
<template slot="field">
4-
<input
5-
:id="field.name"
6-
:type="this.field.type"
7-
class="w-full form-control form-input form-input-bordered"
8-
:class="errorClasses"
9-
:placeholder="field.name"
10-
:value="value"
11-
@input="setFieldAndMessage"
12-
/>
13-
</template>
14-
</default-field>
2+
<default-field :field="field" :errors="errors">
3+
<template slot="field">
4+
<input
5+
:id="field.name"
6+
:type="this.field.type"
7+
class="w-full form-control form-input form-input-bordered"
8+
:class="errorClasses"
9+
:placeholder="field.name"
10+
:value="value | moneyFormat(field.numberFormat)"
11+
@input="setFieldAndMessage"
12+
/>
13+
</template>
14+
</default-field>
1515
</template>
1616

1717
<script>
18-
import { FormField, HandlesValidationErrors } from 'laravel-nova'
18+
import { FormField, HandlesValidationErrors } from "laravel-nova";
19+
import numeral from "numeral";
1920
2021
export default {
21-
mixins: [FormField, HandlesValidationErrors],
22+
mixins: [FormField, HandlesValidationErrors],
2223
23-
props: ['resourceName', 'resourceId', 'field'],
24+
props: ["resourceName", "resourceId", "field"],
2425
25-
methods: {
26-
setFieldAndMessage(el) {
27-
const rawValue = el.target.value;
28-
let parsedValue = rawValue;
26+
methods: {
27+
setFieldAndMessage(el) {
28+
const rawValue = el.target.value;
29+
let parsedValue = rawValue;
2930
30-
if (this.field.type === 'number') {
31-
parsedValue = Number(rawValue)
32-
}
31+
if (this.field.type === "number") {
32+
parsedValue = Number(rawValue);
33+
}
3334
34-
Nova.$emit(this.field.broadcastTo, {
35-
'field_name': this.field.attribute,
36-
'value': parsedValue
37-
});
35+
Nova.$emit(this.field.broadcastTo, {
36+
field_name: this.field.attribute,
37+
value: parsedValue
38+
});
3839
39-
this.value = parsedValue;
40-
},
41-
42-
/*
43-
* Set the initial, internal value for the field.
44-
*/
45-
setInitialValue() {
46-
this.value = this.field.value || ''
47-
},
40+
this.value = parsedValue;
41+
},
4842
49-
/**
50-
* Fill the given FormData object with the field's internal value.
51-
*/
52-
fill(formData) {
53-
formData.append(this.field.attribute, this.value || '')
54-
},
43+
/*
44+
* Set the initial, internal value for the field.
45+
*/
46+
setInitialValue() {
47+
this.value = this.field.value || "";
48+
},
5549
56-
/**
57-
* Update the field's internal value.
58-
*/
59-
handleChange(value) {
60-
this.value = value
61-
},
50+
/**
51+
* Fill the given FormData object with the field's internal value.
52+
*/
53+
fill(formData) {
54+
formData.append(this.field.attribute, this.value || "");
6255
},
63-
}
64-
</script>
56+
57+
/**
58+
* Update the field's internal value.
59+
*/
60+
handleChange(value) {
61+
this.value = value;
62+
}
63+
},
64+
filters: {
65+
moneyFormat(number, format) {
66+
if (!format) {
67+
return number;
68+
}
69+
return numeral(number).format(format);
70+
}
71+
}
72+
};
73+
</script>
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<template>
2-
<panel-item :field="field" />
2+
<panel-item :field="field" />
33
</template>
44

55
<script>
66
export default {
7-
props: ['resource', 'resourceName', 'resourceId', 'field'],
8-
}
7+
props: ["resource", "resourceName", "resourceId", "field"]
8+
};
99
</script>
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,135 @@
11
<template>
2-
<default-field :field="field" :errors="errors">
3-
<template slot="field">
4-
<div class="relative flex items-stretch">
5-
<input
6-
:id="field.name"
7-
type="text"
8-
class="w-full form-control form-input form-input-bordered"
9-
:class="errorClasses"
10-
:placeholder="field.name"
11-
v-model="value"
12-
/>
13-
14-
<div class="absolute rotating text-80 flex justify-center items-center pin-y pin-r mr-3" v-show="calculating">
15-
<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>
16-
</div>
17-
</div>
18-
</template>
19-
</default-field>
2+
<default-field :field="field" :errors="errors">
3+
<template slot="field">
4+
<div class="relative flex items-stretch">
5+
<input
6+
:id="field.name"
7+
type="text"
8+
class="w-full form-control form-input form-input-bordered"
9+
:class="errorClasses"
10+
:placeholder="field.name"
11+
:value="value | moneyFormat(field.numberFormat)"
12+
@input="setFieldAndMessage"
13+
/>
14+
15+
<div
16+
class="absolute rotating text-80 flex justify-center items-center pin-y pin-r mr-3"
17+
v-show="calculating"
18+
>
19+
<svg class="w-4 h-4" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
20+
<path
21+
fill="currentColor"
22+
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"
23+
class
24+
/>
25+
</svg>
26+
</div>
27+
</div>
28+
</template>
29+
</default-field>
2030
</template>
2131

2232
<style lang="scss">
23-
@-webkit-keyframes rotating {
24-
from{
25-
transform: rotate(0deg);
26-
}
27-
to{
28-
transform: rotate(360deg);
29-
}
30-
}
31-
.rotating {
32-
animation: rotating 2s linear infinite;
33-
}
33+
@-webkit-keyframes rotating {
34+
from {
35+
transform: rotate(0deg);
36+
}
37+
to {
38+
transform: rotate(360deg);
39+
}
40+
}
41+
.rotating {
42+
animation: rotating 2s linear infinite;
43+
}
3444
</style>
3545

3646
<script>
37-
import { FormField, HandlesValidationErrors } from 'laravel-nova'
38-
import _ from 'lodash'
47+
import { FormField, HandlesValidationErrors } from "laravel-nova";
48+
import _ from "lodash";
49+
import numeral from "numeral";
3950
4051
export default {
41-
mixins: [FormField, HandlesValidationErrors],
52+
mixins: [FormField, HandlesValidationErrors],
53+
54+
props: ["resourceName", "resourceId", "field"],
55+
56+
created() {
57+
Nova.$on(this.field.listensTo, this.messageReceived);
58+
this.field_values["resourceId"] = parseInt(this.resourceId);
59+
},
60+
61+
data: () => ({
62+
calculating: false,
63+
field_values: {}
64+
}),
65+
66+
methods: {
67+
messageReceived(message) {
68+
this.field_values[message.field_name] = message.value;
69+
this.calculateValue();
70+
},
4271
43-
props: ['resourceName', 'resourceId', 'field'],
72+
setFieldAndMessage(el) {
73+
const rawValue = el.target.value;
74+
let parsedValue = rawValue;
4475
45-
created() {
46-
Nova.$on(this.field.listensTo, this.messageReceived)
76+
if (this.field.type === "number") {
77+
parsedValue = Number(rawValue);
78+
}
79+
80+
Nova.$emit(this.field.broadcastTo, {
81+
field_name: this.field.attribute,
82+
value: parsedValue
83+
});
84+
85+
this.value = parsedValue;
4786
},
4887
49-
data: () => ({
50-
calculating: false,
51-
field_values: {}
52-
}),
53-
54-
methods: {
55-
messageReceived(message) {
56-
this.field_values[message.field_name] = message.value;
57-
this.calculateValue()
58-
},
59-
60-
calculateValue: _.debounce(function () {
61-
this.calculating = true;
62-
63-
Nova.request().post(
64-
`/codebykyle/calculated-field/calculate/${this.resourceName}/${this.field.attribute}`,
65-
this.field_values
66-
).then((response) => {
67-
this.value = response.data.value;
68-
this.calculating = false;
69-
}).catch(() => {
70-
this.calculating = false;
71-
});
72-
}, 500),
73-
74-
/*
75-
* Set the initial, internal value for the field.
76-
*/
77-
setInitialValue() {
78-
this.value = this.field.value || ''
79-
},
80-
81-
/**
82-
* Fill the given FormData object with the field's internal value.
83-
*/
84-
fill(formData) {
85-
formData.append(this.field.attribute, this.value || '')
86-
},
87-
88-
/**
89-
* Update the field's internal value.
90-
*/
91-
handleChange(value) {
92-
this.value = value
93-
},
88+
calculateValue: _.debounce(function() {
89+
this.calculating = true;
90+
91+
Nova.request()
92+
.post(
93+
`/codebykyle/calculated-field/calculate/${this.resourceName}/${this.field.attribute}`,
94+
this.field_values
95+
)
96+
.then(response => {
97+
this.value = response.data.value;
98+
this.calculating = false;
99+
})
100+
.catch(() => {
101+
this.calculating = false;
102+
});
103+
}, 500),
104+
105+
/*
106+
* Set the initial, internal value for the field.
107+
*/
108+
setInitialValue() {
109+
this.value = this.field.value || "";
94110
},
95-
}
111+
112+
/**
113+
* Fill the given FormData object with the field's internal value.
114+
*/
115+
fill(formData) {
116+
formData.append(this.field.attribute, this.value || "");
117+
},
118+
119+
/**
120+
* Update the field's internal value.
121+
*/
122+
handleChange(value) {
123+
this.value = value;
124+
}
125+
},
126+
filters: {
127+
moneyFormat(number, format) {
128+
if (!format) {
129+
return number;
130+
}
131+
return numeral(number).format(format);
132+
}
133+
}
134+
};
96135
</script>

src/BroadcasterField.php

+13
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ public function setType($type) : Element
5050
]);
5151
}
5252

53+
/**
54+
* Allows us to set the format of the number according to numeral.js
55+
* @param $broadcastChannel
56+
* @return Element
57+
*/
58+
public function numberFormat($format) : Element
59+
{
60+
return $this->withMeta([
61+
'numberFormat' => $format
62+
]);
63+
}
64+
5365
/**
5466
* Tells the client side component which channel to broadcast on
5567
* @param $broadcastChannel
@@ -61,4 +73,5 @@ public function broadcastTo($broadcastChannel):Element
6173
'broadcastTo' => $broadcastChannel
6274
]);
6375
}
76+
6477
}

0 commit comments

Comments
 (0)