|
1 | 1 | <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> |
20 | 30 | </template> |
21 | 31 |
|
22 | 32 | <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 | +} |
34 | 44 | </style> |
35 | 45 |
|
36 | 46 | <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"; |
39 | 50 |
|
40 | 51 | 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 | + }, |
42 | 71 |
|
43 | | - props: ['resourceName', 'resourceId', 'field'], |
| 72 | + setFieldAndMessage(el) { |
| 73 | + const rawValue = el.target.value; |
| 74 | + let parsedValue = rawValue; |
44 | 75 |
|
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; |
47 | 86 | }, |
48 | 87 |
|
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 || ""; |
94 | 110 | }, |
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 | +}; |
96 | 135 | </script> |
0 commit comments