PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/cosas/list-master/README.md

https://bitbucket.org/weddcam/develop_weddcam
Markdown | 520 lines | 374 code | 146 blank | 0 comment | 0 complexity | f5f2e05b6ecab53b1605e1678cd25806 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, MIT, LGPL-3.0, LGPL-2.1
  1. # List.js
  2. Do you want a 3 KB (gzipped&minified) cross-browser native JavaScript that makes your plain HTML lists super flexible, searchable, sortable and filterable? **Yeah!**
  3. Do you also want the possibility to add, edit and remove items by dead simple templating? **Hell yeah!**
  4. # Super simple examples
  5. More examples are found at [Listjs.com](http://listjs.com) and
  6. [Listjs.com/examples.html](http://listjs.com/examples.html)
  7. ## Index existing list
  8. HTML
  9. <div id="hacker-list">
  10. <ul class="list">
  11. <li>
  12. <h3 class="name">Jonny</h3>
  13. <p class="city">Stockholm</p>
  14. </li>
  15. <li>
  16. <h3 class="name">Jonas</h3>
  17. <p class="city">Berlin</p>
  18. </li>
  19. </ul>
  20. </div>
  21. Javascript
  22. var options = {
  23. valueNames: [ 'name', 'city' ]
  24. };
  25. var hackerList = new List('hacker-list', options);
  26. ## Create list on initialization
  27. ### Version 1 (does not work with tables)
  28. HTML
  29. <div id="hacker-list">
  30. <ul class="list"></ul>
  31. </div>
  32. JavaScript
  33. var options = {
  34. item: '<li><h3 class="name"></h3><p class="city"></p></li>'
  35. };
  36. var values = [
  37. { name: 'Jonny', city:'Stockholm' }
  38. , { name: 'Jonas', city:'Berlin' }
  39. ];
  40. var hackerList = new List('hacker-list', options, values);
  41. ### Version 2
  42. HTML
  43. <div id="hacker-list">
  44. <ul class="list"></ul>
  45. </div>
  46. <div style="display:none;">
  47. <!-- A template element is needed when list is empty, TODO: needs a better solution -->
  48. <li id="hacker-item">
  49. <h3 class="name"></h3>
  50. <p class="city"></p>
  51. </li>
  52. </div>
  53. JavaScript
  54. var options = {
  55. item: 'hacker-item'
  56. };
  57. var values = [
  58. { name: 'Jonny', city:'Stockholm' }
  59. , { name: 'Jonas', city:'Berlin' }
  60. ];
  61. var hackerList = new List('hacker-list', options, values);
  62. ## Index existing list and then add
  63. HTML
  64. <div id="hacker-list">
  65. <ul class="list">
  66. <li>
  67. <h3 class="name">Jonny</h3>
  68. <p class="city">Stockholm</p>
  69. </li>
  70. </ul>
  71. </div>
  72. JavaScript
  73. var options = {
  74. valueNames: ['name', 'city']
  75. };
  76. var hackerList = new List('hacker-list', options);
  77. hackerList.add( { name: 'Jonas', city:'Berlin' } );
  78. ## Add automagic search and sort inputs and buttons
  79. HTML
  80. <div id="hacker-list">
  81. <input class="search" />
  82. <span class="sort" data-sort="name">Sort by name</span>
  83. <span class="sort" data-sort="city">Sort by city</span>
  84. <ul class="list">
  85. <li>
  86. <h3 class="name">Jonny</h3>
  87. <p class="city">Stockholm</p>
  88. </li>
  89. <li>
  90. <h3 class="name">Jonas</h3>
  91. <p class="city">Berlin</p>
  92. </li>
  93. </ul>
  94. </div>
  95. Javascript (nothing special)
  96. var options = {
  97. valueNames: [ 'name', 'city' ]
  98. };
  99. var hackerList = new List('hacker-list', options);
  100. # Plugins
  101. List.js offers the possiblity to use and add plugins that are integrated in the list objects
  102. and are initiated at the same time as the lists. [Read more here »](http://jonnystromberg.com/listjs-plugins-guide/)
  103. ## List of plugins
  104. * **[Paging plugin](http://jonnystromberg.com/listjs-paging-plugin/)** - A plugin for easily adding
  105. paging to List.js
  106. * **[Fuzzy search plugin](http://jonnystromberg.com/listjs-fuzzy-search-plugin/)** - A plugin for fuzzy search matching
  107. As you can see, there are currently only two plugins. But I would very much like to add your plugin
  108. to this list if you just email me.
  109. # Documentation
  110. ## Search, sort and list container element
  111. The secret behind the search field, the sort buttons, and the list container element are the classes.
  112. By default, all inputs with class `search` become search fields for the list.
  113. <input type="text" class="search" />
  114. The sorting gets activated for all elements with class `sort` and then sorts the
  115. `valueName` corresponding to the the `data-sort` value of the element.
  116. <span class="sort" data-sort="name">Sort names</span>
  117. The element containing the list has to have the class `list` (or one that _you_ define)
  118. <ul class="list"></ul>
  119. # Can be a div, table, dl, or whatever fits your purpose
  120. All of these classes can be defined by yourself when creating the list by setting the options
  121. `searchClass`, `listClass` and `sortClass`.
  122. ## Create a list
  123. ### Constructor
  124. * List(id, options, values)
  125. ### Parameters
  126. * **id** or **element** *(\*required)*
  127. Id the element in which the list area should be initialized. OR the actual element itself.
  128. * **options**
  129. Some of the option parameters are required at some times
  130. * **valueNames** _(Array, default: null) (*only required if the list already contains items before initialization)_
  131. If the list contains items on initialization, then this array
  132. has to contain the value names (class names) for the different values of
  133. each list item.
  134. <ul class="list">
  135. <li>
  136. <span class="name">Jonny</span>
  137. <span class="city">Sundsvall</span>
  138. </li>
  139. </ul>
  140. var valueNames = ['name', 'city'];
  141. * **item** _(String, default: undefined)_
  142. ID to item template element or a string of HTML (**notice**: does not work with `<tr>`)
  143. var options = {
  144. item: "<li><span class='name'></span><span class='city'></span></li>"
  145. }
  146. * **listClass** _(String, default: "list")_
  147. What is the class of the list-container?
  148. * **searchClass** _(String, default: "search")_
  149. What is the class of the search field?
  150. * **sortClass** _(String, default: "sort")_
  151. What is the class of the sort buttons?
  152. * **indexAsync** _(Boolean, default: false)_
  153. If there are already items in the list to which the
  154. List.js-script is added, then should the indexing be done
  155. in a asynchronous way? Good for large lists (> 500 items).
  156. * **page** _(Int, default: 200)_ (maxVisibleItemsCount previously)
  157. Defines how many items that should be visible at the same time. This affects
  158. performance.
  159. * **i** _(Int, default: 1)_
  160. Which item should be shown as the first one.
  161. * **plugins** _(Array, default: undefined)_
  162. [Read more about plugins here](http://jonnystromberg.com/listjs-plugins-guide/)
  163. * **values** _(Array of objects) (*optional)_
  164. Values to add to the list on initialization.
  165. # List API
  166. These methods are available for the List-object.
  167. ### Properties
  168. * **listContainer** _(Element)_
  169. The element node that contains the entire list area.
  170. * **list** _(Element)_
  171. The element containing all items.
  172. * **items** _(Array)_
  173. An Array of all Item-objects in the list.
  174. * **visibleItems** _(Array)_
  175. The currently visible items in the list
  176. * **matchingItems** _(Array)_
  177. The items matching the currently active filter and search.
  178. * **searched** _(Boolean)_
  179. Returns true if the list is searched.
  180. * **filtered** _(Boolean)_
  181. Returns true if there is an active filter.
  182. * **list** _(Element)_
  183. The element containing all items.
  184. * **templateEngines** _(Object)_
  185. Contains all template engines available.
  186. * **plugins** _(Object)_
  187. The currently avaliable plugins.
  188. ### Methods
  189. * **add(values, callback)**
  190. Adds one or more items to the list.
  191. listObj.add({ name: "Jonny", city: "Stockholm" });
  192. listObj.add([
  193. { name: "Gustaf", city: "Sundsvall" }
  194. , { name: "Jonas", city: "Berlin" }
  195. ]);
  196. If `callback` is set then items are added to the list in a asynchronous way, and the
  197. callback is called when all items are added. This is especially useful
  198. when adding very many items (200+ or something), or if you just like the
  199. asynchronous coding style.
  200. listObj.add(arrayWithManyManyItems, function(items) {
  201. console.log('All ' + items.length + ' were added!');
  202. });
  203. * **remove(valueName, value)**
  204. Removes items from the list where the value named `valueName` has value `value`.
  205. Returns the number of items that where removed.
  206. itemsInList = [
  207. { id: 1, name: "Jonny" }
  208. , { id: 2, name "Gustaf" }
  209. ];
  210. listObj.remove("id", 1); -> return 1
  211. * **get(valueName, value)**
  212. Returns values from the list where the value named `valueName` has value `value`.
  213. itemsInList = [
  214. { id: 1, name: "Jonny" }
  215. , { id: 2, name "Gustaf" }
  216. ];
  217. listObj.get("id", 2); -> return { id: 2, name "Gustaf" }
  218. * **sort(valueName, options)**
  219. Sorts the list based on values the in the column named `valueName`. The options
  220. parameter can contain two properties `options.sortFunction` and `options.asc`.
  221. `options.sortFunction` is used if you want to make your own sort function.
  222. The default sort function is found here [http://my.opera.com/GreyWyvern/blog/show.dml/1671288](http://my.opera.com/GreyWyvern/blog/show.dml/1671288)
  223. `options.asc = true` means that you want to sort the list in ascending order. Set
  224. `false` for descending.
  225. listObj.sort('name', { asc: true }); -> Sorts the list in abc-order based on names
  226. listObj.sort('name', { asc: false }); -> Sorts the list in zxy-order based on names
  227. * **search(searchString, columns)**
  228. Searches the list
  229. itemsInList = [
  230. { id: 1, name: "Jonny" }
  231. , { id: 2, name "Gustaf" }
  232. , { id: 3, name "Jonas" }
  233. ];
  234. listObj.search('Jonny'); -> Only item with name Jonny is shown (also returns this item)
  235. listObj.search(); -> Show all items in list
  236. * **clear()**
  237. Removes all items from the list
  238. * **filter(filterFunction)**
  239. itemsInList = [
  240. { id: 1, name: "Jonny" }
  241. , { id: 2, name "Gustaf" }
  242. , { id: 3, name "Jonas" }
  243. ];
  244. listObj.filter(function(item) {
  245. if (item.values().id > 1) {
  246. return true;
  247. } else {
  248. return false;
  249. }
  250. }); -> Only items with id > 1 are shown in list
  251. listObjs.filter(); -> Remove all filters
  252. * **size()**
  253. Returns the size of the list.
  254. * **show(i, page)**
  255. Shows `page` number of items from `i`. Use for paging etc.
  256. itemsInList = [
  257. { id: 1, name: "Jonny" }
  258. , { id: 2, name "Gustaf" }
  259. , { id: 3, name "Jonas" }
  260. , { id: 4, name "Egon" }
  261. , { id: 5, name "Frank" }
  262. , { id: &, name "Ester" }
  263. ];
  264. listObj.show(4, 3); -> Display item 4,5,6
  265. * **update()**
  266. Updates the current state of the list. Meaning that if you for instance
  267. hides some items with the `itemObj.hide()` method then you have to call `listObj.update()`
  268. if you want the paging to update.
  269. * **on(event, callback)**
  270. Execute `callback` when list have been updated (triggered by `update()`, which is used by a lot of methods).
  271. # Item API
  272. These methods are available for all Items that are returned by
  273. the list.
  274. ### Properties
  275. * **elm** _(Element)_
  276. The actual item DOM element
  277. ### Methods
  278. * **values(newValues)**
  279. * newValues _optional_
  280. If variable newValues are present the new values replaces the current item values
  281. and updates the list.
  282. If newValues are not present, the function returns the current values.
  283. item.values() -> { name: "Jonny", age: 24, city: "Umeå" }
  284. item.values({
  285. age: 25,
  286. city: "Stockholm"
  287. });
  288. item.values() -> { name: "Jonny", age: 25, city: "Stockholm" }
  289. * **show()**
  290. Shows the item
  291. * **hide()**
  292. Hides the item (removes the element from the list, and then when its shown it's appended again. The element will thereby change position in the list. A bug, but a good solution is yet to be found.)
  293. * **matching()**
  294. Returns boolean. True if the item matches the current filter and search. Visible items
  295. always matches, but matching items are not always visible.
  296. * **visisble()**
  297. Returns boolean. True if the item is visible. Visible items
  298. always matches, but matching items are not always visible.
  299. ## Helper functions
  300. Called by ListJsHelpers.functionName()
  301. * **getByClass(element, class, isSingle)**
  302. [http://www.dustindiaz.com/getelementsbyclass](http://www.dustindiaz.com/getelementsbyclass)
  303. * **addEvent(element, type, callback)**
  304. [http://net.tutsplus.com/tutorials/javascript-ajax/javascript-from-null-cross-browser-event-binding/](http://net.tutsplus.com/tutorials/javascript-ajax/javascript-from-null-cross-browser-event-binding/)
  305. Updated in some ways, thought.
  306. * **getAttribute(element, attribute)**
  307. [http://stackoverflow.com/questions/3755227/cross-browser-javascript-getattribute-method](http://stackoverflow.com/questions/3755227/cross-browser-javascript-getattribute-method)
  308. * **isNodeList(element)**
  309. [http://stackoverflow.com/questions/7238177/detect-htmlcollection-nodelist-in-javascript](http://stackoverflow.com/questions/7238177/detect-htmlcollection-nodelist-in-javascript)
  310. * **hasClass(element, class)**
  311. Checks if `element` has class name `class`
  312. * **addClass(element, class)**
  313. Adds class name `class` to `element`
  314. * **removeClass(element, class)**
  315. Removes class name `class` from `element`
  316. # Plugins (paging)
  317. Read more about plugins in [this blog post](http://jonnystromberg.com/listjs-plugins-guide/) and find out [how to use the paging plugin here](http://jonnystromberg.com/listjs-paging-plugin/).
  318. # Performance and benchmarking
  319. Read about it at [The List.js Blog: Performance, wroooooom! Index, search and sort thousands of items](http://blog.listjs.com/post/12006163208/performance-wroooooom-index-search-and-sort)
  320. and try it at the [performance test page](http://listjs.com/examples/performance-test.html).
  321. # How to build the script
  322. Type just *ant* in the console while in root folder.
  323. # Changelog
  324. ### 2012-04-24 Beta 0.2.1
  325. * Fuzzy Search plugin, `.filter()` changes and bug fixes **[Read more »](http://jonnystromberg.com/listjs-0-2-1-release-notes/)**
  326. ### 2012-01-23 Beta 0.2.0
  327. * Lots of updates and interesting features. **[Read more »](http://jonnystromberg.com/listjs-0-2-0-plugins-paging/)**
  328. ### 2011-12-15 Beta 0.1.4
  329. * `.filters()`, `.sort()` and `.search()` now deped on each other. If the list is filtered and then
  330. there is a search, the items hidden by the filters will stay hidden etc.
  331. * `.filter()` is the only way to reset filter. `.filter(false)` does not work anymore.
  332. ### 2011-11-29 Beta 0.1.3 release
  333. * Added function `.clear()` that removes all items from the list
  334. * Changed the sort function to be based on `data-sort` instead of `rel`
  335. * When sorting one category, all sort-related classes will be removed from the other sort buttons
  336. * Updated `.sort(valueName, sortFunction)` to `.sort(valueName, options)`, se more info in the documentation
  337. ### 2011-11-16 Beta 0.1.2 release
  338. * Sorting is now indicated by class `asc` or `desc` at sorting buttons
  339. * Added three new small helper functions `hasClass(element, class)`, `addClass(element, class)`
  340. and `removeClass(element, class)`
  341. ### 2011-10-20 Beta 0.1.1 release
  342. * Added possibility to reverse sort the list
  343. ### 2011-10-18 Beta 0.1 release
  344. * Examples at Listjs.com works in IE7,8,9 (IE6 is not tested, should work)
  345. * More documentation
  346. * Misc bug fixes
  347. ### 2011-10-15 Final alpha 0.3 release
  348. * More documentation
  349. * Only show 200 items at same time, huge speed increase
  350. * Misc bug fixes
  351. ### 2011-08-08 Alpha 0.2 release
  352. * Added asynchronous item adding
  353. * Added asynchronous list indexing
  354. * Improved (but incomplete) documentation
  355. * Bugfixes and improved helper functions
  356. * Show helper functions non-minified
  357. ### 2011-07-25 Alpha 0.1 release
  358. # License (MIT)
  359. Copyright (c) 2012 Jonny Strömberg http://jonnystromberg.com
  360. Permission is hereby granted, free of charge, to any person obtaining
  361. a copy of this software and associated documentation files (the "Software"),
  362. to deal in the Software without restriction, including without limitation the
  363. rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  364. sell copies of the Software, and to permit persons to whom the Software is
  365. furnished to do so, subject to the following conditions:
  366. The above copyright notice and this permission notice shall be included in
  367. all copies or substantial portions of the Software.
  368. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  369. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  370. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  371. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  372. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  373. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  374. DEALINGS IN THE SOFTWARE.