Skip to content

Commit ff50481

Browse files
committed
Update and release v1
- Fix bugs - Add lang - Update readme
1 parent 382290e commit ff50481

14 files changed

+134
-83
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
insert_final_newline = true
6+
indent_style = space
7+
indent_size = 4
8+
trim_trailing_whitespace = true

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
Laravel Nova custom field for [Spatie Opening Hours](https://github.com/spatie/opening-hours)
44

5-
![screenshot](screenshot.png)
5+
![Screenshot Index](screenshot-index.png)
6+
7+
![Screenshot Form](screenshot-form.png)
68

79
## Installation
810

@@ -14,6 +16,13 @@ composer require sadekd/nova-opening-hours-field
1416

1517
## Usage
1618

19+
Laravel Migration
20+
21+
```php
22+
$table->json('opening_hours');
23+
```
24+
25+
1726
Laravel Model
1827

1928
```php
@@ -25,12 +34,7 @@ protected $casts = [
2534
Nova Resource
2635

2736
```php
28-
public function fields(Request $request)
29-
{
30-
return [
31-
ID::make(),
32-
NovaOpeningHoursField::make('opening_hours'),
33-
...
37+
NovaOpeningHoursField::make('opening_hours'),
3438
```
3539

3640
## TODO

composer.json

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"name": "sadekd/nova-opening-hours-field",
3-
"description": "A Laravel Nova field.",
3+
"description": "Laravel Nova custom field for Spatie Opening Hours",
44
"keywords": [
55
"laravel",
6-
"nova"
6+
"nova",
7+
"opening hours"
78
],
89
"license": "MIT",
910
"require": {
10-
"php": ">=7.1.0"
11+
"php": "^7.2"
1112
},
1213
"autoload": {
1314
"psr-4": {
@@ -17,13 +18,8 @@
1718
"extra": {
1819
"laravel": {
1920
"providers": [
20-
"Sadekd\\NovaOpeningHoursField\\FieldServiceProvider"
21+
"SadekD\\NovaOpeningHoursField\\FieldServiceProvider"
2122
]
2223
}
23-
},
24-
"config": {
25-
"sort-packages": true
26-
},
27-
"minimum-stability": "dev",
28-
"prefer-stable": true
24+
}
2925
}

dist/js/field.js

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

resources/js/components/DetailField.vue

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,33 @@
22
<panel-item :field="field">
33
<template slot="value">
44
<table class="table w-full">
5-
<tr v-for="day in weekDays">
6-
<td>{{ day }}</td>
7-
<td>
8-
{{ openingHours[day].join(', ') }}
9-
</td>
5+
<tr v-for="(intervals, dayNameAsKey) in openingHours">
6+
<td>{{ __(dayNameAsKey) }}</td>
7+
<td v-if="intervals.length">{{ intervals.join(', ') }}</td>
8+
<td v-else>{{ __('Closed')}}</td>
109
</tr>
1110
</table>
1211
</template>
1312
</panel-item>
1413
</template>
1514

1615
<script>
17-
import "../field"
18-
import {WEEKDAYS} from "../const";
16+
import {EMPTY_WEEK} from "../const";
1917
2018
export default {
19+
2120
props: ['resource', 'resourceName', 'resourceId', 'field'],
2221
2322
data: () => ({
24-
// openingHours: [],
25-
// exceptions: [],
26-
weekDays: WEEKDAYS
27-
})
23+
openingHours: {},
24+
// exceptions: {},
25+
}),
26+
27+
created() {
28+
this.field.value = this.field.value || {}
29+
30+
this.openingHours = {...EMPTY_WEEK, ..._.omit(this.field.value, 'exceptions')}
31+
// this.exceptions = this.field.value.exceptions || {}
32+
},
2833
}
2934
</script>

resources/js/components/FormField.vue

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22
<default-field :field="field" :errors="errors">
33
<template slot="field">
44
<table class="openingHours table w-full">
5-
<tr v-for="(intervals, dayNameIndex) in openingHours">
6-
<td>{{ dayNameIndex }}</td>
7-
<td>
5+
<tr v-for="(intervals, dayNameAsKey) in openingHours">
6+
<td>{{ __(dayNameAsKey) }}</td>
7+
<td v-if="intervals.length">
88
<div v-for="(interval, intervalIndex) in intervals" class="interval">
9-
<input class="form-control form-input form-input-bordered" v-model="openingHours[dayNameIndex][intervalIndex]">
10-
<!--<input class="form-control form-input form-input-bordered" type="time">-->
11-
<!-- - -->
12-
<!--<input class="form-control form-input form-input-bordered" type="time">-->
13-
<button class="btn btn-default btn-danger" @click.prevent="removeInterval(dayNameIndex, intervalIndex)">-
9+
<input class="form-control form-input form-input-bordered"
10+
v-model="openingHours[dayNameAsKey][intervalIndex]"
11+
pattern="^(2[0-3]|[01]?[0-9]):([0-5]?[0-9])-(2[0-3]|[01]?[0-9]):([0-5]?[0-9])$"
12+
required
13+
>
14+
<button class="btn btn-default btn-danger"
15+
@click.prevent="removeInterval(dayNameAsKey, intervalIndex)">-
1416
</button>
1517
</div>
1618
</td>
19+
<td v-else>{{ __('Closed')}}</td>
1720
<td>
18-
<button class="btn btn-default btn-primary" @click.prevent="addInterval(dayNameIndex)">+
21+
<button class="btn btn-default btn-primary" @click.prevent="addInterval(dayNameAsKey)">+
1922
</button>
2023
</td>
2124
</tr>
@@ -26,67 +29,43 @@
2629

2730
<script>
2831
import {FormField, HandlesValidationErrors} from 'laravel-nova'
29-
import {WEEKDAYS} from "../const";
30-
import * as _ from "lodash";
32+
import {EMPTY_WEEK} from "../const";
3133
3234
export default {
33-
mixins: [FormField, HandlesValidationErrors],
3435
35-
data: () => ({
36-
openingHours: [],
37-
exceptions: [],
38-
weekDays: WEEKDAYS
39-
}),
36+
mixins: [FormField, HandlesValidationErrors],
4037
4138
props: ['resourceName', 'resourceId', 'field'],
4239
43-
methods: {
44-
/*
45-
* Set the initial, internal value for the field.
46-
*/
47-
setInitialValue() {
48-
this.value = this.field.value || []
49-
50-
// console.log(this.field.value.(word => word.length > 6));
40+
data: () => ({
41+
openingHours: {},
42+
// exceptions: {},
43+
}),
5144
52-
this.openingHours = _.omit(this.value, ['exceptions'])
53-
this.exceptions = _.pick(this.value, ['exceptions'])
45+
created() {
46+
this.field.value = this.field.value || {}
5447
55-
// console.log(this)
56-
},
48+
this.openingHours = {...EMPTY_WEEK, ..._.omit(this.field.value, 'exceptions')}
49+
// this.exceptions = this.field.value.exceptions || {}
50+
},
5751
58-
/**
59-
* Fill the given FormData object with the field's internal value.
60-
*/
52+
methods: {
6153
fill(formData) {
62-
// formData.append(this.field.attribute, this.value || [])
63-
formData.append(
54+
formData.set(
6455
this.field.attribute,
6556
JSON.stringify({
6657
...this.openingHours,
67-
exceptions: this.exceptions,
68-
}) || [])
69-
},
70-
71-
/**
72-
* Update the field's internal value.
73-
*/
74-
handleChange(value) {
75-
this.value = value
58+
// exceptions: this.exceptions,
59+
}))
7660
},
7761
7862
addInterval(dayName) {
79-
this.openingHours[dayName.toLowerCase()].push("08:00-16:00")
80-
// this.handleChange(this.value)
63+
this.openingHours[dayName].push("08:00-16:00")
8164
},
8265
8366
removeInterval(dayName, index) {
84-
this.openingHours[dayName.toLowerCase()].splice(index, 1)
67+
this.openingHours[dayName].splice(index, 1)
8568
},
86-
87-
// changeInterval(dayName, index, value) {
88-
// this.openingHours[dayName.toLowerCase()][index] = value;
89-
// }
9069
},
9170
}
9271
</script>
Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,49 @@
11
<template>
2-
<index-boolean-field :field="field"/>
2+
<div :class="`text-${field.textAlign}`">
3+
<boolean-icon v-for="k in openingsForWeek" :key="k.name" :value="k.value" />
4+
</div>
35
</template>
46

57
<script>
8+
import {EMPTY_WEEK} from "../const";
9+
610
export default {
11+
712
props: ['resourceName', 'field'],
13+
14+
data: () => ({
15+
openingHours: {},
16+
}),
17+
18+
created() {
19+
this.field.value = this.field.value || {}
20+
21+
this.openingHours = {...EMPTY_WEEK, ..._.omit(this.field.value, 'exceptions')}
22+
},
23+
computed: {
24+
openingsForWeek() {
25+
return _.map(this.openingHours, (i, k) => {
26+
return {
27+
name: k,
28+
label: k,
29+
value: i.length > 0 || false,
30+
}
31+
})
32+
33+
// return {
34+
// value: _.map(this.openingHours, (i, k) => {
35+
// return {
36+
// [k]: i.length > 0 || false,
37+
// }
38+
// }),
39+
// options: _.map(this.openingHours, (i, k) => {
40+
// return {
41+
// name: k,
42+
// label: k,
43+
// }
44+
// })
45+
// };
46+
},
47+
},
848
}
949
</script>

resources/js/const.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
export const WEEKDAYS = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
1+
export const EMPTY_WEEK = {
2+
monday: [],
3+
tuesday: [],
4+
wednesday: [],
5+
thursday: [],
6+
friday: [],
7+
saturday: [],
8+
sunday: [],
9+
}

resources/lang/en.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"monday": "Monday",
3+
"tuesday": "Tuesday",
4+
"wednesday": "Wednesday",
5+
"thursday": "Thursday",
6+
"friday": "Friday",
7+
"saturday": "Saturday",
8+
"sunday": "Sunday",
9+
"closed": "Closed"
10+
}

screenshot-form.png

44.5 KB
Loading

screenshot-index.png

81.5 KB
Loading

screenshot.png

-70.5 KB
Binary file not shown.

src/FieldServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Sadekd\NovaOpeningHoursField;
3+
namespace SadekD\NovaOpeningHoursField;
44

55
use Laravel\Nova\Nova;
66
use Laravel\Nova\Events\ServingNova;
@@ -13,6 +13,7 @@ public function boot()
1313
Nova::serving(function (ServingNova $event) {
1414
Nova::script('nova-opening-hours-field', __DIR__ . '/../dist/js/field.js');
1515
Nova::style('nova-opening-hours-field', __DIR__ . '/../dist/css/field.css');
16+
Nova::translations(__DIR__ . '/../resources/lang/en.json');
1617
});
1718
}
1819

src/NovaOpeningHoursField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Sadekd\NovaOpeningHoursField;
3+
namespace SadekD\NovaOpeningHoursField;
44

55
use Laravel\Nova\Fields\Field;
66
use Laravel\Nova\Http\Requests\NovaRequest;

0 commit comments

Comments
 (0)