Skip to content

Commit

Permalink
- added first version of the tooltip widget
Browse files Browse the repository at this point in the history
- added system to copy default images and javascripts on plugin installation 

git-svn-id: https://rails-widgets.googlecode.com/svn/trunk@30 dbc01b36-f746-0410-8b88-c76c4e1ff24b
  • Loading branch information
paolo committed Oct 13, 2007
1 parent 9df0d54 commit 5cd9931
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 21 deletions.
Binary file added images/tooltip_image.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 16 additions & 6 deletions install.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
# Install hook code here
def copy_image(name)
def copy(file_name, from_dir, to_dir)
from = File.expand_path(File.join(from_dir,file_name))
to = File.expand_path(File.join(to_dir, file_name))
puts "copy #{from} to #{to}"
end

def copy_image(file_name)
plugin_images = File.join(File.dirname(__FILE__), 'images')
app_images = File.join(RAILS_ROOT, 'public/images/widgets')
from = File.expand_path(File.join(plugin_images,name))
to = File.expand_path(File.join(app_images,name))
puts "copy #{from} to #{to}"
copy file_name, plugin_images, app_images
end

copy_image 'tooltip_arrow.gif'
def copy_javascript(file_name)
plugin_javascripts = File.join(File.dirname(__FILE__), 'javascript')
app_javascripts = File.join(RAILS_ROOT, 'public/javascript/widgets')
copy file_name, plugin_javascripts, app_javascripts
end

copy_image 'tooltip_arrow.gif'
copy_image 'tooltip_image.gif'
copy_javascript 'tooltip.js'
9 changes: 9 additions & 0 deletions javascripts/tooltip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// tooltip widget
function toggleTooltip(event, element) {
var __x = Event.pointerX(event);
var __y = Event.pointerY(event);
//alert(__x+","+__y);
element.style.top = __y + 5;
element.style.left = __x - 40;
element.toggle();
}
8 changes: 7 additions & 1 deletion lib/widgets.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Widgets
require 'widgets/core'
require 'widgets/css_template'
require 'widgets/highlightable'

Expand All @@ -24,5 +25,10 @@
# ActionController::Base.helper Widgets::CodeHelper

##### ShowHide #####
require 'widgets'
require 'widgets/showhide_helper'
ActionController::Base.helper Widgets::ShowhideHelper

##### Tooltip #####
require 'widgets/tooltip_helper'
ActionController::Base.helper Widgets::TooltipHelper

11 changes: 11 additions & 0 deletions lib/widgets/core.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module ActionView
module Helpers
module AssetTagHelper
def javascript_include_tag_with_widgets(*sources)
sources << 'widgets/tooltip'
javascript_include_tag_without_widgets(*sources)
end
alias_method_chain :javascript_include_tag, :widgets
end
end
end
13 changes: 13 additions & 0 deletions lib/widgets/tooltip.css.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<style lang="text/css">
.tooltip {
position: absolute;
background-image: url(/images/widgets/tooltip_arrow.gif);
background-repeat: no-repeat;
}

.tooltip_content {
padding: 20px;
margin-top: 20px;
background-color: fdf389;
}
</style>
47 changes: 47 additions & 0 deletions lib/widgets/tooltip_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Widgets
module TooltipHelper
include CssTemplate

def tooltip(name=nil, opts={}, &proc)
opts[:id] ||= rand(1000)
name ||= image_tag('widgets/tooltip_image.gif', :border => 0)
@_binding = proc.binding

out default_css unless @_tooltip_css_done
@_tooltip_css_done = true

out tooltip_link(opts[:id],name)
out javascript_tag(tooltip_link_function(opts[:id]))
out render_tooltip(name, capture(&proc), opts)
nil
end

def tooltip_link(id, name)
link_to name, '#', :id => "#{id}_tooltip_link"
end

def tooltip_link_function(id)
"$('#{id}_tooltip_link').observe('click', function(event){toggleTooltip(event, $('#{id}_tooltip'))});"
end

def close_tooltip_link(id, message)
message ||= 'close'
link_to_function message, "$('#{id}_tooltip').hide()"
end

def render_tooltip(name, content, opts)
html = tag('div', {:id => "#{opts[:id]}_tooltip", :class=>'tooltip', :style => 'display:none'}, true)
html << tag('div', {:id => "#{opts[:id]}_tooltip_content", :class=>'tooltip_content'},true)
html << content
html << '<small>' + close_tooltip_link(opts[:id], opts[:close_message]) + '</small>'
html << '</div></div>'
html
end

# return the name of the erb to parse for the default css generation
def css_template_filename
'tooltip.css.erb'
end

end
end
29 changes: 15 additions & 14 deletions test/table_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ def test_presence_of_instance_methods

def test_should_fail_if_wrong_args
assert_raise(ArgumentError) do
@view.tableize nil, nil
@view.tableize nil
end
assert_raise(ArgumentError) do
@view.tableize nil, []
@view.tableize []
end
assert_raise(ArgumentError) do
@view.tableize 'main', nil
@view.tableize nil, :name => 'main'
end
assert_raise(ArgumentError) do
@view.tableize 'main', []
@view.tableize [], :name => 'main'
end
assert_raise(ArgumentError) do
@view.tableize :the_name, [], :cols => 1 do
@view.tableize [], :cols => 1, :name => :the_name do
# nothing
end
end
Expand All @@ -40,20 +40,20 @@ def test_should_fail_if_wrong_args
def test_block_invariance
_erbout = ''
assert_nothing_raised do
@view.tableize :the_name, ['IS', 'Same!', 'Thing?'] do |i|
@view.tableize ['IS', 'Same!', 'Thing?'], :name => :the_name do |i|
_erbout.concat i
end
end
expected, _erbout = _erbout, ''
assert_nothing_raised do
@view.tableize(:the_name, ['IS', 'Same!', 'Thing?']) { |i| _erbout.concat i }
@view.tableize(['IS', 'Same!', 'Thing?'], :name => :the_name) { |i| _erbout.concat i }
end
assert_dom_equal expected, _erbout, 'Block vs Proc generation differs'
end

def test_empty_layout
_erbout = ''
@view.tableize :empty_layout, [], :cols => 2 do |i|
@view.tableize [], :cols => 2, :name => :empty_layout do |i|
_erbout.concat 'nowhere'
end
root = HTML::Document.new(_erbout).root
Expand All @@ -67,7 +67,7 @@ def test_empty_layout

def test_1_item_layout
_erbout = ''
@view.tableize '1_item_layout', [1] do |i|
@view.tableize [1], :name => '1_item_layout' do |i|
_erbout.concat i.to_s
end
root = HTML::Document.new(_erbout).root
Expand All @@ -85,7 +85,7 @@ def test_1_item_layout

def test_row_less_1_item_layout
_erbout = ''
@view.tableize 'row_less_1_item_layout', %w{1 2 3 4 5}, :cols => 6 do |i|
@view.tableize %w{1 2 3 4 5}, :cols => 6, :name => 'row_less_1_item_layout' do |i|
_erbout.concat i.to_s
end
root = HTML::Document.new(_erbout).root
Expand All @@ -100,12 +100,13 @@ def test_row_less_1_item_layout
assert_select 'td:nth-of-type(5)', '5'
assert_select 'td.blank:last-of-type', '&nbsp;'
end
end end
end
end
end

def test_full_row_layout
_erbout = ''
@view.tableize :full_row_layout, %w{1 2 3 4 5}, :cols => 5 do |i|
@view.tableize %w{1 2 3 4 5}, :cols => 5, :name => :full_row_layout do |i|
_erbout.concat i.to_s
end
root = HTML::Document.new(_erbout).root
Expand All @@ -125,7 +126,7 @@ def test_full_row_layout

def test_row_plus_1_item_layout
_erbout = ''
@view.tableize 'row_plus_1_item_layout', %w{1 2 3 4 5}, :cols => 4 do |i|
@view.tableize %w{1 2 3 4 5}, :cols => 4, :name=> 'row_plus_1_item_layout' do |i|
_erbout.concat i.to_s
end
root = HTML::Document.new(_erbout).root
Expand All @@ -150,7 +151,7 @@ def test_row_plus_1_item_layout

def test_options
_erbout = ''
@view.tableize nil, nil,
@view.tableize nil,
:name => :options,
:collection => %w{1 2 3 4 5},
:generate_css => true,
Expand Down
37 changes: 37 additions & 0 deletions test/tooltip_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require File.dirname(__FILE__) + '/test_helper'

class TooltipHelperTest < Test::Unit::TestCase
attr_accessor :params
include ActionView::Helpers::TagHelper
include ActionView::Helpers::TextHelper
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::CaptureHelper
include Widgets::TooltipHelper

def setup
@params = {}
end

def test_presence_of_instance_methods
%w{tooltip}.each do |instance_method|
assert respond_to?(instance_method), "#{instance_method} is not defined after including the helper"
end
end

def test_tooltip_link_function
expected = "$('one_tooltip_link').observe('click', function(event){toggleTooltip(event, $('one_tooltip'))});"
assert_equal expected.strip, tooltip_link_function(:one);

expected = "$('two_tooltip_link').observe('click', function(event){toggleTooltip(event, $('two_tooltip'))});"
assert_equal expected.strip, tooltip_link_function(:two);
end

def test_close_tooltip_link
expected = "<a href=\"#\" onclick=\"$('first_tooltip').hide(); return false;\">close</a>"
assert_equal expected.strip, close_tooltip_link(:first);

expected = "<a href=\"#\" onclick=\"$('second_tooltip').hide(); return false;\">chiudi</a>"
assert_equal expected.strip, close_tooltip_link(:second, 'chiudi');
end

end

0 comments on commit 5cd9931

Please sign in to comment.