basant307/AI_Governance_Project
048
1import define from '../../define/index.mjs';2import * as is from '../../is.mjs';3import * as math from '../../math.mjs';4import * as util from '../../util/index.mjs';5 6let fn, elesfn;7 8let beforePositionSet = function( eles, newPos, silent ){9 for( let i = 0; i < eles.length; i++ ){10 let ele = eles[i];11 12 if( !ele.locked() ){13 let oldPos = ele._private.position;14 15 let delta = {16 x: newPos.x != null ? newPos.x - oldPos.x : 0,17 y: newPos.y != null ? newPos.y - oldPos.y : 018 };19 20 if( ele.isParent() && !(delta.x === 0 && delta.y === 0) ){21 ele.children().shift( delta, silent );22 }23 24 ele.dirtyBoundingBoxCache();25 }26 }27};28 29let positionDef = {30 field: 'position',31 bindingEvent: 'position',32 allowBinding: true,33 allowSetting: true,34 settingEvent: 'position',35 settingTriggersEvent: true,36 triggerFnName: 'emitAndNotify',37 allowGetting: true,38 validKeys: [ 'x', 'y' ],39 beforeGet: function( ele ){40 ele.updateCompoundBounds();41 },42 beforeSet: function( eles, newPos ){43 beforePositionSet( eles, newPos, false );44 },45 onSet: function( eles ){46 eles.dirtyCompoundBoundsCache();47 },48 canSet: function( ele ){49 return !ele.locked();50 }51};52 53fn = elesfn = ({54 55 position: define.data( positionDef ),56 57 // position but no notification to renderer58 silentPosition: define.data( util.assign( {}, positionDef, {59 allowBinding: false,60 allowSetting: true,61 settingTriggersEvent: false,62 allowGetting: false,63 beforeSet: function( eles, newPos ){64 beforePositionSet( eles, newPos, true );65 },66 onSet: function( eles ){67 eles.dirtyCompoundBoundsCache();68 }69 } ) ),70 71 positions: function( pos, silent ){72 if( is.plainObject( pos ) ){73 if( silent ){74 this.silentPosition( pos );75 } else {76 this.position( pos );77 }78 79 } else if( is.fn( pos ) ){80 let fn = pos;81 let cy = this.cy();82 83 cy.startBatch();84 85 for( let i = 0; i < this.length; i++ ){86 let ele = this[ i ];87 let pos;88 89 if( ( pos = fn(ele, i) ) ){90 if( silent ){91 ele.silentPosition( pos );92 } else {93 ele.position( pos );94 }95 }96 }97 98 cy.endBatch();99 }100 101 return this; // chaining102 },103 104 silentPositions: function( pos ){105 return this.positions( pos, true );106 },107 108 shift: function( dim, val, silent ){109 let delta;110 111 if( is.plainObject( dim ) ){112 delta = {113 x: is.number(dim.x) ? dim.x : 0,114 y: is.number(dim.y) ? dim.y : 0115 };116 117 silent = val;118 } else if( is.string( dim ) && is.number( val ) ){119 delta = { x: 0, y: 0 };120 121 delta[ dim ] = val;122 }123 124 if( delta != null ){125 let cy = this.cy();126 127 cy.startBatch();128 129 for( let i = 0; i < this.length; i++ ){130 let ele = this[i];131 132 // exclude any node that is a descendant of the calling collection133 if (cy.hasCompoundNodes() && ele.isChild() && ele.ancestors().anySame(this)) {134 continue;135 }136 137 let pos = ele.position();138 let newPos = {139 x: pos.x + delta.x,140 y: pos.y + delta.y141 };142 143 if( silent ){144 ele.silentPosition( newPos );145 } else {146 ele.position( newPos );147 }148 }149 150 cy.endBatch();151 }152 153 return this;154 },155 156 silentShift: function( dim, val ){157 if( is.plainObject( dim ) ){158 this.shift( dim, true );159 } else if( is.string( dim ) && is.number( val ) ){160 this.shift( dim, val, true );161 }162 163 return this;164 },165 166 // get/set the rendered (i.e. on screen) positon of the element167 renderedPosition: function( dim, val ){168 let ele = this[0];169 let cy = this.cy();170 let zoom = cy.zoom();171 let pan = cy.pan();172 let rpos = is.plainObject( dim ) ? dim : undefined;173 let setting = rpos !== undefined || ( val !== undefined && is.string( dim ) );174 175 if( ele && ele.isNode() ){ // must have an element and must be a node to return position176 if( setting ){177 for( let i = 0; i < this.length; i++ ){178 let ele = this[ i ];179 180 if( val !== undefined ){ // set one dimension181 ele.position( dim, ( val - pan[ dim ] ) / zoom );182 } else if( rpos !== undefined ){ // set whole position183 ele.position( math.renderedToModelPosition( rpos, zoom, pan ) );184 }185 }186 } else { // getting187 let pos = ele.position();188 rpos = math.modelToRenderedPosition( pos, zoom, pan );189 190 if( dim === undefined ){ // then return the whole rendered position191 return rpos;192 } else { // then return the specified dimension193 return rpos[ dim ];194 }195 }196 } else if( !setting ){197 return undefined; // for empty collection case198 }199 200 return this; // chaining201 },202 203 // get/set the position relative to the parent204 relativePosition: function( dim, val ){205 let ele = this[0];206 let cy = this.cy();207 let ppos = is.plainObject( dim ) ? dim : undefined;208 let setting = ppos !== undefined || ( val !== undefined && is.string( dim ) );209 let hasCompoundNodes = cy.hasCompoundNodes();210 211 if( ele && ele.isNode() ){ // must have an element and must be a node to return position212 if( setting ){213 for( let i = 0; i < this.length; i++ ){214 let ele = this[ i ];215 let parent = hasCompoundNodes ? ele.parent() : null;216 let hasParent = parent && parent.length > 0;217 let relativeToParent = hasParent;218 219 if( hasParent ){220 parent = parent[0];221 }222 223 let origin = relativeToParent ? parent.position() : { x: 0, y: 0 };224 225 if( val !== undefined ){ // set one dimension226 ele.position( dim, val + origin[ dim ] );227 } else if( ppos !== undefined ){ // set whole position228 ele.position({229 x: ppos.x + origin.x,230 y: ppos.y + origin.y231 });232 }233 }234 235 } else { // getting236 let pos = ele.position();237 let parent = hasCompoundNodes ? ele.parent() : null;238 let hasParent = parent && parent.length > 0;239 let relativeToParent = hasParent;240 241 if( hasParent ){242 parent = parent[0];243 }244 245 let origin = relativeToParent ? parent.position() : { x: 0, y: 0 };246 247 ppos = {248 x: pos.x - origin.x,249 y: pos.y - origin.y250 };251 252 if( dim === undefined ){ // then return the whole rendered position253 return ppos;254 } else { // then return the specified dimension255 return ppos[ dim ];256 }257 }258 } else if( !setting ){259 return undefined; // for empty collection case260 }261 262 return this; // chaining263 }264});265 266// aliases267fn.modelPosition = fn.point = fn.position;268fn.modelPositions = fn.points = fn.positions;269fn.renderedPoint = fn.renderedPosition;270fn.relativePoint = fn.relativePosition;271 272export default elesfn;273 