Skip to content

Commit 555eb8b

Browse files
authored
Merge pull request #9 from kirschbaum-development/feature/separate-oxbservers-for-relationships
Add separate observers for relationships
2 parents ea82921 + 9d18693 commit 555eb8b

18 files changed

Lines changed: 601 additions & 245 deletions

dist/js/field.js

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
361361
//
362362
//
363363
//
364+
//
365+
//
364366

365367

366368

@@ -495,19 +497,23 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < argument
495497
//
496498
//
497499
//
500+
//
501+
//
498502

499503
/* harmony default export */ __webpack_exports__["default"] = ({
500504
name: "RelationshipDetailItem",
501505

502506
props: {
503-
'value': Object,
504-
'settings': Object,
505-
'collapsed': {
507+
value: Object,
508+
settings: Object,
509+
collapsed: {
506510
type: Boolean,
507511
default: false
508512
},
509-
'label': String,
510-
'id': Number
513+
label: String,
514+
id: Number,
515+
modelId: Number,
516+
modelKey: String
511517
},
512518

513519
data: function data() {
@@ -641,7 +647,11 @@ var render = function() {
641647
[
642648
_c("detail-" + parameter.meta.component, {
643649
tag: "component",
644-
attrs: { field: parameter.meta }
650+
attrs: {
651+
field: parameter.meta,
652+
"resource-id": _vm.modelId,
653+
"resource-name": _vm.modelKey
654+
}
645655
})
646656
],
647657
1
@@ -696,6 +706,8 @@ var render = function() {
696706
attrs: {
697707
id: index,
698708
value: item,
709+
"model-id": _vm.field.models[index] || 0,
710+
"model-key": _vm.field.modelKey,
699711
label: _vm.field.singularLabel,
700712
settings: _vm.field.settings,
701713
collapsed: _vm.collapsed
@@ -902,11 +914,15 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
902914
fillValueFromChildren: function fillValueFromChildren(formData) {
903915
var _this2 = this;
904916

905-
_(this.$refs).each(function (item) {
906-
if (item && Array.isArray(item) && item[0]) {
907-
item[0].fill(formData, _this2.field.attribute);
908-
}
909-
});
917+
if (!_.isEmpty(this.$refs[0])) {
918+
_(this.$refs).each(function (item) {
919+
if (item && Array.isArray(item) && item[0]) {
920+
item[0].fill(formData, _this2.field.attribute);
921+
}
922+
});
923+
} else {
924+
formData.append(this.field.attribute, []);
925+
}
910926
},
911927

912928
/**
@@ -17028,7 +17044,7 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < argument
1702817044
return _extends({
1702917045
'options': {}
1703017046
}, _this.value[attrib].meta, {
17031-
'attribute': _this.value[attrib].meta.component === "file-field" ? attrib : _this.field.attribute + '_' + _this.id + '_' + attrib, // This is needed to enable delete link for file without triggering duplicate id warning
17047+
'attribute': _this.value[attrib].meta.component === "file-field" ? attrib + '?' + _this.id : _this.field.attribute + '_' + _this.id + '_' + attrib, // This is needed to enable delete link for file without triggering duplicate id warning
1703217048
'name': _this.field.attribute + '[' + _this.id + '][' + attrib + ']',
1703317049
'deletable': _this.modelId > 0, // Hide delete button if model Id is not present, i.e. new model
1703417050
'attrib': attrib
@@ -17045,9 +17061,9 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < argument
1704517061
_(_this2.$refs).each(function (item) {
1704617062
if (item[0].field.component === 'file-field') {
1704717063
if (item[0].file) {
17048-
formData.append(item[0].field.attribute, item[0].file, item[0].fileName);
17064+
formData.append(item[0].field.attrib, item[0].file, item[0].fileName);
1704917065
} else if (item[0].value) {
17050-
formData.append(item[0].field.attribute, String(item[0].value));
17066+
formData.append(item[0].field.attrib, String(item[0].value));
1705117067
}
1705217068
} else if (item[0].field.component === 'boolean-field') {
1705317069
formData.append(item[0].field.attribute, item[0].trueValue);
@@ -17138,47 +17154,49 @@ var render = function() {
1713817154
])
1713917155
]),
1714017156
_vm._v(" "),
17141-
_c("div", { staticClass: "w-1/4 text-right" }, [
17142-
_c(
17143-
"button",
17144-
{
17145-
staticClass:
17146-
"btn btn-default btn-icon btn bg-transparent hover:bg-danger text-danger hover:text-white border border-danger hover:border-transparent inline-flex items-center relative mr-3",
17147-
attrs: { title: "Delete" },
17148-
on: {
17149-
click: function($event) {
17150-
$event.preventDefault()
17151-
return _vm.removeItem()
17152-
}
17153-
}
17154-
},
17155-
[
17157+
_vm.field.deletable
17158+
? _c("div", { staticClass: "w-1/4 text-right" }, [
1715617159
_c(
17157-
"svg",
17160+
"button",
1715817161
{
17159-
staticClass: "fill-current text-0",
17160-
attrs: {
17161-
xmlns: "http://www.w3.org/2000/svg",
17162-
width: "20",
17163-
height: "20",
17164-
viewBox: "0 0 20 20",
17165-
"aria-labelledby": "delete",
17166-
role: "presentation"
17162+
staticClass:
17163+
"btn btn-default btn-icon btn bg-transparent hover:bg-danger text-danger hover:text-white border border-danger hover:border-transparent inline-flex items-center relative mr-3",
17164+
attrs: { title: "Delete" },
17165+
on: {
17166+
click: function($event) {
17167+
$event.preventDefault()
17168+
return _vm.removeItem()
17169+
}
1716717170
}
1716817171
},
1716917172
[
17170-
_c("path", {
17171-
attrs: {
17172-
"fill-rule": "nonzero",
17173-
d:
17174-
"M6 4V2a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2h5a1 1 0 0 1 0 2h-1v12a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V6H1a1 1 0 1 1 0-2h5zM4 6v12h12V6H4zm8-2V2H8v2h4zM8 8a1 1 0 0 1 1 1v6a1 1 0 0 1-2 0V9a1 1 0 0 1 1-1zm4 0a1 1 0 0 1 1 1v6a1 1 0 0 1-2 0V9a1 1 0 0 1 1-1z"
17175-
}
17176-
})
17173+
_c(
17174+
"svg",
17175+
{
17176+
staticClass: "fill-current text-0",
17177+
attrs: {
17178+
xmlns: "http://www.w3.org/2000/svg",
17179+
width: "20",
17180+
height: "20",
17181+
viewBox: "0 0 20 20",
17182+
"aria-labelledby": "delete",
17183+
role: "presentation"
17184+
}
17185+
},
17186+
[
17187+
_c("path", {
17188+
attrs: {
17189+
"fill-rule": "nonzero",
17190+
d:
17191+
"M6 4V2a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2h5a1 1 0 0 1 0 2h-1v12a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V6H1a1 1 0 1 1 0-2h5zM4 6v12h12V6H4zm8-2V2H8v2h4zM8 8a1 1 0 0 1 1 1v6a1 1 0 0 1-2 0V9a1 1 0 0 1 1-1zm4 0a1 1 0 0 1 1 1v6a1 1 0 0 1-2 0V9a1 1 0 0 1 1-1z"
17192+
}
17193+
})
17194+
]
17195+
)
1717717196
]
1717817197
)
17179-
]
17180-
)
17181-
])
17198+
])
17199+
: _vm._e()
1718217200
]),
1718317201
_vm._v(" "),
1718417202
_vm._l(_vm.fields, function(field, attrib) {

resources/js/components/DetailField.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
:id="index"
1515
:key="index"
1616
:value="item"
17+
:model-id="field.models[index]||0"
18+
:model-key="field.modelKey"
1719
:label="field.singularLabel"
1820
:settings="field.settings"
1921
:collapsed="collapsed"

resources/js/components/FormField.vue

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,15 @@ export default {
114114
},
115115
116116
fillValueFromChildren: function(formData) {
117-
_(this.$refs).each(item => {
118-
if(item && Array.isArray(item) && item[0]){
119-
item[0].fill(formData, this.field.attribute);
120-
}
121-
});
117+
if(!_.isEmpty(this.$refs[0])){
118+
_(this.$refs).each(item => {
119+
if(item && Array.isArray(item) && item[0]){
120+
item[0].fill(formData, this.field.attribute);
121+
}
122+
});
123+
}else{
124+
formData.append(this.field.attribute, []);
125+
}
122126
},
123127
124128
/**

resources/js/components/RelationshipDetailItem.vue

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
<component
4848
:is="'detail-' + parameter.meta.component"
4949
:field="parameter.meta"
50+
:resource-id="modelId"
51+
:resource-name="modelKey"
5052
/>
5153
</div>
5254
</div>
@@ -59,14 +61,16 @@
5961
name: "RelationshipDetailItem",
6062
6163
props: {
62-
'value': Object,
63-
'settings': Object,
64-
'collapsed':{
64+
value: Object,
65+
settings: Object,
66+
collapsed:{
6567
type: Boolean,
6668
default: false
6769
},
68-
'label': String,
69-
'id': Number
70+
label: String,
71+
id: Number,
72+
modelId: Number,
73+
modelKey: String,
7074
},
7175
7276
data: function (){
@@ -97,8 +101,6 @@
97101
},
98102
},
99103
100-
101-
102104
watch : {
103105
'collapsed' : function (){
104106
this.isCollapsed = this.collapsed;

resources/js/components/RelationshipFormItem.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
{{ field.singularLabel }} {{ id+1 }}
2323
</h4>
2424
</div>
25-
<div class="w-1/4 text-right">
25+
<div v-if="field.deletable" class="w-1/4 text-right">
2626
<button
2727
class="btn btn-default btn-icon btn bg-transparent hover:bg-danger text-danger hover:text-white border border-danger hover:border-transparent inline-flex items-center relative mr-3"
2828
title="Delete"
@@ -88,7 +88,7 @@
8888
...this.value[attrib].meta,
8989
...{
9090
'attribute': (this.value[attrib].meta.component === "file-field") ?
91-
attrib :
91+
attrib + '?' + this.id :
9292
this.field.attribute + '_' + this.id + '_' + attrib, // This is needed to enable delete link for file without triggering duplicate id warning
9393
'name': this.field.attribute + '[' + this.id + '][' + attrib + ']',
9494
'deletable': this.modelId > 0, // Hide delete button if model Id is not present, i.e. new model
@@ -107,9 +107,9 @@
107107
_(this.$refs).each(item => {
108108
if (item[0].field.component === 'file-field'){
109109
if (item[0].file){
110-
formData.append(item[0].field.attribute, item[0].file, item[0].fileName);
110+
formData.append(item[0].field.attrib, item[0].file, item[0].fileName);
111111
} else if (item[0].value){
112-
formData.append(item[0].field.attribute, String(item[0].value))
112+
formData.append(item[0].field.attrib, String(item[0].value))
113113
}
114114
} else if (item[0].field.component === 'boolean-field'){
115115
formData.append(item[0].field.attribute, item[0].trueValue);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace KirschbaumDevelopment\NovaInlineRelationship\Contracts;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
interface RelationshipObservable
8+
{
9+
/**
10+
* Handle updating event for the relationship
11+
*
12+
* @param Model $model
13+
* @param $attribute
14+
* @param $value
15+
* @return mixed
16+
*/
17+
public function updating(Model $model, $attribute, $value);
18+
19+
/**
20+
* Handle creating event for the relationship
21+
*
22+
* @param Model $model
23+
* @param $attribute
24+
* @param $value
25+
* @return mixed
26+
*/
27+
public function creating(Model $model, $attribute, $value);
28+
29+
/**
30+
* Handle created event for the relationship
31+
*
32+
* @param Model $model
33+
* @param $attribute
34+
* @param $value
35+
* @return mixed
36+
*/
37+
public function created(Model $model, $attribute, $value);
38+
}

src/Exceptions/UnsupportedRelationshipType.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
class UnsupportedRelationshipType extends InvalidArgumentException
88
{
99
/**
10+
* @param string $type
1011
* @param string $key
1112
*
1213
* @return UnsupportedRelationshipType
1314
*/
14-
public static function create(string $key)
15+
public static function create(string $type, string $key)
1516
{
16-
return new static(sprintf('Unsupported Relationship type for a key (%s) in array returned by getPropertyMap function. This package currently supports singular relationships only.', $key));
17+
return new static(sprintf('Unsupported Inline Relationship type (%s) for an attribute (%s).', $type, $key));
1718
}
1819
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace KirschbaumDevelopment\NovaInlineRelationship\Helpers;
4+
5+
class NovaInlineRelationshipHelper
6+
{
7+
/**
8+
* Returns Observer Classname for a relationship
9+
*
10+
* @param $relationship
11+
*
12+
* @return string
13+
*/
14+
public static function getObserver($relationship)
15+
{
16+
return '\\KirschbaumDevelopment\\NovaInlineRelationship\\Observers\\' . class_basename($relationship) . 'Observer';
17+
}
18+
}

0 commit comments

Comments
 (0)