Added support for `dependsOn` with relationships (#3539)

Credit to @tobias-kuendig. Added $.oc.relationBehavior.changed function. To keep track of changes made using the relation controller this new "changed" function is called every time a record is created, added, removed or deleted. The function triggers the change.oc.formwidget event on the form field that belongs to this relation controller so other form fields are notified about the changes.
This commit is contained in:
Tobias Kündig 2018-12-03 17:08:00 +01:00 committed by Luke Towers
parent 18a8e2f840
commit 7a277b4b9c
6 changed files with 20 additions and 1 deletions

View File

@ -59,6 +59,15 @@
})
}
/*
* This function is called every time a record is created, added, removed
* or deleted using the relation widget. It triggers the change.oc.formwidget
* event to notify other elements on the page about the changed form state.
*/
this.changed = function(relationId, event) {
$('[data-field-name="' + relationId + '"]').trigger('change.oc.formwidget', {event: event});
}
/*
* This function transfers the supplied variables as hidden form inputs,
* to any popup that is spawned within the supplied container. The spawned

View File

@ -3,6 +3,7 @@
class="btn btn-sm btn-secondary oc-icon-trash-o"
data-request="onRelationButtonDelete"
data-request-confirm="<?= e(trans('backend::lang.relation.delete_confirm')) ?>"
data-request-success="$.oc.relationBehavior.changed('<?= e($this->vars['relationField']) ?>', 'deleted')"
data-stripe-load-indicator>
<?= e(trans('backend::lang.relation.delete')) ?>
</button>
@ -15,6 +16,7 @@
disabled="disabled"
data-request="onRelationButtonDelete"
data-request-confirm="<?= e(trans('backend::lang.relation.delete_confirm')) ?>"
data-request-success="$.oc.relationBehavior.changed('<?= e($this->vars['relationField']) ?>', 'deleted')"
data-trigger-action="enable"
data-trigger="#<?= $this->relationGetId('view') ?> .control-list input[type=checkbox]"
data-trigger-condition="checked"

View File

@ -2,6 +2,7 @@
<button
class="btn btn-sm btn-secondary oc-icon-minus"
data-request="onRelationButtonRemove"
data-request-success="$.oc.relationBehavior.changed('<?= e($this->vars['relationField']) ?>', 'removed')"
data-stripe-load-indicator>
<?= e(trans('backend::lang.relation.remove')) ?>
</button>
@ -13,6 +14,7 @@
})"
disabled="disabled"
data-request="onRelationButtonRemove"
data-request-success="$.oc.relationBehavior.changed('<?= e($this->vars['relationField']) ?>', 'removed')"
data-trigger-action="enable"
data-trigger="#<?= $this->relationGetId('view') ?> .control-list input[type=checkbox]"
data-trigger-condition="checked"

View File

@ -2,6 +2,7 @@
href="javascript:;"
class="btn btn-sm btn-secondary oc-icon-unlink"
data-request="onRelationButtonUnlink"
data-request-success="$.oc.relationBehavior.changed('<?= e($this->vars['relationField']) ?>', 'removed')"
data-request-confirm="<?= e(trans('backend::lang.relation.unlink_confirm')) ?>"
data-stripe-load-indicator>
<?= e(trans('backend::lang.relation.unlink')) ?>

View File

@ -47,7 +47,11 @@
<?php else: ?>
<?= Form::ajax('onRelationManageCreate', ['data-popup-load-indicator' => true, 'sessionKey' => $newSessionKey]) ?>
<?= Form::ajax('onRelationManageCreate', [
'data-popup-load-indicator' => true,
'data-request-success' => "$.oc.relationBehavior.changed('" . e($this->vars['relationField']) . "', 'created')",
'sessionKey' => $newSessionKey
]) ?>
<!-- Passable fields -->
<input type="hidden" name="_relation_field" value="<?= $relationField ?>" />

View File

@ -21,6 +21,7 @@
class="btn btn-primary"
data-request="onRelationManageAdd"
data-dismiss="popup"
data-request-success="$.oc.relationBehavior.changed('<?= e($this->vars['relationField']) ?>', 'added')"
data-stripe-load-indicator>
<?= e(trans('backend::lang.relation.add_selected')) ?>
</button>