Lungo.View.Template.List.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Auto generate lists based on Template and Data-Binding system
  3. *
  4. * @namespace LUNGO.View.Template
  5. * @class List
  6. *
  7. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  8. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  9. */
  10. LUNGO.View.Template.List = (function(lng, undefined) {
  11. var ERROR = lng.Constants.ERROR;
  12. var ATTRIBUTE = lng.Constants.ATTRIBUTE;
  13. /**
  14. * Create a list based DataBind with a configuration object for an element <article>
  15. * if the config has a 'norecords' property it will display the norecords markup rather than nothing.
  16. *
  17. * @method create
  18. *
  19. * @param {object} Id of the container showing the result of databinding
  20. */
  21. var create = function(config) {
  22. config.container = _getContainer(config.el);
  23. if (_validateConfig(config)) {
  24. config.data = _order(config);
  25. _render(config);
  26. _scroll(config.el);
  27. }
  28. };
  29. /**
  30. * Append a list based DataBind with a configuration object for an element <article>
  31. * if the config has a 'norecords' property it will display the norecords markup rather than nothing.
  32. *
  33. * @method append
  34. *
  35. * @param {object} Id of the container showing the result of databinding
  36. */
  37. var append = function(config) {
  38. var markup = lng.View.Template.markup(config.template, config.data);
  39. var container = _getContainer(config.el);
  40. container.append(markup);
  41. _scroll(config.el, ATTRIBUTE.LAST);
  42. };
  43. /**
  44. * Prepend a list based DataBind with a configuration object for an element <article>
  45. * if the config has a 'norecords' property it will display the norecords markup rather than nothing.
  46. *
  47. * @method prepend
  48. *
  49. * @param {object} Id of the container showing the result of databinding
  50. */
  51. var prepend = function(config) {
  52. var markup = lng.View.Template.markup(config.template, config.data);
  53. var container = _getContainer(config.el);
  54. container.prepend(markup);
  55. _scroll(config.el, ATTRIBUTE.FIRST);
  56. };
  57. var _validateConfig = function(config) {
  58. var checked = false;
  59. var container_exists = !! config.container.length > 0;
  60. var template_exists = lng.View.Template.exists(config.template);
  61. if (container_exists && template_exists) {
  62. var type = lng.Core.toType(config.data);
  63. if (type === 'array' || type === 'object') {
  64. checked = true;
  65. }
  66. } else {
  67. lng.Core.log(3, ERROR.BINDING_LIST);
  68. }
  69. return checked;
  70. };
  71. var _getContainer = function(element) {
  72. return lng.dom(element).children().first();
  73. }
  74. var _order = function(config) {
  75. if (config.order && config.order.field && config.order.type) {
  76. config.data = lng.Core.orderByProperty(config.data, config.order.field, config.order.type);
  77. }
  78. return config.data;
  79. };
  80. var _render = function(config) {
  81. lng.View.Template.render(config.container.selector, config.template, config.data);
  82. };
  83. var _scroll = function(element, direction) {
  84. var element_id = lng.dom(element).attr(ATTRIBUTE.ID);
  85. lng.View.Scroll.init(element_id);
  86. if (direction) {
  87. lng.View.Scroll[(direction === ATTRIBUTE.FIRST) ? ATTRIBUTE.FIRST : ATTRIBUTE.LAST](element_id);
  88. }
  89. };
  90. return {
  91. create: create,
  92. append: append,
  93. prepend: prepend
  94. };
  95. })(LUNGO);