-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.rb
67 lines (55 loc) · 2.28 KB
/
init.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
module ActionView::Helpers::TagHelper
alias_method :old_tag, :tag
alias_method :old_content_tag, :content_tag
def tag(*args)
has_tab_label = !!(args[1] and not args[1]["tab_label"].blank?)
has_prompt = !!(args[1] and not args[1]["prompt"].blank?)
return old_tag(*args) unless has_prompt or has_tab_label
html = ""
id = args[1]["id"]
if has_tab_label
html << tab_label_html(id,args[1]["tab_label"])
end
html << old_tag(*args)
html << prompt_script(id,args[1]["prompt"]) if has_prompt
html << add_tab_label_script(id) if has_tab_label
html
end
def content_tag(*args,&block)
has_tab_label = !!(args[2] and not args[2]["tab_label"].blank?)
has_prompt = !!(args[2] and not args[2]["prompt"].blank?)
has_maxlength = !!(args[2] and not args[2]["maxlength"].blank?)
return old_content_tag(*args,&block) unless ["textarea","select"].include? args[0].to_s
return old_content_tag(*args,&block) unless has_prompt or has_tab_label or has_maxlength
if has_maxlength
counter = args[2]["counter"]
func = counter ? "; enforce_textarea_maxlength(this,$('#{counter}')); " : "; enforce_textarea_maxlength(this); "
args[2].delete("counter")
args[2]["onkeyup"] ||= ""
args[2]["onkeyup"] << func
args[2]["onchange"] ||= "" #onchange will check the length when you paste changes into the field
args[2]["onchange"] << func
end
id = args[2]["id"]
html = ""
if has_tab_label
html << tab_label_html(args,args[2]["tab_label"])
end
html << old_content_tag(*args,&block)
html << prompt_script(id,args[2]["prompt"]) if has_prompt
html << add_tab_label_script(id) if has_tab_label
html << javascript_tag("setTimeout(function(){ enforce_textarea_maxlength($('#{id}'),$('#{counter}'));}, 1);") if counter
html
end
#==========================================================================================
private
def tab_label_html(id,label)
"<div class='tab_label' style='display:none'><span><label for='#{id}'>#{h label}</label></span></div>"
end
def prompt_script(id,prompt)
javascript_tag("add_prompt($('#{id}'),'#{escape_javascript prompt}')")
end
def add_tab_label_script(id)
javascript_tag("add_tab_label($('#{id}'))")
end
end