summaryrefslogtreecommitdiff
path: root/web/lib/javascript.rb
blob: ff9f17824c7f1971c91cea06b62a411ad75e6ac3 (plain)
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
# web/lib/javascript.rb

def javascript(url=nil, &block)
    @javascript ||= Array.new
    @javascript << Javascript.new(url, &block)
end

def javascript_tags
    @javascript.flatten.uniq.map{ |js| js.to_html }.join("\n")
end

def javascript_for(*ids)
    (@javascript ||= [] ) << Javascript.init(ids)
end

class Javascript

    @@js_files = {
#        :common    => [ 'jquery-1.11.1.min', 'jquery-ui-1.9.2.custom.min', 'customSelect.jquery.min', 'jquery.tipsy-minified', 'jquery.cookie-minified' ],
        :common    => [ 'common' ],
        :taginfo   => [ 'taginfo' ],
        :flexigrid => [ 'jquery-migrate-1.2.1.min', 'flexigrid-minified' ],
        :d3        => [ 'd3/d3.v3.min' ],
        :d3_cloud  => [ 'd3/d3.layout.cloud' ],
    }

    def self.init(ids)
        js = []
        ids.each do |id|
            @@js_files[id].each do |file|
                js << self.new(file)
            end
        end
        js
    end

    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