Permalink
Browse files

MDL-28972 formslib: Checkbox controller will use yui to select and un…

…select checkboxes
  • Loading branch information...
Rajesh Taneja
Rajesh Taneja committed Mar 6, 2012
1 parent 23778a4 commit c14584f2066c2d24e32318a52f680eef32019297
Showing with 129 additions and 36 deletions.
  1. +113 −0 lib/form/yui/checkboxcontroller/checkboxcontroller.js
  2. +16 −36 lib/formslib.php
@@ -0,0 +1,113 @@
+// 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/>.
+
+
+/**
+ * Group of date and time input element
+ *
+ * Contains class for a group of elements used to input a date and time.
+ *
+ * @package core_form
+ * @copyright 2012 Rajesh Taneja <rajesh@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+YUI.add('moodle-form-checkboxcontroller', function(Y) {
+ var checkboxcontroller = function() {
+ checkboxcontroller.superclass.constructor.apply(this, arguments);
+ }
+
+ Y.extend(checkboxcontroller, Y.Base, {
+ _controllervaluenode : null,
+ _checkboxclass : null,
+
+ /*
+ * Initialize script if all params passed.
+ *
+ * @param object params values passed while initalizing script
+ */
+ initializer : function(params) {
+ if (params && params.checkboxcontroller &&
+ params.controllerbutton &&
+ params.checkboxclass) {
+ // Id of controller node which keeps value in html.
+ this._controllervaluenode = '#id_'+params.checkboxcontroller;
+
+ // Checkboxes class name by which checkboxes will be selected
+ this._checkboxclass = '.'+params.checkboxclass;
+
+ // Replace submit button with link.
+ this.replaceButton('#id_'+params.controllerbutton);
+ }
+ },
+
+ /**
+ * Replace controller button with link and add event.
+ *
+ * @param string controllerbutton id of the controller button which needs to be replaced
+ */
+ replaceButton : function(controllerbutton) {
+ var controllerbutton = Y.one(controllerbutton);
+ var linkname = controllerbutton.get('value');
+ // Link node which will replace controller button
+ var link = Y.Node.create('<a href="#" onmouseover="window.status=\''+linkname+'\';" onmouseout="window.status=\'\';">'+linkname+'</a>');
+
+ // Attach onclick event to link
+ link.on('click', this.onClick, this);
+ // Hide controller button
+ controllerbutton.hide();
+ // Insert link node
+ controllerbutton.get('parentNode').insert(link, controllerbutton.get('lastNode'));
+ },
+
+ /**
+ * Onclick event will be handled.
+ *
+ * @param Event e
+ */
+ onClick : function(e) {
+ e.preventDefault();
+ this.switchGroupState();
+ },
+
+ /**
+ * Toggles checkboxes status belong to a group
+ */
+ switchGroupState : function() {
+ if (this._checkboxclass) {
+ // Value which should be set on checkboxes
+ var newvalue = '';
+ // Get controller node which keeps value
+ var controllervaluenode = Y.one(this._controllervaluenode);
+ // Get all checkboxes with
+ var checkboxes = Y.all(this._checkboxclass);
+
+ // Toggle checkboxes in group, depending on conroller value
+ if (controllervaluenode.get('value') == 1) {
+ controllervaluenode.set('value', '0');
+ } else {
+ controllervaluenode.set('value', '1');
+ newvalue = 'checked';
+ }
+ checkboxes.set('checked', newvalue);
+ }
+ }
+ });
+
+ M.form = M.form || {};
+
+ M.form.checkboxcontroller = function(params) {
+ return new checkboxcontroller(params);
+ }
+}, '@VERSION@', {requires:['base', 'node', 'util']});
View
@@ -1058,7 +1058,7 @@ function repeat_elements($elementobjs, $repeats, $options, $repeathiddenname,
* @param int $originalValue The original general state of the checkboxes before the user first clicks this element
*/
function add_checkbox_controller($groupid, $text = null, $attributes = null, $originalValue = 0) {
- global $CFG;
+ global $CFG, $PAGE;
// Set the default text if none was specified
if (empty($text)) {
@@ -1068,50 +1068,30 @@ function add_checkbox_controller($groupid, $text = null, $attributes = null, $or
$mform = $this->_form;
$select_value = optional_param('checkbox_controller'. $groupid, null, PARAM_INT);
- if ($select_value == 0 || is_null($select_value)) {
- $new_select_value = 1;
- } else {
+ if (is_null($select_value)) {
$new_select_value = 0;
+ } else {
+ $new_select_value = (int) !$select_value;
}
- $mform->addElement('hidden', "checkbox_controller$groupid");
+ $mform->addElement('hidden', "checkbox_controller".$groupid, null, array('id' => "id_checkbox_controller".$groupid));
$mform->setType("checkbox_controller$groupid", PARAM_INT);
$mform->setConstants(array("checkbox_controller$groupid" => $new_select_value));
$checkbox_controller_name = 'nosubmit_checkbox_controller' . $groupid;
$mform->registerNoSubmitButton($checkbox_controller_name);
- // Prepare Javascript for submit element
- $js = "\n//<![CDATA[\n";
- if (!defined('HTML_QUICKFORM_CHECKBOXCONTROLLER_EXISTS')) {
- $js .= <<<EOS
-function html_quickform_toggle_checkboxes(group) {
- var checkboxes = document.getElementsByClassName('checkboxgroup' + group);
- var newvalue = false;
- var global = eval('html_quickform_checkboxgroup' + group + ';');
- if (global == 1) {
- eval('html_quickform_checkboxgroup' + group + ' = 0;');
- newvalue = '';
- } else {
- eval('html_quickform_checkboxgroup' + group + ' = 1;');
- newvalue = 'checked';
- }
-
- for (i = 0; i < checkboxes.length; i++) {
- checkboxes[i].checked = newvalue;
- }
-}
-EOS;
- define('HTML_QUICKFORM_CHECKBOXCONTROLLER_EXISTS', true);
- }
- $js .= "\nvar html_quickform_checkboxgroup$groupid=$originalValue;\n";
-
- $js .= "//]]>\n";
-
- require_once("$CFG->libdir/form/submitlink.php");
- $submitlink = new MoodleQuickForm_submitlink($checkbox_controller_name, $attributes);
- $submitlink->_js = $js;
- $submitlink->_onclick = "html_quickform_toggle_checkboxes($groupid); return false;";
+ $PAGE->requires->yui_module('moodle-form-checkboxcontroller', 'M.form.checkboxcontroller',
+ array(
+ array('groupid' => $groupid,
+ 'checkboxclass' => 'checkboxgroup'.$groupid,
+ 'checkboxcontroller' => "checkbox_controller".$groupid,
+ 'controllerbutton' => $checkbox_controller_name)
+ )
+ );
+
+ require_once("$CFG->libdir/form/submit.php");
+ $submitlink = new MoodleQuickForm_submit($checkbox_controller_name, $attributes);
$mform->addElement($submitlink);
$mform->setDefault($checkbox_controller_name, $text);
}

0 comments on commit c14584f

Please sign in to comment.