Lungo.View.Element.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Initialize the <articles> layout of a certain <section>
  3. *
  4. * @namespace Lungo.View
  5. * @class Element
  6. *
  7. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  8. */
  9. Lungo.View.Element = (function(lng, undefined) {
  10. var ATTRIBUTE = lng.Constants.ATTRIBUTE;
  11. var BINDING = lng.Constants.BINDING;
  12. var SELECTORS = {
  13. BUBBLE: '.bubble.count',
  14. PROGRESS_VALUE: ' .value',
  15. PROGRESS_PERCENTAGE: ' .value .label'
  16. };
  17. /**
  18. * Set a counter to the element
  19. *
  20. * @method count
  21. *
  22. * @param {string} Element query selector
  23. * @param {number} Value for counter
  24. */
  25. var count = function(selector, count) {
  26. var element = lng.dom(selector);
  27. if (element) {
  28. if (count > 0) {
  29. _setBubble(element, count);
  30. } else {
  31. element.children(SELECTORS.BUBBLE).remove();
  32. }
  33. }
  34. };
  35. /**
  36. * Set a progress to the element
  37. *
  38. * @method progress
  39. *
  40. * @param {string} Element query selector
  41. * @param {number} Percentage
  42. * @param {boolean} Show the labels: description and current percentage
  43. * @param {string} Description
  44. */
  45. var progress = function(selector, percentage, with_label) {
  46. var element = lng.dom(selector);
  47. if (element) {
  48. percentage += ATTRIBUTE.PERCENT;
  49. lng.dom(selector + SELECTORS.PROGRESS_VALUE).style(ATTRIBUTE.WIDTH, percentage);
  50. lng.dom(selector + SELECTORS.PROGRESS_PERCENTAGE).html((with_label) ? percentage : ATTRIBUTE.EMPTY);
  51. }
  52. };
  53. /**
  54. * Set a progress to the element
  55. *
  56. * @method loading
  57. *
  58. * @param {string} Element query selector
  59. * @param {number} stylesheet (null for hide)
  60. */
  61. var loading = function(selector, stylesheet) {
  62. var element = lng.dom(selector);
  63. if (element) {
  64. if (stylesheet) {
  65. _bindAttribute(element, Lungo.Attributes.Data.Loading, stylesheet);
  66. }
  67. else {
  68. element.children('.loading').remove();
  69. }
  70. }
  71. };
  72. var _setBubble = function(element, count) {
  73. var bubbles = element.children(SELECTORS.BUBBLE);
  74. var total_bubbles = bubbles.length;
  75. if (total_bubbles > 0) {
  76. bubbles.html(count);
  77. } else {
  78. _bindAttribute(element, Lungo.Attributes.Data.Count, count);
  79. }
  80. };
  81. var _bindAttribute = function(element, attribute, data) {
  82. var html_binded = attribute.html.replace(BINDING.START + BINDING.KEY + BINDING.END, data);
  83. element.append(html_binded);
  84. };
  85. return {
  86. count: count,
  87. progress: progress,
  88. loading: loading
  89. };
  90. })(Lungo);