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