From 9b473adf41199b264ffc4f660e0315e951d97726 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 23 Jul 2021 14:04:49 -0400 Subject: [PATCH] Add support for PageTable non-editable value rendering per processwire/processwire-issues#1274 --- .../InputfieldPageTable.css | 2 +- .../InputfieldPageTable.js | 45 ++++++++++--------- .../InputfieldPageTable.min.js | 2 +- .../InputfieldPageTable.module | 41 ++++++++++++++--- 4 files changed, 60 insertions(+), 30 deletions(-) diff --git a/wire/modules/Inputfield/InputfieldPageTable/InputfieldPageTable.css b/wire/modules/Inputfield/InputfieldPageTable/InputfieldPageTable.css index f3521555..3b2bc0e2 100644 --- a/wire/modules/Inputfield/InputfieldPageTable/InputfieldPageTable.css +++ b/wire/modules/Inputfield/InputfieldPageTable/InputfieldPageTable.css @@ -20,7 +20,7 @@ opacity: 1; } -.InputfieldPageTable td:not(:last-child) { +.InputfieldPageTable .ui-sortable td:not(:last-child) { cursor: move; } diff --git a/wire/modules/Inputfield/InputfieldPageTable/InputfieldPageTable.js b/wire/modules/Inputfield/InputfieldPageTable/InputfieldPageTable.js index 0d8ec6fe..7c09d098 100644 --- a/wire/modules/Inputfield/InputfieldPageTable/InputfieldPageTable.js +++ b/wire/modules/Inputfield/InputfieldPageTable/InputfieldPageTable.js @@ -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() { diff --git a/wire/modules/Inputfield/InputfieldPageTable/InputfieldPageTable.min.js b/wire/modules/Inputfield/InputfieldPageTable/InputfieldPageTable.min.js index c8c8214e..cf53e542 100644 --- a/wire/modules/Inputfield/InputfieldPageTable/InputfieldPageTable.min.js +++ b/wire/modules/Inputfield/InputfieldPageTable/InputfieldPageTable.min.js @@ -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;n0)setTimeout(function(){for(var i=0;i0)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})}); \ No newline at end of file +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;n0)setTimeout(function(){for(var i=0;i0)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})}); \ No newline at end of file diff --git a/wire/modules/Inputfield/InputfieldPageTable/InputfieldPageTable.module b/wire/modules/Inputfield/InputfieldPageTable/InputfieldPageTable.module index 19768fc5..e1dc511b 100644 --- a/wire/modules/Inputfield/InputfieldPageTable/InputfieldPageTable.module +++ b/wire/modules/Inputfield/InputfieldPageTable/InputfieldPageTable.module @@ -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 * @@ -277,7 +299,8 @@ class InputfieldPageTable extends Inputfield { $out .= "" . $this->wire('sanitizer')->entities($label) . ""; } - $out .= ""; + if(!$this->renderValueMode) $out .= ""; + $out .= ""; 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>$s2"; + $rows[$key] = "$s1>$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 .= ""; + if(!$this->renderValueMode) { + $a = "" . wireIconMarkup('trash-o') . ""; + if(!$item->deletable()) $a = " "; + $out .= ""; + } // wrap the row in a $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, 'needsEditColumn = true;
 
 
" . $this->renderItemLink($item, "") . "" . $this->renderItemLink($item, wireIconMarkup('edit')) . "$a