1/**
2 * Explore plugin.
3 */
4Draw.loadPlugin(function(editorUi)
5{
6	// Trello plugin only works in embed mode
7	if (editorUi.actions.get('exit') == null)
8	{
9		return;
10	}
11
12	// Overridden to redirect modified check to file
13	editorUi.actions.get('exit').funct = function()
14	{
15		var fn = function()
16		{
17			var parent = window.opener || window.parent;
18			parent.postMessage(JSON.stringify({event: 'exit'}), '*');
19		}
20
21		var file = editorUi.getCurrentFile();
22
23		if (file == null || !file.isModified())
24		{
25			fn();
26		}
27		else
28		{
29			editorUi.confirm(mxResources.get('allChangesLost'), null, fn,
30				mxResources.get('cancel'), mxResources.get('discardChanges'));
31		}
32	};
33
34	editorUi.showSplash = function()
35	{
36		this.actions.get('exit').funct();
37	};
38
39	function main()
40	{
41		var name = (urlParams['filename'] != null) ? decodeURIComponent(urlParams['filename']) : null;
42		var card = (urlParams['card'] != null) ? decodeURIComponent(urlParams['card']) : null;
43		var template = (urlParams['template'] != null) ? decodeURIComponent(urlParams['template']) : null;
44
45		if (name != null && card != null)
46		{
47			var doCreateFile = function(templateData)
48			{
49				editorUi.createFile(name, templateData ||
50					editorUi.getFileData(/(\.xml)$/i.test(name) ||
51					name.indexOf('.') < 0, /(\.svg)$/i.test(name),
52					/(\.html)$/i.test(name)), null, 'trello',
53					null, true, card);
54			};
55
56			if (template != null)
57			{
58				editorUi.trello.getFile(card + editorUi.trello.SEPARATOR +
59					template, function(file)
60				{
61					doCreateFile(file.getData());
62				}, function()
63				{
64					doCreateFile();
65				});
66			}
67			else
68			{
69				doCreateFile();
70			}
71		}
72		else if (window.location.hash.substring(0, 2) == '#T')
73		{
74			editorUi.loadFile(editorUi.getDiagramId(), true);
75		}
76
77		editorUi.addEmbedButtons();
78	};
79
80	// Waits for Trello client
81	if (editorUi.trello == null)
82	{
83		var waitForTrello = function()
84		{
85			if (editorUi.trello != null)
86			{
87				editorUi.removeListener(waitForTrello);
88				main();
89			}
90		};
91
92		// Waits for Trello client to load
93		editorUi.addListener('clientLoaded', waitForTrello);
94	}
95	else
96	{
97		main();
98	}
99});
100