diff options
| author | Tristan Zur <tzur@web.web.ccwn.org> | 2014-03-27 22:27:47 +0100 |
|---|---|---|
| committer | Tristan Zur <tzur@web.web.ccwn.org> | 2014-03-27 22:27:47 +0100 |
| commit | b62676ca5d3d6f6ba3f019ea3f99722e165a98d8 (patch) | |
| tree | 86722cb80f07d4569f90088eeaea2fc2f6e2ef94 /js/dojo/dojox/form/manager | |
Diffstat (limited to 'js/dojo/dojox/form/manager')
| -rw-r--r-- | js/dojo/dojox/form/manager/_ClassMixin.js | 73 | ||||
| -rw-r--r-- | js/dojo/dojox/form/manager/_DisplayMixin.js | 66 | ||||
| -rw-r--r-- | js/dojo/dojox/form/manager/_EnableMixin.js | 84 | ||||
| -rw-r--r-- | js/dojo/dojox/form/manager/_FormMixin.js | 156 | ||||
| -rw-r--r-- | js/dojo/dojox/form/manager/_Mixin.js | 498 | ||||
| -rw-r--r-- | js/dojo/dojox/form/manager/_NodeMixin.js | 369 | ||||
| -rw-r--r-- | js/dojo/dojox/form/manager/_ValueMixin.js | 82 |
7 files changed, 1328 insertions, 0 deletions
diff --git a/js/dojo/dojox/form/manager/_ClassMixin.js b/js/dojo/dojox/form/manager/_ClassMixin.js new file mode 100644 index 0000000..81d2b30 --- /dev/null +++ b/js/dojo/dojox/form/manager/_ClassMixin.js @@ -0,0 +1,73 @@ +//>>built +define("dojox/form/manager/_ClassMixin", [ + "dojo/_base/lang", + "dojo/_base/kernel", + "dojo/dom-class", + "./_Mixin", + "dojo/_base/declare" +], function(lang, dojo, domClass, _Mixin, declare){ + var fm = lang.getObject("dojox.form.manager", true), + aa = fm.actionAdapter, + ia = fm.inspectorAdapter; + + return declare("dojox.form.manager._ClassMixin", null, { + // summary: + // Form manager's mixin for testing/assigning/removing + // classes of controlled elements. + // description: + // This mixin provides unified way to check/add/remove a class + // of controlled elements. + // It should be used together with dojox.form.manager.Mixin. + + gatherClassState: function(className, names){ + // summary: + // Gather the presence of a certain class in all controlled elements. + // className: String: + // The class name to test for. + // names: Object?: + // If it is an array, it is a list of names to be processed. + // If it is an object, dictionary keys are names to be processed. + // If it is omitted, all known form elements are to be processed. + + var result = this.inspect(ia(function(name, node){ + return domClass.contains(node, className); + }), names); + + return result; // Object + }, + + addClass: function(className, names){ + // summary: + // Add a class to nodes according to the supplied set of names + // className: String: + // Class name to add. + // names: Object?: + // If it is an array, it is a list of names to be processed. + // If it is an object, dictionary keys are names to be processed. + // If it is omitted, all known form elements are to be processed. + + this.inspect(aa(function(name, node){ + domClass.add(node, className); + }), names); + + return this; // self + }, + + removeClass: function(className, names){ + // summary: + // Remove a class from nodes according to the supplied set of names + // className: String: + // Class name to remove. + // names: Object?: + // If it is an array, it is a list of names to be processed. + // If it is an object, dictionary keys are names to be processed. + // If it is omitted, all known form elements are to be processed. + + this.inspect(aa(function(name, node){ + domClass.remove(node, className); + }), names); + + return this; // self + } + }); +}); diff --git a/js/dojo/dojox/form/manager/_DisplayMixin.js b/js/dojo/dojox/form/manager/_DisplayMixin.js new file mode 100644 index 0000000..ae54494 --- /dev/null +++ b/js/dojo/dojox/form/manager/_DisplayMixin.js @@ -0,0 +1,66 @@ +//>>built +define("dojox/form/manager/_DisplayMixin", [ + "dojo/_base/kernel", + "dojo/dom-style", + "dojo/_base/declare" +], function(dojo, domStyle, declare){ +return declare("dojox.form.manager._DisplayMixin", null, { + // summary: + // Form manager's mixin for controlling show/hide state of + // controlled elements (defined by dojoAttachPoint attributes). + // description: + // This mixin provides unified show/hide functionality for + // controlled elements (indicated by dojoAttachPoint attribute). + // Essentially it provides a way to change "style.display" + // parameter of controlled nodes. + // It should be used together with dojox.form.manager.Mixin. + + gatherDisplayState: function(names){ + // summary: + // Gather display state of all attached elements and return as a dictionary. + // names: Object?: + // If it is an array, it is a list of names to be processed. + // If it is an object, dictionary keys are names to be processed. + // If it is omitted, all known attach point nodes are to be processed. + + var result = this.inspectAttachedPoints(function(name, node){ + return domStyle.get(node, "display") != "none"; + }, names); + + return result; // Object + }, + + show: function(state, defaultState){ + // summary: + // Show attached nodes according to the supplied state object. + // state: Object?: + // Optional. If a name-value dictionary, the value is true + // to show and false to hide. If an array, all names in the + // array will be set to defaultState. If omitted, all form + // elements will be set to defaultState. + // defaultState: Boolean?: + // The default state (true, if omitted). + + if(arguments.length < 2){ + defaultState = true; + } + + this.inspectAttachedPoints(function(name, node, value){ + domStyle.set(node, "display", value ? "" : "none"); + }, state, defaultState); + + return this; // self + }, + + hide: function(state){ + // summary: + // Hide attached nodes according to the supplied state object. + // state: Object?: + // Optional. If a name-value dictionary, the value is true + // to show and false to hide. If an array, all names in the + // array will be hidden. If omitted, all form elements + // will be hidden. + return this.show(state, false); // self + } +}); +}); diff --git a/js/dojo/dojox/form/manager/_EnableMixin.js b/js/dojo/dojox/form/manager/_EnableMixin.js new file mode 100644 index 0000000..2b15df4 --- /dev/null +++ b/js/dojo/dojox/form/manager/_EnableMixin.js @@ -0,0 +1,84 @@ +//>>built +define("dojox/form/manager/_EnableMixin", [ + "dojo/_base/lang", + "dojo/_base/kernel", + "dojo/dom-attr", + "./_Mixin", + "dojo/_base/declare" +], function(lang, dojo, domAttr, _Mixin, declare){ + var fm = lang.getObject("dojox.form.manager", true), + aa = fm.actionAdapter, + ia = fm.inspectorAdapter; + + return declare("dojox.form.manager._EnableMixin", null, { + // summary: + // Form manager's mixin for controlling enable/disable state of + // form elements. + // description: + // This mixin provides unified enable/disable functionality for + // form widgets and form elements. It should be used together + // with dojox.form.manager.Mixin. + + gatherEnableState: function(names){ + // summary: + // Gather enable state of all form elements and return as a dictionary. + // names: Object?: + // If it is an array, it is a list of names to be processed. + // If it is an object, dictionary keys are names to be processed. + // If it is omitted, all known form elements are to be processed. + + var result = this.inspectFormWidgets(ia(function(name, widget){ + return !widget.get("disabled"); + }), names); + + if(this.inspectFormNodes){ + lang.mixin(result, this.inspectFormNodes(ia(function(name, node){ + return !domAttr.get(node, "disabled"); + }), names)); + } + + return result; // Object + }, + + enable: function(state, defaultState){ + // summary: + // Enable form controls according to the supplied state object. + // state: Object?: + // Optional. If a name-value dictionary, the value is true + // to enable and false to disable. If an array, all names in the + // array will be set to defaultState. If omitted, all form + // elements will be set to defaultState. + // defaultState: Boolean: + // The default state (true, if omitted). + + if(arguments.length < 2 || defaultState === undefined){ + defaultState = true; + } + + this.inspectFormWidgets(aa(function(name, widget, value){ + widget.set("disabled", !value); + }), state, defaultState); + + if(this.inspectFormNodes){ + this.inspectFormNodes(aa(function(name, node, value){ + domAttr.set(node, "disabled", !value); + }), state, defaultState); + } + + return this; // self + }, + + disable: function(state){ + // summary: + // Disable form controls according to the supplied state object + // returning the previous state. + // state: Object?: + // Optional. If a name-value dictionary, the value is true + // to enable and false to disable. If an array, all names in the + // array will be disabled. If omitted, disables all. + var oldState = this.gatherEnableState(); + this.enable(state, false); + return oldState; // Object + } + }); +}); diff --git a/js/dojo/dojox/form/manager/_FormMixin.js b/js/dojo/dojox/form/manager/_FormMixin.js new file mode 100644 index 0000000..69a9fa2 --- /dev/null +++ b/js/dojo/dojox/form/manager/_FormMixin.js @@ -0,0 +1,156 @@ +//>>built +define("dojox/form/manager/_FormMixin", [ + "dojo/_base/lang", + "dojo/_base/kernel", + "dojo/_base/event", + "dojo/window", + "./_Mixin", + "dojo/_base/declare" +], function(lang, dojo, event, windowUtils, _Mixin, declare){ + var fm = lang.getObject("dojox.form.manager", true), + aa = fm.actionAdapter; + + return declare("dojox.form.manager._FormMixin", null, { + // summary: + // Form manager's mixin for form-specific functionality. + // description: + // This mixin adds automated "onreset", and "onsubmit" event processing + // if we are based on a form node, defines onReset(), onSubmit(), + // reset(), submit(), and isValid() methods like dijit.form.Form. + // It should be used together with dojox.form.manager.Mixin. + + // HTML <FORM> attributes (if we are based on the form element) + name: "", + action: "", + method: "", + encType: "", + "accept-charset": "", + accept: "", + target: "", + + startup: function(){ + this.isForm = this.domNode.tagName.toLowerCase() == "form"; + if(this.isForm){ + this.connect(this.domNode, "onreset", "_onReset"); + this.connect(this.domNode, "onsubmit", "_onSubmit"); + } + this.inherited(arguments); + }, + + // form-specific functionality + + _onReset: function(evt){ + // NOTE: this function is taken from dijit.formForm, it works only + // for form-based managers. + + // create fake event so we can know if preventDefault() is called + var faux = { + returnValue: true, // the IE way + preventDefault: function(){ // not IE + this.returnValue = false; + }, + stopPropagation: function(){}, currentTarget: evt.currentTarget, target: evt.target + }; + // if return value is not exactly false, and haven't called preventDefault(), then reset + if(!(this.onReset(faux) === false) && faux.returnValue){ + this.reset(); + } + event.stop(evt); + return false; + }, + + onReset: function(){ + // summary: + // Callback when user resets the form. This method is intended + // to be over-ridden. When the `reset` method is called + // programmatically, the return value from `onReset` is used + // to compute whether or not resetting should proceed + return true; // Boolean + }, + + reset: function(){ + // summary: + // Resets form widget values. + this.inspectFormWidgets(aa(function(_, widget){ + if(widget.reset){ + widget.reset(); + } + })); + if(this.isForm){ + this.domNode.reset(); + } + return this; + }, + + _onSubmit: function(evt){ + // NOTE: this function is taken from dijit.formForm, it works only + // for form-based managers. + + if(this.onSubmit(evt) === false){ // only exactly false stops submit + event.stop(evt); + } + }, + + onSubmit: function(){ + // summary: + // Callback when user submits the form. This method is + // intended to be over-ridden, but by default it checks and + // returns the validity of form elements. When the `submit` + // method is called programmatically, the return value from + // `onSubmit` is used to compute whether or not submission + // should proceed + + return this.isValid(); // Boolean + }, + + submit: function(){ + // summary: + // programmatically submit form if and only if the `onSubmit` returns true + if(this.isForm){ + if(!(this.onSubmit() === false)){ + this.domNode.submit(); + } + } + }, + + isValid: function(){ + // summary: + // Make sure that every widget that has a validator function returns true. + for(var name in this.formWidgets){ + var stop = false; + aa(function(_, widget){ + if(!widget.get("disabled") && widget.isValid && !widget.isValid()){ + stop = true; + } + }).call(this, null, this.formWidgets[name].widget); + if(stop){ + return false; + } + } + return true; + }, + validate: function(){ + var isValid = true, + formWidgets = this.formWidgets, + didFocus = false, name; + + for(name in formWidgets){ + aa(function(_, widget){ + // Need to set this so that "required" widgets get their + // state set. + widget._hasBeenBlurred = true; + var valid = widget.disabled || !widget.validate || widget.validate(); + if(!valid && !didFocus){ + // Set focus of the first non-valid widget + windowUtils.scrollIntoView(widget.containerNode || widget.domNode); + widget.focus(); + didFocus = true; + } + isValid = isValid && valid; + }).call(this, null, formWidgets[name].widget); + } + + return isValid; + } + }); +}); diff --git a/js/dojo/dojox/form/manager/_Mixin.js b/js/dojo/dojox/form/manager/_Mixin.js new file mode 100644 index 0000000..b64a725 --- /dev/null +++ b/js/dojo/dojox/form/manager/_Mixin.js @@ -0,0 +1,498 @@ +//>>built +define("dojox/form/manager/_Mixin", [ + "dojo/_base/window", + "dojo/_base/lang", + "dojo/_base/array", + "dojo/_base/connect", + "dojo/dom-attr", + "dojo/dom-class", + "dijit/_base/manager", + "dijit/_Widget", + "dijit/form/_FormWidget", + "dijit/form/Button", + "dijit/form/CheckBox", + "dojo/_base/declare" +], function(win, lang, array, connect, domAttr, domClass, manager, Widget, FormWidget, Button, CheckBox, declare){ + // XXX: This class is loading a bunch of extra widgets just to perform isInstanceOf operations, + // which is wasteful + + var fm = lang.getObject("dojox.form.manager", true), + + aa = fm.actionAdapter = function(action){ + // summary: + // Adapter that automates application of actions to arrays. + // action: Function: + // Function that takes three parameters: a name, an object + // (usually node or widget), and a value. This action will + // be applied to all elements of array. + return function(name, elems, value){ + if(lang.isArray(elems)){ + array.forEach(elems, function(elem){ + action.call(this, name, elem, value); + }, this); + }else{ + action.apply(this, arguments); + } + }; + }, + + ia = fm.inspectorAdapter = function(inspector){ + // summary: + // Adapter that applies an inspector only to the first item of the array. + // inspector: Function: + // Function that takes three parameters: a name, an object + // (usually node or widget), and a value. + return function(name, elem, value){ + return inspector.call(this, name, lang.isArray(elem) ? elem[0] : elem, value); + }; + }, + + skipNames = {domNode: 1, containerNode: 1, srcNodeRef: 1, bgIframe: 1}, + + keys = fm._keys = function(o){ + // similar to dojox.lang.functional.keys + var list = [], key; + for(key in o){ + if(o.hasOwnProperty(key)){ + list.push(key); + } + } + return list; + }, + + registerWidget = function(widget){ + var name = widget.get("name"); + if(name && widget instanceof FormWidget){ + if(name in this.formWidgets){ + var a = this.formWidgets[name].widget; + if(lang.isArray(a)){ + a.push(widget); + }else{ + this.formWidgets[name].widget = [a, widget]; + } + }else{ + this.formWidgets[name] = {widget: widget, connections: []}; + } + }else{ + name = null; + } + return name; + }, + + getObserversFromWidget = function(name){ + var observers = {}; + aa(function(_, w){ + var o = w.get("observer"); + if(o && typeof o == "string"){ + array.forEach(o.split(","), function(o){ + o = lang.trim(o); + if(o && lang.isFunction(this[o])){ + observers[o] = 1; + } + }, this); + } + }).call(this, null, this.formWidgets[name].widget); + return keys(observers); + }, + + connectWidget = function(name, observers){ + var t = this.formWidgets[name], w = t.widget, c = t.connections; + if(c.length){ + array.forEach(c, connect.disconnect); + c = t.connections = []; + } + if(lang.isArray(w)){ + // radio buttons + array.forEach(w, function(w){ + array.forEach(observers, function(o){ + c.push(connect.connect(w, "onChange", this, function(evt){ + // TODO: for some reason for radio button widgets + // w.checked != w.focusNode.checked when value changes. + // We test the underlying value to be 100% sure. + if(this.watching && domAttr.get(w.focusNode, "checked")){ + this[o](w.get("value"), name, w, evt); + } + })); + }, this); + }, this); + }else{ + // the rest + // the next line is a crude workaround for Button that fires onClick instead of onChange + var eventName = w.isInstanceOf(Button) ? + "onClick" : "onChange"; + array.forEach(observers, function(o){ + c.push(connect.connect(w, eventName, this, function(evt){ + if(this.watching){ + this[o](w.get("value"), name, w, evt); + } + })); + }, this); + } + }; + + var _Mixin = declare("dojox.form.manager._Mixin", null, { + // summary: + // Mixin to orchestrate dynamic forms. + // description: + // This mixin provideas a foundation for an enhanced form + // functionality: unified access to individual form elements, + // unified "onchange" event processing, general event + // processing, I/O orchestration, and common form-related + // functionality. See additional mixins in dojox.form.manager + // namespace. + + watching: true, + + startup: function(){ + // summary: + // Called after all the widgets have been instantiated and their + // dom nodes have been inserted somewhere under win.doc.body. + + if(this._started){ return; } + + this.formWidgets = {}; + this.formNodes = {}; + this.registerWidgetDescendants(this); + + this.inherited(arguments); + }, + + destroy: function(){ + // summary: + // Called when the widget is being destroyed + + for(var name in this.formWidgets){ + array.forEach(this.formWidgets[name].connections, connect.disconnect); + } + this.formWidgets = {}; + + this.inherited(arguments); + }, + + // register/unregister widgets and nodes + + registerWidget: function(widget){ + // summary: + // Register a widget with the form manager + // widget: String|Node|dijit.form._FormWidget: + // A widget, or its widgetId, or its DOM node + // returns: Object: + // Returns self + if(typeof widget == "string"){ + widget = manager.byId(widget); + }else if(widget.tagName && widget.cloneNode){ + widget = manager.byNode(widget); + } + var name = registerWidget.call(this, widget); + if(name){ + connectWidget.call(this, name, getObserversFromWidget.call(this, name)); + } + return this; + }, + + unregisterWidget: function(name){ + // summary: + // Removes the widget by name from internal tables unregistering + // connected observers + // name: String: + // Name of the to unregister + // returns: Object: + // Returns self + if(name in this.formWidgets){ + array.forEach(this.formWidgets[name].connections, this.disconnect, this); + delete this.formWidgets[name]; + } + return this; + }, + + registerWidgetDescendants: function(widget){ + // summary: + // Register widget's descendants with the form manager + // widget: String|Node|dijit._Widget: + // A widget, or its widgetId, or its DOM node + // returns: Object: + // Returns self + + // convert to widget, if required + if(typeof widget == "string"){ + widget = manager.byId(widget); + }else if(widget.tagName && widget.cloneNode){ + widget = manager.byNode(widget); + } + + // build the map of widgets + var widgets = array.map(widget.getDescendants(), registerWidget, this); + + // process observers for widgets + array.forEach(widgets, function(name){ + if(name){ + connectWidget.call(this, name, getObserversFromWidget.call(this, name)); + } + }, this); + + // do the same with nodes, if available + return this.registerNodeDescendants ? + this.registerNodeDescendants(widget.domNode) : this; + }, + + unregisterWidgetDescendants: function(widget){ + // summary: + // Unregister widget's descendants with the form manager + // widget: String|Node|dijit._Widget: + // A widget, or its widgetId, or its DOM node + // returns: Object: + // Returns self + + // convert to widget, if required + if(typeof widget == "string"){ + widget = manager.byId(widget); + }else if(widget.tagName && widget.cloneNode){ + widget = manager.byNode(widget); + } + + // unregister widgets by names + array.forEach( + array.map( + widget.getDescendants(), + function(w){ + return w instanceof FormWidget && w.get("name") || null; + } + ), + function(name){ + if(name){ + this.unregisterNode(name); + } + }, + this + ); + + // do the same with nodes, if available + return this.unregisterNodeDescendants ? + this.unregisterNodeDescendants(widget.domNode) : this; + }, + + // value accessors + + formWidgetValue: function(elem, value){ + // summary: + // Set or get a form widget by name. + // elem: String|Object|Array: + // Form element's name, widget object, or array or radio widgets. + // value: Object?: + // Optional. The value to set. + // returns: Object: + // For a getter it returns the value, for a setter it returns + // self. If the elem is not valid, null will be returned. + + var isSetter = arguments.length == 2 && value !== undefined, result; + + if(typeof elem == "string"){ + elem = this.formWidgets[elem]; + if(elem){ + elem = elem.widget; + } + } + + if(!elem){ + return null; // Object + } + + if(lang.isArray(elem)){ + // input/radio array of widgets + if(isSetter){ + array.forEach(elem, function(widget){ + widget.set("checked", false, !this.watching); + }); + array.forEach(elem, function(widget){ + widget.set("checked", widget.value === value, !this.watching); + }); + return this; // self + } + // getter + array.some(elem, function(widget){ + // TODO: for some reason for radio button widgets + // w.checked != w.focusNode.checked when value changes. + // We test the underlying value to be 100% sure. + if(domAttr.get(widget.focusNode, "checked")){ + //if(widget.get("checked")){ + result = widget; + return true; + } + return false; + }); + return result ? result.get("value") : ""; // String + } + + // checkbox widget is a special case :-( + if(elem.isInstanceOf && elem.isInstanceOf(CheckBox)){ + if(isSetter){ + elem.set("value", Boolean(value), !this.watching); + return this; // self + } + return Boolean(elem.get("value")); // Object + } + + // all other elements + if(isSetter){ + elem.set("value", value, !this.watching); + return this; // self + } + return elem.get("value"); // Object + }, + + formPointValue: function(elem, value){ + // summary: + // Set or get a node context by name (using dojoAttachPoint). + // elem: String|Object|Array: + // A node. + // value: Object?: + // Optional. The value to set. + // returns: Object: + // For a getter it returns the value, for a setter it returns + // self. If the elem is not valid, null will be returned. + + if(elem && typeof elem == "string"){ + elem = this[elem]; + } + + if(!elem || !elem.tagName || !elem.cloneNode){ + return null; // Object + } + + if(!domClass.contains(elem, "dojoFormValue")){ + // accessing the value of the attached point not marked with CSS class 'dojoFormValue' + return null; + } + + if(arguments.length == 2 && value !== undefined){ + // setter + elem.innerHTML = value; + return this; // self + } + // getter + return elem.innerHTML; // String + }, + + // inspectors + + inspectFormWidgets: function(inspector, state, defaultValue){ + // summary: + // Run an inspector function on controlled widgets returning a result object. + // inspector: Function: + // A function to be called on a widget. Takes three arguments: a name, a widget object + // or an array of widget objects, and a supplied value. Runs in the context of + // the form manager. Returns a value that will be collected and returned as a state. + // state: Object?: + // Optional. If a name-value dictionary --- only listed names will be processed. + // If an array, all names in the array will be processed with defaultValue. + // If omitted or null, all widgets will be processed with defaultValue. + // defaultValue: Object?: + // Optional. The default state (true, if omitted). + + var name, result = {}; + + if(state){ + if(lang.isArray(state)){ + array.forEach(state, function(name){ + if(name in this.formWidgets){ + result[name] = inspector.call(this, name, this.formWidgets[name].widget, defaultValue); + } + }, this); + }else{ + for(name in state){ + if(name in this.formWidgets){ + result[name] = inspector.call(this, name, this.formWidgets[name].widget, state[name]); + } + } + } + }else{ + for(name in this.formWidgets){ + result[name] = inspector.call(this, name, this.formWidgets[name].widget, defaultValue); + } + } + + return result; // Object + }, + + inspectAttachedPoints: function(inspector, state, defaultValue){ + // summary: + // Run an inspector function on "dojoAttachPoint" nodes returning a result object. + // inspector: Function: + // A function to be called on a node. Takes three arguments: a name, a node or + // an array of nodes, and a supplied value. Runs in the context of the form manager. + // Returns a value that will be collected and returned as a state. + // state: Object?: + // Optional. If a name-value dictionary --- only listed names will be processed. + // If an array, all names in the array will be processed with defaultValue. + // If omitted or null, all attached point nodes will be processed with defaultValue. + // defaultValue: Object?: + // Optional. The default state (true, if omitted). + + var name, result = {}; + + if(state){ + if(lang.isArray(state)){ + array.forEach(state, function(name){ + var elem = this[name]; + if(elem && elem.tagName && elem.cloneNode){ + result[name] = inspector.call(this, name, elem, defaultValue); + } + }, this); + }else{ + for(name in state){ + var elem = this[name]; + if(elem && elem.tagName && elem.cloneNode){ + result[name] = inspector.call(this, name, elem, state[name]); + } + } + } + }else{ + for(name in this){ + if(!(name in skipNames)){ + var elem = this[name]; + if(elem && elem.tagName && elem.cloneNode){ + result[name] = inspector.call(this, name, elem, defaultValue); + } + } + } + } + + return result; // Object + }, + + inspect: function(inspector, state, defaultValue){ + // summary: + // Run an inspector function on controlled elements returning a result object. + // inspector: Function: + // A function to be called on a widget, form element, and an attached node. + // Takes three arguments: a name, a node (domNode in the case of widget) or + // an array of such objects, and a supplied value. Runs in the context of + // the form manager. Returns a value that will be collected and returned as a state. + // state: Object?: + // Optional. If a name-value dictionary --- only listed names will be processed. + // If an array, all names in the array will be processed with defaultValue. + // If omitted or null, all controlled elements will be processed with defaultValue. + // defaultValue: Object?: + // Optional. The default state (true, if omitted). + + var result = this.inspectFormWidgets(function(name, widget, value){ + if(lang.isArray(widget)){ + return inspector.call(this, name, array.map(widget, function(w){ return w.domNode; }), value); + } + return inspector.call(this, name, widget.domNode, value); + }, state, defaultValue); + if(this.inspectFormNodes){ + lang.mixin(result, this.inspectFormNodes(inspector, state, defaultValue)); + } + return lang.mixin(result, this.inspectAttachedPoints(inspector, state, defaultValue)); // Object + } + }); + +// These arguments can be specified for widgets which are used in forms. +// Since any widget can be specified as sub widgets, mix it into the base +// widget class. (This is a hack, but it's effective.) +lang.extend(Widget, { + observer: "" +}); +return _Mixin; +}); diff --git a/js/dojo/dojox/form/manager/_NodeMixin.js b/js/dojo/dojox/form/manager/_NodeMixin.js new file mode 100644 index 0000000..b8a895f --- /dev/null +++ b/js/dojo/dojox/form/manager/_NodeMixin.js @@ -0,0 +1,369 @@ +//>>built +define("dojox/form/manager/_NodeMixin", [ + "dojo/_base/lang", + "dojo/_base/array", + "dojo/_base/connect", + "dojo/dom", + "dojo/dom-attr", + "dojo/query", + "./_Mixin", + "dijit/form/_FormWidget", + "dijit/_base/manager", + "dojo/_base/declare" +], function(lang, array, connect, dom, domAttr, query, _Mixin, _FormWidget, manager, declare){ + var fm = lang.getObject("dojox.form.manager", true), + aa = fm.actionAdapter, + keys = fm._keys, + + ce = fm.changeEvent = function(node){ + // summary: + // Function that returns a valid "onchange" event for a given form node. + // node: Node: + // Form node. + + var eventName = "onclick"; + switch(node.tagName.toLowerCase()){ + case "textarea": + eventName = "onkeyup"; + break; + case "select": + eventName = "onchange"; + break; + case "input": + switch(node.type.toLowerCase()){ + case "text": + case "password": + eventName = "onkeyup"; + break; + } + break; + // button, input/button, input/checkbox, input/radio, + // input/file, input/image, input/submit, input/reset + // use "onclick" (the default) + } + return eventName; // String + }, + + registerNode = function(node, groupNode){ + var name = domAttr.get(node, "name"); + groupNode = groupNode || this.domNode; + if(name && !(name in this.formWidgets)){ + // verify that it is not part of any widget + for(var n = node; n && n !== groupNode; n = n.parentNode){ + if(domAttr.get(n, "widgetId") && manager.byNode(n).isInstanceOf(_FormWidget)){ + // this is a child of some widget --- bail out + return null; + } + } + // register the node + if(node.tagName.toLowerCase() == "input" && node.type.toLowerCase() == "radio"){ + var a = this.formNodes[name]; + a = a && a.node; + if(a && lang.isArray(a)){ + a.push(node); + }else{ + this.formNodes[name] = {node: [node], connections: []}; + } + }else{ + this.formNodes[name] = {node: node, connections: []}; + } + }else{ + name = null; + } + return name; + }, + + getObserversFromNode = function(name){ + var observers = {}; + aa(function(_, n){ + var o = domAttr.get(n, "observer"); + if(o && typeof o == "string"){ + array.forEach(o.split(","), function(o){ + o = lang.trim(o); + if(o && lang.isFunction(this[o])){ + observers[o] = 1; + } + }, this); + } + }).call(this, null, this.formNodes[name].node); + return keys(observers); + }, + + connectNode = function(name, observers){ + var t = this.formNodes[name], c = t.connections; + if(c.length){ + array.forEach(c, connect.disconnect); + c = t.connections = []; + } + aa(function(_, n){ + // the next line is a crude workaround for Button that fires onClick instead of onChange + var eventName = ce(n); + array.forEach(observers, function(o){ + c.push(connect.connect(n, eventName, this, function(evt){ + if(this.watching){ + this[o](this.formNodeValue(name), name, n, evt); + } + })); + }, this); + }).call(this, null, t.node); + }; + + return declare("dojox.form.manager._NodeMixin", null, { + // summary: + // Mixin to orchestrate dynamic forms (works with DOM nodes). + // description: + // This mixin provideas a foundation for an enhanced form + // functionality: unified access to individual form elements, + // unified "onchange" event processing, and general event + // processing. It complements dojox.form.manager._Mixin + // extending the functionality to DOM nodes. + + destroy: function(){ + // summary: + // Called when the widget is being destroyed + + for(var name in this.formNodes){ + array.forEach(this.formNodes[name].connections, connect.disconnect); + } + this.formNodes = {}; + + this.inherited(arguments); + }, + + // register/unregister widgets and nodes + + registerNode: function(node){ + // summary: + // Register a node with the form manager + // node: String|Node: + // A node, or its id + // returns: Object: + // Returns self + if(typeof node == "string"){ + node = dom.byId(node); + } + var name = registerNode.call(this, node); + if(name){ + connectNode.call(this, name, getObserversFromNode.call(this, name)); + } + return this; + }, + + unregisterNode: function(name){ + // summary: + // Removes the node by name from internal tables unregistering + // connected observers + // name: String: + // Name of the to unregister + // returns: Object: + // Returns self + if(name in this.formNodes){ + array.forEach(this.formNodes[name].connections, this.disconnect, this); + delete this.formNodes[name]; + } + return this; + }, + + registerNodeDescendants: function(node){ + // summary: + // Register node's descendants (form nodes) with the form manager + // node: String|Node: + // A widget, or its widgetId, or its DOM node + // returns: Object: + // Returns self + + if(typeof node == "string"){ + node = dom.byId(node); + } + + query("input, select, textarea, button", node). + map(function(n){ + return registerNode.call(this, n, node); + }, this). + forEach(function(name){ + if(name){ + connectNode.call(this, name, getObserversFromNode.call(this, name)); + } + }, this); + + return this; + }, + + unregisterNodeDescendants: function(node){ + // summary: + // Unregister node's descendants (form nodes) with the form manager + // node: String|Node: + // A widget, or its widgetId, or its DOM node + // returns: Object: + // Returns self + + if(typeof node == "string"){ + node = dom.byId(node); + } + + query("input, select, textarea, button", node). + map(function(n){ return domAttr.get(node, "name") || null; }). + forEach(function(name){ + if(name){ + this.unregisterNode(name); + } + }, this); + + return this; + }, + + // value accessors + + formNodeValue: function(elem, value){ + // summary: + // Set or get a form element by name. + // elem: String|Node|Array: + // Form element's name, DOM node, or array or radio nodes. + // value: Object?: + // Optional. The value to set. + // returns: Object: + // For a getter it returns the value, for a setter it returns + // self. If the elem is not valid, null will be returned. + + var isSetter = arguments.length == 2 && value !== undefined, result; + + if(typeof elem == "string"){ + elem = this.formNodes[elem]; + if(elem){ + elem = elem.node; + } + } + + if(!elem){ + return null; // Object + } + + if(lang.isArray(elem)){ + // input/radio array + if(isSetter){ + array.forEach(elem, function(node){ + node.checked = ""; + }); + array.forEach(elem, function(node){ + node.checked = node.value === value ? "checked" : ""; + }); + return this; // self + } + // getter + array.some(elem, function(node){ + if(node.checked){ + result = node; + return true; + } + return false; + }); + return result ? result.value : ""; // String + } + // all other elements + switch(elem.tagName.toLowerCase()){ + case "select": + if(elem.multiple){ + // multiple is allowed + if(isSetter){ + if(lang.isArray(value)){ + var dict = {}; + array.forEach(value, function(v){ + dict[v] = 1; + }); + query("> option", elem).forEach(function(opt){ + opt.selected = opt.value in dict; + }); + return this; // self + } + // singular property + query("> option", elem).forEach(function(opt){ + opt.selected = opt.value === value; + }); + return this; // self + } + // getter + var result = query("> option", elem).filter(function(opt){ + return opt.selected; + }).map(function(opt){ + return opt.value; + }); + return result.length == 1 ? result[0] : result; // Object + } + // singular + if(isSetter){ + query("> option", elem).forEach(function(opt){ + opt.selected = opt.value === value; + }); + return this; // self + } + // getter + return elem.value || ""; // String + case "button": + if(isSetter){ + elem.innerHTML = "" + value; + return this; + } + // getter + return elem.innerHTML; + case "input": + if(elem.type.toLowerCase() == "checkbox"){ + // input/checkbox element + if(isSetter){ + elem.checked = value ? "checked" : ""; + return this; + } + // getter + return Boolean(elem.checked); + } + } + // the rest of inputs + if(isSetter){ + elem.value = "" + value; + return this; + } + // getter + return elem.value; + }, + + // inspectors + + inspectFormNodes: function(inspector, state, defaultValue){ + // summary: + // Run an inspector function on controlled form elements returning a result object. + // inspector: Function: + // A function to be called on a form element. Takes three arguments: a name, a node or + // an array of nodes, and a supplied value. Runs in the context of the form manager. + // Returns a value that will be collected and returned as a state. + // state: Object?: + // Optional. If a name-value dictionary --- only listed names will be processed. + // If an array, all names in the array will be processed with defaultValue. + // If omitted or null, all form elements will be processed with defaultValue. + // defaultValue: Object?: + // Optional. The default state (true, if omitted). + + var name, result = {}; + + if(state){ + if(lang.isArray(state)){ + array.forEach(state, function(name){ + if(name in this.formNodes){ + result[name] = inspector.call(this, name, this.formNodes[name].node, defaultValue); + } + }, this); + }else{ + for(name in state){ + if(name in this.formNodes){ + result[name] = inspector.call(this, name, this.formNodes[name].node, state[name]); + } + } + } + }else{ + for(name in this.formNodes){ + result[name] = inspector.call(this, name, this.formNodes[name].node, defaultValue); + } + } + + return result; // Object + } + }); +}); diff --git a/js/dojo/dojox/form/manager/_ValueMixin.js b/js/dojo/dojox/form/manager/_ValueMixin.js new file mode 100644 index 0000000..785a15f --- /dev/null +++ b/js/dojo/dojox/form/manager/_ValueMixin.js @@ -0,0 +1,82 @@ +//>>built +define("dojox/form/manager/_ValueMixin", [ + "dojo/_base/lang", + "dojo/_base/kernel", + "dojo/_base/declare" +], function(lang, dojo, declare){ +return declare("dojox.form.manager._ValueMixin", null, { + // summary: + // Form manager's mixin for getting/setting form values in the unified manner. + // description: + // This mixin adds unified access to form widgets and form elements + // in terms of name-value regardless of the underlying type of + // an element. It should be used together with dojox.form.manager.Mixin. + + elementValue: function(name, value){ + // summary: + // Set or get a form widget/element or an attached point node by name. + // name: String: + // The name. + // value: Object?: + // Optional. The value to set. + + if(name in this.formWidgets){ + return this.formWidgetValue(name, value); // Object + } + + if(this.formNodes && name in this.formNodes){ + return this.formNodeValue(name, value); // Object + } + + return this.formPointValue(name, value); // Object + }, + + gatherFormValues: function(names){ + // summary: + // Collect form values. + // names: Object?: + // If it is an array, it is a list of names of form elements to be collected. + // If it is an object, dictionary keys are names to be collected. + // If it is omitted, all known form elements are to be collected. + + var result = this.inspectFormWidgets(function(name){ + return this.formWidgetValue(name); + }, names); + + if(this.inspectFormNodes){ + lang.mixin(result, this.inspectFormNodes(function(name){ + return this.formNodeValue(name); + }, names)); + } + + lang.mixin(result, this.inspectAttachedPoints(function(name){ + return this.formPointValue(name); + }, names)); + + return result; // Object + }, + + setFormValues: function(values){ + // summary: + // Set values to form elements + // values: Object: + // A dictionary of key-value pairs. + if(values){ + this.inspectFormWidgets(function(name, widget, value){ + this.formWidgetValue(name, value); + }, values); + + if(this.inspectFormNodes){ + this.inspectFormNodes(function(name, node, value){ + this.formNodeValue(name, value); + }, values); + } + + this.inspectAttachedPoints(function(name, node, value){ + this.formPointValue(name, value); + }, values); + } + return this; + } +}); +}); |
