Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/assets/javascripts/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ export const progressDialog = function(delayedJob, dialog, url, callback, option
});
}, 200);

$(document).on('page:before-change', function() {
$(document).on('turbolinks:before-cache', function cleanupProgressDialog() {
clearTimeout(progressDialogTimerId);
$(document).off('page:before-change');
$(document).off('turbolinks:before-cache', cleanupProgressDialog);
});
}

Expand Down
24 changes: 21 additions & 3 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ $(document).on('turbolinks:load', function() {
Paloma.start();
});

// $(document).on('page:restore', function() {
// Paloma.start();
// });
// Fallback for popstate events that Turbolinks ignores (e.g. history.state
// was wiped by location.replace or replaceState from third-party libs).
// Turbolinks only handles popstate when event.state has its restorationIdentifier.
// When missing, the URL changes but the body is never swapped.
(function() {
var popstateHandled = false;

document.addEventListener('turbolinks:before-render', function() {
popstateHandled = true;
});

window.addEventListener('popstate', function(event) {
popstateHandled = false;

setTimeout(function() {
if (!popstateHandled) {
Turbolinks.visit(window.location.href, { action: 'replace' });
}
}, 50);
});
})();
8 changes: 5 additions & 3 deletions app/assets/javascripts/deliverables_by_vehicles.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const deliverable_by_vehicle_show = function(params) {
pushState: function(p, cb) {

if (!hasState) {
return location.assign(p.URL);
return Turbolinks.visit(p.URL);
}
history.pushState(p.state, p.title, p.URL);
cb(p.state);
Expand All @@ -52,7 +52,9 @@ const deliverable_by_vehicle_show = function(params) {

const popStateHandler = function(event) {
const state = event.state;
if (!state.vehicleId) {
if (!state || !state.vehicleId) {
// Orphan history entry (e.g. navigated away via Turbolinks then came back)
// Let the browser handle it normally
return;
}
$("#vehicle_id")
Expand All @@ -62,7 +64,7 @@ const deliverable_by_vehicle_show = function(params) {
};

window.addEventListener('popstate', popStateHandler);
$(document).on('page:before-change', function() {
$(document).on('turbolinks:before-cache', function() {
window.removeEventListener('popstate', popStateHandler);
});

Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/order_arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ const order_arrays_edit = function(params) {
complete: completeWaiting,
error: ajaxError,
success: function() {
window.location = '/plannings/' + planning_id + '/edit';
Turbolinks.visit('/plannings/' + planning_id + '/edit');
}
});
});
Expand Down
6 changes: 3 additions & 3 deletions app/assets/javascripts/plannings.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ export const plannings_edit = function(params) {
}
initMarkers();
backgroundTaskIntervalId = setInterval(backgroundTask, 60000);
$(document).on('page:before-change', function() {
$(document).on('turbolinks:before-cache', function() {
clearInterval(backgroundTaskIntervalId);
});
}
Expand Down Expand Up @@ -3048,7 +3048,7 @@ var plannings_index = function(params) {
return warning(I18n.t('plannings.index.vehicle_select_error'));
}
planning_ids = $('[name^=planning]:checked').map(function() { return $(this).val(); }).toArray().join(',');
location.assign('/routes_by_vehicles/' + vehicle_id + '?planning_ids=' + planning_ids);
Turbolinks.visit('/routes_by_vehicles/' + vehicle_id + '?planning_ids=' + planning_ids);
});

$('#deliverables-by-vehicle').on('click', function(e) {
Expand All @@ -3057,7 +3057,7 @@ var plannings_index = function(params) {
return warning(I18n.t('plannings.index.vehicle_select_error'));
}
planning_ids = $('[name^=planning]:checked').map(function() { return $(this).val(); }).toArray().join(',');
location.assign('/deliverables_by_vehicles/' + vehicle_id + '?planning_ids=' + planning_ids);
Turbolinks.visit('/deliverables_by_vehicles/' + vehicle_id + '?planning_ids=' + planning_ids);
});

};
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/plannings_by_destinations.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const planningsShow = function(params) {
const countMovedDestinations = function(max) {
movedDestinations++;
$('.progress-bar').css('width', (movedDestinations * 100 / max) + '%');
if (movedDestinations == max) location.href = '';
if (movedDestinations == max) Turbolinks.visit(window.location.pathname, { action: 'replace' });
};

const getQuantitiesByPlanning = function() {
Expand Down
41 changes: 41 additions & 0 deletions app/assets/javascripts/scaffolds.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,45 @@ export const mapInitialize = function(params) {
return map;
};

// Patch leaflet-hash for two issues:
// 1. The original uses location.replace(hash) which wipes history.state,
// breaking Turbolinks back/forward navigation (restorationIdentifier lost).
// 2. ControlledBounds overrides setView to add an offset for controls/sidebar,
// but getCenter() returns the raw container center (already offset). This
// causes double-offset on hash restore: the hash stores offset coords, and
// setView applies the offset again, shifting the map by menu-left/planbar.
function patchLeafletHash() {
if (typeof L !== 'undefined' && L.Hash && L.Hash.prototype) {
L.Hash.prototype.onMapMove = function() {
if (this.movingMap || !this.map._loaded) {
return false;
}
var hash = this.formatHash(this.map);
if (this.lastHash != hash) {
history.replaceState(history.state, '', hash);
this.lastHash = hash;
}
};

var originalFormatHash = L.Hash.formatHash;
L.Hash.formatHash = function(map) {
if (map._deoffsetLatLng) {
var center = map._deoffsetLatLng(map.getCenter());
var zoom = map.getZoom();
var precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2));
return '#' + [zoom,
center.lat.toFixed(precision),
center.lng.toFixed(precision)
].join('/');
}
return originalFormatHash.call(this, map);
};
L.Hash.prototype.formatHash = L.Hash.formatHash;
}
}

patchLeafletHash();

// FIXME initOnly used for api-web because Firefox doesn't support hash replace (in Leaflet Hash) within an iframe. A new url is fetched by Turbolinks. Chrome works.
export const initializeMapHash = function(map, initOnly) {
if (initOnly) {
Expand All @@ -374,7 +413,9 @@ export const initializeMapHash = function(map, initOnly) {
}
// FIXME when turbolinks get updated to work with Edge
else if (navigator.userAgent.indexOf('Edge') === -1) {
patchLeafletHash();
map.addHash();

var removeHash = function() {
if (map.removeHash) {
map.removeHash();
Expand Down
5 changes: 4 additions & 1 deletion app/assets/javascripts/vehicle_usage_sets.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ const vehicle_usage_sets_index = function(params) {
// override accordion collapse bootstrap code
$('a.accordion-toggle').click(function() {
var id = $(this).attr('href');
window.location.hash = id;
// Use replaceState to track accordion state without creating history entries
if (history.replaceState) {
history.replaceState(null, '', id);
}
var allCollapsed = $('.accordion-body.collapse.in').size() ? true : false;
$('.accordion-body.collapse.in').each(function() {
var $this = $(this);
Expand Down
8 changes: 3 additions & 5 deletions app/assets/javascripts/zonings.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,11 @@ export const zonings_edit = function(params) {
e.preventDefault();
}
}
$(document).on('page:change', function() {
$(document).off('page:before-change', checkZoningChanges);
});
}

$(document).on('turbolinks:load', function() {
$(document).on('page:before-change', checkZoningChanges);
$(document).on('turbolinks:before-visit', checkZoningChanges);
$(document).on('turbolinks:before-cache', function() {
$(document).off('turbolinks:before-visit', checkZoningChanges);
});

map.on('pm:drawstart', function(e) {
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/concerns/link_back.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module LinkBack

included do
after_action :save_link_back, only: [:new, :edit]
before_action :clear_stale_link_back, except: [:new, :edit, :create, :update]
end

private
Expand All @@ -27,6 +28,11 @@ def save_link_back
end
end

# Expire stale link_back when navigating away from new/edit/create/update
def clear_stale_link_back
session.delete(:link_back) if session[:link_back]
end

def link_back
session.delete(:link_back)
end
Expand Down
Loading