random.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /**
  2. * Text extraction plugin.
  3. */
  4. Draw.loadPlugin(function(ui)
  5. {
  6. var defaultMax = 20;
  7. var defaultDelay = 2000;
  8. var graph = ui.editor.graph;
  9. // Adds resource for action
  10. mxResources.parse('randomLabel=Random Label...');
  11. // Adds action
  12. ui.actions.addAction('randomLabel', function()
  13. {
  14. var cells = graph.getSelectionCells().slice();
  15. if (cells.length > 0)
  16. {
  17. var delay = parseInt(prompt('Delay (ms)', defaultDelay));
  18. if (!isNaN(delay))
  19. {
  20. defaultDelay = delay;
  21. var max = parseInt(prompt('Cycles', defaultMax));
  22. if (!isNaN(max))
  23. {
  24. defaultMax = max;
  25. var counter = 0;
  26. function schedule()
  27. {
  28. var jitter = 1 + 0.5 * (Math.random() - 0.5);
  29. window.setTimeout(function()
  30. {
  31. for (var i = 0; i < cells.length; i++)
  32. {
  33. graph.labelChanged(cells[i], 'Test ' + Math.round(Math.random() * 100));
  34. }
  35. if (ui.dialog != null)
  36. {
  37. console.log('random.js: randomLabel halted');
  38. }
  39. else
  40. {
  41. if (counter++ < max && ui.dialog == null)
  42. {
  43. console.log('random.js: randomLabel', counter);
  44. schedule();
  45. }
  46. else
  47. {
  48. console.log('random.js: randomLabel halted');
  49. }
  50. }
  51. }, delay * jitter);
  52. }
  53. schedule();
  54. }
  55. }
  56. }
  57. else
  58. {
  59. ui.alert(mxResources.get('nothingIsSelected'));
  60. }
  61. });
  62. // Adds resource for action
  63. mxResources.parse('swapChildren=Swap children...');
  64. // Adds action
  65. ui.actions.addAction('swapChildren', function()
  66. {
  67. var cells = graph.getSelectionCells().slice();
  68. if (cells.length > 1)
  69. {
  70. var delay = parseInt(prompt('Delay (ms)', defaultDelay));
  71. if (!isNaN(delay))
  72. {
  73. defaultDelay = delay;
  74. var max = parseInt(prompt('Cycles', defaultMax));
  75. if (!isNaN(max))
  76. {
  77. defaultMax = max;
  78. var counter = 0;
  79. function schedule()
  80. {
  81. var jitter = 1 + 0.5 * (Math.random() - 0.5);
  82. window.setTimeout(function()
  83. {
  84. // assuming parent is the first cell selected
  85. var parentA = cells[0];
  86. var parentB = cells[1];
  87. var childrenA = parentA.children;
  88. var childrenB = parentB.children;
  89. if (childrenA != null && childrenB != null)
  90. {
  91. var numberA = childrenA.length;
  92. var numberB = childrenB.length;
  93. graph.getModel().beginUpdate();
  94. try
  95. {
  96. // permute children
  97. var passes = Math.floor(Math.random() * numberA) + 1;
  98. console.log('random.js: ' + counter + " - swapping " + passes + " children from parent A to parent B");
  99. for (var i = 0; i < passes; i++)
  100. {
  101. // which child to select from parent A
  102. var k = Math.floor(Math.random() * numberA);
  103. // where to insert it to parent B
  104. var l = Math.floor(Math.random() * (numberB + 1));
  105. graph.model.add(parentB, childrenA[k], l);
  106. numberA -= 1;
  107. numberB += 1;
  108. }
  109. var passes = Math.floor(Math.random() * numberB) + 1;
  110. console.log('random.js: ' + counter + " - swapping " + passes + " children from parent B to parent A");
  111. for (var i = 0; i < passes; i++)
  112. {
  113. // which child to select from parent A
  114. var k = Math.floor(Math.random() * numberB);
  115. // where to insert it to parent B
  116. var l = Math.floor(Math.random() * (numberA + 1));
  117. graph.model.add(parentA, childrenB[k], l);
  118. numberA += 1;
  119. numberB -= 1;
  120. }
  121. }
  122. finally
  123. {
  124. graph.getModel().endUpdate();
  125. }
  126. }
  127. if (ui.dialog != null)
  128. {
  129. console.log('random.js: swapChildren halted');
  130. }
  131. else
  132. {
  133. if (counter++ < max && ui.dialog == null)
  134. {
  135. console.log('random.js: swapChildren', counter + '/' + max);
  136. schedule();
  137. }
  138. else
  139. {
  140. console.log('random.js: swapChildren halted');
  141. }
  142. }
  143. }, delay * jitter);
  144. }
  145. schedule();
  146. }
  147. }
  148. }
  149. else
  150. {
  151. ui.alert(mxResources.get('nothingIsSelected'));
  152. }
  153. });
  154. // Adds resource for action
  155. mxResources.parse('placeChildren=Place children...');
  156. // Adds action
  157. ui.actions.addAction('placeChildren', function()
  158. {
  159. var graph = ui.editor.graph;
  160. var cells = graph.getSelectionCells().slice();
  161. if (cells.length > 1)
  162. {
  163. var counter = 0;
  164. var delay = parseInt(prompt('Delay (ms)', defaultDelay));
  165. var max = parseInt(prompt('Cycles', defaultMax));
  166. function schedule()
  167. {
  168. var jitter = 1 + 0.5 * (Math.random() - 0.5);
  169. window.setTimeout(function()
  170. {
  171. // assuming parent is the first cell selected
  172. var parentA = cells[0];
  173. var parentB = cells[1];
  174. var childrenA = parentA.children;
  175. var childrenB = parentB.children;
  176. var numberA = (childrenA != null)? childrenA.length: 0;
  177. var numberB = (childrenB != null)? childrenB.length: 0;
  178. var n = 0;
  179. if (childrenA != null && childrenA.length > 1 || childrenB != null && childrenB.length > 1)
  180. {
  181. graph.getModel().beginUpdate();
  182. try
  183. {
  184. // first, remove a few children from each parent
  185. childrenA = parentA.children;
  186. childrenB = parentB.children;
  187. numberA = (childrenA != null)? childrenA.length: 0;
  188. numberB = (childrenB != null)? childrenB.length: 0;
  189. // 1st parent
  190. n = Math.floor(Math.random() * 4); // how many to delete? at least 1 should remain
  191. n = Math.min(n, Math.max(0, numberA - 2));
  192. console.log(counter + " - removing " + n + " children from parent A");
  193. for (var i = 0; i < n; i++)
  194. {
  195. childrenA = parentA.children;
  196. numberA = (childrenA != null)? childrenA.length: 0;
  197. if (numberA > 0)
  198. {
  199. var k = Math.floor(Math.random() * numberA);
  200. parentA.remove(k);
  201. }
  202. }
  203. // 2nd parent
  204. n = Math.floor(Math.random() * 4); // how many to delete? at least 1 should remain
  205. n = Math.min(n, Math.max(0, numberB - 2));
  206. console.log(counter + " - removing " + n + " children from parent B");
  207. for (var i = 0; i < n; i++)
  208. {
  209. childrenB = parentB.children;
  210. numberB = (childrenB != null)? childrenB.length: 0;
  211. if (numberB > 0)
  212. {
  213. var k = Math.floor(Math.random() * numberB);
  214. parentB.remove(k);
  215. }
  216. }
  217. // second, insert a few children to each parent
  218. childrenA = parentA.children;
  219. childrenB = parentB.children;
  220. numberA = (childrenA != null)? childrenA.length: 0;
  221. numberB = (childrenB != null)? childrenB.length: 0;
  222. // 1st parent
  223. n = Math.floor(Math.random() * 4); // how many to insert?
  224. console.log(counter + " - inserting " + n + " children into parent A");
  225. for (var i = 0; i < n; i++)
  226. {
  227. childrenA = parentA.children;
  228. numberA = (childrenA != null)? childrenA.length: 0;
  229. if (numberA > 0)
  230. {
  231. var k = Math.floor(Math.random() * numberA);
  232. var x = Math.floor(Math.random() * 200);
  233. var y = Math.floor(Math.random() * 50);
  234. var number = Math.floor(Math.random() * 9000 + 1000);
  235. var child = graph.insertVertex(parentA, null, number.toString(), x, y, 120, 30);
  236. parentA.insert(child, k);
  237. }
  238. }
  239. // 2nd parent
  240. n = Math.floor(Math.random() * 4); // how many to insert?
  241. console.log(counter + " - inserting " + n + " children into parent B");
  242. for (var i = 0; i < n; i++)
  243. {
  244. childrenB = parentB.children;
  245. numberB = (childrenB != null)? childrenB.length: 0;
  246. if (numberB > 0)
  247. {
  248. var k = Math.floor(Math.random() * numberB);
  249. var x = Math.floor(Math.random() * 200);
  250. var y = Math.floor(Math.random() * 50);
  251. var number = Math.floor(Math.random() * 9000 + 1000);
  252. var child = graph.insertVertex(parentB, null, number.toString(), x, y, 120, 30);
  253. parentB.insert(child, k);
  254. }
  255. }
  256. // third, shuffle children in each parent
  257. childrenA = parentA.children;
  258. childrenB = parentB.children;
  259. numberA = (childrenA != null)? childrenA.length: 0;
  260. numberB = (childrenB != null)? childrenB.length: 0;
  261. // 1st parent
  262. n = Math.floor(Math.random() * numberA); // how many to shuffle?
  263. console.log(counter + " - moving " + n + " children inside parent A");
  264. for (var i = 0; i < n; i++)
  265. {
  266. childrenA = parentA.children;
  267. numberA = (childrenA != null)? childrenA.length: 0;
  268. if (numberA > 0)
  269. {
  270. var k = Math.floor(Math.random() * numberA); // from index
  271. var l = Math.floor(Math.random() * numberA); // to index
  272. var child = parentA.getChildAt(k);
  273. parentA.insert(child, l);
  274. }
  275. }
  276. // 2nd parent
  277. n = Math.floor(Math.random() * numberB); // how many to shuffle?
  278. console.log(counter + " - moving " + n + " children inside parent B");
  279. for (var i = 0; i < n; i++)
  280. {
  281. childrenB = parentB.children;
  282. numberB = (childrenB != null)? childrenB.length: 0;
  283. if (numberB > 0)
  284. {
  285. var k = Math.floor(Math.random() * numberB); // from index
  286. var l = Math.floor(Math.random() * numberB); // to index
  287. var child = parentB.getChildAt(k);
  288. parentB.insert(child, l);
  289. }
  290. }
  291. // fourth, exchange a few children between both parents
  292. childrenA = parentA.children;
  293. childrenB = parentB.children;
  294. numberA = (childrenA != null)? childrenA.length: 0;
  295. numberB = (childrenB != null)? childrenB.length: 0;
  296. // permute children
  297. var passes = Math.floor(Math.random() * numberA) + 1;
  298. console.log(counter + " - swapping " + passes + " children from parent A to parent B");
  299. for (var i = 0; i < passes; i++)
  300. {
  301. // which child to select from parent A
  302. var k = Math.floor(Math.random() * numberA);
  303. // where to insert it to parent B
  304. var l = Math.floor(Math.random() * (numberB + 1));
  305. graph.model.add(parentB, childrenA[k], l);
  306. numberA -= 1;
  307. numberB += 1;
  308. }
  309. var passes = Math.floor(Math.random() * numberB) + 1;
  310. console.log(counter + " - swapping " + passes + " children from parent B to parent A");
  311. for (var i = 0; i < passes; i++)
  312. {
  313. // which child to select from parent A
  314. var k = Math.floor(Math.random() * numberB);
  315. // where to insert it to parent B
  316. var l = Math.floor(Math.random() * (numberA + 1));
  317. graph.model.add(parentA, childrenB[k], l);
  318. numberA += 1;
  319. numberB -= 1;
  320. }
  321. }
  322. finally
  323. {
  324. graph.getModel().endUpdate();
  325. }
  326. }
  327. if (ui.dialog != null)
  328. {
  329. console.log('placeChildren halted');
  330. }
  331. else
  332. {
  333. if (counter++ < max && ui.dialog == null)
  334. {
  335. console.log('placeChildren', counter);
  336. schedule();
  337. }
  338. else
  339. {
  340. console.log('placeChildren halted');
  341. }
  342. }
  343. }, delay * jitter);
  344. }
  345. schedule();
  346. }
  347. else
  348. {
  349. ui.alert(mxResources.get('nothingIsSelected'));
  350. }
  351. });
  352. // Adds resource for action
  353. mxResources.parse('reorderChildren=Reorder children...');
  354. // Adds action
  355. ui.actions.addAction('reorderChildren', function()
  356. {
  357. var cells = graph.getSelectionCells().slice();
  358. if (cells.length > 0)
  359. {
  360. var delay = parseInt(prompt('Delay (ms)', defaultDelay));
  361. if (!isNaN(delay))
  362. {
  363. defaultDelay = delay;
  364. var max = parseInt(prompt('Cycles', defaultMax));
  365. if (!isNaN(max))
  366. {
  367. defaultMax = max;
  368. var counter = 0;
  369. function schedule()
  370. {
  371. var jitter = 1 + 0.3 * (Math.random() - 0.5);
  372. window.setTimeout(function()
  373. {
  374. graph.getModel().beginUpdate();
  375. try
  376. {
  377. // assuming parent is the first cell selected
  378. for (var i = 0; i < cells.length; i++)
  379. {
  380. var parent = cells[i];
  381. var children = parent.children;
  382. if (children != null && children.length > 1)
  383. {
  384. // permute children
  385. var number = children.length;
  386. var passes = Math.floor(Math.random() * number) + 1;
  387. console.log('random.js: ' + counter + " - reordering in " + passes + " passes");
  388. for (var j = 0; j < passes; j++)
  389. {
  390. var k = Math.floor(Math.random() * number);
  391. graph.orderCells(true, [children[k]]);
  392. }
  393. }
  394. }
  395. }
  396. finally
  397. {
  398. graph.getModel().endUpdate();
  399. }
  400. if (ui.dialog != null)
  401. {
  402. console.log('random.js: reorderChildren halted');
  403. }
  404. else
  405. {
  406. if (counter++ < max && ui.dialog == null)
  407. {
  408. console.log('random.js: reorderChildren', counter + '/' + max);
  409. schedule();
  410. }
  411. else
  412. {
  413. console.log('random.js: reorderChildren halted');
  414. }
  415. }
  416. }, delay * jitter);
  417. }
  418. schedule();
  419. }
  420. }
  421. }
  422. else
  423. {
  424. ui.alert(mxResources.get('nothingIsSelected'));
  425. }
  426. });
  427. var menu = ui.menus.get((urlParams['test'] == '1') ?
  428. 'testDevelop' : 'extras');
  429. var oldFunct = menu.funct;
  430. menu.funct = function(menu, parent)
  431. {
  432. oldFunct.apply(this, arguments);
  433. ui.menus.addMenuItems(menu, ['-', 'randomLabel', 'reorderChildren', 'swapChildren', 'placeChildren'], parent);
  434. };
  435. });