// ********************************************* // SOLUÇÃO ELEGANTE // ********************************************* String.prototype.replaceAll = function(de, para){ var str = this; var pos = str.indexOf(de); while (pos > -1){ str = str.replace(de, para); pos = str.indexOf(de); } return (str); } var updateFieldRemote = function(path, model, field, field_remote_name, value) { var id = "#" + model + "_" + field_remote_name + "_id"; $(id).html(''); $(id).parent().find('.select2-choice span').html("Carregando ... "); $(id).val('0'); eval($(id).attr('onchange')); if(eval(value) > 0) { $(id).attr('disabled', ''); $.post(path + "/remote", {fk: field, field: field_remote_name, fk_id: field + "_id", value: value}, null, 'script'); } else { $(id).html(''); } } var prepareFields = function(elem) { $(elem).find(".select2-me").select2(); // $('.bootstrap-timepicker') // timepicker $(elem).find('.timepick').timepicker({ defaultTime: false, minuteStep: 1, disableFocus: true, template: 'dropdown', showMeridian: false }).on('changeTime.timepicker', function(e) { hours = e.time.hours; minutes = e.time.minutes; updateDateTimeValue($(this).closest('.bootstrap-timepicker'), hours, minutes); }); // datepicker $(elem).find('.datepick').datepicker({ language: 'pt-BR' }); $(elem).find('input.price_format').maskMoney({thousands:'', decimal:'.', allowNegative:true, allowZero:true}); } var zeroPad = function(num, places) { var zero = places - num.toString().length + 1; return Array(+(zero > 0 && zero)).join("0") + num; } var updateDateTimeValue = function(elem, hours, minutes) { var datetime = ""; var date = $(elem).find('.datepick').val(); if(hours != null && minutes != null) var time = zeroPad(hours, 2) + ":" + zeroPad(minutes, 2); else var time = $(elem).find('.timepick').val(); if (date != "" && time != "") datetime = date + " " + time; $(elem).find('.datetime').val(datetime); } var getTableSelected = function() { var ret = []; $('.table tbody .with-checkbox input:checked').each(function(index, el){ ret.push($(el).val()); }); return ret; } // Handles "data-method" on links such as: // Delete function postPage(link, params, method) { if(method == null) method = 'post'; if(params == null) params = []; var href = link, csrf_token = $('meta[name=csrf-token]').attr('content'), csrf_param = $('meta[name=csrf-param]').attr('content'), form = $('
'), metadata_input = ''; if (csrf_param !== undefined && csrf_token !== undefined) { metadata_input += ''; } $(params).each(function(index, el){ metadata_input += ''; }); form.hide().append(metadata_input).appendTo('body'); form.submit(); } $(document).on('ready page:load', function(){ prepareFields($('body')); $('.btn-remove-all-selected').click(function(e){ e.preventDefault(); var selected = getTableSelected() if(selected.length > 0) { if(confirm("Tem certeza que deseja excluir os itens selecionados?")) { var link = $(this).prop('href'); var params = { name: 'ids', value: selected }; postPage(link, params); } } }); $('.box.list .actions #per_page').on('change', function(e){ window.location.href = $(this).val(); }); $('.thefilter .with-checkbox input[type=checkbox]').click(function(e){ $(this).closest('table').find('.with-checkbox input[type=checkbox]').prop('checked', $(this).prop('checked')); }); $('.bootstrap-timepicker input').on('change', function(e){ updateDateTimeValue($(this).closest('.bootstrap-timepicker')); }); //HAS MANY ITEM $(".add_new_has_many_item").click(function(e){ e.preventDefault(); var limit = eval($(this).closest('.controls').find('.limit').val()); var table = $(this).closest('.controls').find('table'); var count = table.find('tr').length; var last = $(table.find('tr')[count - 1]); var clone = last.clone(); clone.find('input, select').each(function(i, e){ if($(this).attr('id')) $(this).attr('id', $(this).attr('id').replaceAll(count - 2, count - 1) ); if($(this).attr('name')) $(this).attr('name', $(this).attr('name').replaceAll(count - 2, count - 1) ); if(!$(this).hasClass('destroy')) $(this).val(''); $(this).removeClass('hasDatepicker'); }); clone.find('.select2-container').remove(); if(count >= limit && limit > 0) $(this).remove(); last.after(clone); prepareFields(clone); }); })