query_response.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /* This file is part of AToMPM - A Tool for Multi-Paradigm Modelling
  2. * Copyright 2011 by the AToMPM team and licensed under the LGPL
  3. * See COPYING.lesser and README.md in the root of this project for full details
  4. */
  5. var __nextASWSequenceNumber = '/asworker#1',
  6. __nextCSWSequenceNumber = '/csworker#1',
  7. __pendingASWChangelogs = [],
  8. __pendingCSWChangelogs = [];
  9. /* delete pending changelogs older than the specified sequence number */
  10. function __clearObsoleteChangelogs(pendingChangelogs,sn)
  11. {
  12. pendingChangelogs =
  13. pendingChangelogs.filter(
  14. function(c)
  15. {
  16. return utils.sn2int(pc['sequence#']) > utils.sn2int(sn);
  17. });
  18. }
  19. /* set the value of __nextASWSequenceNumber to the given sequence number */
  20. function __forceNextASWSequenceNumber(sn)
  21. {
  22. __nextASWSequenceNumber = sn;
  23. __clearObsoleteChangelogs(__pendingASWChangelogs,sn);
  24. }
  25. /* set the value of __nextCSWSequenceNumber to the given sequence number */
  26. function __forceNextCSWSequenceNumber(sn)
  27. {
  28. __nextCSWSequenceNumber = sn;
  29. __clearObsoleteChangelogs(__pendingCSWChangelogs,sn);
  30. }
  31. //Todo: Shred this into smaller functions
  32. /* apply a changelog (or postpone its application)
  33. 0. check the changelog's sequence number to know if we should handle it
  34. now or later... note that we will be receiving changelogs with both
  35. asworker and csworker sequence numbers
  36. 1. iterate through and handle the changelog's steps
  37. 2. apply next pending changelog, if any and if applicable */
  38. function __handleChangelog(changelog,seqNum,hitchhiker)
  39. {
  40. console.debug(' ++ ('+seqNum+') ',changelog);
  41. var isCSWChangelog = seqNum.match(/csworker/);
  42. nextSeqNum =
  43. (isCSWChangelog ? __nextCSWSequenceNumber : __nextASWSequenceNumber),
  44. pendingChangelogs =
  45. (isCSWChangelog ? __pendingCSWChangelogs : __pendingASWChangelogs);
  46. if( utils.sn2int(seqNum) > utils.sn2int(nextSeqNum) )
  47. {
  48. pendingChangelogs.push(
  49. {'changelog':changelog,
  50. 'sequence#':seqNum,
  51. 'hitchhiker':hitchhiker});
  52. pendingChangelogs.sort(
  53. function(a,b)
  54. {
  55. return utils.sn2int(a['sequence#']) - utils.sn2int(b['sequence#']);
  56. });
  57. return;
  58. }
  59. else if( utils.sn2int(seqNum) < utils.sn2int(nextSeqNum) )
  60. {
  61. WindowManagement.openDialog(
  62. __FATAL_ERROR,
  63. 'invalid changelog sequence# :: '+utils.sn2int(seqNum));
  64. return;
  65. }
  66. // BUG: changes to element values are not being sent as changelog steps
  67. // so for now, mark any change as making the model unsaved
  68. WindowManagement.setWindowTitle(true);
  69. changelog.forEach(
  70. function(step)
  71. {
  72. if( (step['op'] == 'RMNODE' ||
  73. step['op'] == 'RMEDGE' ||
  74. step['op'] == 'CHATTR') &&
  75. __watching(step['id']) )
  76. {
  77. console.warn(
  78. 'someone has altered the node you are currently editing...'+
  79. 'to keep your changes, click OK... to discard them and see'+
  80. ' what has changed, click CANCEL');
  81. __changed(step['id'],true);
  82. }
  83. // record the changes that do not mark the model as unsaved
  84. // (as well, note that SYSOUT can change the model through
  85. // the back-door API 'CLIENT_BDAPI')
  86. let clean_ops = ['RESETM', 'SYSOUT'];
  87. if (!clean_ops.includes(step['op'])){
  88. WindowManagement.setWindowTitle(true);
  89. }
  90. if( step['op'] == 'MKEDGE' )
  91. ;
  92. /* react to the removal of an edge */
  93. else if( step['op'] == 'RMEDGE' )
  94. {
  95. __removeEdge(
  96. step['id1']+'--'+step['id2'],
  97. [step['id1'],step['id2']]);
  98. if( __selection != undefined )
  99. __select( utils.filter(__selection['items'],[edgeId]) );
  100. }
  101. /* react to the creation of a node */
  102. else if( step['op'] == 'MKNODE' )
  103. {
  104. var node = utils.jsonp(step['node']),
  105. icon = __createIcon(node,step['id']);
  106. if( '$segments' in node )
  107. {
  108. var linkStyle = node['link-style']['value'],
  109. segments = node['$segments']['value'];
  110. icon.setAttr('__segments',utils.jsons(segments));
  111. for( var edgeId in segments )
  112. __createEdge(segments[edgeId], linkStyle, edgeId, step['id']);
  113. }
  114. }
  115. /* react to the removal of a node */
  116. else if( step['op'] == 'RMNODE' )
  117. {
  118. __icons[step['id']]['icon'].remove();
  119. __icons[step['id']]['edgesOut'].forEach(__removeEdge);
  120. __icons[step['id']]['edgesIn'].forEach(__removeEdge);
  121. delete __icons[step['id']];
  122. if( __selection != undefined )
  123. __select( utils.filter(__selection['items'],[step['id']]) );
  124. }
  125. /* react to attribute update
  126. CASE 1 : Icon/Link layout attribute (position, etc.)
  127. update the corresponding attribute of the corresponding icon
  128. in __icons + actually effect the layout transformation (via
  129. __setIconTransform())
  130. CASE 2 : Link attribute ($segments / link-style)
  131. redraw existing edges and create any new ones
  132. CASE 3 : VisualObject attribute update
  133. update the corresponding attribute of the corresponding icon's
  134. corresponding vobject */
  135. else if( step['op'] == 'CHATTR' )
  136. {
  137. /* CASE 1 */
  138. if(utils.contains(['position','orientation','scale'],step['attr']))
  139. {
  140. var newVal = step['new_val'],
  141. icon = __icons[step['id']]['icon'];
  142. /* bugfix where newVal would get a string instead of the array of values
  143. should search for reason why it has string in the first place - Vasco - 02-02-2017*/
  144. if( typeof newVal == 'string'){
  145. newVal = newVal.replace('[','');
  146. newVal = newVal.replace(']','');
  147. newVal = newVal.split(',');
  148. }
  149. /* endbugfix*/
  150. if( step['attr'] == 'position' )
  151. {
  152. var bbox = icon.getBBox();
  153. icon.setAttr('__x', __getAbsoluteCoordinate(newVal[0],bbox.width) );
  154. icon.setAttr('__y', __getAbsoluteCoordinate(newVal[1],bbox.height) );
  155. }
  156. else if( step['attr'] == 'orientation' )
  157. icon.setAttr('__r',newVal);
  158. else if( step['attr'] == 'scale' )
  159. {
  160. icon.setAttr('__sx',newVal[0]);
  161. icon.setAttr('__sy',newVal[1]);
  162. }
  163. __setIconTransform(step['id']);
  164. }
  165. /* CASE 2 */
  166. else if(utils.contains(['$segments','link-style','arrowTail','arrowHead'],step['attr']))
  167. {
  168. var icon = __icons[step['id']]['icon'],
  169. segments,
  170. linkStyle;
  171. if( step['attr'] == '$segments' )
  172. {
  173. segments = step['new_val'];
  174. linkStyle = utils.jsonp(icon.getAttr('__linkStyle'));
  175. icon.setAttr('__segments',utils.jsons(segments));
  176. }
  177. else if (step['attr'] == 'link-style')
  178. {
  179. segments = utils.jsonp(icon.getAttr('__segments'));
  180. linkStyle = step['new_val'];
  181. icon.setAttr('__linkStyle',utils.jsons(linkStyle));
  182. }
  183. else
  184. {
  185. segments = utils.jsonp(icon.getAttr('__segments'));
  186. linkStyle = utils.jsonp(icon.getAttr('__linkStyle'));
  187. }
  188. for( var edgeId in segments )
  189. if( edgeId in __edges )
  190. __redrawEdge(edgeId,segments[edgeId],linkStyle);
  191. else
  192. /* react to the creation of an edge */
  193. __createEdge(
  194. segments[edgeId], linkStyle, edgeId, step['id']);
  195. }
  196. /* CASE 3
  197. NOTE:: certain VisualObject parameters need some special care...
  198. a) position, orientation, scale: these can only be changed by
  199. the csworker layout constraint solver and link decorator
  200. positioner... after each change, the transformation applied
  201. to the vobject is overwritten
  202. b) r/rx/ry: changing these causes relevant shapes to grow
  203. around their center thereby changing their top-left corner
  204. ... we translate the said shapes to compensate
  205. c) Polygons/Stars: see __editPolygon/Star(..)
  206. d) style: pass {...,attr:val,...} hash directly to attr(..)
  207. e) src: make sure URL is valid / make it valid
  208. f) Text VisualObject: see __valignText(..) */
  209. else
  210. {
  211. var matches = step['attr'].match(/.*\/(.*)\/(.*)/),
  212. vid = matches[1],
  213. vobj = __icons[step['id']]['vobjects'][vid],
  214. attr = matches[2];
  215. if(utils.contains(['position','scale','orientation'],attr))
  216. {
  217. var newVal = (utils.isArray(step['new_val']) ?
  218. [__getVobjGeomAttrVal(step['new_val'][0]).latest,
  219. __getVobjGeomAttrVal(step['new_val'][1]).latest] :
  220. parseFloat(
  221. __getVobjGeomAttrVal(step['new_val']).latest) );
  222. if( attr == 'position' )
  223. {
  224. vobj.node.setAttribute('__x',
  225. utils.buildVobjGeomAttrVal(
  226. vobj.node.getAttribute('__x'), newVal[0]) );
  227. vobj.node.setAttribute('__y',
  228. utils.buildVobjGeomAttrVal(
  229. vobj.node.getAttribute('__y'), newVal[1]) );
  230. }
  231. else if( attr == 'scale' )
  232. {
  233. var sx = vobj.node.getAttribute('__sx'),
  234. sy = vobj.node.getAttribute('__sy');
  235. vobj.node.setAttribute('__sx',
  236. utils.buildVobjGeomAttrVal(
  237. sx, newVal[0]*__getVobjGeomAttrVal(sx).initial) );
  238. vobj.node.setAttribute('__sy',
  239. utils.buildVobjGeomAttrVal(
  240. sy, newVal[1]*__getVobjGeomAttrVal(sy).initial) );
  241. }
  242. else
  243. {
  244. var r = vobj.node.getAttribute('__r');
  245. vobj.node.setAttribute('__r',
  246. utils.buildVobjGeomAttrVal(
  247. r, newVal+__getVobjGeomAttrVal(r).initial) );
  248. }
  249. __setVobjectTransform(vobj);
  250. }
  251. if(utils.contains(['r','rx','ry'],attr))
  252. {
  253. var oldVal = vobj.attr(attr) || vobj.node.getAttribute('__'+attr);
  254. offset = step['new_val'] - oldVal;
  255. vobj.translate(
  256. utils.contains(['r','rx'],attr) ? offset : 0,
  257. utils.contains(['r','ry'],attr) ? offset : 0);
  258. }
  259. if( step['id'].match(/\/PolygonIcon\//) &&
  260. utils.contains(['r','sides'],attr) )
  261. __editPolygon(vobj,attr,step['new_val']);
  262. else if( step['id'].match(/\/StarIcon\//) &&
  263. utils.contains(['r','rays'],attr) )
  264. __editStar(vobj,attr,step['new_val']);
  265. try {var newVal = utils.jsonp(step['new_val']);}
  266. catch(err) {var newVal = step['new_val'];}
  267. if( attr == 'style' )
  268. vobj.attr( newVal );
  269. else if( attr == 'src' )
  270. vobj.attr( 'src', __relativizeURL(newVal) );
  271. else
  272. vobj.attr( __ATTR_MAP[attr] || attr, newVal );
  273. if( vobj.type == 'text' )
  274. __valignText(vobj);
  275. }
  276. if( __isSelected(step['id']) )
  277. utils.doAfterUnlessRepeated(
  278. function(selection) {
  279. __select(selection);
  280. }, [__selection['items']], 5
  281. );
  282. }
  283. /* react to loading of an IconDefinition (CS metamodel)
  284. 1. setup and show appropriate toolbar
  285. 2. replace default toolbar button icons by icons described in
  286. the loaded metamodel */
  287. else if( step['op'] == 'LOADMM' )
  288. {
  289. var data = eval('('+step['mm']+')');
  290. GUIUtils.setupAndShowToolbar(
  291. step['name']+'.metamodel',
  292. data,
  293. __METAMODEL_TOOLBAR);
  294. for( var t in data.types )
  295. {
  296. if( t.match(/(.*)Link$/) )
  297. continue;
  298. else if( ! GUIUtils.$$(step['name']+'.metamodel/'+t) )
  299. {
  300. if( !t.match(/(.*)Icon$/) )
  301. console.error('Icon typenames must be "<AbstractType>Icon", the following does not conform :: '+t);
  302. else
  303. console.error('found icon definition for unknwon abstract type :: '+t);
  304. continue;
  305. }
  306. var im =
  307. data.types[t].filter(
  308. function(attr)
  309. {
  310. return attr['name'] == '$contents';
  311. })[0]['default'];
  312. CompileUtils.compileAndDrawIconModel(
  313. im,
  314. Raphael(GUIUtils.$$(step['name']+'.metamodel/'+t)),
  315. {'size':__ICON_SIZE,
  316. 'wrap':true});
  317. GUIUtils.$$(step['name']+'.metamodel/'+t).removeChild(
  318. GUIUtils.$$(step['name']+'.metamodel/'+t).lastChild );
  319. }
  320. }
  321. else if( step['op'] == 'LOADASMM' )
  322. __loadedToolbars[step['name']+'.metamodel'] =
  323. eval('('+step['mm']+')');
  324. else if( step['op'] == 'DUMPMM' )
  325. {
  326. var csmmPath = step['name']+'.metamodel';
  327. GUIUtils.removeToolbar(csmmPath);
  328. }
  329. else if( step['op'] == 'RESETM' )
  330. {
  331. var m = utils.jsonp(step['new_model']);
  332. GUIUtils.resetCanvas();
  333. for( var id in m.nodes )
  334. {
  335. var node = m.nodes[id],
  336. icon = __createIcon(node,id);
  337. if( '$segments' in node )
  338. {
  339. var linkStyle = node['link-style']['value'],
  340. segments = node['$segments']['value'];
  341. icon.setAttr('__segments',utils.jsons(segments));
  342. for( var edgeId in segments )
  343. __createEdge( segments[edgeId], linkStyle, edgeId, id);
  344. }
  345. }
  346. WindowManagement.setWindowTitle(false);
  347. }
  348. else if( step['op'] == 'SYSOUT' )
  349. {
  350. if( step['text'].match(/^ERROR :: /) )
  351. console.error(step['text']);
  352. else if( step['text'].match(/^WARNING :: /) )
  353. console.warn(step['text']);
  354. else if( step['text'].match(/^CLIENT_BDAPI :: /) )
  355. {
  356. var op = utils.jsonp(step['text'].match(/^CLIENT_BDAPI :: (.*)/)[1]);
  357. this[op['func']](op['args']);
  358. WindowManagement.setWindowTitle(true);
  359. }
  360. else
  361. console.log('MESSAGE :: '+step['text']);
  362. }
  363. ///////
  364. else if (step['op'] == 'CHAT')
  365. {
  366. Collaboration.updateChat(step['text']);
  367. }
  368. });
  369. if( isCSWChangelog )
  370. nextSeqNum = __nextCSWSequenceNumber =
  371. utils.incrementSequenceNumber(nextSeqNum);
  372. else
  373. nextSeqNum = __nextASWSequenceNumber =
  374. utils.incrementSequenceNumber(nextSeqNum);
  375. if( pendingChangelogs.length > 0 &&
  376. nextSeqNum == pendingChangelogs[0]['sequence#'] )
  377. {
  378. var pc = pendingChangelogs.shift();
  379. __handleChangelog(
  380. pc['changelog'],
  381. pc['sequence#'],
  382. pc['hitchhiker']);
  383. }
  384. }