Source: api/ThreeStateCheckBox.js

Retour à la documentation
define(["dijit/form/CheckBox",
        "dojo/_base/declare",
        "dojo/_base/event",
        "dojo/dom-attr"
       ], function (CheckBox, declare, event, domAttr) {
    "use strict";

    return declare([CheckBox], {
        // baseClass: [protected] String
        //      Root CSS class of the widget (ex: twcCheckBox), used to add CSS
        //      classes of widget.
        //      (ex: 'dijitCheckBox dijitCheckBoxChecked dijitCheckBoxMixed')
        baseClass: 'dijitCheckBox',

        // toggleState: Object
        //      Define the next state when the checkbox is toggled.
        toggleState: {"mixed": true, "true": false, "false": true},

        // value:   Boolean
        //      Indicate if the checkbox is a multi state checkbox or not. If
        //      multiState is true the 'checked' attr can be either: 'mixed',
        //      true or false otherwise 'checked' can only be true or false.
        multiState: true,

        _getCheckedAttr: function () {
            // summary:
            //      Returns the current checked state. This method provides the hook
            //      for get('checked').
            return this.checked;
        },

        _onClick: function (/*Event*/ evt) {
            // summary:
            //      Process a click event on the checkbox.
            // description:
            //      Process a click event on the checkbox. If the checkbox is in a mixed
            //      state it will change to checked. Any other state will just toggle the
            //      current checkbox state.
            //
            //      NOTE: A click event will never change the state to mixed.
            // evt:
            //      Click event object
            //

            if (!this.readOnly && !this.disabled) {
                this.toggle();
                return this.onClick(evt);
            }
            return event.stop(evt);
        },

        _setCheckedAttr: function (/*Boolean | String*/ checked, /*Boolean?*/ priorityChange) {
            // summary
            //      Set the new checked state of the checkbox.
            // description
            //      Set the new checked state of the checkbox.
            // checked:
            //      New state which is either 'mixed', true or false.
            var newState = checked,
                txtState;

            // Normalize the new state
            if (newState !== 'mixed' || !this.multiState) {
                newState = newState ? true : false;
            }
            txtState = (newState == 'mixed' ? newState : (newState ? 'true' : 'false'));

            this._set('checked', newState);
            domAttr.set(this.focusNode || this.domNode, 'checked', newState);
            (this.focusNode || this.domNode).setAttribute('aria-checked', txtState);
            this._handleOnChange(newState, priorityChange);
            return newState;
        },

        _setValueAttr: function (/*String | Boolean*/ newValue) {
            // summary:
            //      Handler for value= attribute to constructor, Overwrites the
            //      default '_setValueAttr' method as we will handle the Checkbox
            //      checked attribute explicitly.
            // description:
            //      If passed a string, changes the value attribute of the CheckBox
            //      (the one specified as 'value' when the CheckBox was constructed).
            //
            //      NOTE: Changing the checkbox value DOES NOT change the checked state.
            // newValue:

            if (typeof newValue == 'string') {
                this.value = newValue;
                domAttr.set(this.focusNode, 'value', newValue);
            }
        },

        /**
         * Inverse le statut de la checkbox
         */
        toggle: function () {
            // summary:
            //      Toggle the current checkbox state and return the new state. If the
            //      checkbox is read-only or disabled the current state is returned.
            //
            var curState = this.get('checked');
            var nxtState;

            if (!this.readOnly && !this.disabled) {
                nxtState = this.toggleState[curState.toString()];
                curState = this._setCheckedAttr(nxtState !== undefined ? nxtState : true);
            }
            return curState;
        }
    }); /* end declare() */
}); /* end define() */