# 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: NewEmptyRowHandler.pm,v 2.2 2010/10/23 14:49:57 moeller Exp $ package Bivio::UI::HTML::Widget::NewEmptyRowHandler; use strict; use Bivio::Base 'UI.Widget'; our($VERSION) = sprintf('%d.%02d', q$Revision: 2.2 $ =~ /\d+/g); my($_JS) = b_use('HTMLWidget.JavaScript'); sub get_html_field_attributes { my($self, $field_name, $source) = @_; return ' onfocus="ner_focus(this, \'' . $source->req('form_model') ->get_field_name_for_html('empty_row_count') . '\')"'; } sub render { my($self, $source, $buffer) = @_; $_JS->render($source, $buffer, $self->package_name, <<'EOF'); function ner_match_name(n) { return new RegExp("(f\\d+\\_)(\\d+)$").exec(n); } function ner_next_name(n) { var match = ner_match_name(n); return match ? (match[1] + (parseInt(match[2]) + 1)) : null; } function ner_rename_children(c) { if (c.name) c.name = ner_next_name(c.name); else { for (var i = 0; i < c.childNodes.length; i++) ner_rename_children(c.childNodes[i]); } return c; } function ner_add_hidden(c) { var count = parseInt(ner_match_name(c.name)[2]); for (var i = 0; i < c.form.childNodes.length; i++) { var child = c.form.childNodes[i]; if (child.type && child.type == "hidden") { var match = ner_match_name(child.name); if (match && parseInt(match[2]) == count) c.form.appendChild(ner_rename_children(child.cloneNode(true))); } } } function ner_focus(c, empty_row_count) { var n = ner_next_name(c.name); if (! n || document.getElementsByName(n).length) return; var tr = c.parentNode.parentNode; tr.parentNode.appendChild(ner_rename_children(tr.cloneNode(true))); var f = document.getElementsByName(empty_row_count)[0]; f.value = parseInt(f.value) + 1; ner_add_hidden(c); } EOF return; } 1;