hashchange: Use hashchange event listener.

The comment that jQuery “doesn’t have” this was nonsense: jQuery
supports every event the browser does.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
Anders Kaseorg 2019-07-08 19:30:51 -07:00 committed by Tim Abbott
parent 6a50460749
commit 0c80fc6aba
2 changed files with 15 additions and 14 deletions

View File

@ -1,8 +1,11 @@
set_global('$', global.make_zjquery());
const window_stub = $.create('window-stub');
global.patch_builtin('window', { global.patch_builtin('window', {
location: { location: {
protocol: 'http:', protocol: 'http:',
host: 'example.com', host: 'example.com',
}, },
to_$: () => window_stub,
}); });
zrequire('people'); zrequire('people');
zrequire('hash_util'); zrequire('hash_util');
@ -177,7 +180,7 @@ run_test('hash_interactions', () => {
]); ]);
helper.clear_events(); helper.clear_events();
window.onhashchange(); $(window).trigger($.Event('hashchange', {}));
helper.assert_events([ helper.assert_events([
'overlays.close_for_hash_change', 'overlays.close_for_hash_change',
'message_viewport.stop_auto_scrolling', 'message_viewport.stop_auto_scrolling',
@ -189,7 +192,7 @@ run_test('hash_interactions', () => {
window.location.hash = '#narrow/stream/Denmark'; window.location.hash = '#narrow/stream/Denmark';
helper.clear_events(); helper.clear_events();
window.onhashchange(); $(window).trigger($.Event('hashchange', {}));
helper.assert_events([ helper.assert_events([
'overlays.close_for_hash_change', 'overlays.close_for_hash_change',
'message_viewport.stop_auto_scrolling', 'message_viewport.stop_auto_scrolling',
@ -203,7 +206,7 @@ run_test('hash_interactions', () => {
window.location.hash = '#narrow'; window.location.hash = '#narrow';
helper.clear_events(); helper.clear_events();
window.onhashchange(); $(window).trigger($.Event('hashchange', {}));
helper.assert_events([ helper.assert_events([
'overlays.close_for_hash_change', 'overlays.close_for_hash_change',
'message_viewport.stop_auto_scrolling', 'message_viewport.stop_auto_scrolling',
@ -217,7 +220,7 @@ run_test('hash_interactions', () => {
window.location.hash = '#streams/whatever'; window.location.hash = '#streams/whatever';
helper.clear_events(); helper.clear_events();
window.onhashchange(); $(window).trigger($.Event('hashchange', {}));
helper.assert_events([ helper.assert_events([
'overlays.close_for_hash_change', 'overlays.close_for_hash_change',
'subs.launch', 'subs.launch',
@ -226,7 +229,7 @@ run_test('hash_interactions', () => {
window.location.hash = '#keyboard-shortcuts/whatever'; window.location.hash = '#keyboard-shortcuts/whatever';
helper.clear_events(); helper.clear_events();
window.onhashchange(); $(window).trigger($.Event('hashchange', {}));
helper.assert_events([ helper.assert_events([
'overlays.close_for_hash_change', 'overlays.close_for_hash_change',
'message_viewport.stop_auto_scrolling', 'message_viewport.stop_auto_scrolling',
@ -236,7 +239,7 @@ run_test('hash_interactions', () => {
window.location.hash = '#message-formatting/whatever'; window.location.hash = '#message-formatting/whatever';
helper.clear_events(); helper.clear_events();
window.onhashchange(); $(window).trigger($.Event('hashchange', {}));
helper.assert_events([ helper.assert_events([
'overlays.close_for_hash_change', 'overlays.close_for_hash_change',
'message_viewport.stop_auto_scrolling', 'message_viewport.stop_auto_scrolling',
@ -246,7 +249,7 @@ run_test('hash_interactions', () => {
window.location.hash = '#search-operators/whatever'; window.location.hash = '#search-operators/whatever';
helper.clear_events(); helper.clear_events();
window.onhashchange(); $(window).trigger($.Event('hashchange', {}));
helper.assert_events([ helper.assert_events([
'overlays.close_for_hash_change', 'overlays.close_for_hash_change',
'message_viewport.stop_auto_scrolling', 'message_viewport.stop_auto_scrolling',
@ -256,7 +259,7 @@ run_test('hash_interactions', () => {
window.location.hash = '#drafts'; window.location.hash = '#drafts';
helper.clear_events(); helper.clear_events();
window.onhashchange(); $(window).trigger($.Event('hashchange', {}));
helper.assert_events([ helper.assert_events([
'overlays.close_for_hash_change', 'overlays.close_for_hash_change',
'drafts.launch', 'drafts.launch',
@ -265,7 +268,7 @@ run_test('hash_interactions', () => {
window.location.hash = '#settings/alert-words'; window.location.hash = '#settings/alert-words';
helper.clear_events(); helper.clear_events();
window.onhashchange(); $(window).trigger($.Event('hashchange', {}));
helper.assert_events([ helper.assert_events([
'overlays.close_for_hash_change', 'overlays.close_for_hash_change',
'settings.launch', 'settings.launch',
@ -274,7 +277,7 @@ run_test('hash_interactions', () => {
window.location.hash = '#organization/user-list-admin'; window.location.hash = '#organization/user-list-admin';
helper.clear_events(); helper.clear_events();
window.onhashchange(); $(window).trigger($.Event('hashchange', {}));
helper.assert_events([ helper.assert_events([
'overlays.close_for_hash_change', 'overlays.close_for_hash_change',
'admin.launch', 'admin.launch',

View File

@ -295,10 +295,8 @@ exports.go_to_location = function (hash) {
}; };
exports.initialize = function () { exports.initialize = function () {
// jQuery doesn't have a hashchange event, so we manually wrap $(window).on('hashchange', function (e) {
// our event handler hashchanged(false, e.originalEvent);
window.onhashchange = blueslip.wrap_function(function (e) {
hashchanged(false, e);
}); });
hashchanged(true); hashchanged(true);
}; };