UNPKG

devextreme

Version:

HTML5 JavaScript Component Suite for Responsive Web Development

213 lines (164 loc) 5.57 kB
"use strict"; require("../integration/jquery"); var $ = require("jquery"), Class = require("../core/class"), Callbacks = require("../core/utils/callbacks"), window = require("../core/utils/window").getWindow(), queue = require("../core/utils/queue"); var ROOT_PAGE_URL = "__root__", BUGGY_ANDROID_BUFFER_PAGE_URL = "__buffer__"; var DefaultBrowserAdapter = Class.inherit({ ctor: function ctor(options) { options = options || {}; this._window = options.window || window; this.popState = Callbacks(); $(this._window).on("hashchange", this._onHashChange.bind(this)); this._tasks = queue.create(); this.canWorkInPureBrowser = true; }, replaceState: function replaceState(uri) { var that = this; return this._addTask(function () { uri = that._normalizeUri(uri); that._window.history.replaceState(null, null, "#" + uri); that._currentTask.resolve(); }); }, pushState: function pushState(uri) { var that = this; return this._addTask(function () { uri = that._normalizeUri(uri); that._window.history.pushState(null, null, "#" + uri); that._currentTask.resolve(); }); }, createRootPage: function createRootPage() { return this.replaceState(ROOT_PAGE_URL); }, _onHashChange: function _onHashChange() { if (this._currentTask) { this._currentTask.resolve(); } this.popState.fire(); }, back: function back() { var that = this; return this._addTask(function () { that._window.history.back(); }); }, getHash: function getHash() { return this._normalizeUri(this._window.location.hash); }, isRootPage: function isRootPage() { return this.getHash() === ROOT_PAGE_URL; }, _normalizeUri: function _normalizeUri(uri) { return (uri || "").replace(/^#+/, ""); }, _addTask: function _addTask(task) { var that = this, d = $.Deferred(); this._tasks.add(function () { that._currentTask = d; task(); return d; }); return d.promise(); } }); var OldBrowserAdapter = DefaultBrowserAdapter.inherit({ ctor: function ctor() { this._innerEventCount = 0; this.callBase.apply(this, arguments); this._skipNextEvent = false; }, replaceState: function replaceState(uri) { var that = this; uri = that._normalizeUri(uri); if (that.getHash() !== uri) { that._addTask(function () { that._skipNextEvent = true; that._window.history.back(); }); return that._addTask(function () { that._skipNextEvent = true; that._window.location.hash = uri; }); } return $.Deferred().resolve().promise(); }, pushState: function pushState(uri) { var that = this; uri = this._normalizeUri(uri); if (this.getHash() !== uri) { return that._addTask(function () { that._skipNextEvent = true; that._window.location.hash = uri; }); } return $.Deferred().resolve().promise(); }, createRootPage: function createRootPage() { return this.pushState(ROOT_PAGE_URL); }, _onHashChange: function _onHashChange() { var currentTask = this._currentTask; this._currentTask = null; if (this._skipNextEvent) { this._skipNextEvent = false; } else { this.popState.fire(); } if (currentTask) { currentTask.resolve(); } } }); var BuggyAndroidBrowserAdapter = OldBrowserAdapter.inherit({ createRootPage: function createRootPage() { this.pushState(BUGGY_ANDROID_BUFFER_PAGE_URL); return this.callBase(); } }); var HistorylessBrowserAdapter = DefaultBrowserAdapter.inherit({ ctor: function ctor(options) { options = options || {}; this._window = options.window || window; this.popState = Callbacks(); $(this._window).on("dxback", this._onHashChange.bind(this)); this._currentHash = this._window.location.hash; }, replaceState: function replaceState(uri) { this._currentHash = this._normalizeUri(uri); return $.Deferred().resolve().promise(); }, pushState: function pushState(uri) { return this.replaceState(uri); }, createRootPage: function createRootPage() { return this.replaceState(ROOT_PAGE_URL); }, getHash: function getHash() { return this._normalizeUri(this._currentHash); }, back: function back() { return this.replaceState(ROOT_PAGE_URL); }, _onHashChange: function _onHashChange() { var promise = this.back(); this.popState.fire(); return promise; } }); var BuggyCordovaWP81BrowserAdapter = DefaultBrowserAdapter.inherit({ ctor: function ctor(options) { this.callBase(options); this.canWorkInPureBrowser = false; } }); exports.DefaultBrowserAdapter = DefaultBrowserAdapter; exports.OldBrowserAdapter = OldBrowserAdapter; exports.BuggyAndroidBrowserAdapter = BuggyAndroidBrowserAdapter; exports.HistorylessBrowserAdapter = HistorylessBrowserAdapter; exports.BuggyCordovaWP81BrowserAdapter = BuggyCordovaWP81BrowserAdapter;