| 1 |
require 'simpleton' |
|---|
| 2 |
|
|---|
| 3 |
module Radiant |
|---|
| 4 |
class AdminUI |
|---|
| 5 |
|
|---|
| 6 |
class DuplicateTabNameError < StandardError; end |
|---|
| 7 |
|
|---|
| 8 |
class Tab |
|---|
| 9 |
attr_accessor :name, :url, :visibility |
|---|
| 10 |
|
|---|
| 11 |
def initialize(name, url, options = {}) |
|---|
| 12 |
@name, @url = name, url |
|---|
| 13 |
@visibility = [options[:for], options[:visibility]].flatten.compact |
|---|
| 14 |
@visibility = [:all] if @visibility.empty? |
|---|
| 15 |
end |
|---|
| 16 |
|
|---|
| 17 |
def shown_for?(user) |
|---|
| 18 |
visibility.include?(:all) or |
|---|
| 19 |
visibility.any? { |role| user.send("#{role}?") } |
|---|
| 20 |
end |
|---|
| 21 |
end |
|---|
| 22 |
|
|---|
| 23 |
class TabSet |
|---|
| 24 |
def initialize |
|---|
| 25 |
@tabs = [] |
|---|
| 26 |
end |
|---|
| 27 |
|
|---|
| 28 |
def add(name, url, options = {}) |
|---|
| 29 |
options.symbolize_keys! |
|---|
| 30 |
before = options.delete(:before) |
|---|
| 31 |
after = options.delete(:after) |
|---|
| 32 |
tab_name = before || after |
|---|
| 33 |
if self[name] |
|---|
| 34 |
raise DuplicateTabNameError.new("duplicate tab name `#{name}'") |
|---|
| 35 |
else |
|---|
| 36 |
if tab_name |
|---|
| 37 |
index = @tabs.index(self[tab_name]) |
|---|
| 38 |
index += 1 if before.nil? |
|---|
| 39 |
@tabs.insert(index, Tab.new(name, url, options)) |
|---|
| 40 |
else |
|---|
| 41 |
@tabs << Tab.new(name, url, options) |
|---|
| 42 |
end |
|---|
| 43 |
end |
|---|
| 44 |
end |
|---|
| 45 |
|
|---|
| 46 |
def remove(name) |
|---|
| 47 |
@tabs.delete(self[name]) |
|---|
| 48 |
end |
|---|
| 49 |
|
|---|
| 50 |
def size |
|---|
| 51 |
@tabs.size |
|---|
| 52 |
end |
|---|
| 53 |
|
|---|
| 54 |
def [](index) |
|---|
| 55 |
if index.kind_of? Integer |
|---|
| 56 |
@tabs[index] |
|---|
| 57 |
else |
|---|
| 58 |
@tabs.find { |tab| tab.name == index } |
|---|
| 59 |
end |
|---|
| 60 |
end |
|---|
| 61 |
|
|---|
| 62 |
def each |
|---|
| 63 |
@tabs.each { |t| yield t } |
|---|
| 64 |
end |
|---|
| 65 |
|
|---|
| 66 |
def clear |
|---|
| 67 |
@tabs.clear |
|---|
| 68 |
end |
|---|
| 69 |
|
|---|
| 70 |
include Enumerable |
|---|
| 71 |
end |
|---|
| 72 |
|
|---|
| 73 |
include Simpleton |
|---|
| 74 |
|
|---|
| 75 |
class PageAdmin |
|---|
| 76 |
class EditUI |
|---|
| 77 |
attr_reader :regions |
|---|
| 78 |
|
|---|
| 79 |
def initialize |
|---|
| 80 |
@regions = HashWithIndifferentAccess.new |
|---|
| 81 |
@partials = [] |
|---|
| 82 |
end |
|---|
| 83 |
|
|---|
| 84 |
def [](name_or_index) |
|---|
| 85 |
case name_or_index |
|---|
| 86 |
when String, Symbol |
|---|
| 87 |
@partials.find {|p| p == name_or_index.to_s } |
|---|
| 88 |
when Integer |
|---|
| 89 |
@partials[name_or_index] |
|---|
| 90 |
end |
|---|
| 91 |
end |
|---|
| 92 |
|
|---|
| 93 |
def <<(partial) |
|---|
| 94 |
add partial |
|---|
| 95 |
end |
|---|
| 96 |
|
|---|
| 97 |
def add(partial, options = {}) |
|---|
| 98 |
after = options.delete(:after) |
|---|
| 99 |
before = options.delete(:before) |
|---|
| 100 |
region = options.delete(:region) |
|---|
| 101 |
partial_name = before || after |
|---|
| 102 |
if region |
|---|
| 103 |
@regions[region] ||= [] |
|---|
| 104 |
if partial_name |
|---|
| 105 |
index = @regions[region].empty? ? 0 : (@regions[region].index(partial_name) || @regions[region].size - 1) |
|---|
| 106 |
index += 1 if before.nil? |
|---|
| 107 |
@regions[region].insert(index, partial) |
|---|
| 108 |
else |
|---|
| 109 |
@regions[region] << partial |
|---|
| 110 |
end |
|---|
| 111 |
elsif partial_name |
|---|
| 112 |
index = @partials.index(partial_name) || size - 1 |
|---|
| 113 |
index += 1 if before.nil? |
|---|
| 114 |
@partials.insert(index, partial) |
|---|
| 115 |
else |
|---|
| 116 |
@partials << partial |
|---|
| 117 |
end |
|---|
| 118 |
return self |
|---|
| 119 |
end |
|---|
| 120 |
|
|---|
| 121 |
def each(&block) |
|---|
| 122 |
@partials.each {|p| yield p } |
|---|
| 123 |
end |
|---|
| 124 |
|
|---|
| 125 |
def size |
|---|
| 126 |
@partials.size |
|---|
| 127 |
end |
|---|
| 128 |
|
|---|
| 129 |
def clear |
|---|
| 130 |
@partials.clear |
|---|
| 131 |
@regions.clear |
|---|
| 132 |
end |
|---|
| 133 |
include Enumerable |
|---|
| 134 |
end |
|---|
| 135 |
|
|---|
| 136 |
attr_reader :edit_partials |
|---|
| 137 |
def initialize |
|---|
| 138 |
@edit_partials = EditUI.new |
|---|
| 139 |
end |
|---|
| 140 |
end |
|---|
| 141 |
|
|---|
| 142 |
attr_accessor :tabs, :page |
|---|
| 143 |
|
|---|
| 144 |
def initialize |
|---|
| 145 |
@tabs = TabSet.new |
|---|
| 146 |
@page = PageAdmin.new |
|---|
| 147 |
end |
|---|
| 148 |
|
|---|
| 149 |
end |
|---|
| 150 |
end |
|---|