oxjs/source/UI/js/Form/FormPanel.js

204 lines
6.3 KiB
JavaScript
Raw Normal View History

2011-11-30 14:52:42 +00:00
'use strict';
2012-05-21 10:38:18 +00:00
/*@
2012-05-31 10:32:54 +00:00
Ox.FormPanel <f> Form Panel
options <o> Options
self <o> Shared private variable
2012-05-31 10:32:54 +00:00
([options[, self]]) -> <o:Ox.Element> Form Panel
2012-07-09 13:59:50 +00:00
change <!> Fires when a value changed
select <!> Fires when a section gets selected
validate <!> Fires when the form becomes valid or invalid
2012-05-21 10:38:18 +00:00
@*/
2011-11-30 14:52:42 +00:00
Ox.FormPanel = function(options, self) {
self = self || {};
var that = Ox.Element({}, self)
.defaults({
2013-10-31 12:46:51 +00:00
form: [],
listSize: 256
2011-11-30 14:52:42 +00:00
})
.options(options || {});
self.section = 0;
2012-06-13 13:56:42 +00:00
self.sectionTitle = self.options.form[self.section].title;
2012-06-27 07:41:10 +00:00
self.$list = Ox.TableList({
2011-11-30 14:52:42 +00:00
columns: [
2012-06-13 13:56:42 +00:00
{
id: 'id',
visible: false
},
2011-11-30 14:52:42 +00:00
{
format: function(value) {
return $('<img>')
.attr({
2014-09-26 12:31:20 +00:00
src: Ox.UI.getImageURL('symbolCheck')
2011-11-30 14:52:42 +00:00
})
.css({
width: '10px',
height: '10px',
margin: '2px 2px 2px 0',
opacity: value ? 1 : 0.1
})
},
id: 'valid',
2013-05-09 13:03:33 +00:00
title: Ox._('Valid'),
2011-11-30 14:52:42 +00:00
visible: true,
width: 16
},
{
format: function(value) {
2012-06-13 13:56:42 +00:00
return (Ox.indexOf(self.options.form, function(section) {
return section.title == value;
}) + 1) + '. ' + value;
2011-11-30 14:52:42 +00:00
},
id: 'title',
2013-05-09 13:03:33 +00:00
title: Ox._('Title'),
2011-11-30 14:52:42 +00:00
visible: true,
2013-10-31 12:50:59 +00:00
width: self.options.listSize - 16
2011-11-30 14:52:42 +00:00
}
],
2011-12-30 09:33:01 +00:00
items: self.options.form.map(function(section) {
2012-06-13 13:56:42 +00:00
return {id: section.id, title: section.title, valid: false};
2011-12-30 09:33:01 +00:00
}),
2011-11-30 14:52:42 +00:00
max: 1,
min: 1,
2012-06-13 13:56:42 +00:00
selected: [self.options.form[0].id],
2011-11-30 14:52:42 +00:00
sort: [{key: 'id', operator: '+'}],
unique: 'id',
2013-10-31 12:46:51 +00:00
width: self.options.listSize
2011-11-30 14:52:42 +00:00
}).bindEvent({
select: function(data) {
self.$sections[self.section].hide();
2012-06-13 13:56:42 +00:00
self.section = Ox.getIndexById(self.options.form, data.ids[0]);
2011-11-30 14:52:42 +00:00
self.$sections[self.section].show();
2012-07-09 13:59:50 +00:00
that.triggerEvent('select', {section: data.ids[0]});
2011-11-30 14:52:42 +00:00
}
});
self.$section = $('<div>')
.css({overflowY: 'auto'});
self.$forms = [];
self.$sections = self.options.form.map(function(section, i) {
return $('<div>')
.css({
width: (
section.descriptionWidth || section.items[0].options('width')
) + 'px',
margin: '16px'
})
.append(
$('<div>')
.addClass('OxSelectable')
2011-11-30 14:52:42 +00:00
.css({marginBottom: '8px', fontWeight: 'bold'})
2011-12-30 09:33:01 +00:00
.html((i + 1) + '. ' + section.title)
2011-11-30 14:52:42 +00:00
)
.append(
$('<div>')
.addClass('OxSelectable')
.css({marginBottom: '16px'})
2011-11-30 14:52:42 +00:00
.html(section.description)
)
.append(
self.$forms[i] = Ox.Form({
2011-12-30 09:33:01 +00:00
items: section.items,
validate: section.validate
2011-11-30 14:52:42 +00:00
})
.bindEvent({
change: function(data) {
2012-06-13 13:56:42 +00:00
self.$list.value(section.id, 'valid', self.$forms[i].valid());
2011-12-30 09:33:01 +00:00
that.triggerEvent('change', {
2012-06-13 13:56:42 +00:00
section: section.id,
2011-12-30 09:33:01 +00:00
data: data
});
2011-11-30 14:52:42 +00:00
},
validate: function(data) {
2012-06-13 13:56:42 +00:00
self.$list.value(section.id, 'valid', data.valid);
2011-12-01 10:52:23 +00:00
that.triggerEvent('validate', {
2012-06-13 13:56:42 +00:00
section: section.id,
2011-12-01 10:52:23 +00:00
data: data
});
2011-11-30 14:52:42 +00:00
}
})
)
.hide()
.appendTo(self.$section);
});
2012-06-27 07:41:10 +00:00
2012-06-27 08:20:11 +00:00
self.$list.bindEvent('load', function() {
self.$forms.forEach(function($form, i) {
self.$list.value(self.options.form[i].id, 'valid', $form.valid());
});
2011-12-30 09:33:01 +00:00
});
2011-11-30 14:52:42 +00:00
self.$sections[0].show();
2014-09-22 11:17:35 +00:00
that.setElement(Ox.SplitPanel({
2011-11-30 14:52:42 +00:00
elements: [
{
element: self.$list,
2013-10-31 12:46:51 +00:00
size: self.options.listSize
2011-11-30 14:52:42 +00:00
},
{
element: self.$section
}
],
orientation: 'horizontal'
2014-09-22 11:17:35 +00:00
}));
2011-11-30 14:52:42 +00:00
2012-05-21 10:38:18 +00:00
/*@
renderPrintVersion <f> renderPrintVersion
(title) -> <s>
@*/
2011-12-30 09:33:01 +00:00
that.renderPrintVersion = function(title) {
var $printVersion = $('<div>').css({overflowY: 'auto'});
$printVersion.append(
$('<div>')
.addClass('OxFormSectionTitle')
.css({
height: '16px',
padding: '16px 16px 8px 16px',
fontWeight: 'bold'
})
.html(title)
);
self.$sections.forEach(function($section, i) {
// jQuery bug: textarea html/val does not survive cloning
// http://bugs.jquery.com/ticket/3016
var $clone = $section.clone(true),
textareas = {
section: $section.find('textarea'),
clone: $clone.find('textarea')
};
textareas.section.each(function(i) {
$(textareas.clone[i]).val($(this).val());
});
$printVersion
.append(
$('<div>').css({
height: '1px',
margin: '8px 0 8px 0',
background: 'rgb(128, 128, 128)'
})
)
.append(
$clone.show()
);
});
return $printVersion;
};
2012-05-21 10:38:18 +00:00
/*@
values <f> values
@*/
2011-11-30 14:52:42 +00:00
that.values = function() {
var values = {};
self.options.form.forEach(function(section, i) {
values[section.id] = self.$forms[i].values();
2011-11-30 14:52:42 +00:00
});
return values;
};
return that;
};