grafo-client
Version:
Grafo Client API Library
1 lines • 9.46 kB
JavaScript
const uuidv4=require("uuid/v4"),util=require("../util");var Iri=require("./iri"),ColorPalette=require("../models/color_palette");const Attribute=require("./attribute"),AttributeGroup=require("./attribute_group"),CustomField=require("./custom_field"),ShareDbCreatePacket=require("./sharedb_create_packet"),ShareDbUpdatePacket=require("./sharedb_update_packet"),Validator=require("./validator"),Mapping=require("./mapping"),GrafoErrorGlossary=require("../errors/GrafoErrorGlossary"),GenericValidationError=require("../errors/GenericValidationError");function Concept(t,i,e,o,r,s,n,p,a){if(this.nodeType="concept",this.deleted=!1,this.fromLinks=[],this.toLinks=[],this.attrOffsetX="none",this.attrOffsetY="none",this.iriChanged=!1,this.attrGrpChanged=!1,this.directPropsChanged=!1,this.coordsChanged=!1,t){if(this.positionX=i,this.positionY=e,this.label=t?t.trim():t,this.description=o,this.fields=[],this.id=p?p.trim():p,s)for(var d=0;d<s.length;d++)s[d]&&s[d].title&&s[d].title.trim()&&this.fields.push(new CustomField(s[d].title,s[d].text,s[d].id));r&&Object.values(ColorPalette.color).includes(r)&&(this.color=r),this.id||(this.id="node-"+uuidv4()),this.label||(this.label="New Concept"),this.description||(this.description=""),n?(this.iri=new Iri,this.iri.initWithIri(n)):this.iri=new Iri(a,this.id,this.label)}}module.exports=Concept,Concept.prototype.initUsingMongoDbStructure=function(t,i,e){if(this.iri=new Iri,this.iri.initWithIri(t.iri,t.hasEdited),this.id=i.id,this.nodeType=i.nodeType,"concept"!=this.nodeType)throw new GenericValidationError(GrafoErrorGlossary.ErrorLevel.INFO,"Hey you got wrong data for node type for node with id = "+i.id+". Please handle for nodeType = "+this.nodeType);if(this.slot=i.slot,this.label=i.label,this.description=i.description,this.fields=[],i.fields)for(var o=0;o<i.fields.length;o++)this.fields.push(new CustomField(i.fields[o].title,i.fields[o].text,i.fields[o].id));if(i.mappings){this.mappings=[];for(o=0;o<i.mappings.length;o++){var r=i.mappings[o],s=new Mapping(r.mappingId,r.dbId,r.mappingType,r.imported,r.id);s.setMappingDetails(r),this.mappings.push(s)}}i.color&&Object.values(ColorPalette.color).includes(i.color)&&(this.color=i.color),this.lastUpdatedBy=i.lastUpdatedBy,this.deleted=i.deleted,this.positionX=e.positionX,this.positionY=e.positionY},Concept.prototype.addAttributesFromMongoDb=async function(t,i){t&&(this.attrGroup||(this.attrGroup=new AttributeGroup(this.id,t.groupId)),this.attrGroup.setSlot(t.slot),this.attrGroup.setLastUpdatedBy(t.lastUpdatedBy),this.attrGroup.setDeleted(t.deleted),await this.attrGroup.addAttributeFromMongoDb(t.attributeList,i))},Concept.prototype.getAttributeGroup=function(){return this.attrGroup||(this.attrGroup=new AttributeGroup(this.id)),this.attrGroup},Concept.prototype.getAttributeList=function(){return this.attrGroup?this.attrGroup.getAttributeList():[]},Concept.prototype.getAttribute=async function(t){if(this.attrGroup)return await this.attrGroup.getAttributeById(t)},Concept.prototype.createAttributeGroup=function(t){this.attrGroup=new AttributeGroup(this.id,t)},Concept.prototype.setId=function(t){this.id=t},Concept.prototype.getId=function(){return this.id},Concept.prototype.getSlot=function(){return this.slot},Concept.prototype.setSlot=function(t){this.slot=t},Concept.prototype.update=function(t){this.slot=t.slot,this.lastUpdatedBy=t.lastUpdatedBy},Concept.prototype.setLastUpdatedBy=function(t){this.lastUpdatedBy=t},Concept.prototype.getLabel=function(){return this.label},Concept.prototype.setLabel=function(t){this.directPropsChanged=!0,this.label=t},Concept.prototype.setDescription=function(t){this.directPropsChanged=!0,this.description=t},Concept.prototype.setFields=function(t,i){if(this.directPropsChanged=!0,t){this.fields||(this.fields=[]);for(var e=0;e<t.length;e++){var o=getFieldIndex(this,t[e]);o>-1?updateField(this,t[e],o):addField(this,t[e],i)}}},Concept.prototype.getFields=function(){return this.fields};var addField=function(t,i,e){e||Validator.validateCustomField(i,!0),t.fields.push(new CustomField(i.title,i.text,i.id))},updateField=function(t,i,e){var o=t.fields[e];i.title&&i.title.trim()&&o.setTitle(i.title),i.text&&i.text.trim()&&o.setText(i.text)};Concept.prototype.deleteField=function(t){this.directPropsChanged=!0;for(var i=-1,e=0;e<this.fields.length;e++)t===this.fields[e].getId()&&(i=e);return i>-1&&this.fields.splice(i,1),i};var getFieldIndex=function(t,i){for(var e=0;e<t.fields.length;e++)if(i.id===t.fields[e].getId())return e;return-1};Concept.prototype.setIri=function(t){this.iriChanged=!0,this.iri=new Iri,t.iri?this.iri.initWithIri(t.iri,t.hasEdited):this.iri.initWithIri(t,!0)},Concept.prototype.initIri=function(t){this.iri=t},Concept.prototype.setColor=function(t){this.directPropsChanged=!0,this.color=t},Concept.prototype.setPositionX=function(t){this.coordsChanged=!0,this.positionX=t},Concept.prototype.setPositionY=function(t){this.coordsChanged=!0,this.positionY=t},Concept.prototype.setDeleted=function(t){this.directPropsChanged=!0,this.deleted=t},Concept.prototype.setMappings=function(t,i){if(this.directPropsChanged=!0,t){this.mappings||(this.mappings=[]);for(var e=0;e<t.length;e++){var o=getMappingIndex(this,t[e]);o>-1?updateMapping(this,t[e],o):addMapping(this,t[e],i)}}},Concept.prototype.getMappings=function(){return this.mappings},Concept.prototype.getAllMappings=function(){var t=[];if(this.mappings)for(var i=0;i<this.mappings.length;i++)t.push(this.mappings[i].toObject());return this.attrGroup&&util.customPush(t,this.attrGroup.getAllMappings()),t},Concept.prototype.addMappings=function(t,i){if(t){this.directPropsChanged=!0,this.mappings||(this.mappings=[]);for(var e=0;e<t.length;e++){var o=t[e];if(o){var r=new Mapping(o.mappingId,o.dbId,o.mappingType,o.imported,o.id);r.setMappingDetails(o),this.mappings.push(r)}}}};var addMapping=function(t,i,e){var o=new Mapping(i.mappingId,i.dbId,i.mappingType,i.imported,i.id);o.setMappingDetails(i),t.mappings.push(o)},updateMapping=function(t,i,e){var o=new Mapping(i.mappingId,i.dbId,i.mappingType,i.imported,i.id);o.setMappingDetails(i),t.mappings[e]=o},getMappingIndex=function(t,i){for(var e=0;e<t.mappings.length;e++)if(i.imported){if(t.mappings[e].dbId===i.dbId&&t.mappings[e].imported)return e}else if(i.id===t.mappings[e].getId())return e;return-1};Concept.prototype.deleteMappings=function(t,i){this.directPropsChanged=!0;for(var e=[],o=0;o<this.mappings.length;o++)t===this.mappings[o].getDbId()&&(i?i===this.mappings[o].isImported()&&e.push(o):e.push(o));e.sort(function(t,i){return i-t});for(o=0;o<e.length;o++){var r=e[o];r>-1&&this.mappings.splice(r,1)}return e},Concept.prototype.getIri=function(){return this.iri.iri},Concept.prototype.getFromLinks=function(){return this.fromLinks},Concept.prototype.setFromLinks=function(t){this.fromLinks&&!this.fromLinks.includes(t)&&this.fromLinks.push(t)},Concept.prototype.getToLinks=function(){return this.toLinks},Concept.prototype.setToLinks=function(t){this.toLinks&&!this.toLinks.includes(t)&&this.toLinks.push(t)},Concept.prototype.getShareDbCreatePackets=function(){var t=getShareDbFormatCoordinates(this),i=getShareDbFormatNodes(this),e=getShareDbFormatIri(this),o=[];return o.push(new ShareDbCreatePacket(this.slot,this.id,"coordinates",t)),o.push(new ShareDbCreatePacket(this.slot,this.id,"nodes",i)),o.push(new ShareDbCreatePacket(this.slot,this.id,"iri",e)),o},Concept.prototype.getShareDbUpdatePackets=function(t){var i=[];if(this.coordsChanged){var e=getShareDbFormatCoordinates(this),o=getShareDbFormatCoordinates(t);i.push(new ShareDbUpdatePacket(this.slot,this.id,"coordinates",e,o))}if(this.directPropsChanged){var r=getShareDbFormatNodes(this),s=getShareDbFormatNodes(t);i.push(new ShareDbUpdatePacket(this.slot,this.id,"nodes",r,s))}if(this.iriChanged){var n=getShareDbFormatIri(this),p=getShareDbFormatIri(t);i.push(new ShareDbUpdatePacket(this.slot,this.id,"iri",n,p))}return i},Concept.prototype.setEkgApi=function(t){this.ekgApi=t},Concept.prototype.getEventCount=function(){return this.iriChanged&&this.directPropsChanged?2:this.directPropsChanged||this.iriChanged?1:0};var getShareDbFormatCoordinates=function(t){return{nodeType:t.nodeType,nodeId:t.id,positionX:t.positionX,positionY:t.positionY,attrOffsetX:t.attrOffsetX,attrOffsetY:t.attrOffsetY,slot:t.slot,lastUpdatedBy:t.lastUpdatedBy}},getShareDbFormatNodes=function(t){for(var i={id:t.id,nodeType:t.nodeType,slot:t.slot,label:t.label,description:t.description,fields:[],color:t.color,lastUpdatedBy:t.lastUpdatedBy,deleted:1==t.deleted||void 0},e=0;e<t.fields.length;e++)i.fields.push(t.fields[e].getCustomField());if(t.mappings&&t.mappings.length>0){i.mappings=[];for(e=0;e<t.mappings.length;e++)i.mappings.push(t.mappings[e].toObject())}return i},getShareDbFormatIri=function(t){return t.iri.getIri()};Concept.prototype.toObject=function(){var t={id:this.id,type:this.nodeType,label:this.label,description:this.description,iri:this.iri.iri,positionX:this.positionX,positionY:this.positionY,color:this.color,lastUpdatedBy:this.lastUpdatedBy,deleted:this.deleted};if(this.fields&&this.fields.length>0){t.fields=[];for(var i=0;i<this.fields.length;i++)t.fields.push(this.fields[i].getCustomField())}if(this.attrGroup&&this.attrGroup.getAttributeList()&&this.attrGroup.getAttributeList().length>0&&(t.attrGroup=this.attrGroup.toObject()),this.mappings&&this.mappings.length>0){t.mappings=[];for(i=0;i<this.mappings.length;i++)t.mappings.push(this.mappings[i].toObject())}return t};