Skip to content

Commit

Permalink
Merge pull request #220 from camptocamp/GSC2CGIF-65-readonly-actions
Browse files Browse the repository at this point in the history
Show item actions in readonly form
  • Loading branch information
arnaud-morvan authored Jun 5, 2020
2 parents 642cb5d + 9202182 commit a35aed8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 38 deletions.
41 changes: 41 additions & 0 deletions c2cgeoform/templates/widgets/_actions.pt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<tal:loop tal:condition="exists: actions" tal:repeat="action actions">
<a tal:condition="not action.method()"
class="btn btn-default c2cgeoform-item-action ${action.name()} ${action.css_class()}"
href="${action.url()}">
<span tal:omit-tag="not action.icon()" class="${action.icon()}"></span>
${action.label()}</a>
<a tal:condition="action.method()"
class="btn btn-default c2cgeoform-item-action c2cgeoform-action-ajax ${action.name()} ${action.css_class()}"
href="#"
data-url="${action.url()}"
data-method="${action.method()}"
data-confirmation="${action.confirmation()}">
<span tal:omit-tag="not action.icon()" class="${action.icon()}"></span>
${action.label()}</a>
</tal:loop>

<script type="text/javascript">
$(function () {
$('#${formid} a.c2cgeoform-action-ajax').on('click', function(e) {
var execute = function() {
$.ajax({
url: $(this).data('url'),
type: $(this).data('method'),
success: function(data) {
window.location = data.redirect;
}
});
}.bind(this);

if ($(this).data('confirmation')) {
if (window.confirm($(this).data('confirmation'))) {
execute();
}
} else {
execute();
}

return false;
});
});
</script>
36 changes: 1 addition & 35 deletions c2cgeoform/templates/widgets/form.pt
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,7 @@
</tal:loop>

<div class="pull-right" i18n:domain="c2cgeoform">
<tal:loop tal:condition="exists: actions" tal:repeat="action actions">
<a tal:condition="not action.confirmation()"
class="btn btn-default c2cgeoform-item-action ${action.name()} ${action.css_class()}"
href="${action.url()}">
<span tal:omit-tag="not action.icon()" class="${action.icon()}"></span>
${action.label()}</a>
<a tal:condition="action.confirmation()"
class="btn btn-default c2cgeoform-item-action c2cgeoform-action-ajax ${action.name()} ${action.css_class()}"
href="#"
data-url="${action.url()}"
data-method="${action.method()}"
data-confirmation="${action.confirmation()}">
<span tal:omit-tag="not action.icon()" class="${action.icon()}"></span>
${action.label()}</a>
</tal:loop>
<tal metal:use-macro="load:_actions.pt"></tal>
</div>
</div>
</div>
Expand Down Expand Up @@ -141,25 +127,5 @@ $(function () {
$('#${field.formid} button[type=submit]').click(function (e) {
$(window).off('beforeunload')
});

$('a.c2cgeoform-action-ajax').on('click', function(e) {
var execute = function() {
$.ajax({
url: $(this).data('url'),
type: $(this).data('method'),
success: function(data) {
window.location = data.redirect;
}
});
}.bind(this);

if ($(this).data('confirmation')) {
if (window.confirm($(this).data('confirmation'))) {
execute();
}
} else {
execute();
}
});
});
</script>
6 changes: 6 additions & 0 deletions c2cgeoform/templates/widgets/readonly/form.pt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
</fieldset>

</div>

<div class="panel-footer clearfix" tal:condition="actions">
<div class="pull-right" i18n:domain="c2cgeoform">
<tal metal:use-macro="load:_actions.pt"></tal>
</div>
</div>
</div>

</form>
6 changes: 3 additions & 3 deletions c2cgeoform/views/abstract_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def _grid_item_actions(self, item):
'c2cgeoform_item',
id=getattr(item, self._id_field))}

def _item_actions(self, item):
def _item_actions(self, item, readonly=False):
actions = []

if inspect(item).persistent and self._model_config().get('duplicate', False):
Expand All @@ -382,7 +382,7 @@ def _item_actions(self, item):
'c2cgeoform_item_duplicate',
id=getattr(item, self._id_field))))

if inspect(item).persistent:
if inspect(item).persistent and not readonly:
actions.append(ItemAction(
name='delete',
label=_('Delete'),
Expand All @@ -405,7 +405,7 @@ def edit(self, schema=None, readonly=False):
dict_.update(self._request.GET)
kwargs = {
"request": self._request,
"actions": self._item_actions(obj),
"actions": self._item_actions(obj, readonly=readonly),
"readonly": readonly,
"obj": obj,
}
Expand Down

0 comments on commit a35aed8

Please sign in to comment.