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