1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-13 02:04:35 +02:00

Add support for PageTable non-editable value rendering per processwire/processwire-issues#1274

This commit is contained in:
Ryan Cramer
2021-07-23 14:04:49 -04:00
parent 24fc229be5
commit 9b473adf41
4 changed files with 60 additions and 30 deletions

View File

@@ -20,7 +20,7 @@
opacity: 1;
}
.InputfieldPageTable td:not(:last-child) {
.InputfieldPageTable .ui-sortable td:not(:last-child) {
cursor: move;
}

View File

@@ -120,28 +120,31 @@ function InputfieldPageTableUpdate($table) {
$input.val(value);
}
function InputfieldPageTableSortable($table) {
$table.find('tbody').sortable({
axis: 'y',
start: function(event, ui) {
var widths = [];
var n = 0;
$table.find('thead').find('th').each(function() {
widths[n] = $(this).width();
n++;
});
n = 0;
ui.helper.find('td').each(function() {
$(this).attr('width', widths[n]);
n++;
});
},
stop: function(event, ui) {
InputfieldPageTableUpdate($(this));
}
});
function InputfieldPageTableSortable($tables) {
$tables.each(function() {
var $table = $(this);
if($table.closest('.InputfieldRenderValueMode').length) return;
$table.find('tbody').sortable({
axis: 'y',
start: function(event, ui) {
var widths = [];
var n = 0;
$table.find('thead').find('th').each(function() {
widths[n] = $(this).width();
n++;
});
n = 0;
ui.helper.find('td').each(function() {
$(this).attr('width', widths[n]);
n++;
});
},
stop: function(event, ui) {
InputfieldPageTableUpdate($(this));
}
});
});
}
function InputfieldPageTableDelete() {

View File

@@ -1 +1 @@
function InputfieldPageTableDialog(){var $a=$(this);var url=$a.attr("data-url");var title=$a.attr("data-title");var closeOnSave=true;var $container=$(this).parents(".InputfieldPageTableContainer");var dialogPageID=0;var noclose=parseInt($container.attr("data-noclose"));var modalSettings={close:function(event,ui){if(dialogPageID>0){var ajaxURL=$container.attr("data-url")+"&InputfieldPageTableAdd="+dialogPageID;var sort=$container.siblings(".InputfieldPageTableSort").val();if(typeof sort!="undefined"&&sort.length)ajaxURL+="&InputfieldPageTableSort="+sort.replace(/\|/g,",");$.get(ajaxURL,function(data){$container.html(data);$container.find(".Inputfield").trigger("reloaded",["InputfieldPageTable"]);$container.effect("highlight",500,function(){var $table=$container.find("table");$table.find("tbody").css("overflow","visible");InputfieldPageTableSortable($table);var deleteIDs=$container.siblings("input.InputfieldPageTableDelete").eq(0).val().split("|");if(deleteIDs.length){for(var n=0;n<deleteIDs.length;n++){var deleteID=deleteIDs[n];$table.find("tr[data-id="+deleteID+"]").addClass("InputfieldPageTableDelete ui-state-error-text ui-state-disabled")}}})})}}};var $iframe=pwModalWindow(url,modalSettings,"large");var closeOnSaveReady=false;if($a.is(".InputfieldPageTableAdd"))closeOnSave=false;$iframe.load(function(){var buttons=[];var $icontents=$iframe.contents();var n=0;dialogPageID=$icontents.find("#Inputfield_id").val();$icontents.find("#wrap_Inputfield_template, #wrap_template, #wrap_parent_id").hide();$icontents.find("#_ProcessPageEditDelete, #_ProcessPageEditChildren").hide();closeOnSave=noclose==0&&$icontents.find("#ProcessPageAdd").length==0;if(closeOnSave&&closeOnSaveReady){if($icontents.find(".NoticeError, .NoticeWarning, .ui-state-error").length==0){if(typeof Notifications!="undefined"){var messages=[];$icontents.find(".NoticeMessage").each(function(){messages[messages.length]=$(this).text()});if(messages.length>0)setTimeout(function(){for(var i=0;i<messages.length;i++){Notifications.message(messages[i])}},500)}$iframe.dialog("close");return}else{}}$icontents.find("#content form button.ui-button[type=submit]").each(function(){var $button=$(this);var text=$button.text();var skip=false;for(var i=0;i<buttons.length;i++){if(buttons[i].text==text||text.length<1)skip=true}if(!skip){buttons[n]={text:text,class:$button.is(".ui-priority-secondary")?"ui-priority-secondary":"",click:function(){$button.click();if(closeOnSave)closeOnSaveReady=true;if(!noclose)closeOnSave=true}};n++}$button.hide()});$iframe.setButtons(buttons)});return false}function InputfieldPageTableUpdate($table){var value="";if(!$table.is("tbody"))$table=$table.find("tbody");$table.find("tr").each(function(){var pageID=$(this).attr("data-id");if(value.length>0)value+="|";value+=pageID});var $container=$table.parents(".InputfieldPageTableContainer");var $input=$container.siblings(".InputfieldPageTableSort");$input.val(value)}function InputfieldPageTableSortable($table){$table.find("tbody").sortable({axis:"y",start:function(event,ui){var widths=[];var n=0;$table.find("thead").find("th").each(function(){widths[n]=$(this).width();n++});n=0;ui.helper.find("td").each(function(){$(this).attr("width",widths[n]);n++})},stop:function(event,ui){InputfieldPageTableUpdate($(this))}})}function InputfieldPageTableDelete(){var $row=$(this).closest("tr");$row.toggleClass("InputfieldPageTableDelete ui-state-error-text ui-state-disabled");var ids="";$row.parents("tbody").children("tr").each(function(){var $tr=$(this);var id=$tr.attr("data-id");if($tr.is(".InputfieldPageTableDelete"))ids+=(ids.length>0?"|":"")+id});var $input=$(this).parents(".InputfieldPageTableContainer").siblings("input.InputfieldPageTableDelete");$input.val(ids);return false}$(document).ready(function(){$(document).on("click",".InputfieldPageTableAdd, .InputfieldPageTableEdit",InputfieldPageTableDialog);$(document).on("click","a.InputfieldPageTableDelete",InputfieldPageTableDelete);$(document).on("dblclick",".InputfieldPageTable .AdminDataTable td",function(){$(this).closest("tr").find(".InputfieldPageTableEdit").click()});InputfieldPageTableSortable($(".InputfieldPageTable table"));$(document).on("reloaded",".InputfieldPageTable",function(){InputfieldPageTableSortable($(this).find(".InputfieldPageTableContainer > table"))});$(document).on("click",".InputfieldPageTableOrphansAll",function(){var $checkboxes=$(this).closest(".InputfieldPageTableOrphans").find("input");$checkboxes.prop("checked",$checkboxes.eq(0).is(":checked")?false:true);return false})});
function InputfieldPageTableDialog(){var $a=$(this);var url=$a.attr("data-url");var title=$a.attr("data-title");var closeOnSave=true;var $container=$(this).parents(".InputfieldPageTableContainer");var dialogPageID=0;var noclose=parseInt($container.attr("data-noclose"));var modalSettings={close:function(event,ui){if(dialogPageID>0){var ajaxURL=$container.attr("data-url")+"&InputfieldPageTableAdd="+dialogPageID;var sort=$container.siblings(".InputfieldPageTableSort").val();if(typeof sort!="undefined"&&sort.length)ajaxURL+="&InputfieldPageTableSort="+sort.replace(/\|/g,",");$.get(ajaxURL,function(data){$container.html(data);$container.find(".Inputfield").trigger("reloaded",["InputfieldPageTable"]);$container.effect("highlight",500,function(){var $table=$container.find("table");$table.find("tbody").css("overflow","visible");InputfieldPageTableSortable($table);var deleteIDs=$container.siblings("input.InputfieldPageTableDelete").eq(0).val().split("|");if(deleteIDs.length){for(var n=0;n<deleteIDs.length;n++){var deleteID=deleteIDs[n];$table.find("tr[data-id="+deleteID+"]").addClass("InputfieldPageTableDelete ui-state-error-text ui-state-disabled")}}})})}}};var $iframe=pwModalWindow(url,modalSettings,"large");var closeOnSaveReady=false;if($a.is(".InputfieldPageTableAdd"))closeOnSave=false;$iframe.load(function(){var buttons=[];var $icontents=$iframe.contents();var n=0;dialogPageID=$icontents.find("#Inputfield_id").val();$icontents.find("#wrap_Inputfield_template, #wrap_template, #wrap_parent_id").hide();$icontents.find("#_ProcessPageEditDelete, #_ProcessPageEditChildren").hide();closeOnSave=noclose==0&&$icontents.find("#ProcessPageAdd").length==0;if(closeOnSave&&closeOnSaveReady){if($icontents.find(".NoticeError, .NoticeWarning, .ui-state-error").length==0){if(typeof Notifications!="undefined"){var messages=[];$icontents.find(".NoticeMessage").each(function(){messages[messages.length]=$(this).text()});if(messages.length>0)setTimeout(function(){for(var i=0;i<messages.length;i++){Notifications.message(messages[i])}},500)}$iframe.dialog("close");return}else{}}$icontents.find("#content form button.ui-button[type=submit]").each(function(){var $button=$(this);var text=$button.text();var skip=false;for(var i=0;i<buttons.length;i++){if(buttons[i].text==text||text.length<1)skip=true}if(!skip){buttons[n]={text:text,class:$button.is(".ui-priority-secondary")?"ui-priority-secondary":"",click:function(){$button.click();if(closeOnSave)closeOnSaveReady=true;if(!noclose)closeOnSave=true}};n++}$button.hide()});$iframe.setButtons(buttons)});return false}function InputfieldPageTableUpdate($table){var value="";if(!$table.is("tbody"))$table=$table.find("tbody");$table.find("tr").each(function(){var pageID=$(this).attr("data-id");if(value.length>0)value+="|";value+=pageID});var $container=$table.parents(".InputfieldPageTableContainer");var $input=$container.siblings(".InputfieldPageTableSort");$input.val(value)}function InputfieldPageTableSortable($tables){$tables.each(function(){var $table=$(this);if($table.closest(".InputfieldRenderValueMode").length)return;$table.find("tbody").sortable({axis:"y",start:function(event,ui){var widths=[];var n=0;$table.find("thead").find("th").each(function(){widths[n]=$(this).width();n++});n=0;ui.helper.find("td").each(function(){$(this).attr("width",widths[n]);n++})},stop:function(event,ui){InputfieldPageTableUpdate($(this))}})})}function InputfieldPageTableDelete(){var $row=$(this).closest("tr");$row.toggleClass("InputfieldPageTableDelete ui-state-error-text ui-state-disabled");var ids="";$row.parents("tbody").children("tr").each(function(){var $tr=$(this);var id=$tr.attr("data-id");if($tr.is(".InputfieldPageTableDelete"))ids+=(ids.length>0?"|":"")+id});var $input=$(this).parents(".InputfieldPageTableContainer").siblings("input.InputfieldPageTableDelete");$input.val(ids);return false}$(document).ready(function(){$(document).on("click",".InputfieldPageTableAdd, .InputfieldPageTableEdit",InputfieldPageTableDialog);$(document).on("click","a.InputfieldPageTableDelete",InputfieldPageTableDelete);$(document).on("dblclick",".InputfieldPageTable .AdminDataTable td",function(){$(this).closest("tr").find(".InputfieldPageTableEdit").click()});InputfieldPageTableSortable($(".InputfieldPageTable table"));$(document).on("reloaded",".InputfieldPageTable",function(){InputfieldPageTableSortable($(this).find(".InputfieldPageTableContainer > table"))});$(document).on("click",".InputfieldPageTableOrphansAll",function(){var $checkboxes=$(this).closest(".InputfieldPageTableOrphans").find("input");$checkboxes.prop("checked",$checkboxes.eq(0).is(":checked")?false:true);return false})});

View File

@@ -7,7 +7,7 @@
* Code by Ryan Cramer
* Sponsored by Avoine
*
* ProcessWire 3.x, Copyright 2016 by Ryan Cramer
* ProcessWire 3.x, Copyright 2021 by Ryan Cramer
* https://processwire.com
*
* @todo add renderValue support (perhaps delegating to Fieldtype::markupValue), likewise for repeaters.
@@ -29,7 +29,7 @@ class InputfieldPageTable extends Inputfield {
return array(
'title' => __('ProFields: Page Table', __FILE__), // Module Title
'summary' => __('Inputfield to accompany FieldtypePageTable', __FILE__), // Module Summary
'version' => 13,
'version' => 14,
'requires' => 'FieldtypePageTable'
);
}
@@ -66,6 +66,14 @@ class InputfieldPageTable extends Inputfield {
*/
protected $needsEditColumn = false;
/**
* True when in renderValue mode
*
* @var bool
*
*/
protected $renderValueMode = false;
/**
* Initialize and establish default values
*
@@ -133,6 +141,7 @@ class InputfieldPageTable extends Inputfield {
// render the table
$out = $this->renderTable($columns);
if($this->renderValueMode) return $out;
$editID = (int) $this->wire('input')->get('id');
if(!$editID && $this->wire('process') instanceof WirePageEditor) $editID = $this->wire('process')->getPage()->id;
@@ -178,6 +187,19 @@ class InputfieldPageTable extends Inputfield {
return $out;
}
/**
* Render non-editable value
*
* @return string
*
*/
public function ___renderValue() {
$this->renderValueMode = true;
$out = $this->render();
$this->renderValueMode = false;
return $out;
}
/**
* Render the outputted PageTable <table>
*
@@ -277,7 +299,8 @@ class InputfieldPageTable extends Inputfield {
$out .= "<th$attr>" . $this->wire('sanitizer')->entities($label) . "</th>";
}
$out .= "<th>&nbsp;</th></tr></thead>";
if(!$this->renderValueMode) $out .= "<th>&nbsp;</th>";
$out .= "</tr></thead>";
return $out;
}
@@ -306,7 +329,7 @@ class InputfieldPageTable extends Inputfield {
foreach($rows as $key => $row) {
list($s1, $s2) = explode('>', $row, 2);
$item = $items[$key];
$rows[$key] = "$s1><td>" . $this->renderItemLink($item, "<i class='fa fa-edit'></i>") . "</td>$s2";
$rows[$key] = "$s1><td>" . $this->renderItemLink($item, wireIconMarkup('edit')) . "</td>$s2";
}
}
@@ -328,12 +351,16 @@ class InputfieldPageTable extends Inputfield {
$n = 0;
foreach($columns as $column => $width) {
$linkURL = $n++ ? '' : $this->getItemEditURL($item);
$linkURL = ($n++ && !$this->renderValueMode ? '' : $this->getItemEditURL($item));
$out .= $this->renderTableCol($item, $fields, $column, $width, $linkURL);
}
// append a delete column/link
$out .= "<td><a class='InputfieldPageTableDelete' href='#'><i class='fa fa-trash-o'></i></a></td>";
if(!$this->renderValueMode) {
$a = "<a class='InputfieldPageTableDelete' href='#'>" . wireIconMarkup('trash-o') . "</a>";
if(!$item->deletable()) $a = "&nbsp;";
$out .= "<td>$a</td>";
}
// wrap the row in a <tr>
$class = '';
@@ -358,7 +385,7 @@ class InputfieldPageTable extends Inputfield {
*/
protected function renderTableCol(Page $item, array $fields, $column, $width, $linkURL = '') {
$out = $this->getItemValue($item, $fields, $column);
if($linkURL) {
if($linkURL && !$this->renderValueMode) {
if(stripos($out, '<a ') !== false || stripos($out, '<li') !== false || !strlen($out)) {
// table will need a separate edit column since this item doesn't work as a link
$this->needsEditColumn = true;