mirror of
https://github.com/moodle/moodle.git
synced 2025-08-07 09:56:38 +02:00
MDL-55495 output: Make url_select a templatable
Part of MDL-55071
This commit is contained in:
parent
8a47abcd8a
commit
f1b34660ed
3 changed files with 209 additions and 1 deletions
|
@ -951,7 +951,7 @@ class single_select implements renderable, templatable {
|
||||||
* @package core
|
* @package core
|
||||||
* @category output
|
* @category output
|
||||||
*/
|
*/
|
||||||
class url_select implements renderable {
|
class url_select implements renderable, templatable {
|
||||||
/**
|
/**
|
||||||
* @var array $urls associative array value=>label ex.: array(1=>'One, 2=>Two)
|
* @var array $urls associative array value=>label ex.: array(1=>'One, 2=>Two)
|
||||||
* it is also possible to specify optgroup as complex label array ex.:
|
* it is also possible to specify optgroup as complex label array ex.:
|
||||||
|
@ -1061,6 +1061,155 @@ class url_select implements renderable {
|
||||||
$this->label = $label;
|
$this->label = $label;
|
||||||
$this->labelattributes = $attributes;
|
$this->labelattributes = $attributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean a URL.
|
||||||
|
*
|
||||||
|
* @param string $value The URL.
|
||||||
|
* @return The cleaned URL.
|
||||||
|
*/
|
||||||
|
protected function clean_url($value) {
|
||||||
|
global $CFG;
|
||||||
|
|
||||||
|
if (empty($value)) {
|
||||||
|
// Nothing.
|
||||||
|
|
||||||
|
} else if (strpos($value, $CFG->wwwroot . '/') === 0) {
|
||||||
|
$value = str_replace($CFG->wwwroot, '', $value);
|
||||||
|
|
||||||
|
} else if (strpos($value, '/') !== 0) {
|
||||||
|
debugging("Invalid url_select urls parameter: url '$value' is not local relative url!", DEBUG_DEVELOPER);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flatten the options for Mustache.
|
||||||
|
*
|
||||||
|
* This also cleans the URLs.
|
||||||
|
*
|
||||||
|
* @param array $options The options.
|
||||||
|
* @param array $nothing The nothing option.
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function flatten_options($options, $nothing) {
|
||||||
|
$flattened = [];
|
||||||
|
|
||||||
|
foreach ($options as $value => $option) {
|
||||||
|
if (is_array($option)) {
|
||||||
|
foreach ($option as $groupname => $optoptions) {
|
||||||
|
if (!isset($flattened[$groupname])) {
|
||||||
|
$flattened[$groupname] = [
|
||||||
|
'name' => $groupname,
|
||||||
|
'isgroup' => true,
|
||||||
|
'options' => []
|
||||||
|
];
|
||||||
|
}
|
||||||
|
foreach ($optoptions as $optvalue => $optoption) {
|
||||||
|
$cleanedvalue = $this->clean_url($optvalue);
|
||||||
|
$flattened[$groupname]['options'][$cleanedvalue] = [
|
||||||
|
'name' => $optoption,
|
||||||
|
'value' => $cleanedvalue,
|
||||||
|
'selected' => $this->selected == $optvalue,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
$cleanedvalue = $this->clean_url($value);
|
||||||
|
$flattened[$cleanedvalue] = [
|
||||||
|
'name' => $option,
|
||||||
|
'value' => $cleanedvalue,
|
||||||
|
'selected' => $this->selected == $value,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($nothing)) {
|
||||||
|
$value = key($nothing);
|
||||||
|
$name = reset($nothing);
|
||||||
|
$flattened = [
|
||||||
|
$value => ['name' => $name, 'value' => $value, 'selected' => $this->selected == $value]
|
||||||
|
] + $flattened;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make non-associative array.
|
||||||
|
foreach ($flattened as $key => $value) {
|
||||||
|
if (!empty($value['options'])) {
|
||||||
|
$flattened[$key]['options'] = array_values($value['options']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$flattened = array_values($flattened);
|
||||||
|
|
||||||
|
return $flattened;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Export for template.
|
||||||
|
*
|
||||||
|
* @param renderer_base $output Renderer.
|
||||||
|
* @return stdClass
|
||||||
|
*/
|
||||||
|
public function export_for_template(renderer_base $output) {
|
||||||
|
$attributes = $this->attributes;
|
||||||
|
|
||||||
|
$data = new stdClass();
|
||||||
|
$data->formid = !empty($this->formid) ? $this->formid : html_writer::random_id('url_select_f');
|
||||||
|
$data->classes = $this->class;
|
||||||
|
$data->label = $this->label;
|
||||||
|
$data->disabled = $this->disabled;
|
||||||
|
$data->title = $this->tooltip;
|
||||||
|
$data->id = !empty($attributes['id']) ? $attributes['id'] : html_writer::random_id('url_select');
|
||||||
|
$data->sesskey = sesskey();
|
||||||
|
$data->action = (new moodle_url('/course/jumpto.php'))->out(false);
|
||||||
|
|
||||||
|
// Remove attributes passed as property directly.
|
||||||
|
unset($attributes['class']);
|
||||||
|
unset($attributes['id']);
|
||||||
|
unset($attributes['name']);
|
||||||
|
unset($attributes['title']);
|
||||||
|
|
||||||
|
$data->showbutton = $this->showbutton;
|
||||||
|
if (empty($this->showbutton)) {
|
||||||
|
$data->classes .= ' autosubmit';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Select options.
|
||||||
|
$nothing = false;
|
||||||
|
if (is_string($this->nothing) && $this->nothing !== '') {
|
||||||
|
$nothing = ['' => $this->nothing];
|
||||||
|
$hasnothing = true;
|
||||||
|
} else if (is_array($this->nothing)) {
|
||||||
|
$key = key($this->nothing);
|
||||||
|
if ($key === 'choose' || $key === 'choosedots') {
|
||||||
|
$nothing = [$key => get_string('choosedots')];
|
||||||
|
} else {
|
||||||
|
$nothing = [$key => reset($this->nothing)];
|
||||||
|
}
|
||||||
|
$hasnothing = true;
|
||||||
|
}
|
||||||
|
$data->hasnothing = !empty($nothing);
|
||||||
|
$data->nothingkey = $data->hasnothing ? key($nothing) : false;
|
||||||
|
$data->options = $this->flatten_options($this->urls, $nothing);
|
||||||
|
|
||||||
|
// Label attributes.
|
||||||
|
$data->labelattributes = [];
|
||||||
|
foreach ($this->labelattributes as $key => $value) {
|
||||||
|
$data->labelattributes[] = ['name' => $key, 'value' => $value];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Help icon.
|
||||||
|
$data->helpicon = !empty($this->helpicon) ? $this->helpicon->export_for_template($output) : false;
|
||||||
|
|
||||||
|
// Finally all the remaining attributes.
|
||||||
|
$data->attributes = [];
|
||||||
|
foreach ($this->attributes as $key => $value) {
|
||||||
|
$data->attributes = ['name' => $key, 'value' => $value];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -30,6 +30,7 @@ use action_menu;
|
||||||
use help_icon;
|
use help_icon;
|
||||||
use single_select;
|
use single_select;
|
||||||
use paging_bar;
|
use paging_bar;
|
||||||
|
use url_select;
|
||||||
|
|
||||||
defined('MOODLE_INTERNAL') || die;
|
defined('MOODLE_INTERNAL') || die;
|
||||||
|
|
||||||
|
@ -297,4 +298,13 @@ class core_renderer extends \core_renderer {
|
||||||
return $this->render_from_template('core/paging_bar', $pagingbar->export_for_template($this));
|
return $this->render_from_template('core/paging_bar', $pagingbar->export_for_template($this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renders a url select.
|
||||||
|
*
|
||||||
|
* @param url_select $select The object.
|
||||||
|
* @return string HTML
|
||||||
|
*/
|
||||||
|
protected function render_url_select(url_select $select) {
|
||||||
|
return $this->render_from_template('core/url_select', $select->export_for_template($this));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
49
theme/noname/templates/core/url_select.mustache
Normal file
49
theme/noname/templates/core/url_select.mustache
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
<div class="{{classes}}">
|
||||||
|
<form method="post" action="{{action}}" class="form-inline" id="{{formid}}">
|
||||||
|
<input type="hidden" name="sesskey" value="{{sesskey}}">
|
||||||
|
<div class="form-group">
|
||||||
|
{{#label}}
|
||||||
|
<label for="{{id}}" {{#labelattributes}}{{name}}="{{value}}" {{/labelattributes}}>
|
||||||
|
{{label}}
|
||||||
|
</label>
|
||||||
|
{{/label}}
|
||||||
|
{{#helpicon}}
|
||||||
|
{{>core/help_icon}}
|
||||||
|
{{/helpicon}}
|
||||||
|
<select {{#attributes}}{{name}}="{{value}}" {{/attributes}} id="{{id}}" class="form-control {{classes}}" name="jump">
|
||||||
|
{{#options}}
|
||||||
|
{{#isgroup}}
|
||||||
|
<optgroup label="{{name}}">
|
||||||
|
{{#options}}
|
||||||
|
<option value="{{value}}" {{#selected}}selected{{/selected}}>{{name}}</option>
|
||||||
|
{{/options}}
|
||||||
|
</optgroup>
|
||||||
|
{{/isgroup}}
|
||||||
|
{{^isgroup}}
|
||||||
|
<option value="{{value}}" {{#selected}}selected{{/selected}}>{{name}}</option>
|
||||||
|
{{/isgroup}}
|
||||||
|
{{/options}}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
{{#showbutton}}
|
||||||
|
<button type="submit" class="btn btn-secondary">{{showbutton}}</button>
|
||||||
|
{{/showbutton}}
|
||||||
|
{{^showbutton}}
|
||||||
|
<noscript>
|
||||||
|
<button type="submit" class="btn btn-secondary">{{#str}}go, core{{/str}}</button>
|
||||||
|
</noscript>
|
||||||
|
{{/showbutton}}
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
{{^showbutton}}
|
||||||
|
{{#js}}
|
||||||
|
require(['core/yui'], function(Y) {
|
||||||
|
Y.use('moodle-core-formautosubmit', function() {
|
||||||
|
M.core.init_formautosubmit({
|
||||||
|
selectid: '{{id}}',
|
||||||
|
nothing: {{#hasnothing}}'{{nothingkey}}'{{/hasnothing}}{{^hasnothing}}false{{/hasnothing}}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
{{/js}}
|
||||||
|
{{/showbutton}}
|
Loading…
Add table
Add a link
Reference in a new issue