module Scrivito class ClientConfig < Struct.new(:obj, :editing_context, :lookup_context, :resource, :return_to) def initialize(obj, editing_context, lookup_context, options = {}) super(obj, editing_context, lookup_context, *options.values_at(:resource, :return_to)) end def to_json config = {} config[:current_page] = current_page_config config[:editing_context] = editing_context_config config[:i18n] = i18n_config config[:resource_dialog] = resource_dialog_config config[:user] = user_config config[:user_permissions] = user_permissions_config config[:is_development_mode] = Rails.env.development? config.to_json end private def editing_context_config { display_mode: editing_context.display_mode, selected_workspace: { id: selected_workspace.id, title: selected_workspace.title, outdated: selected_workspace.outdated? }, visible_workspace: { id: visible_workspace.id, title: visible_workspace.title } } end def i18n_config {locale: Scrivito::Configuration.ui_locale || I18n.locale} end def current_page_config if obj { id: obj.id, obj_class_name: obj.obj_class_name, has_children: obj.children.any?, has_conflict: obj.has_conflict?, has_details_view: obj_has_details_view?, modification: modification(obj), restriction_messages: editor.restriction_messages_for(obj), } else {} end end def obj_has_details_view? lookup_context.find(obj.details_view_path).present? rescue ActionView::MissingTemplate false end def resource_dialog_config if resource { obj: Configuration.obj_formats.fetch('_default').call(resource, editor), return_to: return_to, } else {} end end def user_permissions_config { create_workspace: editor.can?(:create, :workspace), publish_workspace: editor.can?(:publish, selected_workspace), } end def user_config { current: { id: editor.id } } end def modification(obj_or_resource) comparison = editing_context.comparison if comparison.active? comparison.modification(obj_or_resource) else obj_or_resource.modification end end def editor editing_context.editor end def selected_workspace editing_context.selected_workspace end def visible_workspace editing_context.visible_workspace end end end