$(document).ready(function() { //makes buttons buttons $("button").button(); //makes datepickers $( ".datepicker" ).datepicker(); //$('#newworkout').find('input.datepicker').datepicker(); $('.datepicker').datepicker('setDate', new Date()); //$('.datepicker').datepicker("hide"); jQuery.extend({ getValues: function(url) { var result = null; $.ajax({ url: url, type: 'get', dataType: 'json', async: false, success: function(data) { result = data; } }); return result; } }); $("button#addExercise").click(function() { console.log("click!"); var newElem = $('
  • '); $(newElem).appendTo('ul#exercises'); $("button").button(); $( "ul#exercises" ).sortable( "refresh" ); $('#newworkout').trigger('sortupdate'); }); $(document).on('click', '.addSet',(function() { var newElem = $('
  • '); $(this).siblings('ul.sets').append(newElem); $("button").button(); $("ul.sets").sortable({ items: "> li", cancel: ':input, button' , placeholder: "ui-state-highlight", revert: true, start: function(e, ui){ ui.placeholder.height(ui.item.height()); }, stop: function(event, ui) { $('#newworkout').trigger('sortupdate'); } }); $( "#exercises" ).sortable( "refresh" ); $( ".sets" ).sortable( "refresh" ); $('#newworkout').trigger('sortupdate'); })); $(document).on('click', '.delete',function() { $(this).closest('li').remove(); $('#newworkout').trigger('sortupdate'); }); $("#newworkout").bind('sortupdate', function(event, ui) { $('#exercises > li').each(function(){ var itemindex= $(this).index(); var newname = $(this).find('input, select').attr('name').replace(/elements\[[0-9]*\]/,'elements[' + itemindex + ']'); $(this).children('label').html(newname); $(this).children('input').attr('name', newname); $(this).find('ul li').each(function(){ var itemindex2= $(this).index(); $(this).children('input').each(function(){ var newname2 = $(this).attr('name').replace(/\[splits\]\[[0-9]*\]/,'[splits][' + itemindex2 + ']'); newname2 = newname2.replace(/elements\[[0-9]*\]/,'elements[' + itemindex + ']'); $(this).attr('name', newname2); }); $(this).children('label').each(function(){ var newname2 = $(this).attr('name').replace(/\[splits\]\[[0-9]*\]/,'[splits][' + itemindex2 + ']'); newname2 = newname2.replace(/elements\[[0-9]*\]/,'elements[' + itemindex + ']'); $(this).html(newname2); }); if ( $(this).siblings('.set').length > 0 ) { $(this).children('.delete').show(); console.log(">1 " + $(this).siblings('.set').length); } else { $(this).children('.delete').hide(); console.log("=1 " + $(this).siblings('.set').length); } }); }); //return false; }); $("ul#exercises").sortable({ items: "> li", cancel: ':input, button' , placeholder: "ui-state-highlight", revert: true, start: function(e, ui){ ui.placeholder.height(ui.item.height()); }, stop: function(event, ui) { $('#newworkout').trigger('sortupdate'); } }); //$('#newworkout').trigger('sortupdate'); });