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
This commit is contained in:
Hugo Zilliox 2022-12-27 22:53:59 +01:00
parent 9eeea54521
commit 127af3ae25
3 changed files with 44 additions and 18 deletions

View File

@ -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

View File

@ -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}
<div class="conflict-details">
<div class="conflict-journal">
<p><a href='#' onClick="window.location.href=window.location.href">${_TXT_CONFLICT_LINK}</a> <strong>${_TXT_CONFLICT_TXT}</strong></p>
</div>
</div>`
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}
<div class="conflict-details">
<div class="conflict-journal">
<p><a href='#' onClick="window.location.href=window.location.href">${_TXT_CONFLICT_LINK}</a> <strong>${_TXT_CONFLICT_TXT}</strong></p>
</div>
</div>`
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);

View File

@ -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