MDL-56640 templates: move select templates from boost to core

This commit is contained in:
Dan Poltawski
2016-12-19 12:23:54 +00:00
parent 5100c486bd
commit c67e93b235
4 changed files with 2 additions and 197 deletions
+2 -175
View File
@@ -1915,71 +1915,7 @@ class core_renderer extends renderer_base {
* @return string HTML fragment
*/
protected function render_single_select(single_select $select) {
$select = clone($select);
if (empty($select->formid)) {
$select->formid = html_writer::random_id('single_select_f');
}
$output = '';
$params = $select->url->params();
if ($select->method === 'post') {
$params['sesskey'] = sesskey();
}
foreach ($params as $name=>$value) {
$output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$name, 'value'=>$value));
}
if (empty($select->attributes['id'])) {
$select->attributes['id'] = html_writer::random_id('single_select');
}
if ($select->disabled) {
$select->attributes['disabled'] = 'disabled';
}
if ($select->tooltip) {
$select->attributes['title'] = $select->tooltip;
}
$select->attributes['class'] = 'autosubmit';
if ($select->class) {
$select->attributes['class'] .= ' ' . $select->class;
}
if ($select->label) {
$output .= html_writer::label($select->label, $select->attributes['id'], false, $select->labelattributes);
}
if ($select->helpicon instanceof help_icon) {
$output .= $this->render($select->helpicon);
}
$output .= html_writer::select($select->options, $select->name, $select->selected, $select->nothing, $select->attributes);
$go = html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('go')));
$output .= html_writer::tag('noscript', html_writer::tag('div', $go), array('class' => 'inline'));
$nothing = empty($select->nothing) ? false : key($select->nothing);
$this->page->requires->yui_module('moodle-core-formautosubmit',
'M.core.init_formautosubmit',
array(array('selectid' => $select->attributes['id'], 'nothing' => $nothing))
);
// then div wrapper for xhtml strictness
$output = html_writer::tag('div', $output);
// now the form itself around it
if ($select->method === 'get') {
$url = $select->url->out_omit_querystring(true); // url without params, the anchor part allowed
} else {
$url = $select->url->out_omit_querystring(); // url without params, the anchor part not allowed
}
$formattributes = array('method' => $select->method,
'action' => $url,
'id' => $select->formid);
$output = html_writer::tag('form', $output, $formattributes);
// and finally one more wrapper with class
return html_writer::tag('div', $output, array('class' => $select->class));
return $this->render_from_template('core/single_select', $select->export_for_template($this));
}
/**
@@ -2006,116 +1942,7 @@ class core_renderer extends renderer_base {
* @return string HTML fragment
*/
protected function render_url_select(url_select $select) {
global $CFG;
$select = clone($select);
if (empty($select->formid)) {
$select->formid = html_writer::random_id('url_select_f');
}
if (empty($select->attributes['id'])) {
$select->attributes['id'] = html_writer::random_id('url_select');
}
if ($select->disabled) {
$select->attributes['disabled'] = 'disabled';
}
if ($select->tooltip) {
$select->attributes['title'] = $select->tooltip;
}
$output = '';
if ($select->label) {
$output .= html_writer::label($select->label, $select->attributes['id'], false, $select->labelattributes);
}
$classes = array();
if (!$select->showbutton) {
$classes[] = 'autosubmit';
}
if ($select->class) {
$classes[] = $select->class;
}
if (count($classes)) {
$select->attributes['class'] = implode(' ', $classes);
}
if ($select->helpicon instanceof help_icon) {
$output .= $this->render($select->helpicon);
}
// For security reasons, the script course/jumpto.php requires URL starting with '/'. To keep
// backward compatibility, we are removing heading $CFG->wwwroot from URLs here.
$urls = array();
foreach ($select->urls as $k=>$v) {
if (is_array($v)) {
// optgroup structure
foreach ($v as $optgrouptitle => $optgroupoptions) {
foreach ($optgroupoptions as $optionurl => $optiontitle) {
if (empty($optionurl)) {
$safeoptionurl = '';
} else if (strpos($optionurl, $CFG->wwwroot.'/') === 0) {
// debugging('URLs passed to url_select should be in local relative form - please fix the code.', DEBUG_DEVELOPER);
$safeoptionurl = str_replace($CFG->wwwroot, '', $optionurl);
} else if (strpos($optionurl, '/') !== 0) {
debugging("Invalid url_select urls parameter inside optgroup: url '$optionurl' is not local relative url!");
continue;
} else {
$safeoptionurl = $optionurl;
}
$urls[$k][$optgrouptitle][$safeoptionurl] = $optiontitle;
}
}
} else {
// plain list structure
if (empty($k)) {
// nothing selected option
} else if (strpos($k, $CFG->wwwroot.'/') === 0) {
$k = str_replace($CFG->wwwroot, '', $k);
} else if (strpos($k, '/') !== 0) {
debugging("Invalid url_select urls parameter: url '$k' is not local relative url!");
continue;
}
$urls[$k] = $v;
}
}
$selected = $select->selected;
if (!empty($selected)) {
if (strpos($select->selected, $CFG->wwwroot.'/') === 0) {
$selected = str_replace($CFG->wwwroot, '', $selected);
} else if (strpos($selected, '/') !== 0) {
debugging("Invalid value of parameter 'selected': url '$selected' is not local relative url!");
}
}
$output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
$output .= html_writer::select($urls, 'jump', $selected, $select->nothing, $select->attributes);
if (!$select->showbutton) {
$go = html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('go')));
$output .= html_writer::tag('noscript', html_writer::tag('div', $go), array('class' => 'inline'));
$nothing = empty($select->nothing) ? false : key($select->nothing);
$this->page->requires->yui_module('moodle-core-formautosubmit',
'M.core.init_formautosubmit',
array(array('selectid' => $select->attributes['id'], 'nothing' => $nothing))
);
} else {
$output .= html_writer::empty_tag('input', array('type'=>'submit', 'value'=>$select->showbutton));
}
// then div wrapper for xhtml strictness
$output = html_writer::tag('div', $output);
// now the form itself around it
$formattributes = array('method' => 'post',
'action' => new moodle_url('/course/jumpto.php'),
'id' => $select->formid);
$output = html_writer::tag('form', $output, $formattributes);
// and finally one more wrapper with class
return html_writer::tag('div', $output, array('class' => $select->class));
return $this->render_from_template('core/url_select', $select->export_for_template($this));
}
/**
+130
View File
@@ -0,0 +1,130 @@
{{!
This file is part of Moodle - http://moodle.org/
Moodle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template core/single_select
Moodle template for a single select submit form.
Context variables required for this template:
* name - Element name.
* method - get or post.
* action - the action url to submit to.
* classes - Element classes.
* label - Element label.
* disabled - true if this element is disabled.
* title - Element title.
* formid - optional id value for the form.
* id - id for the element.
* params - array of params with name and value attributes.
* hasnothing - true if element has no options.
* nothingkey - nothing key to be set.
* options - Array of options for the select with value, name , slected and optgroup properites.
* labelattributes - Label attributes.
* helpicon - Help icon.
Example context (json):
{
"name": "lang",
"method": "get",
"action": "http://localhost/stable_master/mod/scorm/player.php",
"classes": "autosubmit langmenu",
"label": "Zombies are coming...",
"disabled": false,
"title": null,
"formid": "randomid",
"id": "single_select5833dd4f4b08d108",
"params": [
{
"name": "scoid",
"value": "12"
},
{
"name": "cm",
"value": "15"
},
{
"name": "mode",
"value": "review"
},
{
"name": "currentorg",
"value": "eXeMapADrive4823c6301cf72b22b72"
}
],
"hasnothing": false,
"nothingkey": false,
"options": [
{
"value": "en",
"name": "English (en)",
"selected": true,
"optgroup": false
},
{
"value": "ar",
"name": "Muhaaaa..",
"selected": false,
"optgroup": false
}
],
"labelattributes": [],
"helpicon": false
}
}}
<div class="{{classes}} d-inline-block">
<form method="{{method}}" action="{{action}}" class="form-inline" id="{{formid}}">
{{#params}}
<input type="hidden" name="{{name}}" value="{{value}}">
{{/params}}
{{#label}}
<label for="{{id}}" {{#labelattributes}}{{name}}="{{value}}" {{/labelattributes}}>
{{{label}}}
</label>
{{/label}}
{{#helpicon}}
{{>core/help_icon}}
{{/helpicon}}
<select {{#attributes}}{{name}}="{{value}}" {{/attributes}} id="{{id}}" class="custom-select {{classes}}" name="{{name}}">
{{#options}}
{{#optgroup}}
<optgroup label="{{name}}">
{{#options}}
<option value="{{value}}" {{#selected}}selected{{/selected}}>{{{name}}}</option>
{{/options}}
</optgroup>
{{/optgroup}}
{{^optgroup}}
<option value="{{value}}" {{#selected}}selected{{/selected}}>{{{name}}}</option>
{{/optgroup}}
{{/options}}
</select>
<noscript>
<input type="submit" role="button" class="btn btn-secondary" value="{{#str}}go, core{{/str}}">
</noscript>
</form>
</div>
{{#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}}
+49
View 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="custom-select {{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}}
<input type="submit" class="btn btn-secondary" value="{{showbutton}}">
{{/showbutton}}
{{^showbutton}}
<noscript>
<input type="submit" class="btn btn-secondary" value="{{#str}}go, core{{/str}}">
</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}}