ElementTemplates.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. var values = require('lodash/values');
  3. /**
  4. * The guy knowing all configured element templates.
  5. *
  6. * This registry won't validate. Use the {@link Validator}
  7. * to verify a template is valid prior to adding it to
  8. * this registry.
  9. */
  10. function ElementTemplates() {
  11. this._templates = {};
  12. /**
  13. * Sets the known element templates.
  14. *
  15. * @param {Array<TemplateDescriptor>} descriptors
  16. *
  17. * @return {ElementTemplates}
  18. */
  19. this.set = function(descriptors) {
  20. var templates = this._templates = {};
  21. descriptors.forEach(function(descriptor) {
  22. templates[descriptor.id] = descriptor;
  23. });
  24. return this;
  25. };
  26. /**
  27. * Get template descriptor with given id.
  28. *
  29. * @param {String} id
  30. *
  31. * @return {TemplateDescriptor}
  32. */
  33. this.get = function(id) {
  34. return this._templates[id];
  35. };
  36. /**
  37. * Return all known template descriptors.
  38. *
  39. * @return {Array<TemplateDescriptor>}
  40. */
  41. this.getAll = function() {
  42. return values(this._templates);
  43. };
  44. }
  45. module.exports = ElementTemplates;