File: show_and_hide.rb

package info (click to toggle)
tdiary-contrib 5.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,576 kB
  • ctags: 1,957
  • sloc: ruby: 16,900; lisp: 514; xml: 451; php: 61; sql: 40; sh: 35; makefile: 33
file content (77 lines) | stat: -rw-r--r-- 1,719 bytes parent folder | download | duplicates (5)
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
# -*- coding: utf-8 -*-
#
# show_and_hide.rb - Show or hide the elements with a sliding motion using jQuery.
#
# Copyright (C) 2011, tamoot <tamoot+tdiary@gmail.com>
# You can redistribute it and/or modify it under GPL2.
#

require 'digest/md5'

if /\A(?:latest|day|month|nyear|preview)\z/ =~ @mode
   enable_js('show_and_hide.js')

end

def show_and_hide(contents, title = 'Show contents',
                  type = :link,
                  rss_title = '(Hide contents on RSS. See my page...)')

   data_id = show_and_hide_id(contents)

   toggle_attr = {:class => 'show_and_hide_toggle',
                  :"data-showandhideid" => data_id}

   dom_contents = ''

   if feed? # RSS
      dom_contents = h(rss_title)

   elsif type.to_s == 'button'
      toggle_attr.merge!(:value => title, :type => "button")
      dom_contents = %Q|<input #{hash2attr(toggle_attr)}>| +
                     show_and_hide_contents(contents, data_id)

   else
      toggle_attr.merge!(:href => 'javascript:void(0)')
      dom_contents = %Q|<a #{hash2attr(toggle_attr)}>#{h(title)}</a>| +
                     show_and_hide_contents(contents, data_id)

   end

   dom_contents

end

def show_and_hide_id(contents)
   @@show_and_hide_counter ||= 0
   @@show_and_hide_counter  += 1

   "#{Time::now.strftime("%s")}_#{@@show_and_hide_counter}_#{ Digest::MD5.hexdigest(contents)}"

end

def show_and_hide_contents(contents, id)
   %Q|<pre class="show_and_hide" id="#{id}">#{h(contents)}</pre>|

end

def hash2attr(hash)
   attrs = []

   hash.keys.each do |k|
      attrs << %Q|#{k}="#{hash[k]}"|
   end

   attrs.join(" ")

end

# Local Variables:
# mode: ruby
# indent-tabs-mode: t
# tab-width: 3
# ruby-indent-level: 3
# End:
# vim: ts=3