$('#edit_mode').change(function() {
var edit_mode = $(this);
if( edit_mode.prop('checked') ){
$("*[data-editable='true']").each(function(){
var type_input = $(this).data('type');
var label = $(this);
var text = label.html();
var size = ( text.length + 2 ) * 8;
var name_str = label.attr('name').slice( label.attr('name').indexOf("_") );
if( type_input.search("datetime") > -1 ){
text = text.replace(" ", "T");
label.after("<input type='"+type_input+"' name='input"+name_str+"' class='form-control input-sm' style='display:none;width:200px' value='"+text+"' />");
}
else if(type_input === 'number'){
var min = $(this).data('min');
var max = $(this).data('max');
var element = "<input type='"+type_input+"' name='input"+name_str+"' class='form-control input-sm' style='display:none;width:"+(size + 25)+"px' value='"+text+"' ";
if(typeof min !== 'undefined') element += " min='"+min+"'";
if(typeof max !== 'undefined') element += " max='"+max+"'";
label.after(element + " />");
}
else if(type_input === 'textarea' ){
text = text.replace(/<br *\/?>/gi, '');
label.after("<textarea name='input"+name_str+"' style='display:none;'>"+text+"</textarea>");
}
else if(type_input === 'select'){
var source = $(this).data('source');
var parent = $(this);
$.getJSON('url/ecc/pagina.php',
{action : source}, function(data){
var element = "<select id='input"+name_str+"' name='input"+name_str+"' class='form-control input-sm' style='display:none;'>";
$.each(data, function (i, item) {
var option = "<option value='"+item.id+"'";
if(item.value == text)
option += "selected='selected'";
option += ">"+item.value+"</option>";
element += option;
});
element += "</select>";
label.after(element);
});
}
else{
label.after("<input type='text' name='input"+name_str+"' class='form-control input-sm' style='display:none;width:"+size+"px' value='"+text+"' />");
}
var textbox = $(this).next();
alert( textbox.prop("tagName") );
label.click(function () {
if( edit_mode.prop('checked') ){
$(this).hide();
$(this).next().show().focus();
}
});
textbox.focusout(function () {
var tb = $(this);
var text = tb.val();
$(this).hide();
if( $(this).prop("tagName") == 'INPUT' && $(this).attr('type').search("datetime") > -1 ){
var inputData = new Date(text);
if ( Object.prototype.toString.call(inputData) === "[object Date]" ) {
if ( !isNaN( inputData.getTime() ) ) { // inputData.valueOf() could also work
text = text.replace(/[A-Za-z]/, " ");
}
else {
alert('non valida');
}
}
else {
alert('non valida');
}
}
else if( $(this).prop("tagName") == 'TEXTAREA' ){
text = text.replace(/\r\n|\r|\n/g, "<br />");
}
$(this).prev().html(text);
$(this).prev().show();
});
});
}