-
Notifications
You must be signed in to change notification settings - Fork 50
/
rstar_pack.rb
203 lines (170 loc) · 7.79 KB
/
rstar_pack.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
class Wagn::Renderer::Html
define_view :closed_rule, :tags=>:unknown_ok do |args|
rule_card = card.new_card? ? find_current_rule_card[0] : card
cells = [
["rule-setting",
link_to( card.cardname.tag_name.sub(/^\*/,''), path(:read, :view=>:open_rule),
:class => 'edit-rule-link slotter', :remote => true )
],
["rule-content",
%{<div class="rule-content-container">
<span class="closed-content content">#{rule_card ? subrenderer(rule_card).render_closed_content : ''}</span>
</div> } ],
["rule-type", (rule_card ? rule_card.type_name : '') ],
]
extra_css_class = rule_card && !rule_card.new_card? ? 'known-rule' : 'missing-rule'
%{<tr class="card-slot closed-rule">} +
cells.map do |css_class, content|
%{<td class="rule-cell #{css_class} #{extra_css_class}">#{content}</td>}
end.join("\n") +
'</tr>'
end
define_view :open_rule, :tags=>:unknown_ok do |args|
current_rule, prototype = find_current_rule_card
setting_name = card.cardname.tag_name
current_rule ||= Card.new :name=> "*all+#{setting_name}"
set_selected = false
if params[:type_reload] && card_args=params[:card]
params.delete :success # otherwise updating the editor looks like a successful post
if card_args[:name] && card_args[:name].to_cardname.key != current_rule.key
current_rule = Card.new card_args
else
current_rule = current_rule.refresh if current_rule.frozen?
current_rule.assign_attributes card_args
current_rule.include_set_modules
end
set_selected = card_args[:name].to_cardname.left_name.to_s
end
opts = {
:fallback_set => false,
:open_rule => card,
:edit_mode => (card.ok?(card.new_card? ? :create : :update) && !params[:success]),
:setting_name => setting_name,
:current_set_key => (current_rule.new_card? ? nil : current_rule.cardname.trunk_name.key),
:set_selected => set_selected
}
if !opts[:read_only]
set_options = prototype.set_names.reverse
first = (csk=opts[:current_set_key]) ? set_options.index{|s| s.to_cardname.key == csk} : 0
if first > 0
set_options[0..(first-1)].reverse.each do |set_name|
opts[:fallback_set] = set_name if Card.exists?("#{set_name}+#{opts[:setting_name]}")
end
end
last = set_options.index{|s| s.to_cardname.key == card.cardname.trunk_name.key} || -1
# note, the -1 can happen with virtual cards because the self set doesn't show up in the set_names. FIXME!!
opts[:set_options] = set_options[first..last]
# The above is about creating the options for the sets to which the user can apply the rule.
# The broadest set should always be the currently applied rule
# (for anything more general, they must explicitly choose to "DELETE" the current one)
# the narrowest rule should be the one attached to the set being viewed. So, eg, if you're looking at the "*all plus" set, you shouldn't
# have the option to create rules based on arbitrary narrower sets, though narrower sets will always apply to whatever prototype we create
end
%{
<tr class="card-slot open-rule">
<td class="rule-cell" colspan="3">
#{subrenderer( current_rule )._render_edit_rule opts }
</td>
</tr>
}
end
define_view :edit_rule, :tags=>:unknown_ok do |args|
edit_mode = args[:edit_mode]
setting_name = args[:setting_name]
current_set_key = args[:current_set_key] || '*all' # Card[:all].name (should have a constant for this?)
open_rule = args[:open_rule]
@item_view ||= :link
form_for card, :url=>path(:update, :no_id=>true), :remote=>true, :html=>
{:class=>"card-form card-rule-form #{edit_mode && 'slotter'}" } do |form|
%{
#{ hidden_field_tag( :success, open_rule.name ) }
#{ hidden_field_tag( :view, 'open_rule' ) }
<div class="card-editor">
<div class="rule-column-1">
<div class="rule-setting">
#{ link_to( setting_name.sub(/^\*/,''), path(:read, :card=>open_rule, :view=>:closed_rule),
:remote => true, :class => 'close-rule-link slotter') }
</div>
<ul class="set-editor">
} +
if edit_mode
raw( args[:set_options].map do |set_name|
set_label =Card.fetch(set_name).label
checked = ( args[:set_selected] == set_name or current_set_key && args[:set_options].length==1 )
'<li>' +
raw( form.radio_button( :name, "#{set_name}+#{setting_name}", :checked=> checked ) ) +
if set_name.to_cardname.to_key == current_set_key
%{<span class="set-label current-set-label">#{ set_label } <em>(current)</em></span>}
else
%{<span class="set-label">#{ set_label }</span>}
end.html_safe +
'</li>'
end.join)
else
%{
<label>applies to:</label>
<span class="set-label current-set-label">
#{current_set_key ? Card.fetch(current_set_key).label : 'No Current Rule' }
</span>
}.html_safe
end +
%{ </ul>
</div>
<div class="rule-column-2">
<div class="instruction rule-instruction">
#{ raw process_content( "{{#{setting_name}+*right+*edit help}}" ).html_safe }
</div>
<div class="type-editor"> }+
if edit_mode
%{<label>type:</label>}+
raw(type_field( :class =>'type-field rule-type-field live-type-field', 'data-remote'=>true,
:href => path(:read, :card=>open_rule, :view=>:open_rule, :type_reload=>true) ) )
elsif current_set_key
'<label>type:</label>'+
%{<span class="rule-type">#{ current_set_key ? card.type_name : '' }</span>}
else; ''; end.html_safe +
%{</div>
<div class="rule-content">
#{
case
when edit_mode ; content_field form, :skip_rev_id=>true
when current_set_key ; render_core
else ''
end
}
</div>
</div>
</div> }.html_safe +
if edit_mode || params[:success]
('<div class="edit-button-area">' +
if params[:success]
(button_tag( 'Edit', :class=>'rule-edit-button slotter', :type=>'button',
:href => path(:read, :card=>open_rule, :view=>:open_rule), :remote=>true ) +
button_tag( 'Close', :class=>'rule-cancel-button', :type=>'button' )).html_safe
else
(if !card.new_card?
b_args = { :remote=>true, :class=>'rule-delete-button slotter', :type=>'button' }
b_args[:href] = path :delete, :view=>:open_rule, :success=>open_rule.cardname.to_url_key
if fset = args[:fallback_set]
b_args['data-confirm']="Deleting will revert to #{setting_name} rule for #{Card.fetch(fset).label }"
end
%{<span class="rule-delete-section">#{ button_tag 'Delete', b_args }</span>}
else; ''; end +
submit_tag( 'Submit', :class=>'rule-submit-button') +
button_tag( 'Cancel', :class=>'rule-cancel-button', :type=>'button' )).html_safe
end +
'</div>').html_safe
else ''; end +
notice.html_safe
end.html_safe
end
private
def find_current_rule_card
# self.card is a POTENTIAL rule; it quacks like a rule but may or may not exist.
# This generates a prototypical member of the POTENTIAL rule's set
# and returns that member's ACTUAL rule for the POTENTIAL rule's setting
set_prototype = (proto_set=Card.fetch( card.cardname.trunk_name )).prototype
rule_card = card.new_card? ? set_prototype.rule_card( card.cardname.tag_name ) : card
[ rule_card, set_prototype ]
end
end