file-manager.js 4.1 KB

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