/hippo/src/main/webapp/ext/src/widgets/menu/CheckItem.js

http://hdbc.googlecode.com/ · JavaScript · 114 lines · 49 code · 6 blank · 59 comment · 11 complexity · 862ffcbdb3089edcbf17e1f4010afeb8 MD5 · raw file

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