1'use strict';
2
3function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
4function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
5var Loader = require('./loader');
6var _require = require('./precompiled-loader.js'),
7  PrecompiledLoader = _require.PrecompiledLoader;
8var WebLoader = /*#__PURE__*/function (_Loader) {
9  _inheritsLoose(WebLoader, _Loader);
10  function WebLoader(baseURL, opts) {
11    var _this;
12    _this = _Loader.call(this) || this;
13    _this.baseURL = baseURL || '.';
14    opts = opts || {};
15
16    // By default, the cache is turned off because there's no way
17    // to "watch" templates over HTTP, so they are re-downloaded
18    // and compiled each time. (Remember, PRECOMPILE YOUR
19    // TEMPLATES in production!)
20    _this.useCache = !!opts.useCache;
21
22    // We default `async` to false so that the simple synchronous
23    // API can be used when you aren't doing anything async in
24    // your templates (which is most of the time). This performs a
25    // sync ajax request, but that's ok because it should *only*
26    // happen in development. PRECOMPILE YOUR TEMPLATES.
27    _this.async = !!opts.async;
28    return _this;
29  }
30  var _proto = WebLoader.prototype;
31  _proto.resolve = function resolve(from, to) {
32    throw new Error('relative templates not support in the browser yet');
33  };
34  _proto.getSource = function getSource(name, cb) {
35    var _this2 = this;
36    var useCache = this.useCache;
37    var result;
38    this.fetch(this.baseURL + '/' + name, function (err, src) {
39      if (err) {
40        if (cb) {
41          cb(err.content);
42        } else if (err.status === 404) {
43          result = null;
44        } else {
45          throw err.content;
46        }
47      } else {
48        result = {
49          src: src,
50          path: name,
51          noCache: !useCache
52        };
53        _this2.emit('load', name, result);
54        if (cb) {
55          cb(null, result);
56        }
57      }
58    });
59
60    // if this WebLoader isn't running asynchronously, the
61    // fetch above would actually run sync and we'll have a
62    // result here
63    return result;
64  };
65  _proto.fetch = function fetch(url, cb) {
66    // Only in the browser please
67    if (typeof window === 'undefined') {
68      throw new Error('WebLoader can only by used in a browser');
69    }
70    var ajax = new XMLHttpRequest();
71    var loading = true;
72    ajax.onreadystatechange = function () {
73      if (ajax.readyState === 4 && loading) {
74        loading = false;
75        if (ajax.status === 0 || ajax.status === 200) {
76          cb(null, ajax.responseText);
77        } else {
78          cb({
79            status: ajax.status,
80            content: ajax.responseText
81          });
82        }
83      }
84    };
85    url += (url.indexOf('?') === -1 ? '?' : '&') + 's=' + new Date().getTime();
86    ajax.open('GET', url, this.async);
87    ajax.send();
88  };
89  return WebLoader;
90}(Loader);
91module.exports = {
92  WebLoader: WebLoader,
93  PrecompiledLoader: PrecompiledLoader
94};