# Copyright (c) 2010 bivio Software, Inc. All Rights Reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: DropDown.pm,v 1.13 2010/12/08 21:54:35 schellj Exp $ package Bivio::UI::XHTML::Widget::DropDown; use strict; use Bivio::Base 'Widget.Join'; use Bivio::UI::ViewLanguageAUTOLOAD; our($VERSION) = sprintf('%d.%02d', q$Revision: 1.13 $ =~ /\d+/g); sub initialize { my($self) = @_; my($w) = $self->get_nested('widget'); $self->put_unless_exists(link_class => 'dd_link'); my($id) = $w->get_if_exists_else_put( id => sub {JavaScript()->unique_html_id}); $self->die($id, undef, 'widget.id is not JS identifier') unless $id =~ /^[a-z]\w+$/s; my($local) = JavaScript()->var_name("drop_down_$id"); $self->put_unless_exists(values => [ Script('common'), $self->get('widget'), [sub {_js(@_)}, JavaScript()->var_name('drop_down'), $local, $id], _link($self, $local), ]); return shift->SUPER::initialize(@_); } sub internal_new_args { my(undef, $label, $widget, $attributes) = @_; return { label => $label, widget => $widget, ($attributes ? %$attributes : ()), }; } sub _js { my($source, $global, $local, $id) = @_; my($b) = ''; my($js) = JavaScript(); $js->render( $source, \$b, __PACKAGE__, $js->strip(<<"EOF"), (function (){ $global = $global || {}; var dd = $global; dd.toggle = function (e, stop_prop, b2) { var visible = b_all_elements_by_class('div', 'dd_visible'); for (var i = 0; i < visible.length; i++) { if (!b2 || visible[i] != b2.element) { b_toggle_class(visible[i], 'dd_visible', 'dd_hidden'); } } if (stop_prop) { if (!e) var e = window.event; e.cancelBubble = true; if (e.stopPropagation) e.stopPropagation(); } if (!b2) return; b_toggle_class(b2.element, 'dd_visible', 'dd_hidden'); }; var ocf = document.onclick; document.onclick = function(e) { if (ocf) ocf(e); dd.toggle(e, false, null); }; })(); EOF JavaScript()->strip(<<"EOF"), (function (){ $local = $local || {}; var b = $local; b.toggle = function (e) { b.element = b.element || document.getElementById('$id'); $global.toggle(e, true, $local); }; })(); EOF ); return $b; } sub _link { my($self, $local) = @_; return A( Join([ $self->get('label'), SPAN_dd_arrow(vs_text_as_prose('drop_down_arrow')), ]), { HREF => '#', ONCLICK => "this.blur(); $local.toggle(event); return false", class => $self->get('link_class'), }, ); } 1;