loadManager.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { GlobalState } from "../globalState";
  2. import { Utilities } from "./utilities";
  3. export class LoadManager {
  4. private _previousHash = "";
  5. public constructor(public globalState: GlobalState) {
  6. // Check the url to prepopulate data
  7. this._checkHash();
  8. window.addEventListener("hashchange", () => this._checkHash());
  9. globalState.onLoadRequiredObservable.add((id) => {
  10. globalState.onDisplayWaitRingObservable.notifyObservers(true);
  11. this._loadPlayground(id);
  12. });
  13. }
  14. private _cleanHash() {
  15. var substr = location.hash[1] === "#" ? 2 : 1;
  16. var splits = decodeURIComponent(location.hash.substr(substr)).split("#");
  17. if (splits.length > 2) {
  18. splits.splice(2, splits.length - 2);
  19. }
  20. location.hash = splits.join("#");
  21. }
  22. private _checkHash() {
  23. let pgHash = "";
  24. if (location.search && (!location.pathname || location.pathname === "/") && !location.hash) {
  25. var query = Utilities.ParseQuery();
  26. if (query.pg) {
  27. pgHash = "#" + query.pg + "#" + (query.revision || "0");
  28. }
  29. } else if (location.hash) {
  30. if (this._previousHash !== location.hash) {
  31. this._cleanHash();
  32. pgHash = location.hash;
  33. }
  34. } else if (location.pathname) {
  35. const pgMatch = location.pathname.match(/\/pg\/(.*)/);
  36. const withRevision = location.pathname.match(/\/pg\/(.*)\/revision\/(\d*)/);
  37. if (pgMatch || withRevision) {
  38. if (withRevision) {
  39. pgHash = "#" + withRevision[1] + "#" + withRevision[2];
  40. } else {
  41. pgHash = "#" + pgMatch![1] + "#0";
  42. }
  43. }
  44. }
  45. if (pgHash) {
  46. var match = pgHash.match(/^(#[A-Za-z\d]*)(%23)([\d]+)$/);
  47. if (match) {
  48. pgHash = match[1] + "#" + match[3];
  49. parent.location.hash = pgHash;
  50. }
  51. this._previousHash = pgHash;
  52. this._loadPlayground(pgHash.substr(1));
  53. }
  54. }
  55. private _loadPlayground(id: string) {
  56. this.globalState.loadingCodeInProgress = true;
  57. try {
  58. var xmlHttp = new XMLHttpRequest();
  59. xmlHttp.onreadystatechange = () => {
  60. if (xmlHttp.readyState === 4) {
  61. if (xmlHttp.status === 200) {
  62. if (xmlHttp.responseText.indexOf("class Playground") !== -1) {
  63. if (this.globalState.language === "JS") {
  64. Utilities.SwitchLanguage("TS", this.globalState);
  65. }
  66. } else {
  67. // If we're loading JS content and it's TS page
  68. if (this.globalState.language === "TS") {
  69. Utilities.SwitchLanguage("JS", this.globalState);
  70. }
  71. }
  72. var snippet = JSON.parse(xmlHttp.responseText);
  73. // Check if title / descr / tags are already set
  74. if (snippet.name != null && snippet.name != "") {
  75. this.globalState.currentSnippetTitle = snippet.name;
  76. } else {
  77. this.globalState.currentSnippetTitle = "";
  78. }
  79. if (snippet.description != null && snippet.description != "") {
  80. this.globalState.currentSnippetDescription = snippet.description;
  81. } else {
  82. this.globalState.currentSnippetDescription = "";
  83. }
  84. if (snippet.tags != null && snippet.tags != "") {
  85. this.globalState.currentSnippetTags = snippet.tags;
  86. } else {
  87. this.globalState.currentSnippetTags = "";
  88. }
  89. this.globalState.onCodeLoaded.notifyObservers(JSON.parse(snippet.jsonPayload).code.toString());
  90. this.globalState.onMetadataUpdatedObservable.notifyObservers();
  91. }
  92. }
  93. };
  94. if (id[0] === "#") {
  95. id = id.substr(1);
  96. }
  97. this.globalState.currentSnippetToken = id.split("#")[0];
  98. if (!id.split("#")[1]) {
  99. id += "#0";
  100. }
  101. xmlHttp.open("GET", this.globalState.SnippetServerUrl + "/" + id.replace(/#/g, "/"));
  102. xmlHttp.send();
  103. } catch (e) {
  104. this.globalState.loadingCodeInProgress = false;
  105. this.globalState.onCodeLoaded.notifyObservers("");
  106. }
  107. }
  108. }