-# このページ内専用のjavascriptでjQueryオブジェクトを参照したい場合この関数に記述
:javascript
  local_initialize_functions.push(function($) {
    $("[data-toggle=tooltip]").tooltip({
      animation: false,
      container: "body",
    });
    // セレクトボタン設定
    $(".selectpicker").selectpicker({
      container: "body",
      width: "100%"
    });

    var storage = new Narou.Storage;
    // 直前に開いていたタブを復元
    var related_active_tab = storage.get("related_active_tab");
    if (related_active_tab) {
      $('a[href="#' + related_active_tab + '"]').tab("show");
    }
    // 開いたタブを記憶しておく
    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
      var active_tab = (e.target + "").split("#")[1];
      storage.set("related_active_tab", active_tab);
      storage.save();
    })
  });

%form(action="/settings" method="POST")
  .navbar.navbar-default.navbar-fixed-top(role="navigation")
    .container
      .navbar-header
        %button.navbar-toggle(type="button")
          %span.sr-only Toggle navigation
          %span.icon-bar
          %span.icon-bar
          %span.icon-bar
        %a.navbar-brand(href="#" data-move-to="top") Narou.rb WEB UI
      .collapse.navbar-collapse
        %ul.nav.navbar-nav
          %li.active
            %a(href="/settings") 環境設定
          %li
            %a(href="/help") ヘルプ
          %li
            %a(href="/") 戻る
        .navbar-form.navbar-left
          %input.btn.btn-info(type="submit" value="設定を保存" style="margin-left: 50px")
        %p#queue-text.navbar-text.navbar-right(data-toggle="tooltip" title="キューに積まれた処理の数" data-placement="bottom")
          %span.glyphicon.glyphicon-inbox
          %span#queue 0

  .container.container-main.theme-showcase(role="main")
    .container-settings
      - inventory = Inventory.load("local_setting").merge(Inventory.load("global_setting", :global))
      - local_and_global_variables = @setting_variables[:local].merge(@setting_variables[:global])
      - tab_info = Command::Setting.get_setting_tab_info
      - error_tabs = @error_list.map { |name, _| local_and_global_variables[name][:tab] }.uniq

      %p
        %ul.nav.nav-pills(role="tablist")
          - Command::Setting.get_setting_tab_names.each.with_index do |(key, name), index|
            %li(role="presentation"){class: index == 0 ? "active" : nil}
              %a{ href: "##{key}", :"aria-controls" => "#{key}", role: "tab", :"data-toggle" => "tab" }
                = name
                - if error_tabs.include?(key)
                  %span.text-danger.glyphicon.glyphicon-exclamation-sign
          %li(role="presentation")
            %a(href="#replace" aria-controls="replace" role="tab" data-toggle="tab")
              置換設定

      -# :tab が設定されていないものを hidden として設定しておく
      - local_and_global_variables.each do |name, value|
        - unless value[:tab]
          - item = inventory[name]
          - if value[:type] == :boolean
            - hidden_value = convert_boolean_to_on_off(item)
          - else
            - hidden_value = item
          %input{type:"hidden", name:name, value:hidden_value}

      .tab-content
        - Command::Setting.get_setting_tab_names.each.with_index do |(key, _), index|
          .tab-pane{ role: "tabpanel", id: "#{key}", class: index == 0 ? "active" : nil }
            .panel.panel-info.panel-settings
              - if tab_info.has_key?(key)
                .panel-heading
                  = tab_info[key]
              .list-group
                - local_and_global_variables.each do |name, value|
                  - next if value[:tab] != key
                  - item = inventory[name]
                  - need_clear = false
                  - message = Narou::SETTING_VARIABLES_WEBUI_MESSAGES[name] || value[:help]
                  - message = message.strip.gsub("\n", "<br>")
                  - error_style = {class: "has-error", style: "background-color:#f2dede"}
                  .list-group-item.form-group{@error_list[name] ? error_style : {}}
                    %h4.list-group-item-heading #{name}
                    .list-group-item-text
                      - case value[:type]
                      - when :boolean
                        - if name =~ /^(force|default)\./
                          .switch-toggle.well.switch-3
                            <input id="#{name}-nil" type="radio" name="#{name}" value="nil" #{item.nil? ? "checked" : ""}>
                            %label(for="#{name}-nil")
                              未設定
                            <input id="#{name}-false" type="radio" name="#{name}" value="off" #{item == false ? "checked" : ""}>
                            %label(for="#{name}-false")
                              いいえ
                            <input id="#{name}-true" type="radio" name="#{name}" value="on" #{item ? "checked" : ""}>
                            %label(for="#{name}-true")
                              はい
                            %a.btn.btn-primary
                        - else
                          %label.switch-light.well
                            <input type="checkbox" name="#{name}" #{item ? "checked" : ""}>
                            %span
                              %span いいえ
                              %span はい
                            %a.btn.btn-primary
                      - when :select
                        %div.input-style
                          %select{name: name, class: "selectpicker show-tick"}
                            %option(value="")
                              - if name == "theme"
                                デフォルト
                              - else
                                未設定
                            - value[:select_keys].each_with_index do |key, index|
                              %option{selected: item == key, value: key}
                                = value[:select_summaries][index]
                        - need_clear = true
                      - when :multiple
                        %div.input-style
                          %select{name: "#{name}[]", class: "selectpicker", multiple: true,
                                  title: "未設定", "data-selected-text-format" => "count"}
                            - selected_items = item.to_s.split(",")
                            - value[:select_keys].each_with_index do |key, index|
                              %option{selected: selected_items.include?(key), value: key}
                                = value[:select_summaries][index]
                        - need_clear = true
                      - else
                        %input{type:"text", name:name, value:item, class:"form-control",
                               placeholder:Helper.variable_type_to_description(value[:type]).strip }
                        - need_clear = true
                      %p
                        = message != "" ? message : "&nbsp;"
                      - if @error_list[name]
                        %p.alert-danger
                          = @error_list[name]
                      - if need_clear
                        .clearfix

        .tab-pane(role="tabpanel" id="replace")
          - replace_panel_var = { title: "全小説対象の置換設定",
            panel_class: "panel-info",
            body_class: "list-group-item-default",
            body: ["・全ての小説に対する置換設定を行うことが出来ます",
                   "・変更を反映させるには再度変換を実行する必要があります"] }
          = haml :_edit_replace_txt, locals: { replace_panel_var: replace_panel_var }

        %input(type="submit" value="設定を保存" class="btn btn-info" style="float:none")