1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 09:44:38 +02:00

Update InputfieldAsmSelect to reduce number of options needed in ProcessWire.config, isolating options common to all InputfieldAsmSelect instances from those unique to individual instances

This commit is contained in:
Ryan Cramer
2019-10-04 10:45:24 -04:00
parent 655c4cdd24
commit 0a01b472a1
3 changed files with 95 additions and 17 deletions

View File

@@ -1,11 +1,26 @@
function initInputfieldAsmSelect($select) {
var id = $select.attr('id');
if(typeof ProcessWire.config === 'undefined' || typeof ProcessWire.config[id] === "undefined") {
var options = { sortable: true };
} else {
var options = ProcessWire.config[id];
// determine options common among all InputfieldAsmSelect instances
var options = {};
if(typeof ProcessWire.config == 'undefined') {
options = { sortable: true };
} else if(typeof ProcessWire.config[id] != "undefined") {
options = ProcessWire.config[id]; // deprecated/legacy
} else if(typeof ProcessWire.config['InputfieldAsmSelect'] != "undefined") {
options = ProcessWire.config['InputfieldAsmSelect'];
}
// merge options unique to this instance from select.data-asmopt attribute
var data = $select.attr('data-asmopt');
if(typeof data != "undefined") {
data = JSON.parse(data);
if(data) jQuery.extend(options, data);
}
$select.asmSelect(options);
}

View File

@@ -1 +1 @@
function initInputfieldAsmSelect(b){var c=b.attr("id");if(typeof ProcessWire.config==="undefined"||typeof ProcessWire.config[c]==="undefined"){var a={sortable:true}}else{var a=ProcessWire.config[c]}b.asmSelect(a)}$(document).ready(function(){$(".InputfieldAsmSelect select[multiple=multiple]").each(function(){initInputfieldAsmSelect($(this))});$(document).on("reloaded",".InputfieldAsmSelect, .InputfieldPage",function(){var a=$(this);if(a.hasClass("InputfieldPage")){a=a.find(".InputfieldAsmSelect")}if(!a.length){return}if(a.find(".asmList").length){return}$(this).find("select[multiple=multiple]").each(function(){initInputfieldAsmSelect($(this))})})});
function initInputfieldAsmSelect($select){var id=$select.attr("id");var options={};if(typeof ProcessWire.config=="undefined"){options={sortable:true}}else if(typeof ProcessWire.config[id]!="undefined"){options=ProcessWire.config[id]}else if(typeof ProcessWire.config["InputfieldAsmSelect"]!="undefined"){options=ProcessWire.config["InputfieldAsmSelect"]}var data=$select.attr("data-asmopt");if(typeof data!="undefined"){data=JSON.parse(data);if(data)jQuery.extend(options,data)}$select.asmSelect(options)}$(document).ready(function(){$(".InputfieldAsmSelect select[multiple=multiple]").each(function(){initInputfieldAsmSelect($(this))});$(document).on("reloaded",".InputfieldAsmSelect, .InputfieldPage",function(){var $t=$(this);if($t.hasClass("InputfieldPage"))$t=$t.find(".InputfieldAsmSelect");if(!$t.length)return;if($t.find(".asmList").length)return;$(this).find("select[multiple=multiple]").each(function(){initInputfieldAsmSelect($(this))})})});

View File

@@ -7,23 +7,52 @@
*
*/
class InputfieldAsmSelect extends InputfieldSelectMultiple implements InputfieldHasArrayValue, InputfieldHasSortableValue {
protected $asmOptions = array();
/**
* Module info
*
* @return array
*
*/
public static function getModuleInfo() {
return array(
'title' => __('asmSelect', __FILE__),
'version' => 200,
'version' => 201,
'summary' => __('Multiple selection, progressive enhancement to select multiple', __FILE__), // Module Summary
'permanent' => true,
);
'permanent' => true,
);
}
/**
* Custom defined AsmSelect options
*
* @var array
*
*/
protected $asmOptions = array();
/**
* Options as specified at init() state (common to all instances)
*
* @var array
*
*/
protected $asmDefaults = array();
/**
* Construct
*
*/
public function __construct() {
$this->set('usePageEdit', 0);
parent::__construct();
}
/**
* Init
*
*/
public function init() {
// asmSelect requires jQuery UI, so we enforce it being loaded here
@@ -53,8 +82,18 @@ class InputfieldAsmSelect extends InputfieldSelectMultiple implements Inputfield
// cancel the 'size' attribute used by select multiple
$this->set('size', null);
$this->config->js('InputfieldAsmSelect', $this->asmOptions);
$this->asmDefaults = $this->asmOptions;
}
/**
* Set custom option for AsmSelect
*
* @param string $key
* @param string|bool $value
*
*/
public function setAsmSelectOption($key, $value) {
$this->asmOptions[$key] = $value;
}
@@ -90,15 +129,30 @@ class InputfieldAsmSelect extends InputfieldSelectMultiple implements Inputfield
$this->config->styles->add($this->config->urls->$class . "$class.css?v=$ver");
$this->config->styles->add($this->config->urls->$class . "asmselect/jquery.asmselect.css?v=$ver");
$this->config->js($this->id, $this->asmOptions);
// $this->config->js($this->id, $this->asmOptions); // deprecated/legacy
return parent::renderReady($parent, $renderValueMode);
}
/**
* Render
*
* @return string
*
*/
public function ___render() {
// compile settings unique to this instance into a JSON encoded data-asmopt attribute
$settings = array();
foreach($this->asmOptions as $key => $value) {
if(!isset($this->asmDefaults[$key]) || $this->asmDefaults[$key] != $value) {
$settings[$key] = $value;
}
}
$this->attr('data-asmopt', json_encode($settings));
// ensure selected options are placed as last in the AsmSelect select output
$selectedOptions = $this->attr('value');
foreach($selectedOptions as $id) {
if(!isset($this->options[$id])) continue;
$label = $this->options[$id];
@@ -109,6 +163,12 @@ class InputfieldAsmSelect extends InputfieldSelectMultiple implements Inputfield
return parent::___render();
}
/**
* Field config
*
* @return InputfieldWrapper
*
*/
public function ___getConfigInputfields() {
$inputfields = parent::___getConfigInputfields();
if($this->hasFieldtype != 'FieldtypePage' || !$this->hasField) return $inputfields;
@@ -117,7 +177,10 @@ class InputfieldAsmSelect extends InputfieldSelectMultiple implements Inputfield
$f->label = $this->_('Link selected pages to page editor?');
$f->description = $this->_('When enabled, the selected label(s) will link to edit the selected page.');
$f->addOption(0, $this->_('No'));
$f->addOption(1, $this->_('Yes (in modal window)'));
$f->addOption(1,
$this->_('Yes') . ' ' .
$this->_('(in modal window)')
);
$f->attr('value', $this->usePageEdit);
$f->optionColumns = 1;
$f->collapsed = Inputfield::collapsedBlank;