Sha256: b82664d3cfd382dac53a5d85af80c0496b62f9cc33f54f4f1bb09f7b29fc4a14

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

module SuperInteraction
  class Command < Struct.new(:context)

    attr_accessor :commands

    delegate :helpers, to: :context

    def run
      context.render js: (commands || []).join(";")
    end

    def bs_notice(message)
      bs_alert('info', message)
      self
    end

    def bs_danger(message)
      bs_alert('danger', message)
      self
    end

    def bs_success(message)
      bs_alert('success', message)
    end

    def alert(message)
      cmd("alert('#{helpers.j(message)}');")
    end

    # modal 裡如果有 javascript 需寫在 .modal 層
    # size: sm / md / lg / xl / xxl
    # 注意:不要包 respond_to :js 會有問題
    def modal(partial: nil, size: 'md', title: '', desc: '')
      partial ||= context.action_name
      modal_html = context.render_to_string(partial, layout: ::SuperInteraction::Layout.modal_layout, locals: { bs_modal_size: size, title: title, desc: desc })
      cmd("$(function() { $.modal.show('#{helpers.j(modal_html)}'); });")
    end

    # 關閉 Modal
    def close
      cmd("$.modal.close();")
    end

    # 重新讀取頁面
    def reload
      cmd("Turbolinks.visit(location.toString());");
    end

    # 導入頁面
    def redirect_to(url)
      cmd("Turbolinks.visit('#{url}');");
    end

    def modal_saved_rediret_to(message, redirect_url)
      close.alert(message).redirect_to(redirect_url)
    end

    def modal_saved_reload(message)
      close.alert(message).reload
    end

    def ajax_get(url)
      cmd("$.get('#{url}');")
    end

    def bs_alert(class_type, text)
      cmd("if (typeof($.alert) == undefined) { alert('#{helpers.j(text)}'); } else { $.alert.#{class_type}('#{helpers.j(text)}'); }")
    end

    def cmd(js_code)
      self.commands ||= []
      self.commands.push(js_code)
      self
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
super_tools-0.0.26 lib/super_interaction/command.rb
super_tools-0.0.25 lib/super_interaction/command.rb
super_tools-0.0.21 lib/super_interaction/command.rb
super_tools-0.0.20 lib/super_interaction/command.rb