a zip code crypto-currency system good for red ONLY

infinite-scroll.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. (function (factory) {
  2. if (typeof module === "object" && typeof module.exports === "object") {
  3. var v = factory(require, exports);
  4. if (v !== undefined) module.exports = v;
  5. }
  6. else if (typeof define === "function" && define.amd) {
  7. define(["require", "exports", "@angular/core", "../content/content", "../../platform/dom-controller"], factory);
  8. }
  9. })(function (require, exports) {
  10. "use strict";
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. var core_1 = require("@angular/core");
  13. var content_1 = require("../content/content");
  14. var dom_controller_1 = require("../../platform/dom-controller");
  15. /**
  16. * @name InfiniteScroll
  17. * @description
  18. * The Infinite Scroll allows you to perform an action when the user
  19. * scrolls a specified distance from the bottom or top of the page.
  20. *
  21. * The expression assigned to the `infinite` event is called when
  22. * the user scrolls to the specified distance. When this expression
  23. * has finished its tasks, it should call the `complete()` method
  24. * on the infinite scroll instance.
  25. *
  26. * @usage
  27. * ```html
  28. * <ion-content>
  29. *
  30. * <ion-list>
  31. * <ion-item *ngFor="let i of items">{% raw %}{{i}}{% endraw %}</ion-item>
  32. * </ion-list>
  33. *
  34. * <ion-infinite-scroll (ionInfinite)="doInfinite($event)">
  35. * <ion-infinite-scroll-content></ion-infinite-scroll-content>
  36. * </ion-infinite-scroll>
  37. *
  38. * </ion-content>
  39. * ```
  40. *
  41. * ```ts
  42. * @Component({...})
  43. * export class NewsFeedPage {
  44. * items = [];
  45. *
  46. * constructor() {
  47. * for (let i = 0; i < 30; i++) {
  48. * this.items.push( this.items.length );
  49. * }
  50. * }
  51. *
  52. * doInfinite(infiniteScroll) {
  53. * console.log('Begin async operation');
  54. *
  55. * setTimeout(() => {
  56. * for (let i = 0; i < 30; i++) {
  57. * this.items.push( this.items.length );
  58. * }
  59. *
  60. * console.log('Async operation has ended');
  61. * infiniteScroll.complete();
  62. * }, 500);
  63. * }
  64. *
  65. * }
  66. * ```
  67. *
  68. * ## `waitFor` method of InfiniteScroll
  69. *
  70. * In case if your async operation returns promise you can utilize
  71. * `waitFor` method inside your template.
  72. *
  73. * ```html
  74. * <ion-content>
  75. *
  76. * <ion-list>
  77. * <ion-item *ngFor="let item of items">{{item}}</ion-item>
  78. * </ion-list>
  79. *
  80. * <ion-infinite-scroll (ionInfinite)="$event.waitFor(doInfinite())">
  81. * <ion-infinite-scroll-content></ion-infinite-scroll-content>
  82. * </ion-infinite-scroll>
  83. *
  84. * </ion-content>
  85. * ```
  86. *
  87. * ```ts
  88. * @Component({...})
  89. * export class NewsFeedPage {
  90. * items = [];
  91. *
  92. * constructor() {
  93. * for (var i = 0; i < 30; i++) {
  94. * this.items.push( this.items.length );
  95. * }
  96. * }
  97. *
  98. * doInfinite(): Promise<any> {
  99. * console.log('Begin async operation');
  100. *
  101. * return new Promise((resolve) => {
  102. * setTimeout(() => {
  103. * for (var i = 0; i < 30; i++) {
  104. * this.items.push( this.items.length );
  105. * }
  106. *
  107. * console.log('Async operation has ended');
  108. * resolve();
  109. * }, 500);
  110. * })
  111. * }
  112. * }
  113. * ```
  114. *
  115. * ## Infinite Scroll Content
  116. *
  117. * By default, Ionic uses the infinite scroll spinner that looks
  118. * best for the platform the user is on. However, you can change the
  119. * default spinner or add text by adding properties to the
  120. * `ion-infinite-scroll-content` component.
  121. *
  122. * ```html
  123. * <ion-content>
  124. *
  125. * <ion-infinite-scroll (ionInfinite)="doInfinite($event)">
  126. * <ion-infinite-scroll-content
  127. * loadingSpinner="bubbles"
  128. * loadingText="Loading more data...">
  129. * </ion-infinite-scroll-content>
  130. * </ion-infinite-scroll>
  131. *
  132. * </ion-content>
  133. * ```
  134. *
  135. *
  136. * ## Further Customizing Infinite Scroll Content
  137. *
  138. * The `ion-infinite-scroll` component holds the infinite scroll logic.
  139. * It requires a child component in order to display the content.
  140. * Ionic uses `ion-infinite-scroll-content` by default. This component
  141. * displays the infinite scroll and changes the look depending
  142. * on the infinite scroll's state. Separating these components allows
  143. * developers to create their own infinite scroll content components.
  144. * You could replace our default content with custom SVG or CSS animations.
  145. *
  146. * @demo /docs/demos/src/infinite-scroll/
  147. *
  148. */
  149. var InfiniteScroll = (function () {
  150. function InfiniteScroll(_content, _zone, _elementRef, _dom) {
  151. this._content = _content;
  152. this._zone = _zone;
  153. this._elementRef = _elementRef;
  154. this._dom = _dom;
  155. this._lastCheck = 0;
  156. this._highestY = 0;
  157. this._thr = '15%';
  158. this._thrPx = 0;
  159. this._thrPc = 0.15;
  160. this._position = POSITION_BOTTOM;
  161. this._init = false;
  162. /**
  163. * @internal
  164. */
  165. this.state = STATE_ENABLED;
  166. /**
  167. * @output {event} Emitted when the scroll reaches
  168. * the threshold distance. From within your infinite handler,
  169. * you must call the infinite scroll's `complete()` method when
  170. * your async operation has completed.
  171. */
  172. this.ionInfinite = new core_1.EventEmitter();
  173. _content.setElementClass('has-infinite-scroll', true);
  174. }
  175. Object.defineProperty(InfiniteScroll.prototype, "threshold", {
  176. /**
  177. * @input {string} The threshold distance from the bottom
  178. * of the content to call the `infinite` output event when scrolled.
  179. * The threshold value can be either a percent, or
  180. * in pixels. For example, use the value of `10%` for the `infinite`
  181. * output event to get called when the user has scrolled 10%
  182. * from the bottom of the page. Use the value `100px` when the
  183. * scroll is within 100 pixels from the bottom of the page.
  184. * Default is `15%`.
  185. */
  186. get: function () {
  187. return this._thr;
  188. },
  189. set: function (val) {
  190. this._thr = val;
  191. if (val.indexOf('%') > -1) {
  192. this._thrPx = 0;
  193. this._thrPc = (parseFloat(val) / 100);
  194. }
  195. else {
  196. this._thrPx = parseFloat(val);
  197. this._thrPc = 0;
  198. }
  199. },
  200. enumerable: true,
  201. configurable: true
  202. });
  203. Object.defineProperty(InfiniteScroll.prototype, "enabled", {
  204. /**
  205. * @input {boolean} If true, Whether or not the infinite scroll should be
  206. * enabled or not. Setting to `false` will remove scroll event listeners
  207. * and hide the display.
  208. */
  209. set: function (shouldEnable) {
  210. this.enable(shouldEnable);
  211. },
  212. enumerable: true,
  213. configurable: true
  214. });
  215. Object.defineProperty(InfiniteScroll.prototype, "position", {
  216. /**
  217. * @input {string} The position of the infinite scroll element.
  218. * The value can be either `top` or `bottom`.
  219. * Default is `bottom`.
  220. */
  221. get: function () {
  222. return this._position;
  223. },
  224. set: function (val) {
  225. if (val === POSITION_TOP || val === POSITION_BOTTOM) {
  226. this._position = val;
  227. }
  228. else {
  229. console.error("Invalid value for ion-infinite-scroll's position input. Its value should be '" + POSITION_BOTTOM + "' or '" + POSITION_TOP + "'.");
  230. }
  231. },
  232. enumerable: true,
  233. configurable: true
  234. });
  235. InfiniteScroll.prototype._onScroll = function (ev) {
  236. var _this = this;
  237. if (this.state === STATE_LOADING || this.state === STATE_DISABLED) {
  238. return 1;
  239. }
  240. if (this._lastCheck + 32 > ev.timeStamp) {
  241. // no need to check less than every XXms
  242. return 2;
  243. }
  244. this._lastCheck = ev.timeStamp;
  245. // ******** DOM READ ****************
  246. var infiniteHeight = this._elementRef.nativeElement.scrollHeight;
  247. if (!infiniteHeight) {
  248. // if there is no height of this element then do nothing
  249. return 3;
  250. }
  251. // ******** DOM READ ****************
  252. var d = this._content.getContentDimensions();
  253. var height = d.contentHeight;
  254. var threshold = this._thrPc ? (height * this._thrPc) : this._thrPx;
  255. // ******** DOM READS ABOVE / DOM WRITES BELOW ****************
  256. var distanceFromInfinite;
  257. if (this._position === POSITION_BOTTOM) {
  258. distanceFromInfinite = d.scrollHeight - infiniteHeight - d.scrollTop - height - threshold;
  259. }
  260. else {
  261. (void 0) /* assert */;
  262. distanceFromInfinite = d.scrollTop - infiniteHeight - threshold;
  263. }
  264. if (distanceFromInfinite < 0) {
  265. // ******** DOM WRITE ****************
  266. this._dom.write(function () {
  267. _this._zone.run(function () {
  268. if (_this.state !== STATE_LOADING && _this.state !== STATE_DISABLED) {
  269. _this.state = STATE_LOADING;
  270. _this.ionInfinite.emit(_this);
  271. }
  272. });
  273. });
  274. return 5;
  275. }
  276. return 6;
  277. };
  278. /**
  279. * Call `complete()` within the `infinite` output event handler when
  280. * your async operation has completed. For example, the `loading`
  281. * state is while the app is performing an asynchronous operation,
  282. * such as receiving more data from an AJAX request to add more items
  283. * to a data list. Once the data has been received and UI updated, you
  284. * then call this method to signify that the loading has completed.
  285. * This method will change the infinite scroll's state from `loading`
  286. * to `enabled`.
  287. */
  288. InfiniteScroll.prototype.complete = function () {
  289. var _this = this;
  290. if (this.state !== STATE_LOADING) {
  291. return;
  292. }
  293. if (this._position === POSITION_BOTTOM) {
  294. this.state = STATE_ENABLED;
  295. return;
  296. }
  297. (void 0) /* assert */;
  298. /* New content is being added at the top, but the scrollTop position stays the same,
  299. which causes a scroll jump visually. This algorithm makes sure to prevent this.
  300. (Frame 1)
  301. complete() is called, but the UI hasn't had time to update yet.
  302. Save the current content dimensions.
  303. Wait for the next frame using _dom.read, so the UI will be updated.
  304. (Frame 2)
  305. Read the new content dimensions.
  306. Calculate the height difference and the new scroll position.
  307. Delay the scroll position change until other possible dom reads are done using _dom.write to be performant.
  308. (Still frame 2, if I'm correct)
  309. Change the scroll position (= visually maintain the scroll position).
  310. Change the state to re-enable the InfiniteScroll. This should be after changing the scroll position, or it could cause the InfiniteScroll to be triggered again immediately.
  311. (Frame 3)
  312. Done.
  313. */
  314. // ******** DOM READ ****************
  315. // Save the current content dimensions before the UI updates
  316. var prevDim = this._content.getContentDimensions();
  317. // ******** DOM READ ****************
  318. this._dom.read(function () {
  319. // UI has updated, save the new content dimensions
  320. var newDim = _this._content.getContentDimensions();
  321. // New content was added on top, so the scroll position should be changed immediately to prevent it from jumping around
  322. var newScrollTop = newDim.scrollHeight - (prevDim.scrollHeight - prevDim.scrollTop);
  323. // ******** DOM WRITE ****************
  324. _this._dom.write(function () {
  325. _this._content.scrollTop = newScrollTop;
  326. _this.state = STATE_ENABLED;
  327. });
  328. });
  329. };
  330. /**
  331. * Pass a promise inside `waitFor()` within the `infinite` output event handler in order to
  332. * change state of infiniteScroll to "complete"
  333. */
  334. InfiniteScroll.prototype.waitFor = function (action) {
  335. var enable = this.complete.bind(this);
  336. action.then(enable, enable);
  337. };
  338. /**
  339. * Call `enable(false)` to disable the infinite scroll from actively
  340. * trying to receive new data while scrolling. This method is useful
  341. * when it is known that there is no more data that can be added, and
  342. * the infinite scroll is no longer needed.
  343. * @param {boolean} shouldEnable If the infinite scroll should be
  344. * enabled or not. Setting to `false` will remove scroll event listeners
  345. * and hide the display.
  346. */
  347. InfiniteScroll.prototype.enable = function (shouldEnable) {
  348. this.state = (shouldEnable ? STATE_ENABLED : STATE_DISABLED);
  349. this._setListeners(shouldEnable);
  350. };
  351. /**
  352. * @hidden
  353. */
  354. InfiniteScroll.prototype._setListeners = function (shouldListen) {
  355. if (this._init) {
  356. if (shouldListen) {
  357. if (!this._scLsn) {
  358. this._scLsn = this._content.ionScroll.subscribe(this._onScroll.bind(this));
  359. }
  360. }
  361. else {
  362. this._scLsn && this._scLsn.unsubscribe();
  363. this._scLsn = null;
  364. }
  365. }
  366. };
  367. /**
  368. * @hidden
  369. */
  370. InfiniteScroll.prototype.ngAfterContentInit = function () {
  371. this._init = true;
  372. this._setListeners(this.state !== STATE_DISABLED);
  373. if (this._position === POSITION_TOP) {
  374. this._content.scrollDownOnLoad = true;
  375. }
  376. };
  377. /**
  378. * @hidden
  379. */
  380. InfiniteScroll.prototype.ngOnDestroy = function () {
  381. this._setListeners(false);
  382. };
  383. InfiniteScroll.decorators = [
  384. { type: core_1.Directive, args: [{
  385. selector: 'ion-infinite-scroll'
  386. },] },
  387. ];
  388. /** @nocollapse */
  389. InfiniteScroll.ctorParameters = function () { return [
  390. { type: content_1.Content, },
  391. { type: core_1.NgZone, },
  392. { type: core_1.ElementRef, },
  393. { type: dom_controller_1.DomController, },
  394. ]; };
  395. InfiniteScroll.propDecorators = {
  396. 'threshold': [{ type: core_1.Input },],
  397. 'enabled': [{ type: core_1.Input },],
  398. 'position': [{ type: core_1.Input },],
  399. 'ionInfinite': [{ type: core_1.Output },],
  400. };
  401. return InfiniteScroll;
  402. }());
  403. exports.InfiniteScroll = InfiniteScroll;
  404. var STATE_ENABLED = 'enabled';
  405. var STATE_DISABLED = 'disabled';
  406. var STATE_LOADING = 'loading';
  407. var POSITION_TOP = 'top';
  408. var POSITION_BOTTOM = 'bottom';
  409. });
  410. //# sourceMappingURL=infinite-scroll.js.map