From 3d94a7c5ec20c5b1a7504917b3966bebb8efa556 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Wed, 3 Jul 2019 15:34:31 -0600 Subject: [PATCH] Update public link when changing dictionaries --- src/js/account/render.js | 4 ++-- src/js/account/sync.js | 10 ++++++++-- src/js/account/utilities.js | 16 ++++++++++++++++ src/js/render.js | 9 +++++---- src/php/router.php | 2 +- 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/js/account/render.js b/src/js/account/render.js index 7333389..bfff0e0 100644 --- a/src/js/account/render.js +++ b/src/js/account/render.js @@ -1,4 +1,5 @@ import { setupLoginModal, setupChangeDictionary, setupCreateNewDictionary, setupDeletedDictionaryChangeModal, setupMakePublic } from "./setupListeners"; +import { getPublicLink } from "./utilities"; import { request } from "./helpers"; export function renderLoginForm() { @@ -59,14 +60,13 @@ export function renderMakePublic() { let waitForSync = setInterval(() => { if (window.currentDictionary.hasOwnProperty('externalID') && !isNaN(window.currentDictionary.externalID)) { clearInterval(waitForSync); - const { externalID } = window.currentDictionary; const editSettingsTabHTML = `

Public Link:
- + Copy

`; diff --git a/src/js/account/sync.js b/src/js/account/sync.js index 96c20f8..1272636 100644 --- a/src/js/account/sync.js +++ b/src/js/account/sync.js @@ -1,7 +1,7 @@ import { addMessage } from "../utilities"; import { saveDictionary, clearDictionary } from "../dictionaryManagement"; import { request } from "./helpers"; -import { saveToken, dictionaryIsDefault } from "./utilities"; +import { saveToken, dictionaryIsDefault, getPublicLink } from "./utilities"; import { renderAll } from "../render"; import { sortWords } from "../wordManagement"; import { getLocalDeletedWords, clearLocalDeletedWords, saveDeletedWordsLocally } from "./utilities"; @@ -37,8 +37,14 @@ export function performSync(remoteDictionary) { syncWords(remoteDictionary.words, remoteDictionary.deletedWords).then(success => { if (success) { renderAll(); + document.getElementById('accountSettingsChangeDictionary').value = window.currentDictionary.externalID; - document.getElementById('publicLinkDisplay').style.display = window.currentDictionary.settings.isPublic ? '' : 'none'; + if (document.getElementById('publicLink')) { + document.getElementById('publicLink').value = getPublicLink(); + } + if (document.getElementById('publicLinkDisplay')) { + document.getElementById('publicLinkDisplay').style.display = window.currentDictionary.settings.isPublic ? '' : 'none'; + } } else { console.error('word sync failed'); } diff --git a/src/js/account/utilities.js b/src/js/account/utilities.js index bcbf96d..2a503ba 100644 --- a/src/js/account/utilities.js +++ b/src/js/account/utilities.js @@ -21,6 +21,22 @@ export function dictionaryIsDefault() { return JSON.stringify(defaultDictionary) === JSON.stringify(currentDictionary); } +export function getPublicLink() { + const { externalID } = window.currentDictionary; + let path; + if (externalID) { + path = window.location.pathname.match(new RegExp(externalID + '$')) + ? window.location.pathname + : (window.location.pathname.indexOf(externalID) > -1 + ? window.location.pathname.substring(0, window.location.pathname.indexOf(externalID)) + externalID + : window.location.pathname + externalID + ); + } else { + path = ''; + } + return 'https://' + document.domain + path; +} + export function saveDeletedWordsLocally(wordIds) { let storedDeletedWords = getLocalDeletedWords(); wordIds.forEach(wordId => { diff --git a/src/js/render.js b/src/js/render.js index f51180b..9c45ff0 100644 --- a/src/js/render.js +++ b/src/js/render.js @@ -18,6 +18,7 @@ import { getPaginationData } from './pagination'; import { getOpenEditForms, parseReferences } from './wordManagement'; import { renderAd } from './ads'; import ipaTableFile from './KeyboardFire/phondue/ipa-table.html'; +import { getPublicLink } from './account/utilities'; export function renderAll() { renderTheme(); @@ -54,12 +55,13 @@ export function renderName() { shareLink.id = 'dictionaryShare'; shareLink.classList.add('button'); shareLink.style.float = 'right'; - shareLink.href = window.location.pathname.match(new RegExp(window.currentDictionary.externalID + '$')) ? window.location.pathname - : window.location.pathname.substring(0, window.location.pathname.indexOf(window.currentDictionary.externalID)) + window.currentDictionary.externalID; + shareLink.href = getPublicLink(); shareLink.target = '_blank'; shareLink.title = 'Public Link to Dictionary'; shareLink.innerHTML = '➦'; name.parentElement.insertBefore(shareLink, name); + } else if (isPublic && shareLinkElement) { + shareLinkElement.href = getPublicLink(); } else if (!isPublic && shareLinkElement) { shareLinkElement.parentElement.removeChild(shareLinkElement); } @@ -193,8 +195,7 @@ export function renderWords() { wordId: originalWord.wordId, }); const homonymnNumber = getHomonymnNumber(originalWord); - const shareLink = window.currentDictionary.hasOwnProperty('externalID') - ? window.location.pathname + window.currentDictionary.externalID + '/' + word.wordId : ''; + const shareLink = window.currentDictionary.hasOwnProperty('externalID') ? getPublicLink() + '/' + word.wordId : ''; wordsHTML += renderAd(displayIndex); diff --git a/src/php/router.php b/src/php/router.php index de81048..cf2fac9 100644 --- a/src/php/router.php +++ b/src/php/router.php @@ -119,7 +119,7 @@ switch ($view) { oldLoad && oldLoad(); if (UpUp) { UpUp.start({ - 'cache-version': '2.0.0', + 'cache-version': '2.0.2', 'content-url': 'offline.html', 'assets': [ \"" . implode('","', $files) . "\"