|
1 | | -MegaList [](https://travis-ci.org/lpetrov/megalist) |
| 1 | +# MegaList [](https://travis-ci.org/lpetrov/megalist) |
| 2 | + |
| 3 | +## Examples |
| 4 | +Check out the [examples](https://github.com/lpetrov/megalist/tree/master/examples) directory. |
| 5 | + |
| 6 | +## Description |
| 7 | +MegaList is a very simple but powerful utility for rendering very large lists, without clutering the DOM with thousands |
| 8 | +of DOM nodes. |
| 9 | + |
| 10 | +Every dynamic web app, sooner or later would end up rendering tons of stuff in the DOM Tree, which makes its traversal, |
| 11 | +updating slow and very complicated/painful for developers. |
| 12 | + |
| 13 | +This is why we'd developed a very minimalistic utility that uses [perfect-scrollbar](https://github.com/noraesae/perfect-scrollbar) |
| 14 | +and adds the so called "virtual", "delayed" or "lazy" rendering. |
| 15 | +Basically, the MegaList would *only* render the DOM nodes which are currently in the scrollable viewport. E.g. if your |
| 16 | +app needs to render 10 000 of rows (we call those *items*), by using MegaList, out of the box, it would only render the |
| 17 | +needed/displayed rows while the user scrolls. Which depending on the height of the items and the container, may reduce |
| 18 | +the number of rendered rows from 10 000 to only 10-20-30. |
| 19 | + |
| 20 | +Mean while, in theory this is a really straight forward problem that can be solved manually with your own code, there |
| 21 | +are a lot of edge cases that can/would trigger a DOM repaint or reflow |
| 22 | +(more info: [What forces layout/reflow](https://gist.github.com/paulirish/5d52fb081b3570c81e3a)). This is why, we did |
| 23 | +researched all currently available scrolling libraries and picked [perfect-scrollbar](https://github.com/noraesae/perfect-scrollbar) |
| 24 | +and then simply added a layer of caching and clever math, so that the reflow/repaint/layout is kept at minimum. |
| 25 | + |
| 26 | +## Dependencies |
| 27 | +* [jQuery](https://jquery.com/) |
| 28 | +* [perfect-scrollbar](https://github.com/noraesae/perfect-scrollbar) |
| 29 | + |
| 30 | + |
| 31 | +## Supported browsers |
| 32 | +* IE 11+ |
| 33 | +* Chrome 50+ (may work on older versions) |
| 34 | +* Firefox 48+ (may work on older versions) |
| 35 | + |
| 36 | +## Setup |
| 37 | +1. Ensure jQuery is loaded on your page (at least v2.0). |
| 38 | +2. Include perfect-scrollbar (js and css files) in your html file. |
| 39 | +3. Include MegaList's megalist.js file |
| 40 | +4. Create a container div that contains your list (the containter should have a `position: relative`, `width` and |
| 41 | +`height` set): |
| 42 | +```<div id="exampleContainer"></div>``` |
| 43 | +5. Initialise your MegaList instance and pass `itemWidth` and `itemHeight` (all items in your list should have a static, |
| 44 | +predefined width and height!): |
| 45 | +``` |
| 46 | +var megaList = new MegaList($('#exampleContainer'), { |
| 47 | + itemWidth: 120, |
| 48 | + itemHeight: 120, |
| 49 | + itemRenderFunction: function(id) { |
| 50 | + return $('<div class="item highlight-' + (id % 2) + '">Item #' + (id + 1)+ '</div>'); |
| 51 | + } |
| 52 | +}); |
| 53 | +``` |
| 54 | + |
| 55 | + |
| 56 | +6. Pass a 'itemRenderFunction' option that returns a valid DOM Node or a HTML string. This function would be called |
| 57 | +every time when the MegaList needs to render a item from your list. To identify different nodes, the MegaList would pass |
| 58 | +an `id`, which is the same ID which is used when adding items in the MegaList. |
| 59 | + |
| 60 | +7. Since MegaList does not care on the actual content of your list, you need to only pass IDs of the items, by doing: |
| 61 | +``` |
| 62 | +megaList.batchAdd([1,2,3]); |
| 63 | +``` |
| 64 | + |
| 65 | +8. When you are ready, just call: ```megaList.initialRender();``` and the required DOM nodes would be rendered in |
| 66 | +`#exampleContainer` and Perfect Scrollbar initialised (no need to manually manage the initialisation of PS). |
| 67 | + |
| 68 | +## Features |
| 69 | +* **List view** |
| 70 | + MegaList does calculations and if it can fit only 1 item per row, it would position all items in your list as a list. |
| 71 | + |
| 72 | +* **Grid view** |
| 73 | + If the width of the container is at least x2 of the item width, MegaList would render your items in a grid. |
| 74 | + |
| 75 | +* **Resizeable container** |
| 76 | + In case your app allows the user to resize the actuall UI, MegaList allows you to trigger a resize on the list and |
| 77 | + visible items, by simply calling `megaList.resized();`. |
| 78 | + Note: When the window is resized, the .resized() method would be automatically called, so if your UI does a |
| 79 | + `$(window).trigger('resize')` you may not need to call that method at all. It would work automatically. |
| 80 | + |
| 81 | +* **Cleanup** |
| 82 | + `megaList.destroy()` when called, would clean up all dom nodes, event handlers, etc so that you can free up even more |
| 83 | + memory/DOM nodes if the UI related to the scrollable list is not currently visible/needed at the moment. |
| 84 | + |
| 85 | +* **Dynamic adding/removing of items** |
| 86 | + MegaList is meant to be dynamic, so you can add/remove "items" anytime you want/need to. And to make things even more |
| 87 | + faster/efficient, we are having a `megaList.batchAdd([list of string ids, ...])` and `megaList.batchRemove([ids, ...])` |
| 88 | + that would add all "items" in a batch and only try to trigger 1 repaint/re-render of the list and calculations. |
| 89 | + |
| 90 | +* **API** |
| 91 | + MegaList supports manual scrolling that can be controlled by your JS code, look at the `MegaList.prototype.*` for more |
| 92 | + info and docs. |
| 93 | + |
| 94 | + |
| 95 | +## Contributing |
| 96 | +**Bug fixes** and **new features** can be proposed using [pull requests](https://github.com/lpetrov/megalist/pulls). |
| 97 | +Please read the [contribution guidelines](https://github.com/lpetrov/megalist/blob/master/CONTRIBUTING.md) before submitting a pull request. |
| 98 | + |
| 99 | +## Support |
| 100 | +This project is actively maintained, but there is no official support channel. |
| 101 | +However, you are free to submit [issues, questions or feature requests](https://github.com/lpetrov/megalist/issues), which |
| 102 | +whenever we or other developers like can answer or submit a PR. |
| 103 | + |
| 104 | +## License |
| 105 | +Released under the [MIT license](http://www.opensource.org/licenses/MIT). |
0 commit comments