LSform/LSformElement : permit form element to provide custom clean value method

This commit is contained in:
Benjamin Renard 2014-11-20 00:18:35 +01:00
parent 881323f813
commit 78d68951e2
3 changed files with 42 additions and 29 deletions

View file

@ -1,19 +1,20 @@
var LSform = new Class({ var LSform = new Class({
initialize: function(){ initialize: function(){
this._modules=[]; this._modules=[];
this._fields=[];
this._elements=[]; this._elements=[];
this._tabBtns=[]; this._tabBtns=[];
if ($type($('LSform_idform'))) { if ($type($('LSform_idform'))) {
this.objecttype = $('LSform_objecttype').value; this.objecttype = $('LSform_objecttype').value;
this.objectdn = $('LSform_objectdn').value; this.objectdn = $('LSform_objectdn').value;
this.idform = $('LSform_idform').value; this.idform = $('LSform_idform').value;
} }
this.initializeLSform(); this.initializeLSform();
this.initializeLSformLayout(); this.initializeLSformLayout();
}, },
initializeLSform: function(el) { initializeLSform: function(el) {
this.params={}; this.params={};
if (this.idform) { if (this.idform) {
@ -51,19 +52,19 @@ var LSform = new Class({
this.listAvailableDataEntryForm.addEvent('change',this.onListAvailableDataEntryFormChange.bind(this)); this.listAvailableDataEntryForm.addEvent('change',this.onListAvailableDataEntryFormChange.bind(this));
} }
} }
LSforms = $$('form.LSform'); LSforms = $$('form.LSform');
if ($type(LSforms[0])) { if ($type(LSforms[0])) {
this.LSform = LSforms[0]; this.LSform = LSforms[0];
this.LSform.addEvent('submit',this.ajaxSubmit.bindWithEvent(this)); this.LSform.addEvent('submit',this.ajaxSubmit.bindWithEvent(this));
} }
}, },
initializeLSformLayout: function(el) { initializeLSformLayout: function(el) {
$$('.LSform_layout').each(function(el) { $$('.LSform_layout').each(function(el) {
el.addClass('LSform_layout_active'); el.addClass('LSform_layout_active');
},this); },this);
var LIs = $$('li.LSform_layout'); var LIs = $$('li.LSform_layout');
LIs.each(function(li) { LIs.each(function(li) {
var Layout = this.getLayout(li); var Layout = this.getLayout(li);
@ -84,7 +85,7 @@ var LSform = new Class({
$$('li.LSform_layout a').each(function(a) { $$('li.LSform_layout a').each(function(a) {
this._tabBtns[a.href]=a; this._tabBtns[a.href]=a;
},this); },this);
if (LIs.length != 0) { if (LIs.length != 0) {
if ($type(this._tabBtns[window.location])) { if ($type(this._tabBtns[window.location])) {
this._currentTab = 'default_value'; this._currentTab = 'default_value';
@ -106,7 +107,7 @@ var LSform = new Class({
} }
return $('LSform_layout_btn_'+name[1]); return $('LSform_layout_btn_'+name[1]);
}, },
getLayout: function(btn) { getLayout: function(btn) {
var getName = new RegExp('LSform_layout_btn_(.*)'); var getName = new RegExp('LSform_layout_btn_(.*)');
var name = getName.exec(btn.id); var name = getName.exec(btn.id);
@ -115,7 +116,7 @@ var LSform = new Class({
} }
return $('LSform_layout_div_'+name[1]); return $('LSform_layout_div_'+name[1]);
}, },
onTabBtnClick: function(event,li) { onTabBtnClick: function(event,li) {
if ($type(event)) { if ($type(event)) {
event = new Event(event); event = new Event(event);
@ -124,7 +125,7 @@ var LSform = new Class({
event.target.blur(); event.target.blur();
} }
} }
if (this._currentTab!=li) { if (this._currentTab!=li) {
if (this._currentTab!='default_value') { if (this._currentTab!='default_value') {
this._currentTab.removeClass('LSform_layout_current'); this._currentTab.removeClass('LSform_layout_current');
@ -133,13 +134,13 @@ var LSform = new Class({
oldDiv.removeClass('LSform_layout_current'); oldDiv.removeClass('LSform_layout_current');
} }
} }
this._currentTab = li; this._currentTab = li;
li.addClass('LSform_layout_current'); li.addClass('LSform_layout_current');
var div = this.getLayout(li); var div = this.getLayout(li);
if ($type(div)) { if ($type(div)) {
div.addClass('LSform_layout_current'); div.addClass('LSform_layout_current');
// Focus // Focus
var ul = div.getElement('ul.LSform'); var ul = div.getElement('ul.LSform');
if ($type(ul)) { if ($type(ul)) {
@ -158,13 +159,13 @@ var LSform = new Class({
} }
} }
} }
}, },
addModule: function(name,obj) { addModule: function(name,obj) {
this._modules[name]=obj; this._modules[name]=obj;
}, },
initializeModule: function(fieldType,li) { initializeModule: function(fieldType,li) {
if ($type(this._modules[fieldType])) { if ($type(this._modules[fieldType])) {
try { try {
@ -175,7 +176,17 @@ var LSform = new Class({
} }
} }
}, },
addField: function(name,obj) {
this._fields[name]=obj;
},
clearFieldValue: function(name) {
if ($type(this._fields[name]) && $type(this._fields[name].clearValue)) {
this._fields[name].clearValue();
}
},
getValue: function(fieldName) { getValue: function(fieldName) {
var retVal = Array(); var retVal = Array();
var inputs = this.getInput(fieldName); var inputs = this.getInput(fieldName);
@ -194,7 +205,7 @@ var LSform = new Class({
var elements = ul.getElements('input'); var elements = ul.getElements('input');
elements.combine(ul.getElements('textarea')); elements.combine(ul.getElements('textarea'));
elements.combine(ul.getElements('select')); elements.combine(ul.getElements('select'));
var getName = new RegExp('([a-zA-Z0-9]*)(\[.*\])?'); var getName = new RegExp('([a-zA-Z0-9]*)(\[.*\])?');
elements.each(function(el){ elements.each(function(el){
var name = getName.exec(el.name); var name = getName.exec(el.name);
@ -208,14 +219,14 @@ var LSform = new Class({
} }
return retVal; return retVal;
}, },
ajaxSubmit: function(event) { ajaxSubmit: function(event) {
this.checkUploadFileDefined(); this.checkUploadFileDefined();
if (this._ajaxSubmit) { if (this._ajaxSubmit) {
event = new Event(event); event = new Event(event);
event.stop(); event.stop();
this.LSformAjaxInput = new Element('input'); this.LSformAjaxInput = new Element('input');
this.LSformAjaxInput.setProperties ({ this.LSformAjaxInput.setProperties ({
type: 'hidden', type: 'hidden',
@ -223,7 +234,7 @@ var LSform = new Class({
value: '1' value: '1'
}); });
this.LSformAjaxInput.injectInside(this.LSform); this.LSformAjaxInput.injectInside(this.LSform);
this.LSform.set('send',{ this.LSform.set('send',{
data: this.LSform, data: this.LSform,
onSuccess: this.onAjaxSubmitComplete.bind(this), onSuccess: this.onAjaxSubmitComplete.bind(this),
@ -238,7 +249,7 @@ var LSform = new Class({
} }
} }
}, },
checkUploadFileDefined: function() { checkUploadFileDefined: function() {
this.LSform.getElements('input[type=file]').each(function(ipt) { this.LSform.getElements('input[type=file]').each(function(ipt) {
if (ipt.files.length!=0) { if (ipt.files.length!=0) {
@ -246,7 +257,7 @@ var LSform = new Class({
} }
}, this); }, this);
}, },
onAjaxSubmitComplete: function(responseText, responseXML) { onAjaxSubmitComplete: function(responseText, responseXML) {
var data = JSON.decode(responseText); var data = JSON.decode(responseText);
if ( varLSdefault.checkAjaxReturn(data) ) { if ( varLSdefault.checkAjaxReturn(data) ) {
@ -257,7 +268,7 @@ var LSform = new Class({
} }
} }
}, },
resetErrors: function() { resetErrors: function() {
$$('dd.LSform-errors').each(function(dd) { $$('dd.LSform-errors').each(function(dd) {
dd.destroy(); dd.destroy();
@ -268,9 +279,9 @@ var LSform = new Class({
$$('li.LSform_layout_errors').each(function(li) { $$('li.LSform_layout_errors').each(function(li) {
li.removeClass('LSform_layout_errors'); li.removeClass('LSform_layout_errors');
}); });
}, },
addError: function(errors,name) { addError: function(errors,name) {
var ul = $(name); var ul = $(name);
if ($type(ul)) { if ($type(ul)) {
@ -282,12 +293,12 @@ var LSform = new Class({
dd.set('html',txt); dd.set('html',txt);
dd.injectAfter(this.getParent()); dd.injectAfter(this.getParent());
},ul); },ul);
var dt = ul.getParent('dd.LSform').getPrevious('dt'); var dt = ul.getParent('dd.LSform').getPrevious('dt');
if ($type(dt)) { if ($type(dt)) {
dt.addClass('LSform-errors'); dt.addClass('LSform-errors');
} }
var layout = ul.getParent('div.LSform_layout_active'); var layout = ul.getParent('div.LSform_layout_active');
if ($type(layout)) { if ($type(layout)) {
var li = this.getLayoutBtn(layout); var li = this.getLayoutBtn(layout);

View file

@ -17,7 +17,7 @@ var LSformElement = new Class({
} }
elements.each(function(li) { elements.each(function(li) {
var id='LSformElement_field_'+this.name+'_'+$random(1,1000); var id='LSformElement_field_'+this.name+'_'+$random(1,1000);
this.fields[id] = new LSformElement_field(this,li,id); this.fields[id] = new LSformElement_field(this,li,id,this.name);
}, this); }, this);
}, },

View file

@ -1,8 +1,9 @@
var LSformElement_field = new Class({ var LSformElement_field = new Class({
initialize: function(LSformElement,li,id){ initialize: function(LSformElement,li,id,field_name){
this.id = id; this.id = id;
this.LSformElement = LSformElement; this.LSformElement = LSformElement;
this.li = li; this.li = li;
this.field_name = field_name;
if (this.LSformElement.multiple) { if (this.LSformElement.multiple) {
this.addFieldBtn = new Element('img'); this.addFieldBtn = new Element('img');
@ -36,6 +37,7 @@ var LSformElement_field = new Class({
if ($type(this.getFormField())) { if ($type(this.getFormField())) {
this.getFormField().value=''; this.getFormField().value='';
} }
varLSform.clearFieldValue(this.field_name);
}, },
remove: function() { remove: function() {