_EnableMixin.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. define("dojox/form/manager/_EnableMixin", [
  2. "dojo/_base/lang",
  3. "dojo/_base/kernel",
  4. "dojo/dom-attr",
  5. "./_Mixin",
  6. "dojo/_base/declare"
  7. ], function(lang, dojo, domAttr, _Mixin, declare){
  8. var fm = lang.getObject("dojox.form.manager", true),
  9. aa = fm.actionAdapter,
  10. ia = fm.inspectorAdapter;
  11. return declare("dojox.form.manager._EnableMixin", null, {
  12. // summary:
  13. // Form manager's mixin for controlling enable/disable state of
  14. // form elements.
  15. // description:
  16. // This mixin provides unified enable/disable functionality for
  17. // form widgets and form elements. It should be used together
  18. // with dojox.form.manager.Mixin.
  19. gatherEnableState: function(names){
  20. // summary:
  21. // Gather enable state of all form elements and return as a dictionary.
  22. // names: Object?:
  23. // If it is an array, it is a list of names to be processed.
  24. // If it is an object, dictionary keys are names to be processed.
  25. // If it is omitted, all known form elements are to be processed.
  26. var result = this.inspectFormWidgets(ia(function(name, widget){
  27. return !widget.get("disabled");
  28. }), names);
  29. if(this.inspectFormNodes){
  30. lang.mixin(result, this.inspectFormNodes(ia(function(name, node){
  31. return !domAttr.get(node, "disabled");
  32. }), names));
  33. }
  34. return result; // Object
  35. },
  36. enable: function(state, defaultState){
  37. // summary:
  38. // Enable form controls according to the supplied state object.
  39. // state: Object?:
  40. // Optional. If a name-value dictionary, the value is true
  41. // to enable and false to disable. If an array, all names in the
  42. // array will be set to defaultState. If omitted, all form
  43. // elements will be set to defaultState.
  44. // defaultState: Boolean:
  45. // The default state (true, if omitted).
  46. if(arguments.length < 2 || defaultState === undefined){
  47. defaultState = true;
  48. }
  49. this.inspectFormWidgets(aa(function(name, widget, value){
  50. widget.set("disabled", !value);
  51. }), state, defaultState);
  52. if(this.inspectFormNodes){
  53. this.inspectFormNodes(aa(function(name, node, value){
  54. domAttr.set(node, "disabled", !value);
  55. }), state, defaultState);
  56. }
  57. return this; // self
  58. },
  59. disable: function(state){
  60. // summary:
  61. // Disable form controls according to the supplied state object
  62. // returning the previous state.
  63. // state: Object?:
  64. // Optional. If a name-value dictionary, the value is true
  65. // to enable and false to disable. If an array, all names in the
  66. // array will be disabled. If omitted, disables all.
  67. var oldState = this.gatherEnableState();
  68. this.enable(state, false);
  69. return oldState; // Object
  70. }
  71. });
  72. });