api.js 9.07 KB
Newer Older
Sergey Luzyanin's avatar
Sergey Luzyanin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 *
 * (c) Copyright Ascensio System Limited 2010-2016
 *
 * This program is freeware. You can redistribute it and/or modify it under the terms of the GNU 
 * General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html). 
 * In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that 
 * Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
 *
 * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
 * FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
 *
 * You can contact Ascensio System SIA by email at sales@onlyoffice.com
 *
 * The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display 
 * Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
 *
 * Pursuant to Section 7  3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains 
 * relevant author attributions when distributing the software. If the display of the logo in its graphic 
 * form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE" 
 * in every copy of the program you distribute. 
 * Pursuant to Section 7  3(e) we decline to grant you any rights under trademark law for use of our trademarks.
 *
*/
25 26
"use strict";

27 28 29
// Import
var c_oAscError = Asc.c_oAscError;

30 31 32
/////////////////////////////////////////////////////////
//////////////        OPEN       ////////////////////////
/////////////////////////////////////////////////////////
33
Asc['asc_docs_api'].prototype._OfflineAppDocumentStartLoad = function()
34
{
35 36 37
	this.asc_registerCallback('asc_onDocumentContentReady', function(){
		DesktopOfflineUpdateLocalName(editor);
	});
38 39

	AscCommon.History.UserSaveMode = true;
40 41
    window["AscDesktopEditor"]["LocalStartOpen"]();
};
42
Asc['asc_docs_api'].prototype._OfflineAppDocumentEndLoad = function(_url, _data)
43
{
44
	AscCommon.g_oIdCounter.m_sUserId = window["AscDesktopEditor"]["CheckUserId"]();
Oleg.Korshul's avatar
 
Oleg.Korshul committed
45 46
	if (_data == "")
	{
47
		this.sendEvent("asc_onError", c_oAscError.ID.ConvertationError, c_oAscError.Level.Critical);
Oleg.Korshul's avatar
 
Oleg.Korshul committed
48 49
		return;
	}
50
    if (AscCommon.c_oSerFormat.Signature !== _data.substring(0, AscCommon.c_oSerFormat.Signature.length))
51
	{
Oleg.Korshul's avatar
 
Oleg.Korshul committed
52
		this.OpenDocument(_url, _data);
53
	}
54
    else
55
	{
Oleg.Korshul's avatar
 
Oleg.Korshul committed
56
		this.OpenDocument2(_url, _data);
57 58 59
		this.WordControl.m_oLogicDocument.Set_FastCollaborativeEditing(false);
	}
	DesktopOfflineUpdateLocalName(this);
60 61 62
};
window["DesktopOfflineAppDocumentEndLoad"] = function(_url, _data)
{
63 64
	AscCommon.g_oDocumentUrls.documentUrl = _url;
	if (AscCommon.g_oDocumentUrls.documentUrl.indexOf("file:") != 0)
Oleg.Korshul's avatar
Oleg.Korshul committed
65
	{
66 67 68
		if (AscCommon.g_oDocumentUrls.documentUrl.indexOf("/") != 0)
			AscCommon.g_oDocumentUrls.documentUrl = "/" + AscCommon.g_oDocumentUrls.documentUrl;
		AscCommon.g_oDocumentUrls.documentUrl = "file://" + AscCommon.g_oDocumentUrls.documentUrl;
Oleg.Korshul's avatar
Oleg.Korshul committed
69 70
	}
	
Oleg.Korshul's avatar
 
Oleg.Korshul committed
71
    editor._OfflineAppDocumentEndLoad(_url, _data);
72 73
};

74
Asc['asc_docs_api'].prototype.asc_setAdvancedOptions = function(idOption, option) 
75 76 77
{
	window["AscDesktopEditor"]["SetAdvancedOptions"]("" + option.asc_getCodePage());
};
78
Asc['asc_docs_api'].prototype["asc_setAdvancedOptions"] = Asc['asc_docs_api'].prototype.asc_setAdvancedOptions;
79 80 81 82 83 84

window["asc_initAdvancedOptions"] = function()
{	
	editor._onNeedParams(undefined);
};

85 86 87
/////////////////////////////////////////////////////////
//////////////        CHANGES       /////////////////////
/////////////////////////////////////////////////////////
88
AscCommon.CHistory.prototype.Reset_SavedIndex = function(IsUserSave)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
{
	if (true === this.Is_UserSaveMode())
	{
		this.SavedIndex = this.Index;
		if (true === IsUserSave)
		{
			this.UserSavedIndex = this.Index;
			this.ForceSave      = false;
		}
	}
	else
	{
		this.SavedIndex = this.Index;
		this.ForceSave  = false;
	}
};
105
AscCommon.CHistory.prototype.Have_Changes = function(IsNotUserSave, IsNoSavedNoModifyed)
106
{
Oleg.Korshul's avatar
Oleg.Korshul committed
107
	if (true === this.Is_UserSaveMode() && true !== IsNotUserSave)
108 109 110
	{
		if (-1 === this.Index && null === this.UserSavedIndex && false === this.ForceSave)
		{
111 112 113 114
			if (window["AscDesktopEditor"])
			{
				if (0 != window["AscDesktopEditor"]["LocalFileGetOpenChangesCount"]())
					return true;
Oleg.Korshul's avatar
Oleg.Korshul committed
115
				if (!window["AscDesktopEditor"]["LocalFileGetSaved"]() && IsNoSavedNoModifyed !== true)
116 117
					return true;
			}
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
			return false;
		}

		if (this.Index != this.UserSavedIndex || true === this.ForceSave)
			return true;

		return false;
	}
	else
	{
		if (-1 === this.Index && null === this.SavedIndex && false === this.ForceSave)
			return false;

		if (this.Index != this.SavedIndex || true === this.ForceSave)
			return true;

		return false;
	}
};
	
138 139
window["DesktopOfflineAppDocumentApplyChanges"] = function(_changes)
{
140
	editor._coAuthoringSetChanges(_changes, new AscCommonWord.CDocumentColor( 191, 255, 199 ));
Oleg.Korshul's avatar
 
Oleg.Korshul committed
141 142
    //editor["asc_nativeApplyChanges"](_changes);
	//editor["asc_nativeCalculateFile"]();
143 144 145 146 147
};

/////////////////////////////////////////////////////////
////////////////        SAVE       //////////////////////
/////////////////////////////////////////////////////////
148
Asc['asc_docs_api'].prototype.SetDocumentModified = function(bValue)
Oleg.Korshul's avatar
Oleg.Korshul committed
149 150 151 152 153 154
{
    this.isDocumentModify = bValue;
    this.asc_fireCallback("asc_onDocumentModifiedChanged");

    if (undefined !== window["AscDesktopEditor"])
    {
155
        window["AscDesktopEditor"]["onDocumentModifiedChanged"](AscCommon.History ? AscCommon.History.Have_Changes(undefined, true) : bValue);
Oleg.Korshul's avatar
Oleg.Korshul committed
156 157 158
    }
};

159
Asc['asc_docs_api'].prototype.asc_Save = function (isNoUserSave, isSaveAs)
160 161 162
{
    if (true !== isNoUserSave)
        this.IsUserSave = true;
Oleg.Korshul's avatar
Oleg.Korshul committed
163 164 165
	
	if (this.IsUserSave)
	{
166
		this.LastUserSavedIndex = AscCommon.History.UserSavedIndex;
Oleg.Korshul's avatar
Oleg.Korshul committed
167
	}
168

169
    if (true === this.canSave && !this.isLongAction())
170 171 172 173
	{
		var _isNaturalSave = this.IsUserSave;
		this.canSave = false;
		
174 175
		if (this.WordControl.m_oLogicDocument != null)
		{
176 177 178 179
			var t = this;
			this.CoAuthoringApi.askSaveChanges(function(e) {
				t.onSaveCallback(e);
			});
180 181 182 183 184 185 186 187
			
			if (this.CoAuthoringApi.onUnSaveLock)
				this.CoAuthoringApi.onUnSaveLock();
		}
		else
		{
			this.canSave = true;
		}
188 189
		
		if (_isNaturalSave === true)
Oleg.Korshul's avatar
Oleg.Korshul committed
190
			window["DesktopOfflineAppDocumentStartSave"](isSaveAs);
191 192
	}
};
Oleg.Korshul's avatar
Oleg.Korshul committed
193
window["DesktopOfflineAppDocumentStartSave"] = function(isSaveAs)
194
{
195
    editor.sync_StartAction(Asc.c_oAscAsyncActionType.BlockInteraction, Asc.c_oAscAsyncAction.Save);
196 197
	
	var _param = "";
Oleg.Korshul's avatar
Oleg.Korshul committed
198
	if (isSaveAs === true)
199
		_param += "saveas=true;";
200
	if (AscCommon.AscBrowser.isRetina)
201 202 203
		_param += "retina=true;";
	
	window["AscDesktopEditor"]["LocalFileSave"](_param);
204
};
205
window["DesktopOfflineAppDocumentEndSave"] = function(error)
206
{
207
	editor.sync_EndAction(Asc.c_oAscAsyncActionType.BlockInteraction, Asc.c_oAscAsyncAction.Save);
208
	if (error == 0)
209
		DesktopOfflineUpdateLocalName(editor);
Oleg.Korshul's avatar
Oleg.Korshul committed
210
	else
211
		AscCommon.History.UserSavedIndex = editor.LastUserSavedIndex;
Oleg.Korshul's avatar
Oleg.Korshul committed
212
	
Oleg.Korshul's avatar
 
Oleg.Korshul committed
213
	editor.UpdateInterfaceState();
Oleg.Korshul's avatar
Oleg.Korshul committed
214
	editor.LastUserSavedIndex = undefined;
215 216
	
	if (2 == error)
217
		editor.sendEvent("asc_onError", c_oAscError.ID.ConvertationError, c_oAscError.Level.NoCritical);
218
};
219
Asc['asc_docs_api'].prototype.asc_DownloadAs = function(typeFile, bIsDownloadEvent) 
Oleg.Korshul's avatar
Oleg.Korshul committed
220
{
Oleg.Korshul's avatar
Oleg.Korshul committed
221
	this.asc_Save(false, true);
Oleg.Korshul's avatar
Oleg.Korshul committed
222
};
223

224
Asc['asc_docs_api'].prototype.AddImageUrl = function(url, imgProp)
225 226
{
	var _url = window["AscDesktopEditor"]["LocalFileGetImageUrl"](url);
227
	this.AddImageUrlAction(AscCommon.g_oDocumentUrls.getImageUrl(_url), imgProp);
228
};
229
Asc['asc_docs_api'].prototype.AddImage = function()
230 231 232
{
	window["AscDesktopEditor"]["LocalFileGetImageUrlFromOpenFileDialog"]();
};
233
Asc['asc_docs_api'].prototype.asc_addImage = function()
234 235 236
{
  window["AscDesktopEditor"]["LocalFileGetImageUrlFromOpenFileDialog"]();
};
237
Asc['asc_docs_api'].prototype.asc_isOffline = function()
238 239 240
{
	return true;
};
Oleg.Korshul's avatar
Oleg.Korshul committed
241

Sergey Luzyanin's avatar
Sergey Luzyanin committed
242 243


244 245 246 247 248 249 250
Asc['asc_docs_api'].prototype["asc_addImage"] = Asc['asc_docs_api'].prototype.asc_addImage;
Asc['asc_docs_api'].prototype["AddImageUrl"] = Asc['asc_docs_api'].prototype.AddImageUrl;
Asc['asc_docs_api'].prototype["AddImage"] = Asc['asc_docs_api'].prototype.AddImage;
Asc['asc_docs_api'].prototype["asc_Save"] = Asc['asc_docs_api'].prototype.asc_Save;
Asc['asc_docs_api'].prototype["asc_DownloadAs"] = Asc['asc_docs_api'].prototype.asc_DownloadAs;
Asc['asc_docs_api'].prototype["asc_isOffline"] = Asc['asc_docs_api'].prototype.asc_isOffline;
Asc['asc_docs_api'].prototype["SetDocumentModified"] = Asc['asc_docs_api'].prototype.SetDocumentModified;
Sergey Luzyanin's avatar
Sergey Luzyanin committed
251

252 253 254 255 256 257

window["DesktopOfflineAppDocumentAddImageEnd"] = function(url)
{
	if (url == "")
		return;
	var _url = window["AscDesktopEditor"]["LocalFileGetImageUrl"](url);
258
	editor.AddImageUrlAction(AscCommon.g_oDocumentUrls.getImageUrl(_url));
Oleg.Korshul's avatar
Oleg.Korshul committed
259 260
};

Oleg.Korshul's avatar
Oleg.Korshul committed
261
window["on_editor_native_message"] = function(sCommand, sParam)
Oleg.Korshul's avatar
Oleg.Korshul committed
262 263 264 265 266 267 268 269 270 271
{
	if (!window.editor)
		return;
	
	if (sCommand == "save")
		editor.asc_Save();
	else if (sCommand == "saveAs")
		editor.asc_Save(false, true);
	else if (sCommand == "print")
		editor.asc_Print();
272
};