From 127af3ae255b8c2cc3c3645a3de8007033442811 Mon Sep 17 00:00:00 2001 From: Hugo Zilliox Date: Tue, 27 Dec 2022 22:53:59 +0100 Subject: [PATCH] Check version improved (avoiding update conflicts) (#97) using Redmine REST API : https://www.redmine.org/projects/redmine/wiki/rest_issues and disabling check when tab is not focused --- README.md | 2 + assets/javascripts/issue_dynamic_edit.js | 58 +++++++++++++++++------- init.rb | 2 +- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 96ab6a1..7523565 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,8 @@ Feel free to edit `assets/stylesheets/issue_dynamic_edit.css` to update the look ### 🆕 Changelog +* **v 0.9.1** : Check version improved (avoiding update conflicts) : using [Redmine REST API](https://www.redmine.org/projects/redmine/wiki/rest_issues) and disabling check when tab is not focused (#97) +* **v 0.9.0** : JS rewritten to remove jQuery code * **v 0.8.1** : fixed Github issue #89 : Issue version check (AJAX call) may give glitch while editing text + disable global event listener on ajaxSend * **v 0.8.0** : Complete rework. Compatible with last Redmine version. New settings added : `_CONF_CHECK_ISSUE_UPDATE_CONFLICT` (#70 #88). Removed external lib (FontAwesome) (#74). Mobile style added (#87). Print style added (#84). Bug fix (#79, #85) * **v 0.7.2** : New settings added into config file (`_CONF_DISPLAY_EDIT_ICON` and `_CONF_LISTENER_TYPE_ICON`) see Configuration part for more info ; new event `none` for `_CONF_LISTENER_TYPE_VALUE` disabling listener on value ; css fix diff --git a/assets/javascripts/issue_dynamic_edit.js b/assets/javascripts/issue_dynamic_edit.js index 18fd8ec..ba21433 100644 --- a/assets/javascripts/issue_dynamic_edit.js +++ b/assets/javascripts/issue_dynamic_edit.js @@ -278,7 +278,7 @@ document.onkeydown = function(evt) { } }; -const checkVersion = function(callback){ +const getVersion = function(callback){ fetch(LOCATION_HREF, { method: 'GET', crossDomain: true, @@ -286,22 +286,46 @@ const checkVersion = function(callback){ const parser = new DOMParser(); const doc = parser.parseFromString(data, 'text/html'); const distant_version = doc.querySelector('#issue_lock_version').value; - if(distant_version !== document.querySelector('#issue_lock_version').value){ - if(!document.querySelector('#content . conflict')){ - const msg = document.createElement('div'); - msg.classList.add('conflict'); - msg.innerHTML = `${_TXT_CONFLICT_TITLE} -
-
-

${_TXT_CONFLICT_LINK} ${_TXT_CONFLICT_TXT}

-
-
` - document.querySelector('#content').insertBefore(msg, document.querySelector('#content').firstChild); - } - } else { - if(document.querySelector('#content .conflict')) document.querySelector('#content .conflict').remove(); - } if(callback) callback(distant_version); + return distant_version; + }).catch(err => { + console.warn('Issue while trying to get version (avoiding conflict)'); + console.log(err); + }); +} + +let loadedDate = new Date(); +const checkVersion = function(callback){ + + fetch(LOCATION_HREF + ".json", { + method: 'GET', + crossDomain: true, + }).then(res => res.text()).then(data => { + try { + const parsedData = JSON.parse(data); + const lastUpdate = new Date(parsedData.issue.updated_on); + if(lastUpdate > loadedDate){ + loadedDate = lastUpdate; + if(!document.querySelectorAll('#content .conflict').length){ + let msg = document.createElement('div'); + msg.classList.add('conflict'); + msg.innerHTML = `${_TXT_CONFLICT_TITLE} +
+
+

${_TXT_CONFLICT_LINK} ${_TXT_CONFLICT_TXT}

+
+
` + document.querySelector('#content').insertBefore(msg, document.querySelector('#content').firstChild); + } + if(callback) getVersion(callback); + } else { + if(document.querySelector('#content .conflict')) document.querySelector('#content .conflict').remove(); + if(callback) callback(parseInt(document.querySelector('#issue_lock_version').value)); + } + } catch (e) { + throw new Error('Error occured: ', e); + } + }).catch(err => { console.warn('Issue while trying to get version (avoiding conflict)'); console.log(err); @@ -313,7 +337,7 @@ let setCheckVersionInterval = function(activate){ if(!_CONF_CHECK_ISSUE_UPDATE_CONFLICT) return false; if(activate && !checkVersionInterval){ checkVersionInterval = window.setInterval(function(){ - if(Document.visibilityState === "visible") checkVersion(); + if(document.visibilityState === "visible") checkVersion(); }, 5000); } else { clearInterval(checkVersionInterval); diff --git a/init.rb b/init.rb index bc641fd..e887366 100644 --- a/init.rb +++ b/init.rb @@ -6,7 +6,7 @@ Redmine::Plugin.register :redmine_issue_dynamic_edit do name 'Redmine Dynamic edit Issue plugin' author 'Hugo Zilliox' description 'Allows users to dynamically update issue attributes in detailed view without refreshing the page (JIRA style)' - version '0.8.1' + version '0.9.1' url 'https://github.com/ilogeek/redmine_issue_dynamic_edit' author_url 'https://hzilliox.fr' end