1/**
2 * Copyright (c) 2006-2017, JGraph Ltd
3 * Copyright (c) 2006-2017, Gaudenz Alder
4 */
5DrawioClient = function(editorUi, cookieName)
6{
7	mxEventSource.call(this);
8
9	this.ui = editorUi;
10	this.cookieName = cookieName;
11	this.token = this.getPersistentToken();
12};
13
14// Extends mxEventSource
15mxUtils.extend(DrawioClient, mxEventSource);
16
17/**
18 * Token for the current user.
19 */
20DrawioClient.prototype.token = null;
21
22/**
23 * Token for the current user.
24 */
25DrawioClient.prototype.user = null;
26
27/**
28 * Authorizes the client, gets the userId and calls <open>.
29 */
30DrawioClient.prototype.setUser = function(user)
31{
32	this.user = user;
33	this.fireEvent(new mxEventObject('userChanged'));
34};
35
36/**
37 * Authorizes the client, gets the userId and calls <open>.
38 */
39DrawioClient.prototype.getUser = function()
40{
41	return this.user;
42};
43
44/**
45 *
46 */
47DrawioClient.prototype.clearPersistentToken = function()
48{
49	if (isLocalStorage)
50	{
51		localStorage.removeItem('.' + this.cookieName);
52		sessionStorage.removeItem('.' + this.cookieName);
53	}
54	else if (typeof(Storage) != 'undefined')
55	{
56		var expiration = new Date();
57		expiration.setYear(expiration.getFullYear() - 1);
58		document.cookie = this.cookieName + '=; expires=' + expiration.toUTCString();
59	}
60};
61
62/**
63 * Authorizes the client, gets the userId and calls <open>.
64 */
65DrawioClient.prototype.getPersistentToken = function(trySessionStorage)
66{
67	var token = null;
68
69	if (isLocalStorage)
70	{
71		token = localStorage.getItem('.' + this.cookieName);
72
73		if (token == null && trySessionStorage)
74		{
75			token = sessionStorage.getItem('.' + this.cookieName);
76		}
77	}
78
79	if (token == null && typeof(Storage) != 'undefined')
80	{
81		var cookies = document.cookie;
82		var name = this.cookieName + '=';
83		var start = cookies.indexOf(name);
84
85		if (start >= 0)
86		{
87			start += name.length;
88			var end = cookies.indexOf(';', start);
89
90			if (end < 0)
91			{
92				end = cookies.length;
93			}
94			else
95			{
96				postCookie = cookies.substring(end);
97		    }
98
99			var value = cookies.substring(start, end);
100			token = (value.length > 0) ? value : null;
101
102			if (token != null && isLocalStorage)
103			{
104				// Moves to local storage
105				var expiry = new Date();
106				expiry.setYear(expiry.getFullYear() - 1);
107				document.cookie = name + '; expires=' + expiry.toUTCString();
108				localStorage.setItem('.' + this.cookieName, token);
109			}
110		}
111	}
112
113	return token;
114};
115
116/**
117 * Authorizes the client, gets the userId and calls <open>.
118 */
119DrawioClient.prototype.setPersistentToken = function(token, sessionOnly)
120{
121	try
122	{
123		if (token != null)
124		{
125			if (isLocalStorage)
126			{
127				if (sessionOnly)
128				{
129					sessionStorage.setItem('.' + this.cookieName, token);
130				}
131				else
132				{
133					localStorage.setItem('.' + this.cookieName, token);
134				}
135			}
136			else if (typeof(Storage) != 'undefined')
137			{
138				var expiration = new Date();
139				expiration.setYear(expiration.getFullYear() + 10);
140				var cookie = this.cookieName + '=' + token + '; path=/' + (sessionOnly? '' : '; expires=' + expiration.toUTCString());
141
142				if (document.location.protocol.toLowerCase() == 'https')
143				{
144					cookie = cookie + ';secure';
145				}
146
147				document.cookie = cookie;
148			}
149		}
150		else
151		{
152			this.clearPersistentToken();
153		}
154	}
155	catch (e)
156	{
157		this.ui.handleError(e);
158	}
159};
160