390168152fd005710cc2074d38ad7289e0bcb9c1.svn-base 3.7 KB

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