Alien-GvaScript
view release on metacpan or search on metacpan
lib/Alien/GvaScript/ProtoExtensions.pod view on Meta::CPAN
Element.register(elem, 'input.active', 'mouseover', function(e) {
e._target.addClassName('hover');
});
Element.unregister(elem, 'input.active', 'mouseover');
Element.store(elem, key, value);
Element.retrieve(elem, key, default);
=head1 DESCRIPTION
Some extensions to the basic abstractions supplied by
prototype.js.
=head1 METHODS
=head2 Events delegation
Will be a part of prototype's core distribution starting version 1.7
as discussed here
[https://prototype.lighthouseapp.com/projects/8886/tickets/435-eventdelegate-and-elementmethodsdelegate]
=head2 Prototype.getJSON
Prototype.getJSON(url, callback)
based on: getJSON function by Juriy Zaytsev L<http://github.com/kangax/protolicious/tree/master/get_json.js>
A method to temporarily load JSON data (ideally hosted on a different domain and accessible via JSONP service) and send it over to callback method when ready.
=head2 Hash utilities
=head3 Hash.expand
var tree = Hash.expand(flat_hash);
Transforms a flat structure of key-value pairs into a nested tree
structure, by splitting keys on dots.
The idea and algorithm come from L<CGI::Expand/expand_hash>.
Hash.expand({a: '...', b.0.x: '...', b.0.y: '...', b.1.x: '...', b.1.y: '...'}) =>
{a: '...', b: [{x: '...', y: '...'}, {x: '...', y: '...'}]}
=head3 Hash.flatten
The exact opposite of Hash.expand
Transforms a nested tree structure into a flat hash of key-value pairs
where nested keys are joined by a dot.
=head2 String extensions
=head3 chomp
Strips any trailing line breaks (\r\n) from a String.
=head2 Element extensions
=head3 flash
only applied to ['SPAN', 'DIV', 'INPUT', 'BUTTON', 'TEXTAREA', 'A'] elements.
used to set a classname to the element for a brief moment of time.
Element.flash(elem, {duration: I<time_mes>, classname: I<classname_to_set>});
=head3 hasAnyClass
if (Element.hasAnyClass(elem, ["class1", "class2", ...]) {...}
if (Element.hasAnyClass(elem, "class1") {...}
Returns true if the supplied element has any of the given classes.
For convenience, also accepts a single string instead of an array
when testing for a single class.
=head3 getElementsByClassNames
var subElements
= Element.getElementsByClassNames(rootElement, ["class1", "class2", ...]);
Returns an array of children of C<rootElement> that have any
of the given class names.
=head3 navigateDom
var wantedClasses = ["class1", "class2", ...];
// which direction to navigate (could be "parentNode", etc.)
var direction = "nextSibling";
// some criteria for stopping navigation (can be anything, here a stupid
// example)
var stopCondition = function(elem) {return elem.innerHTML.length > 10}
var nextElement
= Element.navigateDom(startElement, direction, wantedClasses,
stopCondition);
Returns an extended (prototype's C<Element.extend()>) copy of HTMLElement.
Walks through the DOM in the given direction, until finding an
element that has one of the given classnames, or finding a stop
condition (in which case C<null> is returned).
=head3 autoScroll
Element.autoScroll(elem, percentage)
Makes sure that C<elem> is visible in the central area of
its offset parent; if not, the parent is scrolled.
C<percentage> is the ratio between the parent height and the
margin at which scrolling must occur, i.e. if
C<percentage = 20> (the default), then scrolling
occurs if the element is in the higher than the top 20% or
lower than the bottom 20% of the viewport.
=head3 outerHTML
( run in 1.090 second using v1.01-cache-2.11-cpan-0bd6704ced7 )