1 // This file is part of Moodle - http://moodle.org/
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Group of date and time input element
20 * Contains class for a group of elements used to input a date and time.
23 * @copyright 2012 Rajesh Taneja <rajesh@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 YUI.add('moodle-form-checkboxcontroller', function(Y) {
27 var checkboxcontroller = function() {
28 checkboxcontroller.superclass.constructor.apply(this, arguments);
31 Y.extend(checkboxcontroller, Y.Base, {
32 _controllervaluenode : null,
33 _checkboxclass : null,
36 * Initialize script if all params passed.
38 * @param object params values passed while initalizing script
40 initializer : function(params) {
41 if (params && params.checkboxcontroller &&
42 params.controllerbutton &&
43 params.checkboxclass) {
44 // Id of controller node which keeps value in html.
45 this._controllervaluenode = '#id_'+params.checkboxcontroller;
47 // Checkboxes class name by which checkboxes will be selected
48 this._checkboxclass = '.'+params.checkboxclass;
50 // Replace submit button with link.
51 this.replaceButton('#id_'+params.controllerbutton);
56 * Replace controller button with link and add event.
58 * @param string controllerbutton id of the controller button which needs to be replaced
60 replaceButton : function(controllerbutton) {
61 var controllerbutton = Y.one(controllerbutton);
62 var linkname = controllerbutton.get('value');
63 // Link node which will replace controller button
64 var link = Y.Node.create('<a href="#" onmouseover="window.status=\''+linkname+'\';" onmouseout="window.status=\'\';">'+linkname+'</a>');
66 // Attach onclick event to link
67 link.on('click', this.onClick, this);
68 // Hide controller button
69 controllerbutton.hide();
71 controllerbutton.get('parentNode').insert(link, controllerbutton.get('lastNode'));
75 * Onclick event will be handled.
79 onClick : function(e) {
81 this.switchGroupState();
85 * Toggles checkboxes status belong to a group
87 switchGroupState : function() {
88 if (this._checkboxclass) {
89 // Value which should be set on checkboxes
91 // Get controller node which keeps value
92 var controllervaluenode = Y.one(this._controllervaluenode);
93 // Get all checkboxes with
94 var checkboxes = Y.all(this._checkboxclass);
96 // Toggle checkboxes in group, depending on conroller value
97 if (controllervaluenode.get('value') == 1) {
98 controllervaluenode.set('value', '0');
100 controllervaluenode.set('value', '1');
101 newvalue = 'checked';
103 checkboxes.set('checked', newvalue);
108 M.form = M.form || {};
110 M.form.checkboxcontroller = function(params) {
111 return new checkboxcontroller(params);
113 }, '@VERSION@', {requires:['base', 'node', 'util']});