Lungo.Boot.Data.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * Make an analysis of Data attributes in HTML elements and creates a <markup>
  3. * based on each data type.
  4. *
  5. * @namespace Lungo.Boot
  6. * @class Data
  7. *
  8. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  9. * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  10. * @author Ignacio Olalde <ina@tapquo.com> || @piniphone
  11. */
  12. Lungo.Boot.Data = (function(lng, undefined) {
  13. var BINDING = lng.Constants.BINDING;
  14. /**
  15. * Initialize the <markup> data-attributes analisys
  16. *
  17. * @method init
  18. *
  19. *
  20. */
  21. var init = function(selector) {
  22. var el = lng.dom(selector || document.body);
  23. if (el.length > 0) _findDataAttributesIn(el);
  24. };
  25. var _findDataAttributesIn = function(element) {
  26. for (var key in lng.Attributes) {
  27. if (lng.Core.isOwnProperty(lng.Attributes, key)) {
  28. _findElements(element, key);
  29. }
  30. }
  31. };
  32. var _findElements = function(element, key) {
  33. attribute = lng.Attributes[key];
  34. var selector = attribute.selector + "[data-" + key + "]";
  35. element.find(selector).each(function(index, children) {
  36. var el = lng.dom(children);
  37. _bindDataAttribute(el, el.data(key), attribute.html);
  38. });
  39. };
  40. var _bindDataAttribute = function(element, value, html) {
  41. var html_binded = html.replace(BINDING.START + BINDING.KEY + BINDING.END, value);
  42. element.prepend(html_binded);
  43. };
  44. return {
  45. init: init
  46. };
  47. })(Lungo);