module Scrivito class ChildListTag < Struct.new(:tag_name, :obj, :field_name, :view) include TagRenderer def initialize(tag_name, obj, field_name, view) unless field_name == 'toclist' raise "#{field_name} is not (yet) supported. Currently only toclist is supported." end super end def options return {} unless user_present? || obj.path.nil? allowed_classes = if classes = Obj.valid_page_classes_beneath(obj.path) classes.map(&:to_s) end options = { 'private-child-list-allowed-classes' => allowed_classes.to_json, 'private-child-list-path' => obj.path, 'private-obj-description-for-editor' => obj.description_for_editor, } if obj.sortable_toclist? options['private-child-list-order-obj-id'] = obj.id modification = comparison.modification_for_attribute(obj, :child_order) if modification == Modification::EDITED options['private-child-list-modification'] = modification end end options end def content(&block) return unless block_given? is_sortable = obj.sortable_toclist? && user_present? rendered_children = obj.sorted_toclist.map do |child| obj_tag = ObjTag.new(child, is_sortable, comparison.modification(child), view) yield obj_tag, child obj_tag.rendered end view.safe_join(rendered_children) end # # This is a helper class for {ScrivitoHelper#scrivito_tag_list}. # # @api public # class ObjTag < Struct.new(:obj, :is_sortable, :modification, :view) include TagRenderer attr_reader :tag_name, :rendered # # Renders HTML for the corresponding child with in-place editing enabled. # # @api public # # @note This method can be called only _once_ per child. # # @param tag_name [String, Symbol] Name of the HTML tag (e.g. +:li+ or +:div+). # @param html_options [Hash] Options to be passed to +content_tag+. Use them to add HTML # attributes to the tag. # # @return [String] The rendered HTML tag. # # @see ScrivitoHelper#scrivito_tag_list # # @example Render a +div+ element containing text, and assign to it the +my_list_element+ CSS class. # list.tag(:div, class: "my_list_element") do # "random content" # end # def tag(tag_name, html_options = {}, &block) raise '"list.tag" can only be called once per iteration!' if @rendered @tag_name = tag_name @rendered = render(html_options, &block) nil end def options return {} unless is_sortable options = { 'private-obj-id' => obj.id, 'private-obj-description-for-editor' => obj.description_for_editor, } if modification == Modification::NEW || modification == Modification::DELETED options['private-obj-modification'] = modification end options end def content(&block) view.capture { yield } if block_given? end end private def user_present? view.scrivito_user.present? end def comparison EditingContextMiddleware.from_request(view.request).comparison end end end