file-manager.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. /* global window, XMLHttpRequest */
  3. var __extends = (this && this.__extends) || (function () {
  4. var extendStatics = function (d, b) {
  5. extendStatics = Object.setPrototypeOf ||
  6. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  7. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  8. return extendStatics(d, b);
  9. };
  10. return function (d, b) {
  11. extendStatics(d, b);
  12. function __() { this.constructor = d; }
  13. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  14. };
  15. })();
  16. var __importDefault = (this && this.__importDefault) || function (mod) {
  17. return (mod && mod.__esModule) ? mod : { "default": mod };
  18. };
  19. Object.defineProperty(exports, "__esModule", { value: true });
  20. var abstract_file_manager_js_1 = __importDefault(require("../less/environment/abstract-file-manager.js"));
  21. var options;
  22. var logger;
  23. var fileCache = {};
  24. // TODOS - move log somewhere. pathDiff and doing something similar in node. use pathDiff in the other browser file for the initial load
  25. var FileManager = /** @class */ (function (_super) {
  26. __extends(FileManager, _super);
  27. function FileManager() {
  28. return _super !== null && _super.apply(this, arguments) || this;
  29. }
  30. FileManager.prototype.alwaysMakePathsAbsolute = function () {
  31. return true;
  32. };
  33. FileManager.prototype.join = function (basePath, laterPath) {
  34. if (!basePath) {
  35. return laterPath;
  36. }
  37. return this.extractUrlParts(laterPath, basePath).path;
  38. };
  39. FileManager.prototype.doXHR = function (url, type, callback, errback) {
  40. var xhr = new XMLHttpRequest();
  41. var async = options.isFileProtocol ? options.fileAsync : true;
  42. if (typeof xhr.overrideMimeType === 'function') {
  43. xhr.overrideMimeType('text/css');
  44. }
  45. logger.debug("XHR: Getting '" + url + "'");
  46. xhr.open('GET', url, async);
  47. xhr.setRequestHeader('Accept', type || 'text/x-less, text/css; q=0.9, */*; q=0.5');
  48. xhr.send(null);
  49. function handleResponse(xhr, callback, errback) {
  50. if (xhr.status >= 200 && xhr.status < 300) {
  51. callback(xhr.responseText, xhr.getResponseHeader('Last-Modified'));
  52. }
  53. else if (typeof errback === 'function') {
  54. errback(xhr.status, url);
  55. }
  56. }
  57. if (options.isFileProtocol && !options.fileAsync) {
  58. if (xhr.status === 0 || (xhr.status >= 200 && xhr.status < 300)) {
  59. callback(xhr.responseText);
  60. }
  61. else {
  62. errback(xhr.status, url);
  63. }
  64. }
  65. else if (async) {
  66. xhr.onreadystatechange = function () {
  67. if (xhr.readyState == 4) {
  68. handleResponse(xhr, callback, errback);
  69. }
  70. };
  71. }
  72. else {
  73. handleResponse(xhr, callback, errback);
  74. }
  75. };
  76. FileManager.prototype.supports = function () {
  77. return true;
  78. };
  79. FileManager.prototype.clearFileCache = function () {
  80. fileCache = {};
  81. };
  82. FileManager.prototype.loadFile = function (filename, currentDirectory, options, environment) {
  83. // TODO: Add prefix support like less-node?
  84. // What about multiple paths?
  85. if (currentDirectory && !this.isPathAbsolute(filename)) {
  86. filename = currentDirectory + filename;
  87. }
  88. filename = options.ext ? this.tryAppendExtension(filename, options.ext) : filename;
  89. options = options || {};
  90. // sheet may be set to the stylesheet for the initial load or a collection of properties including
  91. // some context variables for imports
  92. var hrefParts = this.extractUrlParts(filename, window.location.href);
  93. var href = hrefParts.url;
  94. var self = this;
  95. return new Promise(function (resolve, reject) {
  96. if (options.useFileCache && fileCache[href]) {
  97. try {
  98. var lessText = fileCache[href];
  99. return resolve({ contents: lessText, filename: href, webInfo: { lastModified: new Date() } });
  100. }
  101. catch (e) {
  102. return reject({ filename: href, message: "Error loading file " + href + " error was " + e.message });
  103. }
  104. }
  105. self.doXHR(href, options.mime, function doXHRCallback(data, lastModified) {
  106. // per file cache
  107. fileCache[href] = data;
  108. // Use remote copy (re-parse)
  109. resolve({ contents: data, filename: href, webInfo: { lastModified: lastModified } });
  110. }, function doXHRError(status, url) {
  111. reject({ type: 'File', message: "'" + url + "' wasn't found (" + status + ")", href: href });
  112. });
  113. });
  114. };
  115. return FileManager;
  116. }(abstract_file_manager_js_1.default));
  117. exports.default = (function (opts, log) {
  118. options = opts;
  119. logger = log;
  120. return FileManager;
  121. });
  122. //# sourceMappingURL=file-manager.js.map