UNPKG

cabbie-async

Version:
284 lines (211 loc) 8.47 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _regenerator = require('babel-runtime/regenerator'); var _regenerator2 = _interopRequireDefault(_regenerator); var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _options = require('./flow-types/options'); var _driver = require('./driver'); var _driver2 = _interopRequireDefault(_driver); var _url = require('url'); var _addDebugging = require('./add-debugging'); var _addDebugging2 = _interopRequireDefault(_addDebugging); var _baseClass = require('./base-class'); var _baseClass2 = _interopRequireDefault(_baseClass); var _flowRuntime = require('flow-runtime'); var _flowRuntime2 = _interopRequireDefault(_flowRuntime); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * * This file is generated automatically, run npm run build to re-generate. **/ var Driver = _flowRuntime2.default.tdz(function () { return _driver2.default; }); /* * Navigation object * * @deprecated These methods all now live directly on the "ActiveWindow" object. */ var Options = _flowRuntime2.default.tdz(function () { return _options.Options; }); var Navigator = function (_BaseClass) { (0, _inherits3.default)(Navigator, _BaseClass); function Navigator(driver /*: Driver*/, options /*: Options*/) { (0, _classCallCheck3.default)(this, Navigator); var _driverType = _flowRuntime2.default.ref(Driver); var _optionsType = _flowRuntime2.default.ref(Options); _flowRuntime2.default.param('driver', _driverType).assert(driver); _flowRuntime2.default.param('options', _optionsType).assert(options); var _this = (0, _possibleConstructorReturn3.default)(this, (Navigator.__proto__ || (0, _getPrototypeOf2.default)(Navigator)).call(this, driver)); _this._options = options; return _this; } /* * Navigate forwards in the browser history, if possible. */ (0, _createClass3.default)(Navigator, [{ key: 'forward', value: function forward() /*: Promise<void>*/ { var _returnType; return _regenerator2.default.async(function forward$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _returnType = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.void(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.void()))); _context.next = 3; return _regenerator2.default.awrap(this.requestJSON('POST', '/forward')); case 3: case 'end': return _context.stop(); } } }, null, this); } /* * Navigate backwards in the browser history, if possible. */ }, { key: 'backward', value: function backward() /*: Promise<void>*/ { var _returnType2; return _regenerator2.default.async(function backward$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _returnType2 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.void(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.void()))); _context2.next = 3; return _regenerator2.default.awrap(this.requestJSON('POST', '/back')); case 3: case 'end': return _context2.stop(); } } }, null, this); } /* * Refreshes the browser */ }, { key: 'refresh', value: function refresh() /*: Promise<void>*/ { var _returnType3; return _regenerator2.default.async(function refresh$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: _returnType3 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.void(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.void()))); _context3.next = 3; return _regenerator2.default.awrap(this.requestJSON('POST', '/refresh')); case 3: case 'end': return _context3.stop(); } } }, null, this); } /* * Get the current url that the browser is displaying */ }, { key: 'getUrl', value: function getUrl() /*: Promise<string>*/ { var _returnType4; return _regenerator2.default.async(function getUrl$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: _returnType4 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.string(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.string()))); _context4.t0 = _returnType4; _context4.next = 4; return _regenerator2.default.awrap(this.requestJSON('GET', '/url')); case 4: _context4.t1 = _context4.sent; return _context4.abrupt('return', _context4.t0.assert.call(_context4.t0, _context4.t1)); case 6: case 'end': return _context4.stop(); } } }, null, this); } /* * Navigates the browser to the specified path * * - if `path` begins with a "/" it is relative to `options.base` * - if `path` begins with "http" it is absolute * - otherwise it is relative to the current path */ }, { key: 'navigateTo', value: function navigateTo(path /*: string*/) /*: Promise<void>*/ { var _pathType, _returnType5, base, _base; return _regenerator2.default.async(function navigateTo$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: _pathType = _flowRuntime2.default.string(); _returnType5 = _flowRuntime2.default.return(_flowRuntime2.default.union(_flowRuntime2.default.void(), _flowRuntime2.default.ref('Promise', _flowRuntime2.default.void()))); _flowRuntime2.default.param('path', _pathType).assert(path); if (!(path[0] === '/')) { _context5.next = 10; break; } base = this._options.base; if (base) { _context5.next = 7; break; } throw new Error('You must provide a "base" option to use urls starting with "/"'); case 7: path = _pathType.assert(base.replace(/\/$/, '') + path); _context5.next = 17; break; case 10: if (!(path.indexOf('http') !== 0)) { _context5.next = 17; break; } _context5.next = 13; return _regenerator2.default.awrap(this.getUrl()); case 13: _base = _context5.sent; _context5.next = 16; return _regenerator2.default.awrap(this.navigateTo((0, _url.resolve)(_base, path))); case 16: return _context5.abrupt('return', _returnType5.assert()); case 17: _context5.next = 19; return _regenerator2.default.awrap(this.driver.requestJSON('POST', '/url', { url: path })); case 19: case 'end': return _context5.stop(); } } }, null, this); } }]); return Navigator; }(_baseClass2.default); /* * Navigates the browser to the specified path * * Alias for `navigateTo` * * @method setUrl * @param {String} path */ _flowRuntime2.default.any().assert(Navigator.prototype).setUrl = Navigator.prototype.navigateTo; (0, _addDebugging2.default)(Navigator); exports.default = Navigator;