(function($) { var filters; $.filters = filters = { append: function(options) { options = options || {}; var field_label = options['label']; var field_name = options['name']; var field_type = options['type']; var field_value = options['value']; var field_operator = options['operator']; var select_options = options['select_options']; var index = options['index']; var value_name = 'f[' + field_name + '][' + index + '][v]'; var operator_name = 'f[' + field_name + '][' + index + '][o]'; var control = null; var additional_control = null; switch(field_type) { case 'boolean': var control = ''; break; case 'date': additional_control = ' ' + ' ' + ''; case 'datetime': case 'timestamp': control = control || '' additional_control = additional_control || ' ' + ' ' + ''; break; case 'enum': var multiple_values = ((field_value instanceof Array) ? true : false) control = '' + ' ' + ''; break; case 'string': case 'text': case 'belongs_to_association': control = '' additional_control = ' '; break; case 'integer': case 'decimal': case 'float': control = '' additional_control = ' ' + ' ' + ''; break; default: control = ' '; break; } var $content = $('
') .addClass('filter form-search') .append(' ' + field_label + '') .append(' ' + control + ' ' + (additional_control || '')); $('#filters_box').append($content); $content.find('.date, .datetime').datetimepicker({ locale: RailsAdmin.I18n.locale, showTodayButton: true, format: options['datetimepicker_format'] }); $("hr.filters_box:hidden").show('slow'); } } $(document).on('click', "#filters a", function(e) { e.preventDefault(); $.filters.append({ label: $(this).data('field-label'), name: $(this).data('field-name'), type: $(this).data('field-type'), value: $(this).data('field-value'), operator: $(this).data('field-operator'), select_options: $(this).data('field-options'), index: $.now().toString().slice(6,11), datetimepicker_format: $(this).data('field-datetimepicker-format') }); }); $(document).on('click', "#filters_box .delete", function(e) { e.preventDefault(); form = $(this).parents('form'); $(this).parents('.filter').remove(); !$("#filters_box").children().length && $("hr.filters_box:visible").hide('slow'); }); $(document).on('click', "#filters_box .switch-select", function(e) { e.preventDefault(); var selected_select = $(this).siblings('select:visible'); var not_selected_select = $(this).siblings('select:hidden'); not_selected_select.attr('name', not_selected_select.data('name')).show('slow'); selected_select.attr('name', null).hide('slow'); $(this).find('i').toggleClass("icon-plus icon-minus") }); $(document).on('change', "#filters_box .switch-additionnal-fieldsets", function(e) { var selected_option = $(this).find('option:selected'); if(klass = $(selected_option).data('additional-fieldset')) { $(this).siblings('.additional-fieldset:not(.' + klass + ')').hide('slow'); $(this).siblings('.' + klass).show('slow'); } else { $(this).siblings('.additional-fieldset').hide('slow'); } }); })( jQuery );