Sha256: 1016e2410ca63bf6d04dde93cc20f22b304dce7971091ee01f3868bdfdcb2dfa

Contents?: true

Size: 1.68 KB

Versions: 46

Compression:

Stored size: 1.68 KB

Contents

# -*- 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

Version data entries

46 entries across 40 versions & 2 rubygems

Version Path
tdiary-contrib-5.3.0 plugin/show_and_hide.rb
tdiary-contrib-5.2.4 plugin/show_and_hide.rb
tdiary-contrib-5.2.3 plugin/show_and_hide.rb
tdiary-contrib-5.2.2 plugin/show_and_hide.rb
tdiary-contrib-5.2.1 plugin/show_and_hide.rb
tdiary-contrib-5.2.0 plugin/show_and_hide.rb
tdiary-contrib-5.1.7 plugin/show_and_hide.rb
tdiary-contrib-5.1.6 plugin/show_and_hide.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/tdiary-contrib-5.1.4/plugin/show_and_hide.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/bundler/gems/tdiary-contrib-d1e41204db13/plugin/show_and_hide.rb
tdiary-contrib-5.1.5 plugin/show_and_hide.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/tdiary-contrib-5.1.4/plugin/show_and_hide.rb
tdiary-contrib-5.1.4 plugin/show_and_hide.rb
tdiary-contrib-5.1.3 plugin/show_and_hide.rb
tdiary-contrib-5.1.2 plugin/show_and_hide.rb
tdiary-contrib-5.1.1 plugin/show_and_hide.rb
tdiary-contrib-5.1.0 plugin/show_and_hide.rb
tdiary-contrib-5.0.13 plugin/show_and_hide.rb
tdiary-contrib-5.0.12 plugin/show_and_hide.rb
tdiary-contrib-5.0.11 plugin/show_and_hide.rb