42
42
dependencyManager.prototype = {
45
_nameCollections : [],
47
_nameCollections : null,
46
49
initializer : function(config) {
47
50
var i = 0, nodeName;
48
51
this._form = Y.one('#'+formid);
49
52
for (i in dependencies) {
50
this._depElements[i] = this.elementsByName(i);
51
if (this._depElements[i].size() == 0) {
53
var elements = this.elementsByName(i);
54
if (elements.size() == 0) {
54
this._depElements[i].each(function(node){
57
elements.each(function(node){
55
58
nodeName = node.get('nodeName').toUpperCase();
56
59
if (nodeName == 'INPUT') {
57
60
if (node.getAttribute('type').match(/^(button|submit|radio|checkbox)$/)) {
58
node.on('click', this.checkDependencies, this);
61
node.on('click', this.updateEventDependencies, this);
60
node.on('blur', this.checkDependencies, this);
63
node.on('blur', this.updateEventDependencies, this);
62
node.on('change', this.checkDependencies, this);
65
node.on('change', this.updateEventDependencies, this);
63
66
} else if (nodeName == 'SELECT') {
64
node.on('change', this.checkDependencies, this);
67
node.on('change', this.updateEventDependencies, this);
66
node.on('click', this.checkDependencies, this);
67
node.on('blur', this.checkDependencies, this);
68
node.on('change', this.checkDependencies, this);
69
node.on('click', this.updateEventDependencies, this);
70
node.on('blur', this.updateEventDependencies, this);
71
node.on('change', this.updateEventDependencies, this);
73
76
if (input.getAttribute('type')=='reset') {
74
77
input.on('click', function(){
75
78
this._form.reset();
76
this.checkDependencies();
79
this.updateAllDependencies();
81
return this.checkDependencies(null);
84
return this.updateAllDependencies();
87
* Initializes the mapping from element name to YUI NodeList
89
initElementsByName : function() {
91
// Collect element names
92
for (var i in dependencies) {
93
names[i] = new Y.NodeList();
94
for (var condition in dependencies[i]) {
95
for (var value in dependencies[i][condition]) {
96
for (var ei in dependencies[i][condition][value]) {
97
names[dependencies[i][condition][value][ei]] = new Y.NodeList();
102
// Locate elements for each name
103
this._form.get('elements').each(function(node){
104
var name = node.getAttribute('name');
106
names[name].push(node);
109
this._nameCollections = names;
84
112
* Gets all elements in the form by their name and returns
115
* @param {string} name The form element name.
116
* @return {Y.NodeList}
88
118
elementsByName : function(name) {
119
if (!this._nameCollections) {
120
this.initElementsByName();
89
122
if (!this._nameCollections[name]) {
91
this._form.get('elements').each(function(){
92
if (this.getAttribute('name') == name) {
96
this._nameCollections[name] = new Y.NodeList(elements);
123
return new Y.NodeList();
98
125
return this._nameCollections[name];
104
131
* Changes are made by functions title _dependency_{dependencytype}
105
132
* and more can easily be introduced by defining further functions.
134
* @param {EventFacade | null} e The event, if any.
135
* @param {string} name The form element name to check dependencies against.
107
checkDependencies : function(e) {
110
dependon, condition, value,
111
lock, hide, checkfunction, result;
112
for (dependon in dependencies) {
113
if (this._depElements[dependon].size() == 0) {
116
for (condition in dependencies[dependon]) {
117
for (value in dependencies[dependon][condition]) {
120
checkfunction = '_dependency_'+condition;
121
if (Y.Lang.isFunction(this[checkfunction])) {
122
result = this[checkfunction].apply(this, [this._depElements[dependon], value, e]);
124
result = this._dependency_default(this._depElements[dependon], value, e);
126
lock = result.lock || false;
127
hide = result.hide || false;
128
for (var ei in dependencies[dependon][condition][value]) {
129
var eltolock = dependencies[dependon][condition][value][ei];
131
tohide[eltolock] = true;
133
if (tolock[eltolock] != null) {
134
tolock[eltolock] = lock || tolock[eltolock];
136
tolock[eltolock] = lock;
137
checkDependencies : function(e, dependon) {
140
condition, value, lock, hide,
141
checkfunction, result, elements;
142
if (!dependencies[dependon]) {
145
elements = this.elementsByName(dependon);
146
for (condition in dependencies[dependon]) {
147
for (value in dependencies[dependon][condition]) {
148
checkfunction = '_dependency_'+condition;
149
if (Y.Lang.isFunction(this[checkfunction])) {
150
result = this[checkfunction].apply(this, [elements, value, e]);
152
result = this._dependency_default(elements, value, e);
154
lock = result.lock || false;
155
hide = result.hide || false;
156
for (var ei in dependencies[dependon][condition][value]) {
157
var eltolock = dependencies[dependon][condition][value][ei];
158
tohide[eltolock] = tohide[eltolock] || hide;
159
tolock[eltolock] = tolock[eltolock] || lock;
142
163
for (var el in tolock) {
143
this._disableElement(el, tolock[el]);
144
if (tohide.propertyIsEnumerable(el)) {
145
this._hideElement(el, tohide[el]);
164
var needsupdate = false;
166
this._locks[el] = this._locks[el] || [];
167
if (!this._locks[el][dependon]) {
168
this._locks[el][dependon] = true;
171
} else if (this._locks[el] && this._locks[el][dependon]) {
172
delete this._locks[el][dependon];
176
this._hides[el] = this._hides[el] || [];
177
if (!this._hides[el][dependon]) {
178
this._hides[el][dependon] = true;
181
} else if (this._hides[el] && this._hides[el][dependon]) {
182
delete this._hides[el][dependon];
186
this._dirty[el] = true;
151
* Disabled all form elements with the given name
192
* Update all dependencies in form
194
updateAllDependencies : function() {
195
for (var el in dependencies) {
196
this.checkDependencies(null, el);
201
* Update dependencies associated with event
203
* @param {Event} e The event.
205
updateEventDependencies : function(e) {
206
var el = e.target.getAttribute('name');
207
this.checkDependencies(e, el);
211
* Flush pending changes to the form
213
updateForm : function() {
214
for (var el in this._dirty) {
215
if (this._locks[el]) {
216
var locked = !this._isObjectEmpty(this._locks[el]);
217
this._disableElement(el, locked);
219
if (this._hides[el]) {
220
var hidden = !this._isObjectEmpty(this._hides[el]);
221
this._hideElement(el, hidden);
227
* Disables or enables all form elements with the given name
229
* @param {string} name The form element name.
230
* @param {boolean} disabled True to disable, false to enable.
153
232
_disableElement : function(name, disabled) {
154
233
var els = this.elementsByName(name);
234
var filepicker = this.isFilePicker(name);
235
els.each(function(node){
158
this.setAttribute('disabled', 'disabled');
237
node.setAttribute('disabled', 'disabled');
160
this.removeAttribute('disabled');
239
node.removeAttribute('disabled');
163
242
// Extra code to disable filepicker or filemanager form elements
164
var fitem = this.ancestor('.fitem');
165
if (fitem && (fitem.hasClass('fitem_ffilemanager') || fitem.hasClass('fitem_ffilepicker'))) {
167
fitem.addClass('disabled');
169
fitem.removeClass('disabled');
244
var fitem = node.ancestor('.fitem');
247
fitem.addClass('disabled');
249
fitem.removeClass('disabled');
175
* Hides all elements with the given name.
256
* Hides or shows all form elements with the given name.
258
* @param {string} name The form element name.
259
* @param {boolean} disabled True to hide, false to show.
177
261
_hideElement : function(name, hidden) {
178
262
var els = this.elementsByName(name);
180
var e = els.ancestor('.fitem');
263
els.each(function(node){
264
var e = node.ancestor('.fitem');
183
267
display : (hidden)?'none':''