1/**
2 * Copyright (c) 2006-2017, JGraph Ltd
3 * Copyright (c) 2006-2017, Gaudenz Alder
4 */
5TrelloFile = function(ui, data, meta)
6{
7	DrawioFile.call(this, ui, data);
8
9	this.meta = meta;
10	this.saveNeededCounter = 0;
11};
12
13//Extends mxEventSource
14mxUtils.extend(TrelloFile, DrawioFile);
15
16/**
17 *
18 */
19TrelloFile.prototype.getHash = function()
20{
21	return 'T' + encodeURIComponent(this.meta.compoundId);
22};
23
24/**
25 *
26 */
27TrelloFile.prototype.getMode = function()
28{
29	return App.MODE_TRELLO;
30};
31
32/**
33 * Overridden to enable the autosave option in the document properties dialog.
34 */
35TrelloFile.prototype.isAutosave = function()
36{
37	return true;
38};
39
40/**
41 *
42 */
43TrelloFile.prototype.getTitle = function()
44{
45	return this.meta.name;
46};
47
48/**
49 *
50 */
51TrelloFile.prototype.isRenamable = function()
52{
53	return false;
54};
55
56/**
57 * Specifies if notify events should be ignored.
58 */
59TrelloFile.prototype.getSize = function()
60{
61	return this.meta.bytes;
62};
63
64/**
65 *
66 */
67TrelloFile.prototype.save = function(revision, success, error)
68{
69	this.doSave(this.getTitle(), success, error);
70};
71
72/**
73 *
74 */
75TrelloFile.prototype.saveAs = function(title, success, error)
76{
77	this.doSave(title, success, error);
78};
79
80/**
81 *
82 */
83TrelloFile.prototype.doSave = function(title, success, error)
84{
85	// Forces update of data for new extensions
86	var prev = this.meta.name;
87	this.meta.name = title;
88
89	DrawioFile.prototype.save.apply(this, [null, mxUtils.bind(this, function()
90	{
91		this.meta.name = prev;
92		this.saveFile(title, false, success, error);
93	}), error]);
94};
95
96/**
97 *
98 */
99TrelloFile.prototype.saveFile = function(title, revision, success, error)
100{
101	if (!this.isEditable())
102	{
103		if (success != null)
104		{
105			success();
106		}
107	}
108	else if (!this.savingFile)
109	{
110		// Sets shadow modified state during save
111		this.savingFileTime = new Date();
112		this.setShadowModified(false);
113		this.savingFile = true;
114
115		if (this.getTitle() == title)
116		{
117			this.ui.trello.saveFile(this, mxUtils.bind(this, function(meta)
118			{
119				// Checks for changes during save
120				this.setModified(this.getShadowModified());
121				this.savingFile = false;
122				this.meta = meta;
123				this.contentChanged();
124
125				if (success != null)
126				{
127					success();
128				}
129
130				if (this.saveNeededCounter > 0)
131				{
132					this.saveNeededCounter--;
133					this.saveFile(title, revision, success, error);
134				}
135			}),
136			mxUtils.bind(this, function(err)
137			{
138				this.savingFile = false;
139
140				if (error != null)
141				{
142					error(err);
143				}
144			}));
145		}
146		else
147		{
148			this.ui.pickFolder(App.MODE_TRELLO, mxUtils.bind(this, function(cardId)
149			{
150				this.ui.trello.insertFile(title, this.getData(), mxUtils.bind(this, function(file)
151				{
152					this.savingFile = false;
153
154					if (success != null)
155					{
156						success();
157					}
158
159					this.ui.fileLoaded(file);
160
161					if (this.saveNeededCounter > 0)
162					{
163						this.saveNeededCounter--;
164						this.saveFile(title, revision, success, error);
165					}
166				}), mxUtils.bind(this, function()
167				{
168					this.savingFile = false;
169
170					if (error != null)
171					{
172						error();
173					}
174				}), false, cardId);
175			}));
176		}
177	}
178	else if (error != null)
179	{
180		this.saveNeededCounter++;
181		error({code: App.ERROR_BUSY});
182	}
183};
184
185