JD2022-TU1/data/EngineData/SubmitAssistant/view/submit_assistant.js

319 lines
11 KiB
JavaScript

define([
"constants",
"storage_utils",
"error_messages",
"view/changelist",
"view/validation",
"text!template/submit_assistant.html",
"jquery",
"underscore",
"backbone",
"bootstrap",
"dropdown",
"messagebox",
],
function(constants, storage_utils, error_messages,
ChangelistView, ValidationView,
tmpl_submit_assistant){
/**
* Main view, manage all the other views.
* Composed of tabs (if jira tab activated) and submit buttons.
* Manage validation process.
*/
var SubmitAssistantView = Backbone.View.extend({
el: "#submit_assistant",
events: {
"click #validate_button": "validateChange",
"click #save_button": "saveChangeAndClose",
"click #cancel_button": "closeSA"
},
/**
* See Backbone documentation.
*
* @param model - the model of the SA.
* @param changelistModel - the model of the CL.
* @param filesCollection - the files of the CL.
*/
initialize: function() {
this.changelistModel = this.options.changelistModel;
this.filesCollection = this.options.filesCollection;
this.checkAPIVersion();
this.render();
//by default changelist view is always displayed
this.changelistView = new ChangelistView({
type_SA: this.model.get("type_SA"),
model: this.changelistModel,
filesCollection: this.filesCollection,
});
//enable buttons if a saved CL with the SA is loaded
this.updateSubmitButtons();
//listen on CL validity to enable / disable buttons
this.changelistModel.on("change:is_valid", this.updateSubmitButtons, this);
if (this.model.get("is_validation_branch")) {
this.validationView = new ValidationView({
model: this.changelistModel,
});
//if validation process is activated, listened on validaton success event to submit CL
this.changelistModel.on(constants.event_success_validation, $.proxy(this.submitChange, this));
}
},
AskForUserConfirmation: function() {
return $.MessageBox({
buttonDone: "Yes",
buttonFail: "No",
message: "Preflight is mandatory on Just Dance. Are you sure you want to submit WITHOUT preflight?"
});
},
render: function() {
var viewModel = this;
var template = _.template(tmpl_submit_assistant, {
model: this.model.toJSON(),
constants: constants,
});
this.$el.html(template);
this.$buttons = this.$("#buttons_div input");
this.$submit_buttons = this.$buttons.filter(".submit");
$(".submitoptions").dropdown();
// this functoin helps in deciding what needs to be done when user select option from submit options dropdown
$(".submitoptions .menu .item").on("click", function(event, info) {
var idOfButtonClicked = $(event.currentTarget).data('value');
var id = parseInt(idOfButtonClicked);
switch (id) {
case 1:
// preflight only
if (viewModel.model.get("type_SA") === constants.type_code) {
viewModel.preflightOnly();
} else {
viewModel.validate();
}
break;
case 2:
// submit only
viewModel.AskForUserConfirmation()
.done(function() {
viewModel.submitChange();
});
break;
}
});
$(".preflightAndSaveButton").on("click", function() {
if (viewModel.model.get("type_SA") === constants.type_code) {
viewModel.preflightAndSubmit();
} else {
viewModel.validateAndSubmit();
}
});
},
/**
* Check P4JsApi version (1.2 needed).
* Close the SA if version < 1.2.
*/
checkAPIVersion: function() {
if (this.model.get("version") < constants.API_version_limit) {
alert(error_messages.API_version);
storage_utils.closeSA();
}
},
/**
* Save the first tag of the description in the localstorage.
*/
saveTag: function() {
if (this.model.get("type_SA") === constants.type_code) {
storage_utils.localSave("scope_tag", this.changelistModel.get("scope_tag"));
} else if (this.model.get("type_SA") === constants.type_data) {
storage_utils.localSave("domain_tag", this.changelistModel.get("domain_tag"));
}
},
/**
* Save the changelist. And returns the id of the changelist created/saved
*/
saveChange: function() {
this.$buttons.attr("disabled", "disabled");
var id = storage_utils.saveCL(this.changelistModel, this.filesCollection);
this.saveTag();
return id;
},
saveChangeAndClose: function () {
this.saveChange();
storage_utils.closeSA();
},
/**
* Submit the changelist.
*/
submitChange: function () {
this.changelistModel.checkAndAddPreflightTag();
this.$buttons.attr("disabled", "disabled");
if (this.validationView) {
this.validationView.hide();
}
var onSubmit = this.changelistView.getSubmitOptions();
storage_utils.submitCL(this.changelistModel, this.filesCollection, onSubmit);
this.saveTag();
storage_utils.closeSA();
},
/**
* Enable / disable submit buttons according to changelist validity.
*/
updateSubmitButtons: function() {
if (this.changelistModel.get("is_valid")) {
this.$submit_buttons.removeAttr("disabled");
$(".preflightAndSaveButton").removeClass("disabled");
$(".submitoptions").removeClass("disabled"); // for the submit option dropdown
$("#save_button").removeClass("disabled");
} else {
this.$submit_buttons.attr("disabled", "disabled");
$(".preflightAndSaveButton").addClass("disabled");
$(".submitoptions").addClass("disabled");
$("#save_button").addClass("disabled");
}
},
/**
* Show the validation window to validate the CL.
*/
validateChange: function() {
this.validationView.show();
},
/**
* Close the SA.
*/
closeSA: function() {
this.$buttons.attr("disabled", "disabled");
storage_utils.closeSA();
},
/*
* Function to check if the user is trying to submit the default changelist. If yes, then we save it as
* a new changelist and return the newly create changelist ID to be used later.
*/
getOrCreateChangelist: function() {
var self = this;
var chageListID = P4JsApi.getSubmitChange();
var IsDefaultChangeList = chageListID == "default" ? true : false;
// CHECK IF THE USER IS TRYING TO SUBMIT THE DEFAULT CHANGLIST
if (IsDefaultChangeList) {
// if yes we do following step before trigerring the preflight and submit
chageListID = self.saveChange();
}
if (!IsDefaultChangeList) {
self.saveChange();
}
return chageListID;
},
/**
* Sends an HTTP request to the TeaBuddy to start a preflight and Submit the changlist.
*/
preflightAndSubmit: function() {
var chageListID = this.getOrCreateChangelist();
var shouldCloseJIRA = this.changelistView.getJIRAToBeClosedStatus();
var macroName = "QuickBuild.PreflightMacros.SubmitCodeChangelist";
var macroArguments = { changelistToPreflight: chageListID, closeJira: shouldCloseJIRA };
this.makeRequestToTeaBox(macroName, macroArguments);
},
/**
* Sends an HTTP request to the TeaBuddy to start a preflight.
*/
preflightOnly: function () {
var chageListID = this.getOrCreateChangelist();
var macroName = "QuickBuild.PreflightMacros.PreflightCodeChangelist";
var macroArguments = { changelistToPreflight: chageListID };
this.makeRequestToTeaBox(macroName, macroArguments);
},
validateAndSubmit: function () {
var chageListID = this.getOrCreateChangelist();
var shouldCloseJIRA = this.changelistView.getJIRAToBeClosedStatus();
var macroName = "QuickBuild.PreflightMacros.SubmitDataChangelist";
var macroArguments = { changelistToPreflight: chageListID, closeJira: shouldCloseJIRA };
this.makeRequestToTeaBox(macroName, macroArguments);
},
validate: function () {
var chageListID = this.getOrCreateChangelist();
var macroName = "QuickBuild.PreflightMacros.PreflightDataChangelist";
var args = { changelistToPreflight: chageListID };
this.makeRequestToTeaBox(macroName, args);
},
makeRequestToTeaBox: function (macroName, macroArguments) {
var self = this;
jQuery.ajax({
type: "POST",
url: "http://127.0.0.1:4242/",
data: JSON.stringify({
name: macroName,
args: macroArguments
})
})
.done(function(){
document.getElementById("http_error").style.display = "none";
self.closeSA();
})
.fail(function (jqHXR, textStatus, errorThrown) {
var error = jqHXR.status >= 400 && jqHXR.status < 500
? "Could not reach TeaBox. Please make sure it is running.<br />You can find it in: <i>//JD_CODE/<streamName>/tools/apps/TeaBox/TeaBox.exe</i>"
: jqHXR.responseText;
document.getElementById("http_error").innerHTML = "<strong>Error!</strong> " + error;
document.getElementById("http_error").style.display = "block";
});
},
});
return SubmitAssistantView;
});