MDL-28972 formslib: whitespace fixes
[moodle.git] / lib / form / yui / checkboxcontroller / checkboxcontroller.js
blobabcbbbd84763af3d897dd7a0f74e9d0aed7a5350
1 // This file is part of Moodle - http://moodle.org/
2 //
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.
7 //
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/>.
17 /**
18  * Group of date and time input element
19  *
20  * Contains class for a group of elements used to input a date and time.
21  *
22  * @package   core_form
23  * @copyright 2012 Rajesh Taneja <rajesh@moodle.com>
24  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
26 YUI.add('moodle-form-checkboxcontroller', function(Y) {
27     var checkboxcontroller = function() {
28         checkboxcontroller.superclass.constructor.apply(this, arguments);
29     }
31     Y.extend(checkboxcontroller, Y.Base, {
32         _controllervaluenode : null,
33         _checkboxclass : null,
35         /*
36          * Initialize script if all params passed.
37          *
38          * @param object params values passed while initalizing script
39          */
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);
52             }
53         },
55         /**
56          * Replace controller button with link and add event.
57          *
58          * @param string controllerbutton id of the controller button which needs to be replaced
59          */
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();
70             // Insert link node
71             controllerbutton.get('parentNode').insert(link, controllerbutton.get('lastNode'));
72         },
74         /**
75          * Onclick event will be handled.
76          *
77          * @param Event e
78          */
79         onClick : function(e) {
80             e.preventDefault();
81             this.switchGroupState();
82         },
84         /**
85          * Toggles checkboxes status belong to a group
86          */
87         switchGroupState : function() {
88             if (this._checkboxclass) {
89                 // Value which should be set on checkboxes
90                 var newvalue = '';
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');
99                 } else {
100                     controllervaluenode.set('value', '1');
101                     newvalue = 'checked';
102                 }
103                 checkboxes.set('checked', newvalue);
104             }
105         }
106     });
108     M.form = M.form || {};
110     M.form.checkboxcontroller = function(params) {
111         return new checkboxcontroller(params);
112     }
113 }, '@VERSION@', {requires:['base', 'node', 'util']});