You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * MINITPL
  3. *********************************************************
  4. * @licstart The following is the entire license notice for the
  5. * JavaScript code in this page.
  6. *
  7. * Copyright (C) 2013 Ophir LOJKINE
  8. *
  9. *
  10. * The JavaScript code in this page is free software: you can
  11. * redistribute it and/or modify it under the terms of the GNU
  12. * General Public License (GNU GPL) as published by the Free Software
  13. * Foundation, either version 3 of the License, or (at your option)
  14. * any later version. The code is distributed WITHOUT ANY WARRANTY;
  15. * without even the implied warranty of MERCHANTABILITY or FITNESS
  16. * FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
  17. *
  18. * As additional permission under GNU GPL version 3 section 7, you
  19. * may distribute non-source (e.g., minimized or compacted) forms of
  20. * that code without the copy of the GNU GPL normally required by
  21. * section 4, provided you include this license notice and a URL
  22. * through which recipients can access the Corresponding Source.
  23. *
  24. * @licend
  25. */
  26. Minitpl = (function () {
  27. function Minitpl(elem, data) {
  28. this.elem = (typeof (elem) === "string") ? document.querySelector(elem) : elem;
  29. if (!elem) {
  30. throw "Invalid element!";
  31. }
  32. this.parent = this.elem.parentNode;
  33. this.parent.removeChild(this.elem);
  34. }
  35. function transform(element, transformer) {
  36. if (typeof (transformer) === "function") {
  37. transformer(element);
  38. } else {
  39. element.textContent = transformer;
  40. }
  41. }
  42. Minitpl.prototype.add = function (data) {
  43. var newElem = this.elem.cloneNode(true);
  44. if (typeof (data) === "object") {
  45. for (var key in data) {
  46. var matches = newElem.querySelectorAll(key);
  47. for (var i = 0; i < matches.length; i++) {
  48. transform(matches[i], data[key]);
  49. }
  50. }
  51. } else {
  52. transform(newElem, data);
  53. }
  54. this.parent.appendChild(newElem);
  55. return newElem;
  56. }
  57. return Minitpl;
  58. }());