Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Adds a duplicate row button #88

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/collective/z3cform/datagridfield/datagridfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class DataGridField(MultiWidget):
schema.Object and an interface"""

allow_insert = True
allow_duplicate = True
allow_delete = True
allow_reorder = False
auto_append = True
Expand Down Expand Up @@ -290,6 +291,9 @@ class DataGridFieldObject(ObjectWidget):
def isInsertEnabled(self):
return self.__parent__.allow_insert

def isDuplicteEnabled(self):
return self.__parent__.allow_duplicate

def isDeleteEnabled(self):
return self.__parent__.allow_delete

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
</th>
</tal:block>
<th id="" class="header" tal:condition="view/allow_insert"> </th>
<th id="" class="header" tal:condition="view/allow_duplicate"> </th>
<th id="" class="header" tal:condition="view/allow_delete"> </th>
<th id="" class="header" tal:condition="view/allow_reorder"> </th>
<th id="" class="header" tal:condition="view/allow_reorder"> </th>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<!-- -->
</th>
<th id="" class="header" tal:condition="view/allow_insert"> </th>
<th id="" class="header" tal:condition="view/allow_duplicate"> </th>
<th id="" class="header" tal:condition="view/allow_delete"> </th>
<th id="" class="header" tal:condition="view/allow_reorder"> </th>
<th id="" class="header" tal:condition="view/allow_reorder"> </th>
Expand Down
18 changes: 18 additions & 0 deletions src/collective/z3cform/datagridfield/datagridfieldobject_input.pt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@
<span class="sr-only" i18n:translate="label_datagridwidget_manipulators_addrow">Add row</span>
</a>

</td>

<td class="datagridwidget-manipulator duplicate-row"
tal:condition="view/isDuplicteEnabled">

<a href=""
onclick="dataGridField2Functions.duplicateRowAfter(this); return false"
id="btn-duplicaterow"
class="btn btn-default"
title=""
aria-label="Duplicate row"
data-original-title="Duplicate row"
i18n:attributes="aria-label label_datagridwidget_manipulators_duplicaterow;
data-original-title label_datagridwidget_manipulators_duplicaterow">
<span class="glyphicon glyphicon-duplicate" aria-hidden="true"></span>
<span class="sr-only" i18n:translate="label_datagridwidget_manipulators_addrow">Duplicate row</span>
</a>

</td>
<td class="datagridwidget-manipulator delete-row"
tal:condition="view/isDeleteEnabled">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@
<span class="sr-only" i18n:translate="label_datagridwidget_manipulators_addrow">Add row</span>
</a>

</td>
<td class="datagridwidget-manipulator duplicate-row"
tal:condition="view/isDuplicteEnabled">

<a href=""
onclick="dataGridField2Functions.duplicateRowAfter(this); return false"
id="btn-duplicaterow"
class="btn btn-default"
title=""
aria-label="Duplicate row"
data-original-title="Duplicate row"
i18n:attributes="aria-label label_datagridwidget_manipulators_duplicaterow;
data-original-title label_datagridwidget_manipulators_duplicaterow">
<span class="glyphicon glyphicon-duplicate" aria-hidden="true"></span>
<span class="sr-only" i18n:translate="label_datagridwidget_manipulators_addrow">Duplicate row</span>
</a>

</td>
<td class="datagridwidget-manipulator delete-row"
tal:condition="view/isDeleteEnabled">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@

/* You cannot delete the auto-append row */
.auto-append > .datagridwidget-manipulator.delete-row a,
.auto-append > .datagridwidget-manipulator.duplicate-row a,
.auto-append > .datagridwidget-manipulator.move-up a,
.auto-append > .datagridwidget-manipulator.move-down a,
.auto-append > .datagridwidget-manipulator.insert-row a {
Expand Down
49 changes: 49 additions & 0 deletions src/collective/z3cform/datagridfield/static/datagridfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,55 @@ require([
this.updateOrderIndex(tbody, true);
dgf.trigger("afteraddrow", [dgf, newtr]);
};
/**
* Copys the current row after the target row.
*
* @param {Object} currnode DOM <tr>
*/
dataGridField2Functions.duplicateRowAfter = function(currnode) {
// fetch required data structure
var tbody = this.getParentByClass(currnode, "datagridwidget-body");
var dgf = $(dataGridField2Functions.getParentByClass(currnode, "datagridwidget-table-view"));
var thisRow = this.getParentRow(currnode);
var filteredRows = this.getVisibleRows(currnode);

// find all select2 and destroy them
// https://stackoverflow.com/a/17381913
var all_select2 = thisRow.find(".pat-select2");
for (let i = 0; i < all_select2.length; i++) {
$(all_select2[i]).select2('destroy');
}
//clone the row
var duplicatedRow = thisRow.clone();
dgf.trigger("beforeaddrow", [dgf, duplicatedRow]);

// If using auto-append we add the "real" row before AA
// We have a special case when there is only one visible in the gid
if (thisRow.hasClass('auto-append') && !thisRow.hasClass("minimum-row")) {
$(duplicatedRow).insertBefore(thisRow);
} else {
$(duplicatedRow).insertAfter(thisRow);
}

// Ensure minimum special behavior is no longer needed as we have now at least 2 rows
if(thisRow.hasClass("minimum-row")) {
this.supressEnsureMinimum(tbody);
}

// update orderindex hidden fields
this.updateOrderIndex(tbody, true);
dgf.trigger("afteraddrow", [dgf, duplicatedRow]);

//we must have to re-initialize select2
let existingRowSelects = thisRow.find(".pat-select2");
for (let i = 0; i < existingRowSelects.length; i++) {
$(existingRowSelects[i]).select2();
}
let duplicatedRowSelects = duplicatedRow.find(".pat-select2");
for (let i = 0; i < existingRowSelects.length; i++) {
$(duplicatedRowSelects[i]).select2();
}
};

/**
* Creates a new row.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
display: none;
}
}
>.datagridwidget-manipulator.duplicate-row {
img {
display: none;
}
}
}

.minimum-row {
Expand Down