From bebeff0cbcc42ddeb9f318fe0b37902d333d1088 Mon Sep 17 00:00:00 2001 From: Daniel B Date: Wed, 25 Sep 2019 14:30:04 -0500 Subject: [PATCH] 2019.09.25.01 --- WME-URComments-Enhanced.js | 79 +++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 36 deletions(-) diff --git a/WME-URComments-Enhanced.js b/WME-URComments-Enhanced.js index 25de46a..d610377 100644 --- a/WME-URComments-Enhanced.js +++ b/WME-URComments-Enhanced.js @@ -1,7 +1,7 @@ // ==UserScript== // @name WME URComments-Enhanced (beta) // @namespace https://greasyfork.org/users/166843 -// @version 2019.09.18.01 +// @version 2019.09.25.01 // eslint-disable-next-line max-len // @description URComments-Enhanced (URC-E) allows Waze editors to handle WME update requests more quickly and efficiently. Also adds many UR filtering options, ability to change the markers, plus much, much, more! // @grant none @@ -41,7 +41,9 @@ const SCRIPT_NAME = GM_info.script.name.replace('(beta)', 'β'), SCRIPT_VERSION_CHANGES = ['NEW: Remember collapsed state of More Information box in UR Panel.', 'NEW: Unknown venue name (no name on venue / place) is now translatable to locale(s).', 'NEW: Unknown road name (no name on road / segment) is now translatable to locale(s).', - 'BUGFIX: Translations not loading correctly in certain situations.' + 'BUGFIX: Translations not loading correctly in certain situations.', + 'BUGFIX: Variable detection improvement.', + 'BUGFIX: Variables slipping through auto-post reminder routine in certain situations.' ], DOUBLE_CLICK_ICON = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAGnRFWHRTb2Z0d2FyZQBQYWludC5ORVQgdjMuNS4xMDD0cqEAAAMnSURBVFhH7ZdNSFRRGIZH509ndGb8nZuCCSNE4CyGURmkTVCuBEmEiMSZBmaoRYsIgiDMhVFEFERBZITbEINQbFMtclGQtUgIalG0ioiMFkWlZc+53WN3rmfG64wSgS+8fOd8c8533u/83HPGsRZcLtedqqqqU0Z189De3q4ZxRyUlZVN+3y+EaNaENXV1VecTue8HZLYPO0v6B1jsZiG42soFErpDhPsCshkMgHM8npI7F/YP6ivr0+Wl5f/CAQCOSLsCkgmkyGMHtjtds8Q66Ig2Y5Jfx7+RV1dnS6CNT9kuBzUp5iZI0Y1L8wCEHzW4/Hs9Xq9MRJqEb7KysrHiPmM/w18JdvCXNTW1g4JEQTRRbS1tYkAOejt7Q12dnZqXV1d4VQq5RE+swAG+sKSfmImbkkB7LEo5QeNjY3DrP0x2RauBhkPof7ZwMCAHlygubm5o6KiYpyg76jKzsuIXULshFkA/Q9idUgBgmS+h/aXZN2gGul02i1sIpEgvm/M2DArHRlkP/5JUUbUE6uAmpqaEyTxgUE/Ch8JxPDfa2hoOM1yHJdtxTmfQpXYNDqZvplIJLKdHx3xeNxHgIcrjU0ks13slZuirBLQ2tq6MxwO72NfZYWPuPeJv4B9iX0u2zoIcpJMhiXpfJgfdPj9/huYnIElCwkg8ymEnzd4TfrzUI2mpqYO67SbaREwl81mi/kOCKsG6zSOWdVJ0iyAZVzo7u72MWPXqb+wS07DZawa1t1upVmAIIIno9HoNsqlo7+/f83ptAoQFFPKJluURNQE/vWDoxfG5AxopUqAgtNw/ZAC+PAMs74ZFfliapsugON0hqk8mo8csaeiXQGWJmADuCVgS8B/KoDv+r8V0NfX5zduqpLId0I8WIoDl9FbjDKwXXIXjGKLA52vYpSB7ZIHaAJbHDRN28HTaZGiMvha5B55NDs7S7EEcNmcwygHKESEfyeBOOXSMDg46OKVc5uiciAVxaxxUx6gvDFAhJOn0wiBv1FVDirJxn3Ns3s35Y0Hz+wWZmOUozXHe0D8xfrJgEvwPdf23WAwmO7p6fEazW3C4fgNPVAixOZacokAAAAASUVORK5CYII=', DEBUG = true, @@ -428,7 +430,7 @@ async function loadSettingsFromStorage(restoreSettings, proceedWithRestore) { outputText += `${I18n.t('urce.common.None')}`; outputText += `

${I18n.t('urce.prompts.RestoreSettingsConfirmation')}`; return WazeWrap.Alerts.confirm(SCRIPT_NAME, - formatText(outputText, true, false), + formatText(outputText, true, false, undefined), () => { loadSettingsFromStorage(restoreSettings, true); }, () => { }, I18n.t('urce.common.Yes'), I18n.t('urce.common.No')); @@ -940,7 +942,7 @@ async function handleUpdateRequestContainer(urId, caller) { } function checkValue() { - const varsFound = this.value.match(/(\B\$[A-Za-z0-9]*\$?)/gm); + const varsFound = this.value.match(/\B\$\S*\$\B/gm) || this.value.match(/(\$SELSEGS|\$USERNAME|\$URD)/gm); if (varsFound) { let title; if (this.value.indexOf('$SELSEGS') > -1) @@ -1055,7 +1057,9 @@ function convertTimeOfDayToCasual(hour) { return casualText; } -function formatText(text, replaceVars, shortcutClicked) { +function formatText(text, replaceVars, shortcutClicked, urId) { + if (!urId && _selUr && _selUr.urId) + ({ urId } = _selUr); if (replaceVars && shortcutClicked && (text.indexOf('$SELSEGS') > -1)) { const selFeatures = W.selectionManager.getSelectedFeatures(); let output = ''; @@ -1119,7 +1123,7 @@ function formatText(text, replaceVars, shortcutClicked) { WazeWrap.Alerts.error(SCRIPT_NAME, I18n.t('urce.prompts.SelSegsInsertError')); } } - if (replaceVars && (_selUr.urId > -1)) { + if (replaceVars && (urId > -1)) { if (text.search(/(\$(CURRENTDATE_DAY_OF_WEEK|CURRENTDATE_DATE|CURRENTDATE_DATE_CASUAL|CURRENTDATE_TIME|CURRENTDATE_TIME_CASUAL)\$)/gm) > -1) { if (text.indexOf('$CURRENTDATE_DAY_OF_WEEK$') > -1) text = text.replace('$CURRENTDATE_DAY_OF_WEEK$', new Date().toLocaleDateString(I18n.currentLocale(), { weekday: 'long' })); @@ -1133,20 +1137,20 @@ function formatText(text, replaceVars, shortcutClicked) { text = text.replace('$CURRENTDATE_TIME_CASUAL$', convertTimeOfDayToCasual(new Date().getHours())); } if (text.search(/(\$(DRIVEDATE_DAY_OF_WEEK|DRIVEDATE_DATE|DRIVEDATE_DATE_CASUAL|DRIVEDATE_DAYS_AGO|DRIVEDATE_TIME|DRIVEDATE_TIME_CASUAL|DRIVEDATE_TIME_CASUALMODE)\$)/gm) > -1) { - if (W.model.mapUpdateRequests.objects[_selUr.urId]) { + if (W.model.mapUpdateRequests.objects[urId]) { if (text.indexOf('$DRIVEDATE_DAY_OF_WEEK$') > -1) { - if (W.model.mapUpdateRequests.objects[_selUr.urId].attributes && (W.model.mapUpdateRequests.objects[_selUr.urId].attributes.driveDate > -1)) { + if (W.model.mapUpdateRequests.objects[urId].attributes && (W.model.mapUpdateRequests.objects[urId].attributes.driveDate > -1)) { text = text.replace('$DRIVEDATE_DAY_OF_WEEK$', - new Date(W.model.mapUpdateRequests.objects[_selUr.urId].attributes.driveDate).toLocaleDateString(I18n.currentLocale(), { weekday: 'long' })); + new Date(W.model.mapUpdateRequests.objects[urId].attributes.driveDate).toLocaleDateString(I18n.currentLocale(), { weekday: 'long' })); } else { text = text.replace('$DRIVEDATE_DAY_OF_WEEK$', ''); } } if (text.indexOf('$DRIVEDATE_DATE$') > -1) { - if (W.model.mapUpdateRequests.objects[_selUr.urId].attributes && (W.model.mapUpdateRequests.objects[_selUr.urId].attributes.driveDate > -1)) { + if (W.model.mapUpdateRequests.objects[urId].attributes && (W.model.mapUpdateRequests.objects[urId].attributes.driveDate > -1)) { text = text.replace('$DRIVEDATE_DATE$', - new Date(W.model.mapUpdateRequests.objects[_selUr.urId].attributes.driveDate).toLocaleDateString(I18n.currentLocale(), + new Date(W.model.mapUpdateRequests.objects[urId].attributes.driveDate).toLocaleDateString(I18n.currentLocale(), { month: '2-digit', day: '2-digit', year: 'numeric' })); } else { @@ -1154,24 +1158,24 @@ function formatText(text, replaceVars, shortcutClicked) { } } if (text.indexOf('$DRIVEDATE_DATE_CASUAL$') > -1) { - if (W.model.mapUpdateRequests.objects[_selUr.urId].attributes && (W.model.mapUpdateRequests.objects[_selUr.urId].attributes.driveDate > -1)) { + if (W.model.mapUpdateRequests.objects[urId].attributes && (W.model.mapUpdateRequests.objects[urId].attributes.driveDate > -1)) { text = text.replace('$DRIVEDATE_DATE_CASUAL$', - new Date(W.model.mapUpdateRequests.objects[_selUr.urId].attributes.driveDate).toLocaleDateString(I18n.currentLocale(), { month: 'long', day: '2-digit' })); + new Date(W.model.mapUpdateRequests.objects[urId].attributes.driveDate).toLocaleDateString(I18n.currentLocale(), { month: 'long', day: '2-digit' })); } else { text = text.replace('$DRIVEDATE_DATE_CASUAL$', ''); } } if (text.indexOf('$DRIVEDATE_DAYS_AGO$') > -1) { - if (W.model.mapUpdateRequests.objects[_selUr.urId].attributes.urceData && (W.model.mapUpdateRequests.objects[_selUr.urId].attributes.urceData.driveDaysOld > -1)) - text = text.replace('$DRIVEDATE_DAYS_AGO$', parseDaysAgo(W.model.mapUpdateRequests.objects[_selUr.urId].attributes.urceData.driveDaysOld)); + if (W.model.mapUpdateRequests.objects[urId].attributes.urceData && (W.model.mapUpdateRequests.objects[urId].attributes.urceData.driveDaysOld > -1)) + text = text.replace('$DRIVEDATE_DAYS_AGO$', parseDaysAgo(W.model.mapUpdateRequests.objects[urId].attributes.urceData.driveDaysOld)); else text = text.replace('$DRIVEDATE_DAYS_AGO$', ''); } if (text.indexOf('$DRIVEDATE_TIME$') > -1) { - if (W.model.mapUpdateRequests.objects[_selUr.urId].attributes && (W.model.mapUpdateRequests.objects[_selUr.urId].attributes.driveDate > -1)) { + if (W.model.mapUpdateRequests.objects[urId].attributes && (W.model.mapUpdateRequests.objects[urId].attributes.driveDate > -1)) { text = text.replace('$DRIVEDATE_TIME$', - new Date(W.model.mapUpdateRequests.objects[_selUr.urId].attributes.driveDate).toLocaleTimeString(I18n.currentLocale(), + new Date(W.model.mapUpdateRequests.objects[urId].attributes.driveDate).toLocaleTimeString(I18n.currentLocale(), { hour: '2-digit', minute: '2-digit', timeZoneName: 'short' })); } else { @@ -1179,16 +1183,16 @@ function formatText(text, replaceVars, shortcutClicked) { } } if (text.indexOf('$DRIVEDATE_TIME_CASUAL$') > -1) { - if (W.model.mapUpdateRequests.objects[_selUr.urId].attributes && (W.model.mapUpdateRequests.objects[_selUr.urId].attributes.driveDate > -1)) - text = text.replace('$DRIVEDATE_TIME_CASUAL$', convertTimeOfDayToCasual(new Date(W.model.mapUpdateRequests.objects[_selUr.urId].attributes.driveDate).getHours())); + if (W.model.mapUpdateRequests.objects[urId].attributes && (W.model.mapUpdateRequests.objects[urId].attributes.driveDate > -1)) + text = text.replace('$DRIVEDATE_TIME_CASUAL$', convertTimeOfDayToCasual(new Date(W.model.mapUpdateRequests.objects[urId].attributes.driveDate).getHours())); else text = text.replace('$DRIVEDATE_TIME_CASUAL$', ''); } if (text.indexOf('$DRIVEDATE_TIME_CASUALMODE$') > -1) { - if (W.model.mapUpdateRequests.objects[_selUr.urId].attributes.urceData) { - const driveDaysAgo = W.model.mapUpdateRequests.objects[_selUr.urId].attributes.urceData.driveDaysOld; - const driveHour = new Date(W.model.mapUpdateRequests.objects[_selUr.urId].attributes.driveDate).getHours(); - let dayOfWeek = new Date(W.model.mapUpdateRequests.objects[_selUr.urId].attributes.driveDate).getDay(), + if (W.model.mapUpdateRequests.objects[urId].attributes.urceData) { + const driveDaysAgo = W.model.mapUpdateRequests.objects[urId].attributes.urceData.driveDaysOld; + const driveHour = new Date(W.model.mapUpdateRequests.objects[urId].attributes.driveDate).getHours(); + let dayOfWeek = new Date(W.model.mapUpdateRequests.objects[urId].attributes.driveDate).getDay(), casualText; if ((driveDaysAgo < 21) && (driveHour > -1) && (driveHour < 4)) dayOfWeek = (dayOfWeek > 0) ? dayOfWeek - 1 : 6; @@ -1261,19 +1265,19 @@ function formatText(text, replaceVars, shortcutClicked) { text = text.replace(/(\$USERNAME\$?)+/gmi, ''); } if (replaceVars && (text.indexOf('$URD') > -1)) { - if (W.model.mapUpdateRequests.objects[_selUr.urId].attributes.description) - text = text.replace(/("?\$URD\$?"?)+/gmi, `"${W.model.mapUpdateRequests.objects[_selUr.urId].attributes.description}"`).replace(/\n+/gmi, ''); + if (W.model.mapUpdateRequests.objects[urId].attributes.description) + text = text.replace(/("?\$URD\$?"?)+/gmi, `"${W.model.mapUpdateRequests.objects[urId].attributes.description}"`).replace(/\n+/gmi, ''); else text = text.replace(/("?\$URD\$?"?)+/gmi, ''); } if (replaceVars && text.indexOf('$CUSTOMTAGLINE$') > -1) { if (_settings.perCommentListSettings[_currentCommentList].customTagline.length > 0) - text = text.replace('$CUSTOMTAGLINE$', formatText(_settings.perCommentListSettings[_currentCommentList].customTagline, true)); + text = text.replace('$CUSTOMTAGLINE$', formatText(_settings.perCommentListSettings[_currentCommentList].customTagline, true, shortcutClicked, urId)); else text = text.replace('$CUSTOMTAGLINE$', ''); } if (replaceVars && text.indexOf('$URTYPE$') > -1) - text = text.replace('$URTYPE$', W.model.mapUpdateRequests.objects[_selUr.urId].attributes.typeText); + text = text.replace('$URTYPE$', W.model.mapUpdateRequests.objects[urId].attributes.typeText); if (replaceVars && text.indexOf('$PLACE_NAME$') > -1) { const placeObj = W.selectionManager.getSelectedFeatures()[0]; if (placeObj && (placeObj.model.type === 'venue')) { @@ -1318,7 +1322,7 @@ function formatText(text, replaceVars, shortcutClicked) { if (replaceVars && _customReplaceVars && (_customReplaceVars.length > 0)) { _customReplaceVars.forEach(customReplaceVar => { if (text.indexOf(customReplaceVar.customVar) > -1) - text = text.replace(customReplaceVar.customVar, formatText(customReplaceVar.replaceText, true)); + text = text.replace(customReplaceVar.customVar, formatText(customReplaceVar.replaceText, true, shortcutClicked, urId)); }); } return text.replace(/\\[r|n]+/gm, '\n'); @@ -1415,7 +1419,7 @@ function handleClickedShortcut(shortcut) { else { return; } - let outputText = formatText(replaceText, true, true); + let outputText = formatText(replaceText, true, true, undefined); if ((((shortcut === 'selSegs') || (shortcut === 'selSegsWithCity')) && (outputText.search(/\$SELSEGS\$?/gm) > -1)) || ((shortcut === 'placeName') && (outputText.indexOf('$PLACE_NAME$') > -1)) || ((shortcut === 'placeAddress') && (outputText.indexOf('$PLACE_ADDRESS$') > -1)) @@ -1442,6 +1446,8 @@ function handleClickedShortcut(shortcut) { function autoPostReminderComment(urId, comment) { return new Promise(resolve => { try { + if ((comment.search(/\B\$\S*\$\B/gm) > -1) || (comment.search(/(\$SELSEGS|\$USERNAME|\$URD)/gm) > -1)) + throw new Error(`Did not auto-post reminder comment for urId ${urId} because a variable was not replaced.`); W.model.updateRequestSessions.objects[urId].addComment(comment); W.model.mapUpdateRequests.objects[urId].attributes.reminderSent = true; } @@ -1483,7 +1489,7 @@ function postUrComment(commentStr, doubleClick) { newVal += '\n\n'; newCursorPos += 2; } - newVal += formatText(comment, true, false); + newVal += formatText(comment, true, false, undefined); if (currVal.slice(cursorPos).length > 0) { if (currVal.substr(cursorPos, 1).search(/[\n\r]/) > -1) { if (currVal.substr(cursorPos + 1, 1).search(/[\n\r]/) === -1) { @@ -1500,7 +1506,7 @@ function postUrComment(commentStr, doubleClick) { commentOutput = newVal; } else { - commentOutput = formatText(comment, true, false); + commentOutput = formatText(comment, true, false, undefined); } if (commentOutput.length > 2000) { WazeWrap.Alerts.error(SCRIPT_NAME, I18n.t('urce.prompts.CommentTooLong')); @@ -1515,7 +1521,7 @@ function postUrComment(commentStr, doubleClick) { else { $('#panel-container .mapUpdateRequest .top-section .body .conversation .new-comment-text').val(commentOutput).change().keyup().focus(); } - if (commentOutput.match(/(\B\$[A-Za-z0-9]*\$?)/gm) === null) + if ((commentOutput.search(/\B\$\S*\$\B/gm) === -1) && (commentOutput.search(/(\$SELSEGS|\$USERNAME|\$URD)/gm) === -1)) $('#panel-container .mapUpdateRequest .top-section .body .conversation .new-comment-text').blur().focus(); else $('#panel-container .mapUpdateRequest .top-section .body .conversation .new-comment-text').focus(); @@ -2260,7 +2266,8 @@ function updateUrceData(urIds) { && (_wmeUserId === urceData.lastCommentBy) && !mapUrsObj[idx].attributes.reminderSent ) { - const autoPostReminderCommentResult = await autoPostReminderComment(chunk[idx], formatText(_commentList[_defaultComments.dr.commentNum].comment, true, false)); + const autoPostReminderCommentResult = await autoPostReminderComment(chunk[idx], + formatText(_commentList[_defaultComments.dr.commentNum].comment, true, false, urSessionsObj[idx].id)); if (autoPostReminderCommentResult.error) { urceData.needsReminder = true; logWarning(autoPostReminderCommentResult.text); // Don't return here as we should go ahead and process the urceData. @@ -3071,11 +3078,11 @@ function processCommentList(data) { outputItems[idx].items.push({ linkClass, commentId, - title: formatText(rowObj.comment, false, false), + title: formatText(rowObj.comment, false, false, undefined), name: rowObj.title, divDoubleClickId, divDoubleClickStyle, - divDoubleClickTitle: `${I18n.t('urce.common.DoubleClickTitle')}:\n${formatText(rowObj.comment, false, false)}` + divDoubleClickTitle: `${I18n.t('urce.common.DoubleClickTitle')}:\n${formatText(rowObj.comment, false, false, undefined)}` }); commentId++; } @@ -3850,7 +3857,7 @@ function initSettingsTab() { }, buildTextFirstNumSetting = (setting, urceprefs, min, max, step, postText) => { const translationName = I18n.t(`urce.prefs.${setting.charAt(0).toUpperCase()}${setting.slice(1)}`), - translationTitle = formatText(I18n.t(`urce.prefs.${setting.charAt(0).toUpperCase()}${setting.slice(1)}Title`), false, false); + translationTitle = formatText(I18n.t(`urce.prefs.${setting.charAt(0).toUpperCase()}${setting.slice(1)}Title`), false, false, undefined); let rVal = `
${translationName} `; if (postText !== undefined) rVal += `
${postText}
`;