Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 6b3afbb

Browse files
Splaktarmmalerba
authored andcommitted
docs(virtualRepeat): clean up and clarify (#11287)
- fix typos - fix invalid types - remove out of date TODO - fix invalid comments - add missing JSDoc - add link to CSP Closes #11280.
1 parent 371bb5c commit 6b3afbb

File tree

2 files changed

+71
-58
lines changed

2 files changed

+71
-58
lines changed

src/components/autocomplete/js/autocompleteDirective.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,24 @@ angular
4545
*
4646
* **Autocomplete Dropdown Items Rendering**
4747
*
48-
* The `md-autocomplete` uses the the <a ng-href="api/directive/mdVirtualRepeatContainer">VirtualRepeat</a>
49-
* directive for displaying the results inside of the dropdown.<br/>
48+
* The `md-autocomplete` uses the the <a ng-href="api/directive/mdVirtualRepeat">
49+
* mdVirtualRepeat</a> directive for displaying the results inside of the dropdown.<br/>
5050
*
5151
* > When encountering issues regarding the item template please take a look at the
5252
* <a ng-href="api/directive/mdVirtualRepeatContainer">VirtualRepeatContainer</a> documentation.
5353
*
5454
* **Autocomplete inside of a Virtual Repeat**
5555
*
5656
* When using the `md-autocomplete` directive inside of a
57-
* <a ng-href="api/directive/mdVirtualRepeatContainer">VirtualRepeatContainer</a> the dropdown items might
58-
* not update properly, because caching of the results is enabled by default.
57+
* <a ng-href="api/directive/mdVirtualRepeatContainer">VirtualRepeatContainer</a> the dropdown items
58+
* might not update properly, because caching of the results is enabled by default.
5959
*
60-
* The autocomplete will then show invalid dropdown items, because the VirtualRepeat only updates the
61-
* scope bindings, rather than re-creating the `md-autocomplete` and the previous cached results will be used.
60+
* The autocomplete will then show invalid dropdown items, because the Virtual Repeat only updates
61+
* the scope bindings rather than re-creating the `md-autocomplete`. This means that the previous
62+
* cached results will be used.
6263
*
63-
* > To avoid such problems ensure that the autocomplete does not cache any results.
64+
* > To avoid such problems, ensure that the autocomplete does not cache any results via
65+
* `md-no-cache="true"`:
6466
*
6567
* <hljs lang="html">
6668
* <md-autocomplete
@@ -106,11 +108,10 @@ angular
106108
* @param {string=} md-menu-class This class will be applied to the dropdown menu for styling.
107109
* @param {string=} md-menu-container-class This class will be applied to the parent container
108110
* of the dropdown panel.
109-
* @param {string=} md-input-class This will be applied to the input for styling. This attribute is only valid when a `md-floating-label` is defined
111+
* @param {string=} md-input-class This will be applied to the input for styling. This attribute
112+
* is only valid when a `md-floating-label` is defined.
110113
* @param {string=} md-floating-label This will add a floating label to autocomplete and wrap it in
111114
* `md-input-container`
112-
* @param {string=} md-input-name The name attribute given to the input element to be used with
113-
* FormController
114115
* @param {string=} md-select-on-focus When present the inputs text will be automatically selected
115116
* on focus.
116117
* @param {string=} md-input-id An ID to be added to the input element.

src/components/virtualRepeat/virtual-repeater.js

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,33 @@ angular.module('material.components.virtualRepeat', [
1717
* @module material.components.virtualRepeat
1818
* @restrict E
1919
* @description
20-
* `md-virtual-repeat-container` provides the scroll container for md-virtual-repeat.
20+
* `md-virtual-repeat-container` provides the scroll container for
21+
* <a ng-href="api/directive/mdVirtualRepeat">md-virtual-repeat</a>.
2122
*
22-
* VirtualRepeat is a limited substitute for ng-repeat that renders only
23-
* enough DOM nodes to fill the container and recycling them as the user scrolls.
23+
* VirtualRepeat is a limited substitute for `ng-repeat` that renders only
24+
* enough DOM nodes to fill the container, recycling them as the user scrolls.
2425
*
25-
* Once an element is not visible anymore, the VirtualRepeat recycles it and will reuse it for
26-
* another visible item by replacing the previous dataset with the new one.
26+
* Once an element is not visible anymore, the Virtual Repeat recycles the element and reuses it
27+
* for another visible item by replacing the previous data set with the set of currently visible
28+
* elements.
2729
*
2830
* ### Common Issues
2931
*
30-
* - When having one-time bindings inside of the view template, the VirtualRepeat will not properly
32+
* - When having one-time bindings inside of the view template, the Virtual Repeat will not properly
3133
* update the bindings for new items, since the view will be recycled.
32-
* - Directives inside of a VirtualRepeat will be only compiled (linked) once, because those
34+
* - Directives inside of a Virtual Repeat will be only compiled (linked) once, because those
3335
* items will be recycled and used for other items.
34-
* The VirtualRepeat just updates the scope bindings.
36+
* The Virtual Repeat just updates the scope bindings.
3537
*
3638
*
3739
* ### Notes
3840
*
3941
* > The VirtualRepeat is a similar implementation to the Android
40-
* [RecyclerView](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html)
42+
* [RecyclerView](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html).
4143
*
4244
* <!-- This comment forces a break between blockquotes //-->
4345
*
44-
* > Please also review the <a ng-href="api/directive/mdVirtualRepeat">VirtualRepeat</a>
46+
* > Please also review the <a ng-href="api/directive/mdVirtualRepeat">mdVirtualRepeat</a>
4547
* documentation for more information.
4648
*
4749
*
@@ -53,14 +55,14 @@ angular.module('material.components.virtualRepeat', [
5355
* </md-virtual-repeat-container>
5456
* </hljs>
5557
*
58+
* @param {boolean=} md-auto-shrink When present and the container will shrink to fit
59+
* the number of items in the `md-virtual-repeat`.
60+
* @param {number=} md-auto-shrink-min Minimum number of items that md-auto-shrink
61+
* will shrink to. Default: `0`.
62+
* @param {boolean=} md-orient-horizontal Whether the container should scroll horizontally.
63+
* The default is `false` which indicates vertical orientation and scrolling.
5664
* @param {number=} md-top-index Binds the index of the item that is at the top of the scroll
5765
* container to `$scope`. It can both read and set the scroll position.
58-
* @param {boolean=} md-orient-horizontal Whether the container should scroll horizontally
59-
* (defaults to orientation and scrolling vertically).
60-
* @param {boolean=} md-auto-shrink When present, the container will shrink to fit
61-
* the number of items when that number is less than its original size.
62-
* @param {number=} md-auto-shrink-min Minimum number of items that md-auto-shrink
63-
* will shrink to (default: 0).
6466
*/
6567
function VirtualRepeatContainerDirective() {
6668
return {
@@ -148,7 +150,7 @@ function VirtualRepeatContainerController($$rAF, $mdUtil, $mdConstant, $parse, $
148150
this.sizer = this.scroller.querySelector('.md-virtual-repeat-sizer');
149151
this.offsetter = this.scroller.querySelector('.md-virtual-repeat-offsetter');
150152

151-
// After the dom stablizes, measure the initial size of the container and
153+
// After the DOM stabilizes, measure the initial size of the container and
152154
// make a best effort at re-measuring as it changes.
153155
var boundUpdateSize = angular.bind(this, this.updateSize);
154156

@@ -418,18 +420,23 @@ VirtualRepeatContainerController.prototype.handleScroll_ = function() {
418420
* @restrict A
419421
* @priority 1000
420422
* @description
421-
* `md-virtual-repeat` specifies an element to repeat using virtual scrolling.
423+
* The `md-virtual-repeat` attribute is applied to a template that is repeated using virtual
424+
* scrolling. This provides smooth and performant scrolling through very large lists of elements.
425+
*
426+
* Virtual repeat is a limited substitute for `ng-repeat` that renders only
427+
* enough DOM nodes to fill the container, recycling them as the user scrolls.
422428
*
423-
* Virtual repeat is a limited substitute for ng-repeat that renders only
424-
* enough DOM nodes to fill the container and recycling them as the user scrolls.
429+
* ### Notes
425430
*
426-
* Arrays, but not objects are supported for iteration.
427-
* Track by, as alias, and (key, value) syntax are not supported.
431+
* - Arrays are supported for iteration by default.
432+
* - An object can used use if `md-on-demand` is specified and the object implements the interface
433+
* described in the `md-on-demand` <a ng-href="#attributes">documentation</a>.
434+
* - `trackBy`, `as` alias, and `(key, value)` syntax from `ng-repeat` are not supported.
428435
*
429436
* ### On-Demand Async Item Loading
430437
*
431-
* When using the `md-on-demand` attribute and loading some asynchronous data, the `getItemAtIndex` function will
432-
* mostly return nothing.
438+
* When using the `md-on-demand` attribute and loading some asynchronous data,
439+
* the `getItemAtIndex` function will mostly return nothing.
433440
*
434441
* <hljs lang="js">
435442
* DynamicItems.prototype.getItemAtIndex = function(index) {
@@ -442,10 +449,10 @@ VirtualRepeatContainerController.prototype.handleScroll_ = function() {
442449
* };
443450
* </hljs>
444451
*
445-
* This means that the VirtualRepeat will not have any value for the given index.<br/>
446-
* After the data loading completed, the user expects the VirtualRepeat to recognize the change.
452+
* This means that the Virtual Repeat will not have any value for the given index.<br/>
453+
* After the data loading completes, the user expects the Virtual Repeat to recognize the change.
447454
*
448-
* To make sure that the VirtualRepeat properly detects any change, you need to run the operation
455+
* To make sure that the Virtual Repeat properly detects any change, you need to run the operation
449456
* in another digest.
450457
*
451458
* <hljs lang="js">
@@ -474,19 +481,20 @@ VirtualRepeatContainerController.prototype.handleScroll_ = function() {
474481
* </md-virtual-repeat-container>
475482
* </hljs>
476483
*
477-
* @param {number=} md-item-size The height or width of the repeated elements (which must be
478-
* identical for each element). Optional. Will attempt to read the size from the dom if missing,
479-
* but still assumes that all repeated nodes have same height or width.
480-
* @param {string=} md-extra-name Evaluates to an additional name to which the current iterated item
484+
* @param {expression=} md-extra-name Evaluates to an additional name to which the current iterated item
481485
* can be assigned on the repeated scope (needed for use in `md-autocomplete`).
482-
* @param {boolean=} md-on-demand When present, treats the md-virtual-repeat argument as an object
486+
* @param {number=} md-item-size Optional height or width of the repeated elements (which **must be
487+
* identical for each element**). Virtual repeat will attempt to read the size from the DOM,
488+
* if missing, but it still assumes that all repeated nodes have the **same height**
489+
* (when scrolling vertically) or **same width** (when scrolling horizontally).
490+
* @param {boolean=} md-on-demand When present, treats the `md-virtual-repeat` argument as an object
483491
* that can fetch rows rather than an array.
484492
*
485-
* **NOTE:** This object must implement the following interface with two (2) methods:
493+
* **NOTE:** This object **must** implement the following interface with two methods:
486494
*
487-
* - `getItemAtIndex: function(index) [object]` The item at that index or null if it is not yet
488-
* loaded (it should start downloading the item in that case).
489-
* - `getLength: function() [number]` The data length to which the repeater container
495+
* - `getItemAtIndex` - `{function(index): Object}`: The item at that `index` or `null` if it is not
496+
* yet loaded (it should start downloading the item in that case).
497+
* - `getLength` - `{function(): number}`: The data length to which the repeater container
490498
* should be sized. Ideally, when the count is known, this method should return it.
491499
* Otherwise, return a higher number than the currently loaded items to produce an
492500
* infinite-scroll behavior.
@@ -540,8 +548,6 @@ function VirtualRepeatController($scope, $element, $attrs, $browser, $document,
540548
this.startIndex = 0;
541549
/** @type {number} Previous ending repeat index (based on scroll offset) */
542550
this.endIndex = 0;
543-
// TODO: measure width/height of first element from dom if not provided.
544-
// getComputedStyle?
545551
/** @type {?number} Height/width of repeated elements. */
546552
this.itemSize = $scope.$eval($attrs.mdItemSize) || null;
547553

@@ -565,7 +571,7 @@ function VirtualRepeatController($scope, $element, $attrs, $browser, $document,
565571

566572
/**
567573
* Presently rendered blocks by repeat index.
568-
* @type {Object<number, !VirtualRepeatController.Block}
574+
* @type {Object<number, !VirtualRepeatController.Block>}
569575
*/
570576
this.blocks = {};
571577
/** @type {Array<!VirtualRepeatController.Block>} A pool of presently unused blocks. */
@@ -661,7 +667,7 @@ VirtualRepeatController.prototype.repeatListExpression_ = function(scope) {
661667

662668

663669
/**
664-
* Called by the container. Informs us that the containers scroll or size has
670+
* Called by the container. Informs us that the container's scroll or size has
665671
* changed.
666672
*/
667673
VirtualRepeatController.prototype.containerUpdated = function() {
@@ -711,16 +717,16 @@ VirtualRepeatController.prototype.containerUpdated = function() {
711717

712718
/**
713719
* Called by the container. Returns the size of a single repeated item.
714-
* @return {?number} Size of a repeated item.
720+
* @return {?number} size of a repeated item.
715721
*/
716722
VirtualRepeatController.prototype.getItemSize = function() {
717723
return this.itemSize;
718724
};
719725

720726

721727
/**
722-
* Called by the container. Returns the size of a single repeated item.
723-
* @return {?number} Size of a repeated item.
728+
* Called by the container.
729+
* @return {?number} the most recently seen length of items.
724730
*/
725731
VirtualRepeatController.prototype.getItemCount = function() {
726732
return this.itemsLength;
@@ -729,7 +735,9 @@ VirtualRepeatController.prototype.getItemCount = function() {
729735

730736
/**
731737
* Updates the order and visible offset of repeated blocks in response to scrolling
732-
* or items updates.
738+
* or updates to `items`.
739+
* @param items {Array} visible elements
740+
* @param oldItems {Array} previously visible elements
733741
* @private
734742
*/
735743
VirtualRepeatController.prototype.virtualRepeatUpdate_ = function(items, oldItems) {
@@ -882,8 +890,8 @@ VirtualRepeatController.prototype.updateBlock_ = function(block, index) {
882890
this.updateScope_(block.scope, index);
883891

884892
// Perform digest before reattaching the block.
885-
// Any resulting synchronous dom mutations should be much faster as a result.
886-
// This might break some directives, but I'm going to try it for now.
893+
// Any resulting synchronous DOM mutations should be much faster as a result.
894+
// This might break some directives.
887895
if (!this.$rootScope.$$phase) {
888896
block.scope.$digest();
889897
}
@@ -997,8 +1005,12 @@ VirtualRepeatModelArrayLike.prototype.$$includeIndexes = function(start, end) {
9971005
* @restrict A
9981006
* @description
9991007
*
1000-
* Force an element to have a certain px height. This is used in place of a style tag in order to
1001-
* conform to the Content Security Policy regarding unsafe-inline style tags.
1008+
* Force an element to have a certain `px` height. This is used in place of a style tag in order to
1009+
* conform to the
1010+
* <a href="https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/script-src">
1011+
* Content Security Policy</a> regarding `unsafe-inline` `<style>` tags.
1012+
*
1013+
* This directive is related to <a ng-href="api/directive/mdVirtualRepeat">mdVirtualRepeat</a>.
10021014
*
10031015
* @usage
10041016
* <hljs lang="html">

0 commit comments

Comments
 (0)