_EnableMixin.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.form.manager._EnableMixin"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.form.manager._EnableMixin"] = true;
  8. dojo.provide("dojox.form.manager._EnableMixin");
  9. dojo.require("dojox.form.manager._Mixin");
  10. (function(){
  11. var fm = dojox.form.manager,
  12. aa = fm.actionAdapter,
  13. ia = fm.inspectorAdapter;
  14. dojo.declare("dojox.form.manager._EnableMixin", null, {
  15. // summary:
  16. // Form manager's mixin for controlling enable/disable state of
  17. // form elements.
  18. // description:
  19. // This mixin provides unified enable/disable functionality for
  20. // form widgets and form elements. It should be used together
  21. // with dojox.form.manager.Mixin.
  22. gatherEnableState: function(names){
  23. // summary:
  24. // Gather enable state of all form elements and return as a dictionary.
  25. // names: Object?:
  26. // If it is an array, it is a list of names to be processed.
  27. // If it is an object, dictionary keys are names to be processed.
  28. // If it is omitted, all known form elements are to be processed.
  29. var result = this.inspectFormWidgets(ia(function(name, widget){
  30. return !widget.get("disabled");
  31. }), names);
  32. if(this.inspectFormNodes){
  33. dojo.mixin(result, this.inspectFormNodes(ia(function(name, node){
  34. return !dojo.attr(node, "disabled");
  35. }), names));
  36. }
  37. return result; // Object
  38. },
  39. enable: function(state, defaultState){
  40. // summary:
  41. // Enable form controls according to the supplied state object.
  42. // state: Object?:
  43. // Optional. If a name-value dictionary, the value is true
  44. // to enable and false to disable. If an array, all names in the
  45. // array will be set to defaultState. If omitted, all form
  46. // elements will be set to defaultState.
  47. // defaultState: Boolean:
  48. // The default state (true, if omitted).
  49. if(arguments.length < 2 || defaultState === undefined){
  50. defaultState = true;
  51. }
  52. this.inspectFormWidgets(aa(function(name, widget, value){
  53. widget.set("disabled", !value);
  54. }), state, defaultState);
  55. if(this.inspectFormNodes){
  56. this.inspectFormNodes(aa(function(name, node, value){
  57. dojo.attr(node, "disabled", !value);
  58. }), state, defaultState);
  59. }
  60. return this; // self
  61. },
  62. disable: function(state){
  63. // summary:
  64. // Disable form controls according to the supplied state object
  65. // returning the previous state.
  66. // state: Object?:
  67. // Optional. If a name-value dictionary, the value is true
  68. // to enable and false to disable. If an array, all names in the
  69. // array will be disabled. If omitted, disables all.
  70. var oldState = this.gatherEnableState();
  71. this.enable(state, false);
  72. return oldState; // Object
  73. }
  74. });
  75. })();
  76. }