File: | lib/Railsish/ViewHelpers.pm |
Coverage: | 48.8% |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package Railsish::ViewHelpers; | ||||||
2 | 11 11 11 | 93 31 118 | use strict; | ||||
3 | 11 11 11 | 111 48 100 | use warnings; | ||||
4 | 11 11 11 | 2319 32 88 | use Railsish::CoreHelpers; | ||||
5 | 11 11 11 | 892 51 220 | use HTML::Entities; | ||||
6 | |||||||
7 | 11 11 11 | 133 33 131 | use Exporter::Lite; | ||||
8 | our @EXPORT = qw(render_stickies stylesheet_link_tag javascript_include_tag link_to); | ||||||
9 | |||||||
10 | sub stylesheet_link_tag { | ||||||
11 | 0 | 0 | 0 | my (@css) = @_; | |||
12 | |||||||
13 | 0 | 0 | my $out = ""; | ||||
14 | 0 | 0 | for my $css (@css) { | ||||
15 | 0 | 0 | my $uri; | ||||
16 | 0 | 0 | if ($css =~ /^http:/) { | ||||
17 | 0 | 0 | $uri = $css; | ||||
18 | } | ||||||
19 | else { | ||||||
20 | 0 | 0 | my $dir = app_root("/public/stylesheets"); | ||||
21 | |||||||
22 | 0 | 0 | my $file = "${dir}/${css}.css"; | ||||
23 | 0 | 0 | $file = "${dir}/${css}" unless -f $file; | ||||
24 | 0 | 0 | $file .= "?" . (stat($file))[9]; | ||||
25 | 0 | 0 | $uri = $file; | ||||
26 | 0 | 0 | $uri =~ s/^\Q$dir\E/\/stylesheets/; | ||||
27 | } | ||||||
28 | |||||||
29 | 0 | 0 | if ($uri) { | ||||
30 | 0 | 0 | $out .= qq{<link href="$uri" media="all" rel="stylesheet" type="text/css">\n} | ||||
31 | } | ||||||
32 | } | ||||||
33 | 0 | 0 | return $out; | ||||
34 | }; | ||||||
35 | |||||||
36 | sub javascript_include_tag { | ||||||
37 | 6 | 0 | 66 | my @sources = @_; | |||
38 | 6 | 48 | my $out = ""; | ||||
39 | 6 | 46 | for my $source (@sources) { | ||||
40 | 6 | 36 | my $uri; | ||||
41 | 6 | 68 | if ($source =~ /^\w+:\/\//) { | ||||
42 | 2 | 14 | $uri = $source; | ||||
43 | } | ||||||
44 | else { | ||||||
45 | 4 | 30 | $uri = $source; | ||||
46 | 4 | 48 | $uri .= ".js" if $source !~ /\./; | ||||
47 | 4 | 48 | $uri = "/javascripts/$uri" if $source !~ /\//; | ||||
48 | } | ||||||
49 | |||||||
50 | 6 | 72 | $out .= qq{<script type="text/javascript" src="$uri"></script>\n}; | ||||
51 | } | ||||||
52 | 6 | 130 | return $out; | ||||
53 | } | ||||||
54 | |||||||
55 | sub link_to { | ||||||
56 | 3 | 0 | 32 | my ($label, $url, @attr) = @_; | |||
57 | |||||||
58 | 3 | 15 | my $attr = ""; | ||||
59 | 3 | 11 | my %attr = (); | ||||
60 | 3 | 34 | if (@attr == 1 && ref($attr[0]) eq 'HASH') { | ||||
61 | 0 0 | 0 0 | %attr = %{$attr[0]}; | ||||
62 | } | ||||||
63 | elsif (@attr % 2 == 0) { | ||||||
64 | 3 | 15 | %attr = (@attr); | ||||
65 | } | ||||||
66 | |||||||
67 | 3 | 33 | if (%attr) { | ||||
68 | 3 | 7 | my $js; | ||||
69 | 3 | 21 | if ($attr{method} && $attr{method} eq 'delete') { | ||||
70 | 0 | 0 | $js = <<JS; | ||||
71 | var f = document.createElement('form'); | ||||||
72 | f.style.display = 'none'; this.parentNode.appendChild(f); | ||||||
73 | f.method = 'POST'; f.action = this.href; | ||||||
74 | var m = document.createElement('input'); | ||||||
75 | m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); | ||||||
76 | m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); | ||||||
77 | JS | ||||||
78 | } | ||||||
79 | |||||||
80 | 3 | 33 | if (my $confirm = delete $attr{confirm}) { | ||||
81 | 0 | 0 | $js ||= "return true;"; | ||||
82 | 0 | 0 | $attr{onclick} ||= ""; | ||||
83 | 0 | 0 | $attr{onclick} .= ";if(confirm(\"$confirm\")) { $js }; return false;"; | ||||
84 | } elsif ($js) { | ||||||
85 | 0 | 0 | $attr{onclick} ||= ""; | ||||
86 | 0 | 0 | $attr{onclick} .= "$js"; | ||||
87 | } | ||||||
88 | |||||||
89 | 3 3 3 | 6 27 22 | $attr .= qq{ $_="@{[ encode_entities($attr{$_}, '<>&"') ]}"} for keys %attr; | ||||
90 | } | ||||||
91 | 3 3 | 80 15 | qq{<a href="$url"$attr>@{[ encode_entities($label, '<>&') ]}</a>}; | ||||
92 | } | ||||||
93 | |||||||
94 | sub render_stickies { | ||||||
95 | 0 | 0 | my $session = &Railsish::Controller::session; | ||||
96 | |||||||
97 | 0 0 | return "" unless @{$session->{notice_stickies}||[]} > 0; | |||||
98 | |||||||
99 | 0 | my $out = '<div id="notice_stickies" class="message notice">'; | |||||
100 | 0 0 | while(my $stickie = pop @{$session->{notice_stickies}}) { | |||||
101 | 0 | $out .= "<p>" . $stickie->{text} . "</p>"; | |||||
102 | } | ||||||
103 | 0 | $out .= "</div>"; | |||||
104 | |||||||
105 | 0 | return $out; | |||||
106 | } | ||||||
107 | |||||||
108 | 1; |