/
slot.rb
405 lines (337 loc) · 12.3 KB
/
slot.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
class Slot < Renderer
cattr_accessor :render_actions
attr_accessor :options_need_save, :js_queue_initialized,
:position, :start_time, :skip_autosave
# This creates a separate class hash in the subclass
class << self
def actions() @@render_actions||={} end
end
def action_method(key)
(cls=self.class).actions.has_key?(key) ? cls.actions[key] : super
end
# FIXME: simplify this to (card, opts)
def initialize(card, opts=nil)
super
Renderer.current_slot ||= self
@context = "main_1" unless @context =~ /\_/
@position = @context.split('_').last
@state = :view
@renders = {}
@js_queue_initialized = {}
if card and card.is_collection? and item_param=params[:item]
@item_view = item_param if !item_param.blank?
end
end
### --- render action declarations --- wrapped views are defined for slots
view(:layout) do |args|
@main_card, mc = args.delete(:main_card), args.delete(:main_content)
@main_content = (mc.blank? || mc.nil?) ? nil : wrap_main(mc)
_render_core
end
view(:content) do |args|
@state = :view
self.requested_view = args[:action] = 'content'
c = _render_naked(args)
c = "<span class=\"faint\">--</span>" if c.size < 10 && strip_tags(c).blank?
wrap(args) { wrap_content(c) }
end
view(:titled) do |args|
self.requested_view = 'titled'
args[:action] = 'content'
wrap(args) do
content_tag( :h1, fancy_title(card.name) ) +
wrap_content(_render_naked(args))
end
end
view(:new) do |args|
wrap(args) { render_partial('views/new') }
end
view(:open) do |args|
@state = :view
self.requested_view = 'open'
wrap(args) { render_partial('views/open') }
end
view(:closed) do |args|
@state = :line
self.requested_view = args[:action] = 'closed'
wrap(args) { render_partial('views/closed') }
end
view(:setting) do |args|
self.requested_view = args[:action] = 'content'
wrap( args) { render_partial('views/setting') }
end
view(:edit) do |args|
@state=:edit
card.content_template ? _render_multi_edit(args) : content_field(slot.form)
end
view(:multi_edit) do |args|
@state=:edit
args[:add_javascript]=true
@form = form_for_multi
hidden_field_tag(:multi_edit, true) + _render_core(args)
end
view(:change) do |args|
self.requested_view = args[:action] = 'content'
wrap(args) { render_partial('views/change') }
end
###---( EDIT VIEWS )
view(:edit_in_form) do |args|
render_partial('views/edit_in_form', args.merge(:form=>form_for_multi))
end
def js
@js ||= SlotJavascript.new(self)
end
# FIXME: passing a block seems to only work in the templates and not from
# internal slot calls, so I added the option passing internal content which
# makes all the ugly block_given? ifs..
def wrap(args = {})
render_wrap = ( args.key?(:add_slot) ? args.delete(:add_slot) : !skip_outer_wrap_for_ajax? )
return yield if !render_wrap
css_class = case args[:action].to_s
when 'content' ; 'transcluded'
when 'exception'; 'exception'
when 'closed' ; 'card-slot line'
else ; 'card-slot paragraph'
end
css_class << " " + Wagn::Pattern.css_names( card ) if card
attributes = {
:class => css_class,
:cardId => (card && card.id),
:position => UUID.new.generate.gsub(/^(\w+)0-(\w+)-(\w+)-(\w+)-(\w+)/,'\1')
}
[:style, :view, :item, :base].each { |key| attributes[key] = args[key] }
div( attributes ) { yield }
end
def skip_outer_wrap_for_ajax?
# we often skip the outermost slot in ajax calls because the slot is already there.
ajax_call? && outer_level?
end
def wrap_content( content="" )
%{<span class="#{canonicalize_view(requested_view)}-content content editOnDoubleClick">} +
content.to_s +
%{</span>} #<!--[if IE]> <![endif]-->}
end
def wrap_main(content)
return content if p=root.params and p[:layout]=='none'
%{<div id="main" context="main">#{content}</div>}
end
#### -------------------- additional helpers ---------------- ###
def notice
# this used to access controller.notice, but as near I can tell
# nothing ever assigns to controller.notice, so I took it away.
# entries in flash[:notice] would be more appropriate in the page-wide
# alert area. a quick javascript hack to have this put them there resulted in
# odd behavior so leaving it off for now -LWH
%{<span class="notice"></span>}
end
def id(area="")
area, id = area.to_s, ""
id << "javascript:#{get(area)}"
end
def parent
"javascript:getSlotSpan(getSlotSpan(this).parentNode)"
end
def nested_context?
context.split('_').length > 2
end
def get(area="")
area.empty? ? "getSlotSpan(this)" : "getSlotElement(this, '#{area}')"
end
def selector(area="")
"getSlotFromContext('#{context}')";
end
def card_id
(card.new_record? && card.name) ? Cardname.escape(card.name) : card.id
end
def editor_id(area="")
area, eid = area.to_s, ""
eid << context
eid << (area.blank? ? '' : "-#{area}")
end
def edit_submenu(on)
div(:class=>'submenu') do
[[ :content, true ],
[ :name, true, ],
[ :type, !(card.type_template? || (card.type=='Cardtype' and ct=card.me_type and !ct.find_all_by_trash(false).empty?))],
[ :codename, (System.always_ok? && card.type=='Cardtype')],
[ :inclusions, !(card.out_transclusions.empty? || card.template? || card.hard_template), {:inclusions=>true} ]
].map do |key,ok,args|
link_to_remote( key,
{ :url=>url_for("card/edit", args, key), :update => ([:name,:type,:codename].member?(key) ? id('card-body') : id) },
:class=>(key==on ? 'on' : '')
) if ok
end.compact.join
end
end
def options_submenu(on)
div(:class=>'submenu') do
[:permissions, :settings].map do |key|
link_to_remote( key,
{ :url=>url_for("card/options", {}, key), :update => id },
:class=>(key==on ? 'on' : '')
)
end.join
end
end
def url_for(url, args=nil, attribute=nil)
# recently changed URI.escape to CGI.escape to address question mark issue, but I'm still concerned neither is perfect
# so long as we keep doing the weird Cardname.escape thing.
url = "javascript:'/#{url}"
url << "/#{escape_javascript(CGI.escape(card_id.to_s))}" if (card and card_id)
url << "/#{attribute}" if attribute
url << "?context='+getSlotContext(this)"
url << "+'&' + getSlotOptions(this)"
url << ("+'"+ args.map{|k,v| "&#{k}=#{escape_javascript(CGI.escape(v.to_s))}"}.join('') + "'") if args
url
end
def header
template.render :partial=>'card/header', :locals=>{ :card=>card, :slot=>self }
end
def menu
if card.virtual?
return %{<span class="card-menu faint">Virtual</span>\n}
end
menu_options = [:view,:changes,:options,:related,:edit]
top_option = menu_options.pop
menu = %{<span class="card-menu">\n}
menu << %{<span class="card-menu-left">\n}
menu_options.each do |opt|
menu << link_to_menu_action(opt.to_s)
end
menu << "</span>"
menu << link_to_menu_action(top_option.to_s)
menu << "</span>"
end
def footer
render_partial 'card/footer'
end
def footer_links
render_partial( 'card/footer_links' ) # this is ugly reusing this cache code
end
def option( *args, &proc)
args = args[0]||{} #unless Hash===args
args[:label] ||= args[:name]
args[:editable]= true unless args.has_key?(:editable)
self.options_need_save = true if args[:editable]
concat %{<tr>
<td class="inline label"><label for="#{args[:name]}">#{args[:label]}</label></td>
<td class="inline field">
}, proc.binding
yield
concat %{
</td>
<td class="help">#{args[:help]}</td>
</tr>
}, proc.binding
end
def option_header(title)
%{<tr><td colspan="3" class="option-header"><h2>#{title}</h2></td></tr>}
end
def link_to_menu_action( to_action)
menu_action = (%w{ show update }.member?(action) ? 'view' : action)
content_tag( :li, link_to_action( to_action.capitalize, to_action, {} ),
:class=> (menu_action==to_action ? 'current' : ''))
end
def link_to_action( text, to_action, remote_opts={}, html_opts={})
link_to_remote text, {
:url=>url_for("card/#{to_action}"),
:update => id
}.merge(remote_opts), html_opts
end
def button_to_action( text, to_action, remote_opts={}, html_opts={})
if remote_opts.delete(:replace)
r_opts = { :url=>url_for("card/#{to_action}", :replace=>id ) }.merge(remote_opts)
else
r_opts = { :url=>url_for("card/#{to_action}" ), :update => id }.merge(remote_opts)
end
button_to_remote( text, r_opts, html_opts )
end
def name_field(form,options={})
form.text_field( :name, { :class=>'field card-name-field', :autocomplete=>'off'}.merge(options))
end
def cardtype_field(form,options={})
template.select_tag('card[type]', cardtype_options_for_select(Cardtype.name_for(card.type)), options)
end
def update_cardtype_function(options={})
fn = ['File','Image'].include?(card.type) ?
"Wagn.onSaveQueue['#{context}']=[];" :
"Wagn.runQueue(Wagn.onSaveQueue['#{context}']); "
fn << remote_function( options )
end
def js_content_element
@card.hard_template ? "" : ",getSlotElement(this,'form').elements['card[content]']"
end
def content_field(form,options={})
self.form = form
@nested = options[:nested]
pre_content = (card and !card.new_record?) ? form.hidden_field(:current_revision_id, :class=>'current_revision_id') : ''
User.as :wagbot do
pre_content + clear_queues + self.render_partial( card_partial('editor'), options ) + setup_autosave
end
end
def clear_queues
queue_context = get_queue_context
return '' if root.js_queue_initialized.has_key?(queue_context)
root.js_queue_initialized[queue_context]=true
javascript_tag(
"Wagn.onSaveQueue['#{queue_context}']=[];\n"+
"Wagn.onCancelQueue['#{queue_context}']=[];"
)
end
def save_function
"if(ds=Wagn.draftSavers['#{context}']){ds.stop()}; if (Wagn.runQueue(Wagn.onSaveQueue['#{context}'])) { } else {return false}"
end
def cancel_function
"if(ds=Wagn.draftSavers['#{context}']){ds.stop()}; Wagn.runQueue(Wagn.onCancelQueue['#{context}']);"
end
def get_queue_context
#FIXME: this looks like it won't work for arbitraritly nested forms. 1-level only
@nested ? context.split('_')[0..-2].join('_') : context
end
def editor_hooks(hooks)
# it seems as though code executed inline on ajax requests works fine
# to initialize the editor, but when loading a full page it fails-- so
# we run it in an onLoad queue. the rest of this code we always run
# inline-- at least until that causes problems.
queue_context = get_queue_context
code = ""
if hooks[:setup]
code << "Wagn.onLoadQueue.push(function(){\n" unless ajax_call?
code << hooks[:setup]
code << "});\n" unless ajax_call?
end
if hooks[:save]
code << "Wagn.onSaveQueue['#{queue_context}'].push(function(){\n #{hooks[:save]} \n });\n"
end
if hooks[:cancel]
code << "Wagn.onCancelQueue['#{queue_context}'].push(function(){\n #{hooks[:cancel]} \n });\n"
end
javascript_tag code
end
def setup_autosave
if @nested or skip_autosave
""
else
javascript_tag "Wagn.setupAutosave('#{card.id}', '#{context}');\n"
end
end
def half_captcha
if controller && captcha_required?
key = card.new_record? ? "new" : card.key
javascript_tag(%{loadScript("http://api.recaptcha.net/js/recaptcha_ajax.js")}) +
recaptcha_tags( :ajax=>true, :display=>{:theme=>'white'}, :id=>key)
end
end
def full_captcha
if captcha_required?
key = card.new_record? ? "new" : card.key
recaptcha_tags( :ajax=>true, :display=>{:theme=>'white'}, :id=>key ) +
javascript_tag(
%{jQuery.getScript("http://api.recaptcha.net/js/recaptcha_ajax.js", function(){
document.getElementById('dynamic_recaptcha-#{key}').innerHTML='<span class="faint">loading captcha</span>';
Recaptcha.create('#{ENV['RECAPTCHA_PUBLIC_KEY']}', document.getElementById('dynamic_recaptcha-#{key}'),RecaptchaOptions);
});
})
end
end
end