DrawioClient.js 3.1 KB

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