DrawioClient.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. }
  46. else if (typeof(Storage) != 'undefined')
  47. {
  48. var expiration = new Date();
  49. expiration.setYear(expiration.getFullYear() - 1);
  50. document.cookie = this.cookieName + '=; expires=' + expiration.toUTCString();
  51. }
  52. };
  53. /**
  54. * Authorizes the client, gets the userId and calls <open>.
  55. */
  56. DrawioClient.prototype.getPersistentToken = function()
  57. {
  58. var token = null;
  59. if (isLocalStorage)
  60. {
  61. token = localStorage.getItem('.' + this.cookieName);
  62. }
  63. if (token == null && typeof(Storage) != 'undefined')
  64. {
  65. var cookies = document.cookie;
  66. var name = this.cookieName + '=';
  67. var start = cookies.indexOf(name);
  68. if (start >= 0)
  69. {
  70. start += name.length;
  71. var end = cookies.indexOf(';', start);
  72. if (end < 0)
  73. {
  74. end = cookies.length;
  75. }
  76. else
  77. {
  78. postCookie = cookies.substring(end);
  79. }
  80. var value = cookies.substring(start, end);
  81. token = (value.length > 0) ? value : null;
  82. if (token != null && isLocalStorage)
  83. {
  84. // Moves to local storage
  85. var expiry = new Date();
  86. expiry.setYear(expiry.getFullYear() - 1);
  87. document.cookie = name + '; expires=' + expiry.toUTCString();
  88. localStorage.setItem('.' + this.cookieName, token);
  89. }
  90. }
  91. }
  92. return token;
  93. };
  94. /**
  95. * Authorizes the client, gets the userId and calls <open>.
  96. */
  97. DrawioClient.prototype.setPersistentToken = function(token)
  98. {
  99. if (token != null)
  100. {
  101. if (isLocalStorage)
  102. {
  103. localStorage.setItem('.' + this.cookieName, token);
  104. }
  105. else if (typeof(Storage) != 'undefined')
  106. {
  107. var expiration = new Date();
  108. expiration.setYear(expiration.getFullYear() + 10);
  109. var cookie = this.cookieName + '=' + token +'; path=/; expires=' + expiration.toUTCString();
  110. if (document.location.protocol.toLowerCase() == 'https')
  111. {
  112. cookie = cookie + ';secure';
  113. }
  114. document.cookie = cookie;
  115. }
  116. }
  117. else
  118. {
  119. this.clearPersistentToken();
  120. }
  121. };