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
|
# web/lib/javascript.rb
def javascript(url=nil, &block)
@javascript ||= Array.new
@javascript << Javascript.new(url, &block)
end
def javascript_tags
@javascript.map{ |js| js.to_html }.join("\n")
end
class Javascript
def initialize(file)
if file.nil?
c = ''
r = yield c
@content = (c == '' ? r : c)
else
@file = file
end
end
def to_html
if @file.nil?
%Q{ <script type="text/javascript">//<![CDATA[\n#{ @content }//]]></script>}
else
%Q{ <script type="text/javascript" src="/js/#{ @file }.js"></script>}
end
end
end
class JQuery
# "include" the convenience methods from R18n::Helpers.
# Uses extend instead of include, because we want this
# to work not with instances of JQuery but they should
# show up as JQuery class methods.
extend R18n::Helpers
def self.flexigrid(id, options)
defaults = {
:method => 'GET',
:dataType => 'json',
:pagetext => t.flexigrid.pagetext,
:pagestat => t.flexigrid.pagestat,
:outof => t.flexigrid.outof,
:findtext => t.flexigrid.findtext,
:procmsg => t.flexigrid.procmsg,
:nomsg => t.flexigrid.nomsg,
:errormsg => t.flexigrid.errormsg,
:showToggleBtn => false,
:usepager => true,
:useRp => true,
:rp => 15,
:rpOptions => [10,15,20,25,50,100],
}
"jQuery('##{id}').flexigrid(" + defaults.merge(options).to_json + ");\n"
end
end
class JS
#
# Careful, deep magic!
#
# We redefine the to_json method of the String argument to return
# the raw string. This way we can do JS.raw("foo").to_json and get "foo".
#
def self.raw(code)
code.instance_eval do
def to_json(state=nil)
to_s
end
end
code
end
end
|