2011-07-29 18:48:43 +00:00
|
|
|
// vim: et:ts=4:sw=4:sts=4:ft=javascript
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2011-05-16 08:24:46 +00:00
|
|
|
/*@
|
|
|
|
Ox.Form <f:Ox.Element> Form Object
|
|
|
|
() -> <f> Form Object
|
|
|
|
(options) -> <f> Form Object
|
|
|
|
(options, self) -> <f> Form Object
|
|
|
|
options <o> Options object
|
|
|
|
error <s> error
|
|
|
|
id <s> id
|
2011-05-16 10:49:48 +00:00
|
|
|
items <a|[]> []
|
|
|
|
submit <f|null> on submit function
|
2011-05-16 08:24:46 +00:00
|
|
|
self <o> shared private variable
|
|
|
|
@*/
|
|
|
|
|
|
|
|
Ox.Form = function(options, self) {
|
2011-04-22 22:03:10 +00:00
|
|
|
|
2011-06-19 17:48:32 +00:00
|
|
|
self = self || {};
|
|
|
|
var that = Ox.Element({}, self)
|
2011-04-22 22:03:10 +00:00
|
|
|
.defaults({
|
|
|
|
error: '',
|
|
|
|
id: '',
|
|
|
|
items: [],
|
2011-11-07 12:15:10 +00:00
|
|
|
// FIXME: don't pass function,
|
|
|
|
// listen to event instead!
|
2011-04-22 22:03:10 +00:00
|
|
|
submit: null
|
|
|
|
})
|
|
|
|
.options(options || {}) // fixme: the || {} can be done once, in the options function
|
|
|
|
.addClass('OxForm');
|
|
|
|
|
2011-09-17 18:36:09 +00:00
|
|
|
Ox.extend(self, {
|
2011-04-22 22:03:10 +00:00
|
|
|
$items: [],
|
|
|
|
$messages: [],
|
|
|
|
formIsValid: false,
|
|
|
|
itemIds: [],
|
|
|
|
itemIsValid: []
|
|
|
|
});
|
|
|
|
|
|
|
|
self.options.items.forEach(function(item, i) {
|
2011-11-02 17:27:20 +00:00
|
|
|
var validateItem = item.options('validate');
|
|
|
|
if (validateItem) {
|
|
|
|
validateItem(item.value(), function(data) {
|
|
|
|
self.itemIsValid[i] = data.valid;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
self.itemIsValid[i] = item.value().length > 0;
|
|
|
|
}
|
2011-04-22 22:03:10 +00:00
|
|
|
self.itemIds[i] = item.options('id') || item.id;
|
2011-11-30 14:56:01 +00:00
|
|
|
self.$items[i] = Ox.FormItem({element: item}).appendTo(that);
|
2011-04-22 22:03:10 +00:00
|
|
|
item.bindEvent({
|
|
|
|
/*
|
2011-09-17 17:39:38 +00:00
|
|
|
blur: function(data) {
|
2011-04-22 22:03:10 +00:00
|
|
|
validate(i, data.valid);
|
|
|
|
if (data.valid) {
|
|
|
|
self.$messages[i].html('').hide();
|
|
|
|
} else {
|
|
|
|
self.$messages[i].html(data.message).show();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
*/
|
2011-09-17 17:39:38 +00:00
|
|
|
autovalidate: function(data) {
|
2011-11-02 17:27:20 +00:00
|
|
|
validateForm(i, data.valid);
|
2011-04-22 22:03:10 +00:00
|
|
|
data.valid && self.$items[i].setMessage('');
|
2011-10-03 16:14:01 +00:00
|
|
|
},
|
2011-10-09 21:13:16 +00:00
|
|
|
/*
|
2011-10-03 16:14:01 +00:00
|
|
|
// fixme: should't inputs also trigger a change event?
|
|
|
|
blur: function(data) {
|
|
|
|
that.triggerEvent('change', {
|
|
|
|
id: self.itemIds[i],
|
|
|
|
data: data
|
|
|
|
});
|
|
|
|
},
|
2011-10-09 21:13:16 +00:00
|
|
|
*/
|
2011-10-03 16:14:01 +00:00
|
|
|
change: function(data) {
|
2011-10-22 14:50:31 +00:00
|
|
|
// fixme: shouldn't this be key/value instead of id/data?
|
2011-10-03 16:14:01 +00:00
|
|
|
that.triggerEvent('change', {
|
|
|
|
id: self.itemIds[i],
|
|
|
|
data: data
|
|
|
|
});
|
2011-04-22 22:03:10 +00:00
|
|
|
},
|
2011-09-17 17:39:38 +00:00
|
|
|
submit: function(data) {
|
2011-04-22 22:03:10 +00:00
|
|
|
self.formIsValid && that.submit();
|
|
|
|
},
|
2011-09-17 17:39:38 +00:00
|
|
|
validate: function(data) {
|
2011-11-02 17:27:20 +00:00
|
|
|
validateForm(i, data.valid);
|
2011-10-29 10:34:41 +00:00
|
|
|
// timeout needed for cases where the form is removed
|
|
|
|
// from the DOM, triggering blur of an empty item -
|
|
|
|
// in this case, we don't want the message to appear
|
|
|
|
setTimeout(function() {
|
|
|
|
self.$items[i].setMessage(data.valid ? '' : data.message);
|
|
|
|
}, 0);
|
2011-04-22 22:03:10 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-05-22 12:39:57 +00:00
|
|
|
function getItemIndexById(id) {
|
2011-04-22 22:03:10 +00:00
|
|
|
return self.itemIds.indexOf(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
function submitCallback(data) {
|
2011-11-02 17:27:20 +00:00
|
|
|
Ox.forEach(data, function(v, i) {
|
2011-10-22 15:22:39 +00:00
|
|
|
self.$items[getItemIndexById(v.id)].setMessage(v.message);
|
2011-04-22 22:03:10 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-11-02 17:27:20 +00:00
|
|
|
function validateForm(pos, valid) {
|
2011-04-22 22:03:10 +00:00
|
|
|
self.itemIsValid[pos] = valid;
|
2011-11-04 15:54:28 +00:00
|
|
|
Ox.Log('Form', 'VALID???', self.itemIsValid)
|
2011-04-22 22:03:10 +00:00
|
|
|
if (Ox.every(self.itemIsValid) != self.formIsValid) {
|
|
|
|
self.formIsValid = !self.formIsValid;
|
|
|
|
that.triggerEvent('validate', {
|
|
|
|
valid: self.formIsValid
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
that.addItem = function(pos, item) {
|
2011-11-04 15:54:28 +00:00
|
|
|
Ox.Log('Form', 'addItem', pos)
|
2011-04-22 22:03:10 +00:00
|
|
|
self.options.items.splice(pos, 0, item);
|
2011-06-19 17:48:32 +00:00
|
|
|
self.$items.splice(pos, 0, Ox.FormItem({element: item}));
|
2011-04-22 22:03:10 +00:00
|
|
|
pos == 0 ?
|
|
|
|
self.$items[pos].insertBefore(self.$items[0]) :
|
|
|
|
self.$items[pos].insertAfter(self.$items[pos - 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
that.removeItem = function(pos) {
|
2011-11-04 15:54:28 +00:00
|
|
|
Ox.Log('Form', 'removeItem', pos);
|
2011-11-04 22:14:30 +00:00
|
|
|
self.$items[pos].remove();
|
2011-04-22 22:03:10 +00:00
|
|
|
self.options.items.splice(pos, 1);
|
|
|
|
self.$items.splice(pos, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
that.submit = function() {
|
2011-11-02 17:27:20 +00:00
|
|
|
// fixme: this seems to be unneeded
|
2011-11-04 15:54:28 +00:00
|
|
|
//Ox.Log('Form', '---- that.values()', that.values())
|
2011-04-22 22:03:10 +00:00
|
|
|
self.options.submit(that.values(), submitCallback);
|
|
|
|
};
|
|
|
|
|
|
|
|
that.values = function() { // fixme: can this be private?
|
|
|
|
/*
|
|
|
|
get/set form values
|
|
|
|
call without arguments to get current form values
|
|
|
|
pass values as array to set values (not implemented)
|
|
|
|
*/
|
|
|
|
var values = {};
|
|
|
|
if (arguments.length == 0) {
|
|
|
|
self.$items.forEach(function($item, i) {
|
|
|
|
values[self.itemIds[i]] = self.$items[i].value();
|
2011-05-30 12:06:58 +00:00
|
|
|
//fixme: make the following work
|
2011-12-21 15:33:52 +00:00
|
|
|
//values[self.itemIds[i]] = self.$items[i].value();
|
2011-04-22 22:03:10 +00:00
|
|
|
});
|
2011-11-04 15:54:28 +00:00
|
|
|
//Ox.Log('Form', 'VALUES', values)
|
2011-04-22 22:03:10 +00:00
|
|
|
return values;
|
|
|
|
} else {
|
2011-11-04 15:54:28 +00:00
|
|
|
Ox.Log('Form', 'SET FORM VALUES', arguments[0])
|
2011-05-22 12:39:57 +00:00
|
|
|
Ox.forEach(arguments[0], function(value, key) {
|
|
|
|
var index = getItemIndexById(key);
|
2011-11-04 15:54:28 +00:00
|
|
|
//index > -1 && Ox.Log('Form', ':::::::', key, value)
|
2011-12-21 15:33:52 +00:00
|
|
|
index > -1 && self.options.items[index].value(value);
|
2011-04-22 22:03:10 +00:00
|
|
|
});
|
|
|
|
return that;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return that;
|
|
|
|
|
|
|
|
};
|