app/controllers/lookbook/previews_controller.rb in lookbook-1.1.1 vs app/controllers/lookbook/previews_controller.rb in lookbook-1.2.0

- old
+ new

@@ -8,10 +8,11 @@ "lookbook/previews" end before_action :lookup_entities, only: [:preview, :show] before_action :set_title + before_action :set_display_options before_action :set_params def preview if @target begin @@ -90,10 +91,42 @@ def set_title @title = @target.present? ? [@target&.label, @preview&.label].compact.join(" :: ") : "Not found" end + def set_display_options + @dynamic_display_options = [] + @static_display_options = [] + + if @target.present? + opts = @target.display_options + @dynamic_display_options = opts.select { _2.is_a?(Array) || _2.is_a?(Hash) } + @static_display_options = opts.except(*@dynamic_display_options.keys) + + if params[:_display] + display_params = SearchParamParser.call(params[:_display]) + display_params.each do |name, value| + if @dynamic_display_options.key?(name) + cookies["lookbook-display-#{name}"] = value + end + end + end + + @dynamic_display_options.each do |name, opts| + choices = opts.is_a?(Hash) ? opts[:choices].to_a : opts + @static_display_options[name] ||= cookies.fetch("lookbook-display-#{name}", choices.first) + end + + unless params[:_display] + display_params = @dynamic_display_options.map do |name, opts| + [name, @static_display_options[name]] + end.to_h + request.query_parameters[:_display] = SearchParamBuilder.call(display_params) + end + end + end + def set_params if @target # cast known params to type @target.params.each do |param| if preview_controller.params.key?(param[:name]) @@ -101,11 +134,11 @@ end end # set display and data params preview_controller.params.merge!({ lookbook: { - display: @target.display_params, + display: @static_display_options, data: Lookbook.data } }) end end @@ -154,41 +187,19 @@ data: Lookbook.data, app: Lookbook }) end - def panels - return @panels if @panels.present? - inspector_data_hash = inspector_data.to_h - panel_counts = {} - - @panels = Lookbook.config.inspector_panels.map do |name, config| - config_with_defaults = Lookbook.config.inspector_panel_defaults.merge(config) - panel_counts[config_with_defaults[:pane].to_sym] ||= 0 - panel_counts[config_with_defaults[:pane].to_sym] += 1 - - callable_data = { - name: name.to_s, - index_position: panel_counts[config_with_defaults[:pane].to_sym], - **inspector_data_hash - } - - resolved_config = config_with_defaults.transform_values do |value| - value.instance_of?(Proc) ? value.call(Lookbook::Store.new(callable_data)) : value - end - resolved_config[:name] = name.to_s - Store.new(resolved_config) + def main_panels + Engine.panels.in_group(:main).map do |config| + PanelStore.resolve_config(config, inspector_data) end - - @panels = @panels.select(&:show).sort_by { |p| [p.position, p.label] } end - def main_panels - panels.select { |panel| panel.pane == :main } - end - def drawer_panels - panels.select { |panel| panel.pane == :drawer } + Engine.panels.in_group(:drawer).map do |config| + PanelStore.resolve_config(config, inspector_data) + end end def preview_controller return @preview_controller if @preview_controller controller = Lookbook::Engine.preview_controller.new