Lungo.View.Element.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. PROGRESS_DESCRIPTION: ' .labels'
  17. };
  18. /**
  19. * Set a counter to the element
  20. *
  21. * @method count
  22. *
  23. * @param {string} Element query selector
  24. * @param {number} Value for counter
  25. */
  26. var count = function(selector, count) {
  27. var element = lng.dom(selector);
  28. if (element) {
  29. if (count > 0) {
  30. _setBubble(element, count);
  31. } else {
  32. element.children(SELECTORS.BUBBLE).remove();
  33. }
  34. }
  35. };
  36. /**
  37. * Set a progress to the element
  38. *
  39. * @method progress
  40. *
  41. * @param {string} Element query selector
  42. * @param {number} Percentage
  43. * @param {boolean} Show the labels: description and current percentage
  44. * @param {string} Description
  45. */
  46. var progress = function(selector, percentage, with_labels, description) {
  47. var element = lng.dom(selector);
  48. if (element) {
  49. percentage += ATTRIBUTE.PERCENT;
  50. lng.dom(selector + SELECTORS.PROGRESS_VALUE).style(ATTRIBUTE.WIDTH, percentage);
  51. _setProgressLabel(selector + SELECTORS.PROGRESS_PERCENTAGE, with_labels, percentage);
  52. _setProgressLabel(selector + SELECTORS.PROGRESS_DESCRIPTION, with_labels, description);
  53. }
  54. };
  55. var _setBubble = function(element, count) {
  56. var bubbles = element.children(SELECTORS.BUBBLE);
  57. var total_bubbles = bubbles.length;
  58. if (total_bubbles > 0) {
  59. bubbles.html(count);
  60. } else {
  61. var count_html = LUNGO.Attributes.Data.Count.html;
  62. var html_binded = count_html.replace(BINDING.START + BINDING.KEY + BINDING.END, count);
  63. element.append(html_binded);
  64. }
  65. };
  66. var _setProgressLabel = function(selector, with_labels, attribute) {
  67. lng.dom(selector).html((with_labels) ? attribute : ATTRIBUTE.EMPTY);
  68. };
  69. return {
  70. count: count,
  71. progress: progress
  72. };
  73. })(LUNGO);