diff --git a/assets/javascripts/issue_dynamic_edit.js b/assets/javascripts/issue_dynamic_edit.js index 57a1576..dab37bc 100644 --- a/assets/javascripts/issue_dynamic_edit.js +++ b/assets/javascripts/issue_dynamic_edit.js @@ -6,119 +6,141 @@ var _CONF_LISTENER_TYPE = _CONF_LISTENER_TYPE || "click"; var _CONF_LISTENER_TARGET = _CONF_LISTENER_TARGET || "value"; var _CONF_EXCLUDED_FIELD_ID = _CONF_EXCLUDED_FIELD_ID || []; -/* - * Allow inclusion from other page +/* + * Allow inclusion from other page * See https://github.com/Ilogeek/redmine_issue_dynamic_edit/commit/26684a2dd9b12dcc7377afd79e9fe5c142d26ebd for more info */ -var LOCATION_HREF = typeof custom_location_href !== 'undefined' ? custom_location_href : window.location.href; -if(_CONF_FORCE_HTTPS) { LOCATION_HREF = LOCATION_HREF.replace(/^http:\/\//i, 'https://'); } +var LOCATION_HREF = typeof custom_location_href !== 'undefined' ? custom_location_href : window.location.href; + +if (_CONF_FORCE_HTTPS) { + LOCATION_HREF = LOCATION_HREF.replace(/^http:\/\//i, 'https://'); +} + /* FontAwesome inclusion */ var cssId = 'fontAwesome'; - if (!document.getElementById(cssId)) - { - var head = document.getElementsByTagName('head')[0]; - var link = document.createElement('link'); - link.id = cssId; - link.rel = 'stylesheet'; - link.type = 'text/css'; - link.href = 'https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css'; - link.media = 'all'; - head.appendChild(link); - } -$(document).on(_CONF_LISTENER_TYPE, function(e){ +if (!document.getElementById(cssId)) { + var head = document.getElementsByTagName('head')[0]; + var link = document.createElement('link'); + link.id = cssId; + link.rel = 'stylesheet'; + link.type = 'text/css'; + link.href = 'https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css'; + link.media = 'all'; + head.appendChild(link); +} + +$(document).on(_CONF_LISTENER_TYPE, function(e) { $('.issue .attributes .attribute .value').removeClass('edited'); - if($(e.target).closest('a').length){ return; } - if($(e.target).closest('.' + _CONF_LISTENER_TARGET).length) { + + if ($(e.target).closest('a').length) { return; } + if ($(e.target).closest('.' + _CONF_LISTENER_TARGET).length) { // avoid text selection if dblclick var sel = window.getSelection ? window.getSelection() : document.selection; var activeElement = document.activeElement; var inputs = ['input', 'select', 'button', 'textarea']; if (sel && inputs.indexOf(activeElement.tagName.toLowerCase()) === -1) { - if (sel.removeAllRanges) { - sel.removeAllRanges(); - } else if (sel.empty) { - sel.empty(); - } + if (sel.removeAllRanges) { + sel.removeAllRanges(); + } else if (sel.empty) { + sel.empty(); + } } + // we show the edit box - $(e.target).closest('.value').addClass('edited'); - } + $(e.target).closest('.value').addClass('edited'); + } }); function isExcluded(elmt_id) { - return _CONF_EXCLUDED_FIELD_ID.indexOf(elmt_id) > -1; + return _CONF_EXCLUDED_FIELD_ID.indexOf(elmt_id) > -1; } -function initEditFields() -{ - /* Put new dropdown lists in the detailed info block */ - if($('#statusListDropdown').length > 0 && !isExcluded('statusListDropdown')) { +function initEditFields() { + /* Put new dropdown lists in the detailed info block */ + if ($('#statusListDropdown').length > 0 && !isExcluded('statusListDropdown')) { var htmlCopy = $('#statusListDropdown').get(0).outerHTML; $('#statusListDropdown').remove(); - $('.details .attributes .status.attribute .value').html( '' + - $('.details .attributes .status.attribute .value').html() + ' ' + - htmlCopy); - } - - if($('#prioritiesListDropdown').length > 0 && !isExcluded('prioritiesListDropdown')) { - var htmlCopy = $('#prioritiesListDropdown').get(0).outerHTML; - $('#prioritiesListDropdown').remove(); - $('.details .attributes .priority.attribute .value').html( '' + - $('.details .attributes .priority.attribute .value').html() + ' ' + - htmlCopy); + $('.details .attributes .status.attribute .value').html( + '' + + $('.details .attributes .status.attribute .value').html() + + ' ' + + htmlCopy + ); } - if($('#doneRatioListDropdown').length > 0 && !isExcluded('doneRatioListDropdown')) { + if ($('#prioritiesListDropdown').length > 0 && !isExcluded('prioritiesListDropdown')) { + var htmlCopy = $('#prioritiesListDropdown').get(0).outerHTML; + $('#prioritiesListDropdown').remove(); + $('.details .attributes .priority.attribute .value').html( + '' + + $('.details .attributes .priority.attribute .value').html() + + ' ' + + htmlCopy + ); + } + + if ($('#doneRatioListDropdown').length > 0 && !isExcluded('doneRatioListDropdown')) { var htmlCopy = $('#doneRatioListDropdown').get(0).outerHTML; $('#doneRatioListDropdown').remove(); - $('.details .attributes .progress.attribute .value').html('' + + $('.details .attributes .progress.attribute .value').html('' + $('.details .attributes .progress.attribute .value').html() + ' ' + htmlCopy); } - if($('#EstimatedTimeInput').length > 0 && !isExcluded('EstimatedTimeInput')) { + if ($('#EstimatedTimeInput').length > 0 && !isExcluded('EstimatedTimeInput')) { var htmlCopy = $('#EstimatedTimeInput').get(0).outerHTML; $('#EstimatedTimeInput').remove(); - $('.details .attributes .estimated-hours.attribute .value').html('' + - $('.details .attributes .estimated-hours.attribute .value').html() + ' ' + - htmlCopy); + $('.details .attributes .estimated-hours.attribute .value').html( + '' + + $('.details .attributes .estimated-hours.attribute .value').html() + + ' ' + + htmlCopy + ); } - if($('#StartDateInput').length > 0 && !isExcluded('StartDateInput')) { + if ($('#StartDateInput').length > 0 && !isExcluded('StartDateInput')) { var htmlCopy = $('#StartDateInput').get(0).outerHTML; $('#StartDateInput').remove(); - $('.details .attributes .start-date.attribute .value').html('' + - $('.details .attributes .start-date.attribute .value').html() + ' ' + - htmlCopy); + $('.details .attributes .start-date.attribute .value').html( + '' + + $('.details .attributes .start-date.attribute .value').html() + + ' ' + + htmlCopy + ); } - if($('#DueDateInput').length > 0 && !isExcluded('DueDateInput')) { + if ($('#DueDateInput').length > 0 && !isExcluded('DueDateInput')) { var htmlCopy = $('#DueDateInput').get(0).outerHTML; $('#DueDateInput').remove(); - $('.details .attributes .due-date.attribute .value').html('' + - $('.details .attributes .due-date.attribute .value').html() + ' ' + - htmlCopy); + $('.details .attributes .due-date.attribute .value').html( + '' + + $('.details .attributes .due-date.attribute .value').html() + + ' ' + + htmlCopy + ); } - if($('#TitleInput').length > 0 && !isExcluded('TitleInput')) { + if ($('#TitleInput').length > 0 && !isExcluded('TitleInput')) { var htmlCopy = $('#TitleInput').get(0).outerHTML; $('#TitleInput').remove(); $('.subject h3').html('' + $('.subject h3').html() + ' ' + htmlCopy).addClass('value'); } - if($('#DescriptionInput').length > 0 && !isExcluded('DescriptionInput')) { + if ($('#DescriptionInput').length > 0 && !isExcluded('DescriptionInput')) { var htmlCopy = $('#DescriptionInput').get(0).outerHTML; $('#DescriptionInput').remove(); - $('div.description .wiki').html(' ' + $('div.description .wiki').html() + '' + - htmlCopy).addClass('value'); + $('div.description .wiki').html( + ' ' + + $('div.description .wiki').html() + '' + + htmlCopy + ).addClass('value'); } - if($('select#issue_assigned_to_id').length > 0 && !isExcluded('issue_assigned_to_id')) - { + if ($('select#issue_assigned_to_id').length > 0 && !isExcluded('issue_assigned_to_id')) { var htmlCopy = $('select#issue_assigned_to_id').get(0).outerHTML; // 2 technics with simple or double quote (safety first) htmlCopy = htmlCopy.replace(/id="/g, 'id="dynamic_').replace(/id='/g, "id='dynamic_"); @@ -127,14 +149,16 @@ function initEditFields() editHTML += htmlCopy; editHTML += " "; editHTML += ""; - - $('.details .attributes .assigned-to.attribute .value').html('' + - $('.details .attributes .assigned-to.attribute .value').html() + ' ' + - editHTML); + + $('.details .attributes .assigned-to.attribute .value').html( + '' + + $('.details .attributes .assigned-to.attribute .value').html() + + ' ' + + editHTML + ); } - if($('select#issue_fixed_version_id').length > 0 && !isExcluded('issue_fixed_version_id')) - { + if ($('select#issue_fixed_version_id').length > 0 && !isExcluded('issue_fixed_version_id')) { var htmlCopy = $('select#issue_fixed_version_id').get(0).outerHTML; // 2 technics with simple or double quote (safety first) htmlCopy = htmlCopy.replace(/id="/g, 'id="dynamic_').replace(/id='/g, "id='dynamic_"); @@ -143,46 +167,53 @@ function initEditFields() editHTML += htmlCopy; editHTML += " "; editHTML += ""; - - $('.details .attributes .fixed-version.attribute .value').html('' + - $('.details .attributes .fixed-version.attribute .value').html() + ' ' + - editHTML); + + $('.details .attributes .fixed-version.attribute .value').html( + '' + + $('.details .attributes .fixed-version.attribute .value').html() + + ' ' + + editHTML + ); } - for(var i = 0 ; i < CF_VALUE_JSON.length ; i++) - { + for (var i = 0 ; i < CF_VALUE_JSON.length ; i++) { var info = CF_VALUE_JSON[i].custom_field; var value = CF_VALUE_JSON[i].value; - if(info.visible && info.editable && !isExcluded("issue_custom_field_values_" + info.id)) - { - if($('.details .attributes .cf_' + info.id + '.attribute .value').length - && $('#issue_custom_field_values_' + info.id).length ) - { + + if (info.visible && info.editable && !isExcluded("issue_custom_field_values_" + info.id)) { + if ( + $('.details .attributes .cf_' + info.id + '.attribute .value').length && + $('#issue_custom_field_values_' + info.id).length + ) { var htmlCopy = $('#issue_custom_field_values_' + info.id).get(0).outerHTML; - if(info.field_format == "version" && info.format_store.edit_tag_style == "check_box") - { + if (info.field_format == "version" && info.format_store.edit_tag_style == "check_box") { htmlCopy = $('#issue_custom_field_values_' + info.id).parents('span').html(); } + // 2 technics with simple or double quote (safety first) htmlCopy = htmlCopy.replace(/id="/g, 'id="dynamic_').replace(/id='/g, "id='dynamic_"); htmlCopy = htmlCopy.replace(/class="/g, 'class="cf_' + info.id + ' ').replace(/class='/g, "class='cf_" + info.id + " "); - + var editHTML = ""; - editHTML += htmlCopy; - editHTML += " "; - editHTML += " "; - editHTML += ""; - $('.details .attributes .cf_' + info.id + '.attribute .value').html('' + - $('.details .attributes .cf_' + info.id + '.attribute .value').html() + ' ' + - editHTML); + editHTML += htmlCopy; + editHTML += " "; + editHTML += " "; + editHTML += ""; - if(info.field_format == "date") - { - if( $('body').find('#dynamic_issue_custom_field_values_' + info.id).length - && $('body').find('#dynamic_issue_custom_field_values_' + info.id).datepickerFallback instanceof Function - && typeof datepickerOptions !== 'undefined') - { + $('.details .attributes .cf_' + info.id + '.attribute .value').html( + '' + + $('.details .attributes .cf_' + info.id + '.attribute .value').html() + + ' ' + + editHTML + ); + + if (info.field_format == "date") { + if ( + $('body').find('#dynamic_issue_custom_field_values_' + info.id).length && + $('body').find('#dynamic_issue_custom_field_values_' + info.id).datepickerFallback instanceof Function && + typeof datepickerOptions !== 'undefined' + ) { $('body').find('#dynamic_issue_custom_field_values_' + info.id).datepickerFallback(datepickerOptions); } } @@ -194,20 +225,22 @@ function initEditFields() initEditFields(); /* Add required style to attributes */ -function updateRequiredFields(reqFieldsArray) -{ +function updateRequiredFields(reqFieldsArray) { for (var i = 0; i < reqFieldsArray.length; i++) { - var htmlLabel = reqFieldsArray[i].replace(/_/g, '-'); - $('.issue.details .attribute.' + htmlLabel + ' .label').html('' + $('.issue.details .attribute.' + htmlLabel + ' .label').html() + ' *'); + var htmlLabel = reqFieldsArray[i].replace(/_/g, '-'); + $('.issue.details .attribute.' + htmlLabel + ' .label').html( + '' + + $('.issue.details .attribute.' + htmlLabel + ' .label').html() + + ' *' + ); } - } - -if($('#required_field_array').length) { - updateRequiredFields(JSON.parse($('#required_field_array').html())); } +if ($('#required_field_array').length) { + updateRequiredFields(JSON.parse($('#required_field_array').html())); +} -$('body.controller-issues.action-show').on('click', '.btn.close', function(e){ +$('body.controller-issues.action-show').on('click', '.btn.close', function(e) { e.preventDefault(); $(e.target).closest('.value').removeClass('edited'); return false; @@ -216,153 +249,146 @@ $('body.controller-issues.action-show').on('click', '.btn.close', function(e){ function getLastLockVersion() { var token = $("meta[name=csrf-token]").attr('content'); var lock_version = $('#issue_lock_version').val(); + + jQuery.ajax({ - type: 'GET', - url: LOCATION_HREF, - data: { "authenticity_token" : token }, + type: 'GET', + url: LOCATION_HREF, + data: { "authenticity_token" : token }, crossDomain: true, - async: false, - beforeSend: function(xhr) { - xhr.setRequestHeader("authenticity_token", token); - }, - success: function(msg) { - parsed = $.parseHTML(msg); - lock_version = $(parsed).find('#issue_lock_version').val(); - } + async: false, + beforeSend: function(xhr) { + xhr.setRequestHeader("authenticity_token", token); + }, + success: function(msg) { + parsed = $.parseHTML(msg); + lock_version = $(parsed).find('#issue_lock_version').val(); + } }); return lock_version; } -function issueDynamicUpdate(field_name, field_value, type, cssClass){ - +function issueDynamicUpdate(field_name, field_value, type, cssClass) { /* hide edit field */ $('.details .' + cssClass + ' .value').removeClass('edited'); /* add spin notification */ $('.details .' + cssClass + ' .value').append(' '); - + /* update value displayed */ - $('.details .' + cssClass + ' .showValue').html(function(){ - if(type == "select") - { + $('.details .' + cssClass + ' .showValue').html(function() { + if (type == "select") { return $('.details .' + cssClass + ' .value select option:selected').html() - } else if (type == "input") - { + } else if (type == "input") { return $('.details .' + cssClass + ' .value input').val() - } else if(type == "textarea") - { + } else if (type == "textarea") { return $('.details .' + cssClass + ' .value textarea').val() - } else if(type == "date") - { + } else if (type == "date") { return "XXXX/XX/XX"; } }); - + /* lost focus on element */ - if( type != "select") - { + if (type != "select") { $('.details .' + cssClass + ' .value input').blur(); } - + var token = $("meta[name=csrf-token]").attr('content'); - + $('#issue-form').find("#issue_" + field_name).val(field_value).css({"display": "inline-block"}); // avoid conflict revision var lastLockVersion = getLastLockVersion(); $('#issue_lock_version').val(lastLockVersion); var formData = $('#issue-form').serialize(); - + + jQuery.ajax({ - type: 'POST', - url: LOCATION_HREF, - data: formData, + type: 'POST', + url: LOCATION_HREF, + data: formData, beforeSend: function(xhr) { - xhr.setRequestHeader("authenticity_token", token); - }, - success: function(msg) { - /* get result page content (updated issue detail page with new status) */ - - var parsed = $.parseHTML(msg); - + xhr.setRequestHeader("authenticity_token", token); + }, + success: function(msg) { + /* get result page content (updated issue detail page with new status) */ + + var parsed = $.parseHTML(msg); + var error = $(parsed).find("#errorExplanation"); - - if(error.length) - { - if($('html').find("#errorExplanation").length == 0) - { + + if (error.length) { + if ($('html').find("#errorExplanation").length == 0) { $('.issue.details').before("