module Scrivito # this class is the server-side equivalent of the JavaScript class `cms_field_element` class CmsFieldTag < Struct.new( :view_context, :editing_context, :obj_or_widget, :field_name ) def options formatted_options = {} raw_options.each do |key, value| formatted_options["data-scrivito-#{key}"] = value end formatted_options end def field_type @field_type ||= obj_or_widget.type_of_attribute(field_name) end def default_content calculate_content_and_modification @content_and_modification.first end def modification_info calculate_content_and_modification @content_and_modification.second end private def calculate_content_and_modification @content_and_modification ||= comparison.diff_for(obj_or_widget, field_name) || [current_value, nil] end def raw_options return {} unless inplace_editing_allowed? options = { 'private-field-workspace-id' => current_workspace_id, 'field-name' => field_name, 'field-obj-class' => obj_or_widget.obj_class_name, 'field-type' => field_type, } modification = comparison.modification_for_attribute(obj_or_widget, field_name) if modification == Modification::EDITED && field_type != 'widget' options['field-modification'] = modification end if obj_or_widget.kind_of?(BasicWidget) options['private-field-id'] = obj_or_widget.obj.id options['private-field-widget-id'] = obj_or_widget.id else options['private-field-id'] = obj_or_widget.id end if FIELD_TYPES_WITH_ORIGINAL_CONTENT.include?(field_type) original_value = view_context.display_value(current_value) original_content = original_content(field_type, original_value) encoded_content = Base64.strict_encode64(MultiJson.encode(original_content)) options['private-field-original-content'] = encoded_content end if field_type == 'widget' valid_widget_classes = obj_or_widget.valid_widget_classes_for(field_name.to_s) if valid_widget_classes options['private-field-widget-allowed-classes'] = valid_widget_classes.to_json end end options end def comparison editing_context.comparison end def current_value obj_or_widget[field_name] end def current_workspace_id Workspace.current.id end FIELD_TYPES_WITH_ORIGINAL_CONTENT = %w[ enum html multienum reference referencelist string text ] def inplace_editing_allowed? editing_context.authenticated_editor? end def original_content(field_type, field_value) case field_type when 'reference' then field_value.try(:id) when 'referencelist' then field_value.map(&:id) else field_value end end end end