Skip to content

Commit 14e7943

Browse files
Add documentation for Element.Layout and Event.on.
1 parent 23a1444 commit 14e7943

File tree

2 files changed

+126
-4
lines changed

2 files changed

+126
-4
lines changed

src/dom/event.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,7 @@
937937
* avoids adding an observer to a number of individual elements and instead
938938
* listens on a _common ancestor_ element.
939939
*
940+
* For more information on usage, see [[Event.on]].
940941
**/
941942
Event.Handler = Class.create({
942943
/**
@@ -992,14 +993,69 @@
992993
});
993994

994995
/**
995-
* Event.on(element, eventName, selector, callback) -> Event.Handler
996+
* Event.on(element, eventName[, selector], callback) -> Event.Handler
997+
* - element (Element | String): The DOM element to observe, or its ID.
998+
* - eventName (String): The name of the event, in all lower case, without
999+
* the "on" prefix — e.g., "click" (not "onclick").
1000+
* - selector (String): A CSS selector. If specified, will call `callback`
1001+
* _only_ when it can find an element that matches `selector` somewhere
1002+
* in the ancestor chain between the event's target element and the
1003+
* given `element`.
1004+
* - callback (Function): The event handler function. Should expect two
1005+
* arguments: the event object _and_ the element that received the
1006+
* event. (If `selector` was given, this element will be the one that
1007+
* satisfies the criteria described just above; if not, it will be the
1008+
* one specified in the `element` argument). This function is **always**
1009+
* bound to `element`.
9961010
*
9971011
* Listens for events on an element's descendants, optionally filtering
9981012
* to match a given CSS selector.
9991013
*
10001014
* Creates an instance of [[Event.Handler]], calls [[Event.Handler#start]],
10011015
* then returns that instance. Keep a reference to this returned instance if
10021016
* you later want to unregister the observer.
1017+
*
1018+
* ##### Usage
1019+
*
1020+
* `Event.on` can be used to set up event handlers with or without event
1021+
* delegation. In its simplest form, it works just like [[Event.observe]]:
1022+
*
1023+
* $("messages").on("click", function(event) {
1024+
* // ...
1025+
* });
1026+
*
1027+
* An optional second argument lets you specify a CSS selector for event
1028+
* delegation. This encapsulates the pattern of using [[Event#findElement]]
1029+
* to retrieve the first ancestor element matching a specific selector.
1030+
*
1031+
* $("messages").on("click", "a.comment", function(event, element) {
1032+
* // ...
1033+
* });
1034+
*
1035+
* Note the second argument in the handler above: it references the
1036+
* element matched by the selector (in this case, an `a` tag with a class
1037+
* of `comment`). This argument is important to use because within the
1038+
* callback, the `this` keyword **will always refer to the original
1039+
* element** (in this case, the element with the id of `messages`).
1040+
*
1041+
* `Event.on` differs from `Event.observe` in one other important way:
1042+
* its return value is an instance of [[Event.Handler]]. This instance
1043+
* has a `stop` method that will remove the event handler when invoked
1044+
* (and a `start` method that will attach the event handler again after
1045+
* it's been removed).
1046+
*
1047+
* // Register the handler:
1048+
* var handler = $("messages").on("click", "a.comment",
1049+
* this.click.bind(this));
1050+
*
1051+
* // Unregister the handler:
1052+
* handler.stop();
1053+
*
1054+
* // Re-register the handler:
1055+
* handler.start();
1056+
*
1057+
* This means that, unlike `Event.stopObserving`, there's no need to
1058+
* retain a reference to the handler function.
10031059
**/
10041060
function on(element, eventName, selector, callback) {
10051061
element = $(element);
@@ -1146,6 +1202,11 @@
11461202
**/
11471203
stopObserving: stopObserving.methodize(),
11481204

1205+
/**
1206+
* Element.on(@element, eventName[, selector], callback) -> Event.Handler
1207+
*
1208+
* See [[Event.on]].
1209+
**/
11491210
on: on.methodize(),
11501211

11511212
/**

src/dom/layout.js

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,10 @@
224224
**/
225225
Element.Layout = Class.create(Hash, {
226226
/**
227-
* new Element.Layout(element[, preCompute])
227+
* new Element.Layout(element[, preCompute = false])
228228
* - element (Element): The element to be measured.
229-
* - preCompute (Boolean): Whether to compute all values at once.
229+
* - preCompute (Boolean): Whether to compute all values at once. Default
230+
* is `false`.
230231
*
231232
* Declare a new layout hash.
232233
*
@@ -273,6 +274,11 @@
273274
*
274275
* Retrieve the measurement specified by `property`. Will throw an error
275276
* if the property is invalid.
277+
*
278+
* ##### Caveats
279+
*
280+
* * `Element.Layout` can measure the dimensions of an element hidden with
281+
* CSS (`display: none`), but _only_ if its parent element is visible.
276282
**/
277283
get: function($super, property) {
278284
// Try to fetch from the cache.
@@ -386,6 +392,10 @@
386392
*
387393
* Keys can be passed into this method as individual arguments _or_
388394
* separated by spaces within a string.
395+
*
396+
* // Equivalent statements:
397+
* someLayout.toObject('top', 'bottom', 'left', 'right');
398+
* someLayout.toObject('top bottom left right');
389399
**/
390400
toObject: function() {
391401
var args = $A(arguments);
@@ -410,6 +420,10 @@
410420
*
411421
* Keys can be passed into this method as individual arguments _or_
412422
* separated by spaces within a string.
423+
*
424+
* // Equivalent statements:
425+
* someLayout.toHash('top', 'bottom', 'left', 'right');
426+
* someLayout.toHash('top bottom left right');
413427
**/
414428
toHash: function() {
415429
var obj = this.toObject.apply(this, arguments);
@@ -712,6 +726,8 @@
712726

713727
/**
714728
* Element.Offset#inspect() -> String
729+
*
730+
* Returns a debug-friendly representation of the offset.
715731
**/
716732
inspect: function() {
717733
return "#<Element.Offset left: #{left} top: #{top}>".interpolate(this);
@@ -726,14 +742,19 @@
726742

727743
/**
728744
* Element.Offset#toArray() -> Array
745+
*
746+
* Returns an array representation fo the offset in [x, y] format.
729747
**/
730748
toArray: function() {
731749
return [this.left, this.top];
732750
}
733751
});
734752

735753
/**
736-
* Element.getLayout(@element, preCompute) -> Element.Layout
754+
* Element.getLayout(@element[, preCompute = false]) -> Element.Layout
755+
* - element (Element): The element to be measured.
756+
* - preCompute (Boolean): Whether to compute all values at once. Default
757+
* is `false`.
737758
*
738759
* Returns an instance of [[Element.Layout]] for measuring an element's
739760
* dimensions.
@@ -744,6 +765,35 @@
744765
* `Element.getLayout` whenever you need a measurement. You should call
745766
* `Element.getLayout` again only when the values in an existing
746767
* `Element.Layout` object have become outdated.
768+
*
769+
* Remember that instances of `Element.Layout` compute values the first
770+
* time they're asked for and remember those values for later retrieval.
771+
* If you want to compute all an element's measurements at once, pass
772+
*
773+
* ##### Examples
774+
*
775+
* var layout = $('troz').getLayout();
776+
*
777+
* layout.get('width'); //-> 150
778+
* layout.get('height'); //-> 500
779+
* layout.get('padding-left'); //-> 10
780+
* layout.get('margin-left'); //-> 25
781+
* layout.get('border-top'); //-> 5
782+
* layout.get('border-bottom'); //-> 5
783+
*
784+
* // Won't re-compute width; remembers value from first time.
785+
* layout.get('width'); //-> 150
786+
*
787+
* // Composite values obtained by adding together other properties;
788+
* // will re-use any values we've already looked up above.
789+
* layout.get('padding-box-width'); //-> 170
790+
* layout.get('border-box-height'); //-> 510
791+
*
792+
* ##### Caveats
793+
*
794+
* * Instances of `Element.Layout` can measure the dimensions of an
795+
* element hidden with CSS (`display: none`), but _only_ if its parent
796+
* element is visible.
747797
**/
748798
function getLayout(element, preCompute) {
749799
return new Element.Layout(element, preCompute);
@@ -759,6 +809,17 @@
759809
* calling this method frequently over short spans of code, you might want
760810
* to call [[Element.getLayout]] and operate on the [[Element.Layout]]
761811
* object itself (thereby taking advantage of measurement caching).
812+
*
813+
* ##### Examples
814+
*
815+
* $('troz').measure('width'); //-> 150
816+
* $('troz').measure('border-top'); //-> 5
817+
* $('troz').measure('top'); //-> 226
818+
*
819+
* ##### Caveats
820+
*
821+
* * `Element.measure` can measure the dimensions of an element hidden with
822+
* CSS (`display: none`), but _only_ if its parent element is visible.
762823
**/
763824
function measure(element, property) {
764825
return $(element).getLayout().get(property);

0 commit comments

Comments
 (0)