roojs-core.js
[roojs1] / Roo / menu / CheckItem.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11  
12 /**
13  * @class Roo.menu.CheckItem
14  * @extends Roo.menu.Item
15  * Adds a menu item that contains a checkbox by default, but can also be part of a radio group.
16  * @constructor
17  * Creates a new CheckItem
18  * @param {Object} config Configuration options
19  */
20 Roo.menu.CheckItem = function(config){
21     Roo.menu.CheckItem.superclass.constructor.call(this, config);
22     this.addEvents({
23         /**
24          * @event beforecheckchange
25          * Fires before the checked value is set, providing an opportunity to cancel if needed
26          * @param {Roo.menu.CheckItem} this
27          * @param {Boolean} checked The new checked value that will be set
28          */
29         "beforecheckchange" : true,
30         /**
31          * @event checkchange
32          * Fires after the checked value has been set
33          * @param {Roo.menu.CheckItem} this
34          * @param {Boolean} checked The checked value that was set
35          */
36         "checkchange" : true
37     });
38     if(this.checkHandler){
39         this.on('checkchange', this.checkHandler, this.scope);
40     }
41 };
42 Roo.extend(Roo.menu.CheckItem, Roo.menu.Item, {
43     /**
44      * @cfg {String} group
45      * All check items with the same group name will automatically be grouped into a single-select
46      * radio button group (defaults to '')
47      */
48     /**
49      * @cfg {String} itemCls The default CSS class to use for check items (defaults to "x-menu-item x-menu-check-item")
50      */
51     itemCls : "x-menu-item x-menu-check-item",
52     /**
53      * @cfg {String} groupClass The default CSS class to use for radio group check items (defaults to "x-menu-group-item")
54      */
55     groupClass : "x-menu-group-item",
56
57     /**
58      * @cfg {Boolean} checked True to initialize this checkbox as checked (defaults to false).  Note that
59      * if this checkbox is part of a radio group (group = true) only the last item in the group that is
60      * initialized with checked = true will be rendered as checked.
61      */
62     checked: false,
63
64     // private
65     ctype: "Roo.menu.CheckItem",
66
67     // private
68     onRender : function(c){
69         Roo.menu.CheckItem.superclass.onRender.apply(this, arguments);
70         if(this.group){
71             this.el.addClass(this.groupClass);
72         }
73         Roo.menu.MenuMgr.registerCheckable(this);
74         if(this.checked){
75             this.checked = false;
76             this.setChecked(true, true);
77         }
78     },
79
80     // private
81     destroy : function(){
82         if(this.rendered){
83             Roo.menu.MenuMgr.unregisterCheckable(this);
84         }
85         Roo.menu.CheckItem.superclass.destroy.apply(this, arguments);
86     },
87
88     /**
89      * Set the checked state of this item
90      * @param {Boolean} checked The new checked value
91      * @param {Boolean} suppressEvent (optional) True to prevent the checkchange event from firing (defaults to false)
92      */
93     setChecked : function(state, suppressEvent){
94         if(this.checked != state && this.fireEvent("beforecheckchange", this, state) !== false){
95             if(this.container){
96                 this.container[state ? "addClass" : "removeClass"]("x-menu-item-checked");
97             }
98             this.checked = state;
99             if(suppressEvent !== true){
100                 this.fireEvent("checkchange", this, state);
101             }
102         }
103     },
104
105     // private
106     handleClick : function(e){
107        if(!this.disabled && !(this.checked && this.group)){// disable unselect on radio item
108            this.setChecked(!this.checked);
109        }
110        Roo.menu.CheckItem.superclass.handleClick.apply(this, arguments);
111     }
112 });