Permalink
Browse files

delegate, position related and repor-js bugfixes

  • Loading branch information...
1 parent c3b885e commit 1f8ffe3f8346bf8e0b0ba4ad66e165d6f9a260d0 kangax committed Jun 20, 2008
Showing with 65 additions and 15 deletions.
  1. +8 −0 README.rdoc
  2. +48 −0 element.methods.js
  3. +9 −15 experimental/report.js
View
@@ -12,6 +12,14 @@ Protolicious is a set of javascript snippets based on prototype.js
* <tt>Element#contains</tt>
* <tt>Element#indexOf</tt>
* <tt>Element#isTagName</tt>
+* <tt>Element#getContentWidth</tt>
+* <tt>Element#getContentHeight</tt>
+* <tt>Element#setWidth</tt>
+* <tt>Element#setHeight</tt>
+* <tt>Element#appearVisible</tt>
+* <tt>Element#delegate</tt>
+* <tt>Element#fillDocument</tt>
+* <tt>Element#centerInViewport</tt>
== Function extensions
View
@@ -307,4 +307,52 @@ Element.Methods.appearVisible = function(element, options) {
.morph('opacity:1', options);
};
+/**
+ * Element.delegate(@element, eventName, selector, handler) -> element
+ * Invokes handler when specified element receives an event from one of its siblings (via bubbling)
+ * A cornerstone of "event delegation"
+ *
+ * document.delegate('click', 'a.foo', function(e) {
+ * e.stop();
+ * // original even is passed through
+ * // handler is called in a context of an element which was first to match the selector
+ * // ("a.foo" in this case)
+ * })
+ *
+ **/
+Element.Methods.delegate = function(element, eventName, selector, handler) {
+ if (Object.isElement(selector)) {
+ return Event.observe(element, eventName, function(e) {
+ if (e.target == selector || e.target.descendantOf(selector))
+ handler.call(selector, e);
+ })
+ }
+ else {
+ return Event.observe(element, eventName, function(e, element) {
+ if (!(element = e.findElement(selector))) return;
+ handler.call(e.target, e);
+ })
+ }
+};
+document.delegate = Element.Methods.delegate.curry(document);
+
+Element.Methods.fillDocument = function(element) {
+ element = $(element);
+ var vpDim = document.viewport.getDimensions();
+ var docDim = $(document.documentElement).getDimensions();
+ element.style.width = Math.max(docDim.width, vpDim.width) + 'px';
+ element.style.height = Math.max(docDim.height, vpDim.height) + 'px';
+ return element;
+};
+
+Element.Methods.centerInViewport = function(element) {
+ element = $(element);
+ var vpDim = document.viewport.getDimensions();
+ var offsets = document.viewport.getScrollOffsets();
+ var elDim = Element.getDimensions(element);
+ element.style.left = (((vpDim.width - elDim.width) / 2) + offsets.left) + 'px';
+ element.style.top = (((vpDim.height - elDim.height) / 2) + offsets.top) + 'px';
+ return element;
+};
+
Element.addMethods();
View
@@ -19,23 +19,19 @@ function report() {
var results = 0, c = Event.cache;
for (var element in c) {
for (var handler in c[element]) {
- for (var eventName in c[element]) {
- if (eventName == 'element') continue;
- results += c[element][eventName].length;
- }
+ if (handler == 'element') continue;
+ results += c[element][handler].length;
}
}
// find non-unique ids
- var ids = $$('*').pluck('id').without('');
+ var withId = $$('[id]'), ids = withId.pluck('id');
var nonUnique = [];
- ids.each(function(id) {
- if (!isUnique(ids, id))
- (nonUnique.indexOf(id) == -1) && nonUnique.push(id);
+ ids.each(function(id, i) {
+ !isUnique(ids, id) && nonUnique.push(withId[i]);
})
// find collapsing names/ids (IE)
- var withName = $$('*[name]');
- var withId = $$('*[id]');
+ var withName = $$('[name]');
var collapsing = [];
withId.each(function(el) {
var collapsed = withName.find(function(_el){ return (_el.name == el.id) && _el != el })
@@ -44,9 +40,7 @@ function report() {
}
});
- // return result
- return ['Event handlers: ', results,
- '\nNon-Unique ID\'s: ', (nonUnique.length ? nonUnique : 'none'),
- '\nCollapsing names/ID\'s: ', (collapsing.length ? collapsing : 'none')
- ].join('');
+ console.log('Event handlers:\n',
+ results, '\n\nNon-Unique ID\'s:\n',
+ nonUnique, '\n\nCollapsing names/ID\'s:\n', collapsing);
}

0 comments on commit 1f8ffe3

Please sign in to comment.