$ -> # Integrates a content browser based editor for reference CMS attributes. scrivito.on 'load', -> if scrivito.in_editable_view() $('body').on 'click', '[data-scrivito-field-type="reference"]:not([data-editor]), [data-editor~="reference"]', (event) -> event.preventDefault() cmsField = $(event.currentTarget) selected = [cmsField.scrivito('content')].filter (element) -> element filters = cmsField.data('filters') filterContext = $.extend {_image: cmsField.is('img')}, cmsField.data('filterContext') # Open content browser with current reference selected. scrivito.content_browser.open filters: filters filter_context: filterContext selection: selected selection_mode: 'single' .done (selection) => onContentBrowserSave(selection, cmsField) # Save single reference when content browser calls "save". onContentBrowserSave = (selection, cmsField) -> value = selection[0] || null cmsField.scrivito('save', value) .done -> cmsField.trigger('save.scrivito_editors') cmsField.scrivito('reload') true