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.0.10 plugin/show_and_hide.rb
tdiary-contrib-5.0.9 plugin/show_and_hide.rb
tdiary-contrib-5.0.8 plugin/show_and_hide.rb
tdiary-contrib-5.0.7 plugin/show_and_hide.rb
tdiary-contrib-5.0.6 plugin/show_and_hide.rb
tdiary-contrib-5.0.5 plugin/show_and_hide.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/bundler/gems/tdiary-contrib-8299b30cbf3f/plugin/show_and_hide.rb
tdiary-5.0.5 vendor/bundle/bundler/gems/tdiary-contrib-fa9c20aa0d97/plugin/show_and_hide.rb
tdiary-5.0.5 vendor/bundle/bundler/gems/tdiary-contrib-8299b30cbf3f/plugin/show_and_hide.rb
tdiary-5.0.5 vendor/bundle/bundler/gems/tdiary-contrib-a3c5f37278c4/plugin/show_and_hide.rb
tdiary-5.0.5 vendor/bundle/bundler/gems/tdiary-contrib-f08988dfa0f6/plugin/show_and_hide.rb
tdiary-contrib-5.0.4 plugin/show_and_hide.rb
tdiary-5.0.4 vendor/bundle/bundler/gems/tdiary-contrib-8299b30cbf3f/plugin/show_and_hide.rb
tdiary-contrib-5.0.3 plugin/show_and_hide.rb
tdiary-contrib-5.0.2 plugin/show_and_hide.rb
tdiary-contrib-5.0.1 plugin/show_and_hide.rb
tdiary-contrib-5.0.0 plugin/show_and_hide.rb
tdiary-contrib-4.2.1 plugin/show_and_hide.rb
tdiary-4.2.1 vendor/bundle/ruby/2.3.0/bundler/gems/tdiary-contrib-d189eb6688f1/plugin/show_and_hide.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/bundler/gems/tdiary-contrib-2ab36447ae2a/plugin/show_and_hide.rb