mirror of
https://github.com/processwire/processwire.git
synced 2025-08-15 11:14:12 +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:
@@ -1,11 +1,26 @@
|
|||||||
|
|
||||||
function initInputfieldAsmSelect($select) {
|
function initInputfieldAsmSelect($select) {
|
||||||
var id = $select.attr('id');
|
var id = $select.attr('id');
|
||||||
if(typeof ProcessWire.config === 'undefined' || typeof ProcessWire.config[id] === "undefined") {
|
|
||||||
var options = { sortable: true };
|
// determine options common among all InputfieldAsmSelect instances
|
||||||
} else {
|
var options = {};
|
||||||
var options = ProcessWire.config[id];
|
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);
|
$select.asmSelect(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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))})})});
|
@@ -7,23 +7,52 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class InputfieldAsmSelect extends InputfieldSelectMultiple implements InputfieldHasArrayValue, InputfieldHasSortableValue {
|
class InputfieldAsmSelect extends InputfieldSelectMultiple implements InputfieldHasArrayValue, InputfieldHasSortableValue {
|
||||||
|
|
||||||
protected $asmOptions = array();
|
/**
|
||||||
|
* Module info
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*
|
||||||
|
*/
|
||||||
public static function getModuleInfo() {
|
public static function getModuleInfo() {
|
||||||
return array(
|
return array(
|
||||||
'title' => __('asmSelect', __FILE__),
|
'title' => __('asmSelect', __FILE__),
|
||||||
'version' => 200,
|
'version' => 201,
|
||||||
'summary' => __('Multiple selection, progressive enhancement to select multiple', __FILE__), // Module Summary
|
'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() {
|
public function __construct() {
|
||||||
$this->set('usePageEdit', 0);
|
$this->set('usePageEdit', 0);
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Init
|
||||||
|
*
|
||||||
|
*/
|
||||||
public function init() {
|
public function init() {
|
||||||
|
|
||||||
// asmSelect requires jQuery UI, so we enforce it being loaded here
|
// 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
|
// cancel the 'size' attribute used by select multiple
|
||||||
$this->set('size', null);
|
$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) {
|
public function setAsmSelectOption($key, $value) {
|
||||||
$this->asmOptions[$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 . "$class.css?v=$ver");
|
||||||
$this->config->styles->add($this->config->urls->$class . "asmselect/jquery.asmselect.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);
|
return parent::renderReady($parent, $renderValueMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
*/
|
||||||
public function ___render() {
|
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');
|
$selectedOptions = $this->attr('value');
|
||||||
|
|
||||||
foreach($selectedOptions as $id) {
|
foreach($selectedOptions as $id) {
|
||||||
if(!isset($this->options[$id])) continue;
|
if(!isset($this->options[$id])) continue;
|
||||||
$label = $this->options[$id];
|
$label = $this->options[$id];
|
||||||
@@ -109,6 +163,12 @@ class InputfieldAsmSelect extends InputfieldSelectMultiple implements Inputfield
|
|||||||
return parent::___render();
|
return parent::___render();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Field config
|
||||||
|
*
|
||||||
|
* @return InputfieldWrapper
|
||||||
|
*
|
||||||
|
*/
|
||||||
public function ___getConfigInputfields() {
|
public function ___getConfigInputfields() {
|
||||||
$inputfields = parent::___getConfigInputfields();
|
$inputfields = parent::___getConfigInputfields();
|
||||||
if($this->hasFieldtype != 'FieldtypePage' || !$this->hasField) return $inputfields;
|
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->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->description = $this->_('When enabled, the selected label(s) will link to edit the selected page.');
|
||||||
$f->addOption(0, $this->_('No'));
|
$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->attr('value', $this->usePageEdit);
|
||||||
$f->optionColumns = 1;
|
$f->optionColumns = 1;
|
||||||
$f->collapsed = Inputfield::collapsedBlank;
|
$f->collapsed = Inputfield::collapsedBlank;
|
||||||
|
Reference in New Issue
Block a user