Skip to content

A platform for community discussion. Free, open, simple.

8.8 KB
Raw Blame
# frozen_string_literal: true

module PageObjects
  module Pages
    class AdminCustomizeThemes < PageObjects::Pages::AdminBase
      def visit(theme_or_theme_id)
        page.visit(
          "/admin/customize/themes/#{theme_or_theme_id.is_a?(Theme) ? theme_or_theme_id.id : theme_or_theme_id}",
        )
        self
      end

      def finish_install
        find(".finish-install").click
      end

      def has_incomplete_installation?
        has_button?(I18n.t("admin_js.admin.customize.theme.finish_install"))
      end

      def visit_editor(theme, target: "common", field: "scss")
        page.visit("/admin/customize/themes/#{theme.id}/#{target}/#{field}/edit")
        self
      end

      def click_editor_field(field)
        find(".nav-pills.fields a[href$='/#{field}/edit']").click
      end

      def has_editor_field_description?(field)
        has_css?(".field-info", text: I18n.t("admin_js.admin.customize.theme.#{field}.title"))
      end

      def has_colors_tab?
        header.has_tab?("colors")
      end

      def has_colors_tab_active?
        header.has_active_tab?("colors")
      end

      def has_no_colors_tab?
        header.has_no_tab?("colors")
      end

      def colors_tab
        header.tab("colors")
      end

      def has_settings_tab?
        header.has_tab?("settings")
      end

      def has_no_settings_tab?
        header.has_no_tab?("settings")
      end

      def settings_tab
        header.tab("settings")
      end

      def changes_banner
        PageObjects::Components::AdminChangesBanner.new
      end

      def has_color_scheme_selector?
        page.has_css?(".theme-settings__color-scheme")
      end

      def has_no_color_scheme_selector?
        page.has_no_css?(".theme-settings__color-scheme")
      end

      def color_palette_editor
        PageObjects::Components::ColorPaletteEditor.new(page.find(".color-palette-editor"))
      end

      def palette_editor_save_button
        page.find(".admin-theme__save-palette-changes")
      end

      def has_palette_editor_save_button?
        page.has_css?(".admin-theme__save-palette-changes")
      end

      def has_no_palette_editor_save_button?
        page.has_no_css?(".admin-theme__save-palette-changes")
      end

      def palette_editor_discard_button
        page.find(".admin-theme__discard-palette-changes")
      end

      def has_palette_editor_discard_button?
        page.has_css?(".admin-theme__discard-palette-changes")
      end

      def has_no_palette_editor_discard_button?
        page.has_no_css?(".admin-theme__discard-palette-changes")
      end

      def has_inactive_themes?
        has_css?(".inactive-indicator")
      end

      def has_no_inactive_themes?
        has_no_css?(".inactive-indicator")
      end

      def has_select_inactive_mode_button?
        has_css?(".select-inactive-mode")
      end

      def has_overridden_setting?(setting_name)
        has_css?(setting_selector(setting_name, overridden: true))
      end

      def has_no_overriden_setting?(setting_name)
        has_no_css?(setting_selector(setting_name, overridden: true))
      end

      def has_setting_description?(setting_name, description)
        has_css?("#{setting_selector(setting_name)} .desc", exact_text: description)
      end

      def has_theme_site_setting?(setting_name)
        find(theme_site_setting_selector(setting_name)).has_css?(
          ".setting-label",
          text: SiteSetting.humanized_name(setting_name),
        )
        find(theme_site_setting_selector(setting_name)).has_css?(
          ".setting-value",
          text: SiteSetting.description(setting_name),
        )
      end

      def has_overridden_theme_site_setting?(setting_name)
        has_css?(theme_site_setting_selector(setting_name, overridden: true))
      end

      def has_no_overridden_theme_site_setting?(setting_name)
        has_no_css?(theme_site_setting_selector(setting_name, overridden: true))
      end

      def toggle_theme_site_setting(setting_name)
        find(theme_site_setting_selector(setting_name)).find(
          ".setting-value input[type='checkbox']",
        ).click
        find(theme_site_setting_selector(setting_name)).find(".setting-controls .ok").click
      end

      def reset_overridden_theme_site_setting(setting_name)
        find(theme_site_setting_selector(setting_name, overridden: true)).find(
          ".setting-controls__undo",
        ).click
        find(theme_site_setting_selector(setting_name)).find(".setting-controls .ok").click
      end

      def has_no_themes_list?
        has_no_css?(".themes-list-header")
      end

      def has_back_button_to_themes_page?
        has_css?(
          '.back-to-themes-and-components a[href="/admin/config/customize/themes"]',
          text: I18n.t("admin_js.admin.config_areas.themes_and_components.themes.back"),
        )
      end

      def click_back_to_themes
        find(".back-to-themes-and-components a").click
      end

      def has_back_button_to_components_page?
        has_css?(
          '.back-to-themes-and-components a[href="/admin/config/customize/components"]',
          text: I18n.t("admin_js.admin.config_areas.themes_and_components.components.back"),
        )
      end

      def click_back_to_components
        find('.back-to-themes-and-components a[href="/admin/config/customize/components"]').click
      end

      def fill_in_setting(setting_name, value)
        find("#{setting_selector(setting_name)} input.input-setting-string").fill_in(with: value)
        self
      end

      def has_setting_value?(setting_name, value)
        has_css?("#{setting_selector(setting_name)} input.input-setting-string") do |input|
          input.value == value
        end
      end

      def click_edit_code
        find(".edit-code").click
      end

      def click_add_all_themes_button
        find(".relative-theme-selector .setting-label .btn-link").click
        find(".setting-controls .ok").click
      end

      def has_no_page_header?
        has_no_css?(".d-page-header")
      end

      def reset_overridden_setting(setting_name)
        setting_section = find("section.theme.settings .setting[data-setting=\"#{setting_name}\"]")
        setting_section.click_button(I18n.t("admin_js.admin.settings.reset"))
        setting_section.find(".setting-controls .ok").click
      end

      def click_select_inactive_mode
        find(".select-inactive-mode").click
      end

      def cancel_select_inactive_mode
        find(".cancel-select-inactive-mode").click
      end

      def has_inactive_themes_selected?(count:)
        has_css?(".inactive-theme input:checked", count: count)
      end

      def has_themes?(count:)
        has_css?(".themes-list-container__item", count: count)
      end

      def toggle_all_inactive
        find(".toggle-all-inactive").click
      end

      def has_disabled_delete_theme_button?
        find_button("Delete", disabled: true)
      end

      def click_delete_button_and_confirm
        find(".delete").click
        PageObjects::Components::Dialog.new.click_danger
        self
      end

      def click_edit_objects_setting_button(setting_name)
        find(".theme-setting[data-setting=\"#{setting_name}\"] .setting-value-edit-button").click
        PageObjects::Pages::AdminObjectsSettingEditor.new
      end

      def click_theme_settings_editor_button
        click_button(I18n.t("admin_js.admin.customize.theme.settings_editor"))
        PageObjects::Components::AdminThemeSettingsEditor.new.opened?
      end

      def switch_to_components
        find(".components-tab").click
      end

      def switch_to_themes
        find(".themes-tab").click
      end

      def search(term)
        find(".themes-list-search__input").fill_in with: term
      end

      def has_no_search?
        has_no_css?(".themes-list-search__input")
      end

      def click_delete
        find(".theme-controls .btn-danger").click
      end

      def confirm_delete
        find(".dialog-footer .btn-danger").click
      end

      def parent_themes_selector
        PageObjects::Components::SelectKit.new(".parent-themes-setting .select-kit")
      end

      def included_components_selector
        PageObjects::Components::SelectKit.new(".included-components-setting .select-kit")
      end

      def relative_themes_save_button
        find(".relative-theme-selector .setting-controls__ok")
      end

      def has_reset_button_for_setting?(css_selector)
        has_css?("#{css_selector} .setting-controls__undo")
      end

      private

      def setting_selector(setting_name, overridden: false)
        "section.theme.settings .setting#{overridden ? ".overridden" : ""}[data-setting=\"#{setting_name}\"]"
      end

      def theme_site_setting_selector(setting_name, overridden: false)
        "section.theme.theme-site-settings .setting#{overridden ? ".overridden" : ""}.theme-site-setting[data-setting=\"#{setting_name}\"]"
      end
    end
  end
end