238 lines
11 KiB
JavaScript
238 lines
11 KiB
JavaScript
/* ===========================================================
|
|
* scenarioStatusToggle.js
|
|
* ===========================================================
|
|
* Copyright 2017 Prevu
|
|
* ========================================================== */
|
|
|
|
(function ($) {
|
|
|
|
"use strict"; // jshint ;_;
|
|
/* scenarioStatusToggle CLASS DEFINITION
|
|
* ====================== */
|
|
|
|
var scenarioStatusToggle = function (element, options) {
|
|
this.init(element, options);
|
|
};
|
|
var createActivateScenarioDialog = function (activationConfirmMessage, saveButtonText) {
|
|
var dlgActivateScenario = $('#dlgActivateScenario');
|
|
if (dlgActivateScenario.length <= 0) {
|
|
$('body').append('<div id="dlgActivateScenario" class="modal fade" role="dialog" style="display: none;" data-backdrop="static"></div>');
|
|
dlgActivateScenario = $('#dlgActivateScenario');
|
|
}
|
|
|
|
var markup =
|
|
'<div class="modal-dialog" style="width:730px;">\
|
|
<div class="modal-content">\
|
|
<div class="modal-header">\
|
|
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>\
|
|
<h4 class="modal-title">Scenario Activation</h4>\
|
|
</div>\
|
|
<div class="modal-body" style="padding: 0 15px;">\
|
|
<div class="panel-body form-horizontal">\
|
|
<div class="alert alert-warning active-scenario-change-confirmation hidden">\
|
|
'+ activationConfirmMessage + '\
|
|
</div>\
|
|
<form action="#" method="post">\
|
|
<div class="dependency-container hidden"></div>\
|
|
</form>\
|
|
</div>\
|
|
</div>\
|
|
<div class="modal-footer">\
|
|
<button type="button" class="btn btn-primary">' + saveButtonText +'</button>\
|
|
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>\
|
|
</div>\
|
|
</div>\
|
|
</div>';
|
|
dlgActivateScenario.html(markup);
|
|
|
|
return dlgActivateScenario;
|
|
};
|
|
|
|
scenarioStatusToggle.prototype = {
|
|
constructor: scenarioStatusToggle,
|
|
init: function (element, options) {
|
|
var plugin = this;
|
|
plugin.$options = options;
|
|
plugin.$element = $(element);
|
|
plugin.$scenario = plugin.$options.scenario || plugin.$element.data('status-toggle-scenario');
|
|
if (!plugin.$scenario) {
|
|
throw "Required scenario parameter does not exist. Initiating of scenarioStatusToggle widget cannot be performed.";
|
|
}
|
|
if (plugin.$options.runType !== 'OnDemand') {
|
|
plugin.$element.on('click', function () {
|
|
plugin['toggleStatus'].apply(plugin);
|
|
});
|
|
}
|
|
},
|
|
toggleStatus: function () {
|
|
var args = Array.prototype.slice.call(arguments || {});
|
|
var plugin = this;
|
|
if (!CheckLock(plugin.$element.attr('id'), 'Scenario', plugin.$scenario.scenarioId)) {
|
|
if (plugin.$options.cancel && typeof plugin.$options.cancel === 'function') {
|
|
plugin.$options.cancel(args);
|
|
}
|
|
return;
|
|
}
|
|
|
|
var request = {
|
|
'projectId': plugin.$scenario.projectId
|
|
};
|
|
$.get(plugin.$options.checkActiveScenarioUrl, request, function (data) {
|
|
if (!data) {
|
|
throw 'Cannot get information about active scenario for project = ' + plugin.$scenario.projectId;
|
|
}
|
|
|
|
if (plugin.$scenario.scenarioId !== data.activeScenarioId || plugin.$options.alwaysShowConfirmation) {
|
|
var $document = $(document);
|
|
var $dlgForm = createActivateScenarioDialog(plugin.$options.activationConfirmMessage, plugin.$options.saveButtonText);
|
|
var $container = $dlgForm.find('.modal-body > .panel-body > form > div.dependency-container');
|
|
var $panelBody = $dlgForm.find('.modal-body > .panel-body');
|
|
var $confirmationEl = $panelBody.find('div.active-scenario-change-confirmation');
|
|
var $submit = $dlgForm.find('.modal-footer button.btn-primary');
|
|
|
|
plugin.$dlgForm = $dlgForm;
|
|
plugin.$container = $container;
|
|
$submit.on('click', function () {
|
|
plugin['submitResolveConflictsForm'].apply(plugin, args);
|
|
});
|
|
$container.resolveForm({
|
|
projectId: plugin.$scenario.projectId,
|
|
scenarioId: plugin.$scenario.scenarioId,
|
|
forceReinit: true,
|
|
minStartDate: plugin.$options.minStartDate,
|
|
maxEndDate: plugin.$options.maxEndDate,
|
|
});
|
|
$container.resolveForm('validate', [plugin.$scenario.startDate, plugin.$scenario.endDate, function (status, hasConflicts) {
|
|
// window must be shown in 3 cases:
|
|
// 1) project already has active scenario
|
|
// 2) project depends on other project or vise versa and there is conflict in the relation between them
|
|
// 3) for some reasons we always have to show confirmation (e.g. for scenario activation from compare feature)
|
|
if (data.activeScenarioId || hasConflicts || plugin.$options.alwaysShowConfirmation) {
|
|
if (data.activeScenarioId || plugin.$options.alwaysShowConfirmation) {
|
|
$confirmationEl.removeClass('hidden');
|
|
}
|
|
if (hasConflicts) {
|
|
$container.removeClass('hidden');
|
|
}
|
|
$dlgForm.modalDialogForm("show");
|
|
if (plugin.$options.cancel && typeof plugin.$options.cancel === 'function') {
|
|
$document.off('hide.bs.modal', '#dlgActivateScenario')
|
|
.on('hide.bs.modal', '#dlgActivateScenario', function () {
|
|
// we should handle cancel event only if it happened by manual window closing
|
|
if (!plugin.$cancelledBySave) {
|
|
plugin.$options.cancel(args);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
else {
|
|
plugin['toggleStatusConfirmed'].apply(plugin, [{
|
|
scenarioId: plugin.$scenario.scenarioId
|
|
}, null].concat(args));
|
|
}
|
|
}]);
|
|
} else {
|
|
plugin['toggleStatusConfirmed'].apply(plugin, [{
|
|
scenarioId: plugin.$scenario.scenarioId
|
|
}, null].concat(args));
|
|
}
|
|
});
|
|
},
|
|
submitResolveConflictsForm: function () {
|
|
var args = Array.prototype.slice.call(arguments || {});
|
|
var plugin = this;
|
|
var form = plugin.$dlgForm.find('.modal-body > .panel-body form');
|
|
if (form.valid()) {
|
|
var dependencies = plugin.$container.data('resolveForm').getData();
|
|
plugin.$container.resolveForm('validate', [dependencies.StartDate, dependencies.EndDate, function (status) {
|
|
var saveModel = {
|
|
ScenarioId: dependencies.ScenarioId,
|
|
Dependencies: dependencies
|
|
};
|
|
plugin['toggleStatusConfirmed'].apply(plugin, [saveModel, function () {
|
|
plugin.$cancelledBySave = true;
|
|
plugin.$dlgForm.modalDialogForm("hide");
|
|
}].concat(args));
|
|
}]);
|
|
}
|
|
},
|
|
toggleStatusConfirmed: function (model, fnCallback) {
|
|
// take only additional arguments passed to the function
|
|
var args = Array.prototype.slice.call(arguments || {}, 2);
|
|
if (!model) {
|
|
throw 'model is null or undefined';
|
|
}
|
|
|
|
blockUI();
|
|
var plugin = this;
|
|
var form = plugin.$dlgForm ? plugin.$dlgForm.find('.modal-body > .panel-body form') : null;
|
|
var request = getAntiXSRFRequest(plugin.$options.toggleStatusUrl, model);
|
|
$.ajax(request).success(function (result, status, headers, config) {
|
|
var handleResponse = function () {
|
|
if (fnCallback && typeof fnCallback === 'function') {
|
|
fnCallback();
|
|
}
|
|
if (plugin.$options.success && typeof plugin.$options.success === 'function') {
|
|
plugin.$options.success(args);
|
|
}
|
|
};
|
|
if (form) {
|
|
handleAjaxResponse(result, function (response) {
|
|
handleResponse();
|
|
}, null, null, form);
|
|
} else {
|
|
handleResponse();
|
|
}
|
|
unblockUI();
|
|
}).error(function (data, status, headers, config) {
|
|
if (plugin.$options.error && typeof plugin.$options.error === 'function') {
|
|
plugin.$options.error(args);
|
|
}
|
|
unblockUI();
|
|
});
|
|
},
|
|
destroy: function () {
|
|
var plugin = this;
|
|
var e = $.Event('destroy');
|
|
plugin.$element.trigger(e);
|
|
if (e.isDefaultPrevented())
|
|
return;
|
|
|
|
plugin.$element.removeData('scenarioStatusToggle');
|
|
plugin.$element = null;
|
|
plugin.$dlgForm = null;
|
|
plugin.$container = null;
|
|
plugin.$cancelledBySave = null;
|
|
}
|
|
};
|
|
/* scenarioStatusToggle PLUGIN DEFINITION
|
|
* ======================= */
|
|
$.fn.scenarioStatusToggle = function (option, args) {
|
|
return this.each(function () {
|
|
var $this = $(this),
|
|
plugin = $this.data('scenarioStatusToggle'),
|
|
options = $.extend({}, $.fn.scenarioStatusToggle.defaults, $this.data(), typeof option === 'object' && option);
|
|
|
|
if (!plugin) {
|
|
$this.data('scenarioStatusToggle', (plugin = new scenarioStatusToggle(this, options)));
|
|
}
|
|
if (typeof option === 'string')
|
|
plugin[option].apply(plugin, [].concat(args));
|
|
});
|
|
};
|
|
$.fn.scenarioStatusToggle.defaults = {
|
|
checkActiveScenarioUrl: '/ForecastDashboard/CheckActiveScenario/',
|
|
toggleStatusUrl: '/Scenarios/ToggleStatus/',
|
|
activationConfirmMessage: 'There is an active scenario for this project already. Are you sure you want to activate this scenario instead of active one?',
|
|
saveButtonText: 'OK',
|
|
alwaysShowConfirmation: false,
|
|
scenario: null,
|
|
minStartDate: null,
|
|
maxEndDate: null,
|
|
runType: null,
|
|
success: null,
|
|
cancel: null,
|
|
error: null,
|
|
};
|
|
$.fn.scenarioStatusToggle.Constructor = scenarioStatusToggle;
|
|
}(jQuery)); |