relationships-reader.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. exports.readRelationships = readRelationships;
  2. exports.defaultValue = new Relationships([]);
  3. exports.Relationships = Relationships;
  4. function readRelationships(element) {
  5. var relationships = [];
  6. element.children.forEach(function(child) {
  7. if (child.name === "relationships:Relationship") {
  8. var relationship = {
  9. relationshipId: child.attributes.Id,
  10. target: child.attributes.Target,
  11. type: child.attributes.Type
  12. };
  13. relationships.push(relationship);
  14. }
  15. });
  16. return new Relationships(relationships);
  17. }
  18. function Relationships(relationships) {
  19. var targetsByRelationshipId = {};
  20. relationships.forEach(function(relationship) {
  21. targetsByRelationshipId[relationship.relationshipId] = relationship.target;
  22. });
  23. var targetsByType = {};
  24. relationships.forEach(function(relationship) {
  25. if (!targetsByType[relationship.type]) {
  26. targetsByType[relationship.type] = [];
  27. }
  28. targetsByType[relationship.type].push(relationship.target);
  29. });
  30. return {
  31. findTargetByRelationshipId: function(relationshipId) {
  32. return targetsByRelationshipId[relationshipId];
  33. },
  34. findTargetsByType: function(type) {
  35. return targetsByType[type] || [];
  36. }
  37. };
  38. }