module Scrivito class UiConfig < Struct.new(:editing_context, :resource, :return_to, :app_extension_tags, :obj_models, :widget_models, :lookup_context) def initialize(**named_params) named_params.each { |key, value| self[key] = value } end def to_json { app_extension_tags: app_extension_tags, backend_endpoint: Configuration.endpoint, tenant: Configuration.tenant, i18n: i18n_config, is_development_mode: Rails.env.development?, resource_dialog: resource_dialog_config, user: user_config, user_permissions: user_permissions_config, class_datas: obj_class_config + widget_class_config, useRailsEngine: true, }.to_json end private def i18n_config {locale: Scrivito::Configuration.ui_locale || I18n.locale} end def obj_class_config obj_models.map do |obj_class| obj_class_formatter.format(obj_class) end end def widget_class_config widget_models.map do |widget_class| obj_class_formatter.format(widget_class) end end def resource_dialog_config return {} unless resource { obj: Configuration.obj_formats.fetch('_default').call(resource, editor), return_to: return_to, } end def user_permissions_config { create_workspace: editor.can?(:create, :workspace), rename_workspace: editor.can?(:write, selected_workspace), invite_to_workspace: editor.can?(:invite_to, selected_workspace), rebase_workspace: editor.can?(:write, selected_workspace), delete_workspace: editor.can?(:delete, selected_workspace), publish_workspace: editor.can?(:publish, selected_workspace), } end def user_config { current: editor.as_json, } end def editor editing_context.editor end def selected_workspace editing_context.selected_workspace end def obj_class_formatter @obj_class_formatter ||= UiObjClassesJsonFormatter.new(lookup_context) end end end