(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 required = options['required']; 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': control = $('') .prop('name', value_name) .append('') .append($('').prop('selected', field_value == "true").text(RailsAdmin.I18n.t("true"))) .append($('').prop('selected', field_value == "false").text(RailsAdmin.I18n.t("false"))) if (!required) { control.append([ '', $('').prop('selected', field_value == "_present").text(RailsAdmin.I18n.t("is_present")), $('').prop('selected', field_value == "_blank").text(RailsAdmin.I18n.t("is_blank")) ]) } break; case 'date': additional_control = $('') .css('display', (!field_operator || field_operator == "default") ? 'inline-block' : 'none') .prop('name', value_name + '[]') .prop('value', field_value[0] || '') .add( $('') .css('display', (field_operator == "between") ? 'inline-block' : 'none') .prop('name', value_name + '[]') .prop('value', field_value[1] || '') ) .add( $('') .css('display', (field_operator == "between") ? 'inline-block' : 'none') .prop('name', value_name + '[]') .prop('value', field_value[2] || '') ); case 'datetime': case 'timestamp': control = control || $('') .prop('name', operator_name) .append($('').prop('selected', field_operator == "default").text(RailsAdmin.I18n.t("date"))) .append($('').prop('selected', field_operator == "between").text(RailsAdmin.I18n.t("between_and_"))) .append($('').prop('selected', field_operator == "today").text(RailsAdmin.I18n.t("today"))) .append($('').prop('selected', field_operator == "yesterday").text(RailsAdmin.I18n.t("yesterday"))) .append($('').prop('selected', field_operator == "this_week").text(RailsAdmin.I18n.t("this_week"))) .append($('').prop('selected', field_operator == "last_week").text(RailsAdmin.I18n.t("last_week"))) if (!required) { control.append([ '', $('').prop('selected', field_operator == "_not_null").text(RailsAdmin.I18n.t("is_present")), $('').prop('selected', field_operator == "_null").text(RailsAdmin.I18n.t("is_blank")) ]) } additional_control = additional_control || $('') .css('display', (!field_operator || field_operator == "default") ? 'inline-block' : 'none') .prop('name', value_name + '[]') .prop('value', field_value[0] || '') .add( $('') .css('display', (field_operator == "between") ? 'inline-block' : 'none') .prop('name', value_name + '[]') .prop('value', field_value[1] || '') ) .add( $('') .css('display', (field_operator == "between") ? 'inline-block' : 'none') .prop('name', value_name + '[]') .prop('value', field_value[2] || '') ); break; case 'enum': var multiple_values = ((field_value instanceof Array) ? true : false) control = $('') .css('display', multiple_values ? 'none' : 'inline-block') .prop('name', multiple_values ? undefined : value_name) .data('name', value_name) .append('') .append(required ? [] : [ $('').prop('selected', field_value == "_present").text(RailsAdmin.I18n.t("is_present")), $('').prop('selected', field_value == "_blank").text(RailsAdmin.I18n.t("is_blank")), '' ]) .append(select_options) .add( $('') .css('display', multiple_values ? 'inline-block' : 'none') .prop('name', multiple_values ? value_name + '[]' : undefined) .data('name', value_name + '[]') .append(select_options) ) .add( $('') .append($('').addClass('icon-' + (multiple_values ? 'minus' : 'plus'))) ); break; case 'string': case 'text': case 'belongs_to_association': control = $('') .prop('value', field_operator) .prop('name', operator_name) .append('') .append($('').prop('selected', field_operator == "like").text(RailsAdmin.I18n.t("contains"))) .append( $( '' ) .prop("selected", field_operator == "not_like") .text(RailsAdmin.I18n.t("does_not_contain")) ) .append($('').prop('selected', field_operator == "is").text(RailsAdmin.I18n.t("is_exactly"))) .append($('').prop('selected', field_operator == "starts_with").text(RailsAdmin.I18n.t("starts_with"))) .append($('').prop('selected', field_operator == "ends_with").text(RailsAdmin.I18n.t("ends_with"))) if (!required) { control.append([ '', $('').prop('selected', field_operator == "_present").text(RailsAdmin.I18n.t("is_present")), $('').prop('selected', field_operator == "_blank").text(RailsAdmin.I18n.t("is_blank")) ]) } additional_control = $('') .css('display', field_operator == "_present" || field_operator == "_blank" ? 'none' : 'inline-block') .prop('name', value_name) .prop('value', field_value); break; case 'integer': case 'decimal': case 'float': control = $('') .prop('name', operator_name) .append($('').prop('selected', field_operator == "default").text(RailsAdmin.I18n.t("number"))) .append($('').prop('selected', field_operator == "between").text(RailsAdmin.I18n.t("between_and_"))) if (!required) { control.append([ '', $('').prop('selected', field_operator == "_not_null").text(RailsAdmin.I18n.t("is_present")), $('').prop('selected', field_operator == "_null").text(RailsAdmin.I18n.t("is_blank")) ]) } additional_control = $('') .css('display', (!field_operator || field_operator == "default") ? 'inline-block' : 'none') .prop('type', field_type) .prop('name', value_name + '[]') .prop('value', field_value[0] || '') .add( $('') .css('display', (field_operator == "between") ? 'inline-block' : 'none') .prop('type', field_type) .prop('name', value_name + '[]') .prop('value', field_value[1] || '') ) .add( $('') .css('display', (field_operator == "between") ? 'inline-block' : 'none') .prop('type', field_type) .prop('name', value_name + '[]') .prop('value', field_value[2] || '') ); break; default: control = $('') .prop('name', value_name) .prop('value', field_value); break; } var filterContainerId = field_name + '-' + index + '-filter-container'; $('p#' + filterContainerId).remove(); var $content = $('

') .attr('id', filterContainerId) .addClass('filter form-search') .append( $('') .append($('').append('').append(document.createTextNode(field_label))) ) .append(' ') .append(control) .append(' ') .append(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'), required: $(this).data('field-required'), 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-additional-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 );