hy-push-state
Version:
Turn static web sites into dynamic web apps
177 lines (146 loc) • 8.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.eventMixin = void 0;
var _esm = require("rxjs/_esm5");
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function () { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
// ### Event functions
// These functions are called at various points along the observable pipeline to fire events,
// and cause other side effects.
var eventMixin = function eventMixin(C) {
return /*#__PURE__*/function (_C) {
_inherits(_class, _C);
var _super = _createSuper(_class);
function _class() {
_classCallCheck(this, _class);
return _super.apply(this, arguments);
}
_createClass(_class, [{
key: "onStart",
// #### On start
value: function onStart(context) {
var _this = this;
// By default, hy-push-state will wait at least `duration` ms before replacing the content,
// so that animations have enough time to finish.
// The behavior is encoded with a promise that resolves after `duration` ms.
this.animPromise = (0, _esm.timer)(this.duration); // The `transitionUntil` function lets users of this component override the animation promise.
// This allows for event-based code execution, rather than timing-based, which prevents hiccups
// and glitches when, for example, painting takes longer than expected.
var transitionUntil = function transitionUntil(promise) {
if (process.env.DEBUG && !(promise instanceof Promise || promise instanceof _esm.Observable)) {
console.warn("transitionUntil expects a Promise as first argument.");
}
_this.animPromise = promise;
};
this.fireEvent("start", {
detail: Object.assign(context, {
transitionUntil: transitionUntil
})
});
} // Example usage of `transitionUntil`:
//
// ```js
// hyPushStateEl.addEventListener('hy-push-state-start', ({ detail }) => {
// const animPromise = new Promise((resolve) => {
// const anim = myContent.animate(...);
// anim.addEventListener('finish', resolve);
// });
// detail.transitionUntil(animPromise);
// });
// ```
// {:style="font-style:italic"}
// #### Error callbacks
// This function handles errors while trying to insert the new content into the document.
// If the retrieved documened doesn't contain the ids we are looking for
// we can't insert the content dynamically, so we tell the browser to open the link directly.
}, {
key: "onDOMError",
value: function onDOMError(context) {
var replaceElMissing = context.replaceElMissing,
url = context.url; // Ideally you should prevent this situation by adding the
// `no-push-state` CSS class
// on links to documents that don't match the expected document layout.
// This only serves as a fallback.
if (replaceElMissing) {
if (process.env.DEBUG) {
var ids = this.replaceIds.concat(this.el.id || []).map(function (x) {
return "#".concat(x);
}).join(", ");
console.warn("Couldn't find one or more ids of '".concat(ids, "' in the document at '").concat(window.location, "'. Opening the link directly."));
} // To open the link directly, we first pop one entry off the browser history.
// We have to do this because (some) browsers won't handle the back button correctly otherwise.
// We then wait for a short time and change the document's location.
// TODO: If we didn't call `pushState` optimistically we wouldn't have to do this.
window.history.back();
setTimeout(function () {
document.location.href = url;
}, 100); // If it's a different error, throw the generic `error` event.
} else {
if (process.env.DEBUG) console.error(context);
this.fireEvent("error", {
detail: context
});
}
} // If there is a network error during (pre-) fetching, fire `networkerror` event.
}, {
key: "onNetworkError",
value: function onNetworkError(context) {
if (process.env.DEBUG) console.error(context);
this.fireEvent("networkerror", {
detail: context
});
} // When using the experimental script feature,
// fire `scripterror` event if something goes wrong during script insertion.
}, {
key: "onError",
value: function onError(context) {
if (process.env.DEBUG) console.error(context);
this.fireEvent("error", {
detail: context
});
} // #### Others
// These event callbacks simply fire an event and pass the context as `detail`.
}, {
key: "onReady",
value: function onReady(context) {
this.fireEvent("ready", {
detail: context
});
}
}, {
key: "onAfter",
value: function onAfter(context) {
this.fireEvent("after", {
detail: context
});
}
}, {
key: "onProgress",
value: function onProgress(context) {
this.fireEvent("progress", {
detail: context
});
}
}, {
key: "onLoad",
value: function onLoad(context) {
this.fireEvent("load", {
detail: context
});
}
}]);
return _class;
}(C);
};
exports.eventMixin = eventMixin;