$(document).on 'rails_admin.dom_ready', (e, content) ->
content = if content then content else $('form')
if content.length # don't waste time otherwise
# colorpicker
content.find('[data-color]').each ->
that = this
$(this).ColorPicker
color: $(that).val()
onShow: (el) ->
$(el).fadeIn(500)
false
onHide: (el) ->
$(el).fadeOut(500)
false
onChange: (hsb, hex, rgb) ->
$(that).val(hex)
$(that).css('backgroundColor', '#' + hex)
# datetime picker
$.fn.datetimepicker.defaults.icons =
time: 'fa fa-clock'
date: 'fa fa-calendar'
up: 'fa fa-chevron-up'
down: 'fa fa-chevron-down'
previous: 'fa fa-angle-double-left'
next: 'fa fa-angle-double-right'
today: 'fa fa-dot-circle'
clear: 'fa fa-trash'
close: 'fa fa-times'
content.find('[data-datetimepicker]').each ->
options = $(this).data('options')
$.extend(options, {locale: RailsAdmin.I18n.locale})
$(this).datetimepicker options
# enumeration
content.find('[data-enumeration]').each ->
if $(this).is('[multiple]')
$(this).filteringMultiselect $(this).data('options')
else
$(this).filteringSelect $(this).data('options')
# fileupload
content.find('[data-fileupload]').each ->
input = this
$(this).on 'click', ".delete input[type='checkbox']", ->
$(input).children('.toggle').toggle('slow')
# fileupload-preview
content.find('[data-fileupload]').change ->
input = this
image_container = $("#" + input.id).parent().children(".preview")
unless image_container.length
image_container = $("#" + input.id).parent().prepend($('').addClass('preview').addClass('img-thumbnail')).find('img.preview')
image_container.parent().find('img:not(.preview)').hide()
ext = $("#" + input.id).val().split('.').pop().toLowerCase()
if input.files and input.files[0] and $.inArray(ext, ['gif', 'png', 'jpg', 'jpeg', 'bmp']) != -1
reader = new FileReader()
reader.onload = (e) ->
image_container.attr "src", e.target.result
reader.readAsDataURL input.files[0]
image_container.show()
else
image_container.hide()
# filtering-multiselect
content.find('[data-filteringmultiselect]').each ->
$(this).filteringMultiselect $(this).data('options')
if $(this).parents("#modal").length # hide link if we already are inside a dialog (endless issues on nested dialogs with JS)
$(this).siblings('.btn').remove()
else
$(this).parents('.control-group').first().remoteForm()
# filtering-select
content.find('[data-filteringselect]').each ->
$(this).filteringSelect $(this).data('options')
if $(this).parents("#modal").length # hide link if we already are inside a dialog (endless issues on nested dialogs with JS)
$(this).siblings('.btn').remove()
else
$(this).parents('.control-group').first().remoteForm()
# nested-many
content.find('[data-nestedmany]').each ->
field = $(this).parents('.control-group').first()
nav = field.find('> .controls > .nav')
tab_content = field.find('> .tab-content')
toggler = field.find('> .controls > .btn-group > .toggler')
# add each nested field to a tab-pane and reference it in the nav
tab_content.children('.fields:not(.tab-pane)').addClass('tab-pane').each ->
$(this).attr('id', 'unique-id-' + (new Date().getTime()) + Math.floor(Math.random() * 100000)) # some elements are created on the same ms
nav.append('