From 4261f0d712b8005800aff81e0bd4f85f12523ca1 Mon Sep 17 00:00:00 2001 From: Thomas Portelange Date: Fri, 17 Mar 2023 13:13:59 +0100 Subject: [PATCH] ENH Add subsite change detection Closes https://github.com/silverstripe/silverstripe-subsites/issues/515 --- client/dist/js/LeftAndMain_Subsites.js | 2 +- client/src/js/LeftAndMain_Subsites.js | 35 +++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/client/dist/js/LeftAndMain_Subsites.js b/client/dist/js/LeftAndMain_Subsites.js index 248cc619..1a55e793 100644 --- a/client/dist/js/LeftAndMain_Subsites.js +++ b/client/dist/js/LeftAndMain_Subsites.js @@ -1 +1 @@ -!function(){"use strict";var t={311:function(t){t.exports=jQuery}},e={};function i(s){var n=e[s];if(void 0!==n)return n.exports;var r=e[s]={exports:{}};return t[s](r,r.exports,i),r.exports}!function(){var t,e=i(311);(t=e).entwine("ss",(t=>{t("#SubsitesSelect").entwine({onadd(){this.on("change",(function(){window.location.search=t.query.set("SubsiteID",t(this).val())}))}}),t(".cms-container .cms-menu-list li a").entwine({onclick(e){t(".cms-container").loadFragment("admin/subsite_xhr","SubsiteList"),this._super(e)}}),t(".cms-container .SubsiteAdmin .cms-edit-form fieldset.ss-gridfield").entwine({onreload(e){t(".cms-container").loadFragment("admin/subsite_xhr","SubsiteList"),this._super(e)}}),t(".cms-container .tab.subsite-model").entwine({onadd(e){t(".cms-container").loadFragment("admin/subsite_xhr","SubsiteList"),this._super(e)}}),t("#Form_ItemEditForm_AccessAllSubsites").entwine({onmatch(){this.showHideSubsiteList();const e=this;t("#Form_ItemEditForm_AccessAllSubsites input").change((()=>{e.showHideSubsiteList()}))},showHideSubsiteList(){t("#Form_ItemEditForm_Subsites").parent().parent().css("display",t("#Form_ItemEditForm_AccessAllSubsites_1").is(":checked")?"none":"")}}),t(".cms-edit-form").entwine({getChangeTrackerOptions(){const e=void 0===this.entwineData("ChangeTrackerOptions");let i=this._super();return e&&(i=t.extend({},i),i.ignoreFieldSelector+=", input[name=IsSubsite]",this.setChangeTrackerOptions(i)),i}}),t(".cms-edit-form input[name=action_copytosubsite]").entwine({onclick(t){this.closest("form").trigger("submit",[this])}})})),t.entwine("ss.preview",(t=>{t(".cms-preview").entwine({onafterIframeAdjustedForPreview(e,i){const s=t(i).find("meta[name=x-subsite-id]").attr("content");s&&(t(i).find("a").each((function(){const e=t(this).attr("href");void 0===e||e.match(/^http:\/\//)||t(this).attr("href",t.path.addSearchParams(e,{SubsiteID:s}))})),t(i).find("form").each((function(){const e=t(this).attr("action");void 0===e||e.match(/^http:\/\//)||t(this).append(``)})))}})}))}()}(); \ No newline at end of file +!function(){"use strict";var t={311:function(t){t.exports=jQuery}},e={};function i(s){var n=e[s];if(void 0!==n)return n.exports;var o=e[s]={exports:{}};return t[s](o,o.exports,i),o.exports}!function(){var t,e=i(311);(t=e).entwine("ss",(t=>{t("#SubsitesSelect").entwine({detectSubsiteChange(t){const e="admin_subsite_id";let i=!1;try{localStorage.setItem(e,t),window.addEventListener("storage",(()=>{if(i)return;const s=localStorage.getItem(e);if(s&&t!=s){const t=ss.i18n._t("Admin.SUBSITECHANGED","You've changed subsite in another tab, do you want to reload the page?");i=!0,confirm(t)&&window.location.reload()}}))}catch(t){}},onmatch(){this.detectSubsiteChange(this.find("option[selected]").attr("value"))},onadd(){this.on("change",(function(){window.location.search=t.query.set("SubsiteID",t(this).val())}))}}),t(".cms-container .cms-menu-list li a").entwine({onclick(e){t(".cms-container").loadFragment("admin/subsite_xhr","SubsiteList"),this._super(e)}}),t(".cms-container .SubsiteAdmin .cms-edit-form fieldset.ss-gridfield").entwine({onreload(e){t(".cms-container").loadFragment("admin/subsite_xhr","SubsiteList"),this._super(e)}}),t(".cms-container .tab.subsite-model").entwine({onadd(e){t(".cms-container").loadFragment("admin/subsite_xhr","SubsiteList"),this._super(e)}}),t("#Form_ItemEditForm_AccessAllSubsites").entwine({onmatch(){this.showHideSubsiteList();const e=this;t("#Form_ItemEditForm_AccessAllSubsites input").change((()=>{e.showHideSubsiteList()}))},showHideSubsiteList(){t("#Form_ItemEditForm_Subsites").parent().parent().css("display",t("#Form_ItemEditForm_AccessAllSubsites_1").is(":checked")?"none":"")}}),t(".cms-edit-form").entwine({getChangeTrackerOptions(){const e=void 0===this.entwineData("ChangeTrackerOptions");let i=this._super();return e&&(i=t.extend({},i),i.ignoreFieldSelector+=", input[name=IsSubsite]",this.setChangeTrackerOptions(i)),i}}),t(".cms-edit-form input[name=action_copytosubsite]").entwine({onclick(t){this.closest("form").trigger("submit",[this])}})})),t.entwine("ss.preview",(t=>{t(".cms-preview").entwine({onafterIframeAdjustedForPreview(e,i){const s=t(i).find("meta[name=x-subsite-id]").attr("content");s&&(t(i).find("a").each((function(){const e=t(this).attr("href");void 0===e||e.match(/^http:\/\//)||t(this).attr("href",t.path.addSearchParams(e,{SubsiteID:s}))})),t(i).find("form").each((function(){const e=t(this).attr("action");void 0===e||e.match(/^http:\/\//)||t(this).append(``)})))}})}))}()}(); \ No newline at end of file diff --git a/client/src/js/LeftAndMain_Subsites.js b/client/src/js/LeftAndMain_Subsites.js index c0ee6b5f..60f04bcf 100644 --- a/client/src/js/LeftAndMain_Subsites.js +++ b/client/src/js/LeftAndMain_Subsites.js @@ -1,9 +1,42 @@ /* jslint browser: true, nomen: true */ -/* global $, window, jQuery */ +/* global $, ss, window, jQuery */ (function ($) { // eslint-disable-next-line no-shadow $.entwine('ss', ($) => { $('#SubsitesSelect').entwine({ + /** + * Store current subsite id and ask to reload the page if it detects any change + */ + detectSubsiteChange(selectedId) { + const sessionKey = 'admin_subsite_id'; + let reloadPending = false; + try { + localStorage.setItem(sessionKey, selectedId); + + window.addEventListener('storage', () => { + if (reloadPending) { + return; + } + const tabId = localStorage.getItem(sessionKey); + // eslint-disable-next-line eqeqeq + if (tabId && selectedId != tabId) { + const msg = ss.i18n._t('Admin.SUBSITECHANGED', 'You\'ve changed subsite in another tab, do you want to reload the page?'); + reloadPending = true; // Don't trigger multiple confirm dialog + // eslint-disable-next-line no-alert + if (confirm(msg)) { + window.location.reload(); + } + // Don't ask again if cancelled + } + }); + } catch (e) { + // Maybe storage is full or not available, disable this feature and ignore error + } + }, + + onmatch() { + this.detectSubsiteChange(this.find('option[selected]').attr('value')); + }, onadd() { this.on('change', function () { window.location.search = $.query.set('SubsiteID', $(this).val());