/** * Sami's editable cells v.1.0 * Makes the content dynamically editable * NOTE: requires jQuery * @param object element - The object that contains the editable elements. * @param string editable_cell_class - The class of the editable elements. By default editable_cell * @param string text_input_class - The class or classes of the text inputs. Used for CSS styling. * @param string callback_url - The url to be called for dynamic update. Cold be empty. * @param function callback_function - A callback function with parameters: cell value, the cell ID, the response of the callback URL if any; * @author Samuil Banti - http://samiwell.eu * @copyright (C) 2015 - Samuil Banti * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html * @return NULL */ (function( $ ){ $.fn.samis_editable_cells = function( options ) { var settings = $.extend( { editable_cell_class: 'editable_cell', text_input_class: 'editable_cell_input', callback_url: false, callback_function: function(cell_id, cell_value, calback_response){ //console.log('cell_id: '+cell_id); //console.log('cell_value: '+cell_value); //console.log('calback_response: '+calback_response); }, }, options); return this.each(function() { $(this).on('click', '.'+settings.editable_cell_class, function(){ if(!$(this).hasClass('samis_editing')) { $(this).addClass('samis_editing'); var input_html = ''; $(this).html(input_html); $('#samis_editable_cell').focus(); } }); $(this).on('focusout', '#samis_editable_cell', function(){ submit_field($(this)); }); $(this).on('keyup', '#samis_editable_cell', function(e){ if(e.keyCode == 13) { submit_field($(this)); } }); }); function submit_field(element) { element.parent().removeClass('samis_editing'); element.replaceWith(element.val()); if(settings.callback_url.length) { var url = settings.callback_url+'cell_id='+element.attr('name')+'&cell_value='+element.val(); $.get(url, function(ajax_result){ settings.callback_function(element.attr('name'), element.val(), ajax_result); }); } else { settings.callback_function(element.attr('name'), element.val()); } } }; })( jQuery );