Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Dash - Form element for color picker.
19
 *
20
 * @package   block_dash
21
 * @copyright 2021 bdecent gmbh <https://bdecent.de>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die();
26
 
27
require_once('HTML/QuickForm/input.php');
28
require_once($CFG->dirroot.'/lib/form/templatable_form_element.php');
29
require_once($CFG->dirroot.'/lib/form/text.php');
30
 
31
/**
32
 * Form element for color picker.
33
 *
34
 * @package   block_dash
35
 * @copyright  2021 bdecent gmbh <https://bdecent.de>
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class moodlequickform_dashcolorpicker extends MoodleQuickForm_text implements templatable {
39
 
40
    use templatable_form_element {
41
        export_for_template as export_for_template_base;
42
    }
43
 
44
    /**
45
     * Constructor.
46
     *
47
     * @param string $elementname (optional) Name of the text field.
48
     * @param string $elementlabel (optional) Text field label.
49
     * @param string $attributes (optional) Either a typical HTML attribute string or an associative array.
50
     */
51
    public function __construct($elementname=null, $elementlabel=null, $attributes=null) {
52
        parent::__construct($elementname, $elementlabel, $attributes);
53
        $this->setType('text');
54
 
55
        // Add a CSS class for styling the color picker.
56
        $class = $this->getAttribute('class');
57
        if (empty($class)) {
58
            $class = '';
59
        }
60
        $this->updateAttributes(['class' => $class.' block_dash-form-colour-picker ']);
61
    }
62
 
63
    /**
64
     * Export for template.
65
     *
66
     * @param renderer_base $output
67
     * @return array|stdClass
68
     */
69
    public function export_for_template(renderer_base $output) {
70
        global $PAGE;
71
 
72
        // Compose template context for the mform element.
73
        $context = $this->export_for_template_base($output);
74
 
75
        // Build loading icon.
76
        $icon = new pix_icon('i/loading', get_string('loading', 'admin'), 'moodle', ['class' => 'loadingicon']);
77
        $icondata = $icon->export_for_template($output);
78
        $iconoutput = $output->render_from_template('core/pix_icon', $icondata);
79
 
80
        // Get ID of the element.
81
        $id = $this->getAttribute('id');
82
 
83
        // Add JS to append the color picker div before the element and initiate the color picker utility method.
84
        $PAGE->requires->js_amd_inline("
85
            var element = document.getElementById('$id');
86
            var pickerDiv = document.createElement('div');
87
            pickerDiv.classList.add('admin_colourpicker', 'clearfix');
88
            pickerDiv.innerHTML = '$iconoutput'; // Add loading icon.
89
            element.parentNode.prepend(pickerDiv);
90
            element.parentNode.style.flexDirection = 'column';
91
 
92
            // Init color picker utility.
93
            M.util.init_colour_picker(Y, '$id');
94
        ");
95
 
96
        return $context;
97
    }
98
}