/*
* views/infoController.js Copyright 2022 cPanel, L.L.C.
* All rights reserved.
* copyright@cpanel.net http://cpanel.net
* This code is subject to the cPanel license. Unauthorized copying is prohibited
*/
/* eslint-env amd */
define([
"angular",
"cjt/util/locale",
"ngSanitize",
"cjt/directives/validationContainerDirective",
"cjt/directives/validationItemDirective",
"cjt/validator/domain-validators",
"cjt/validator/email-validator",
"app/services/setupService",
], function(angular, LOCALE) {
"use strict";
function InfoController(PAGE, $location, setupService, $scope, $window, $q) {
this.PAGE = PAGE;
this.$location = $location;
this.setupService = setupService;
this.$scope = $scope;
this.$window = $window;
this.$q = $q;
if (!PAGE.has_accepted_legal_agreements) {
$location.path("/legal");
}
if (PAGE.has_completed_initial_setup || PAGE.is_dnsonly) {
this.exit();
}
// Create copies of the initial data, for later comparisons
this.email = PAGE.email;
this.nameservers = PAGE.nameservers && PAGE.nameservers.slice() || [];
this.finishButtonText = LOCALE.maketext("Finish");
if (!PAGE.has_license && !this.setupService.isEligibleForTrial) {
this.finishButtonText = LOCALE.maketext("Next");
}
}
InfoController.$inject = [
"PAGE",
"$location",
"setupService",
"$scope",
"$window",
"$q",
];
/**
* Sets the provided values via the API and pushes forward to the next step.
* @method submit
*/
InfoController.prototype.submit = function() {
var self = this;
if (self.isSubmitting) {
return;
}
if (self.$scope.infoForm.$invalid) {
_validateControls(self.$scope.infoForm); // Recursively run $validate() on all ngModelControllers
return;
}
self.isSubmitting = true;
// Check whether or not anything has changed from the initial values to see if we should make an API call
var emailHasChanged = _normalizeFalsyVals(self.PAGE.email) !== _normalizeFalsyVals(self.email);
var nameserversHaveChanged = self.nameservers.some(function(nameserver, index) {
return _normalizeFalsyVals(nameserver) !== _normalizeFalsyVals(self.PAGE.nameservers[index]);
});
// If nothing has changed, we can exit early
if (!emailHasChanged && !nameserversHaveChanged) {
self.exit();
return;
}
// Otherwise, we need to wait on the API calls
var promise = self.$q.resolve();
var errorKeys = [];
if ( emailHasChanged ) {
promise = promise.then(function() {
return self.setupService.setEmail(self.email);
}).catch(function(error) {
errorKeys.push("contact_email");
});
}
if ( nameserversHaveChanged ) {
promise = promise.then(function() {
return self.setupService.setNameservers(self.nameservers);
}).catch(function(error) {
errorKeys.push("nameservers");
});
}
promise.then(function() {
if (errorKeys.length) {
return self.setupService.saveErrors(errorKeys);
}
}).finally(function() {
self.exit();
});
};
/**
* Runs $validate() on any NgModelController instances found. If the controller
* passed in is an instance of FormController, then this function will process
* the entire tree of FormController instances.
*
* @param {Controller} controller The FormController or NgModelController to process.
*/
function _validateControls(controller) {
if (controller.$validate) {
controller.$setDirty();
controller.$validate();
} else {
// Hack until we update AngularJS and get FormController.getControls()
Object.keys(controller).forEach(function(key) {
// Skip built-in keys
if (key[0] === "$") {
return;
}
_validateControls( controller[key] );
});
}
}
/**
* Sets the given nameserver index back to its initial value.
*
* @param {Number} index The index in this.nameservers/PAGE.nameservers
*/
InfoController.prototype.resetNsInput = function(index) {
this.nameservers[index] = this.PAGE.nameservers[index];
};
/**
* Gives the text for the reset button title.
*
* @param {Number} index The index in this.nameservers
* @return {String} The title text.
*/
InfoController.prototype.resetTitleText = function(index) {
return LOCALE.maketext("Reset to the original value: [_1]", this.PAGE.nameservers[index]);
};
/**
* Determines whether or not a reset button should be disabled.
* @param {Number} index The index in this.nameservers
* @return {Boolean} True if it should be disabled. False otherwise
*/
InfoController.prototype.shouldDisableReset = function(index) {
return _normalizeFalsyVals( this.PAGE.nameservers[index] ) === _normalizeFalsyVals( this.nameservers[index] );
};
function _normalizeFalsyVals(str) {
return !str ? "" : str;
}
/**
* Exits the initial setup assistant and drops them to the next step.
* @method exit
*/
InfoController.prototype.exit = function() {
if (this.PAGE.requested_initial_website) {
this.$location.path("/initial-website");
} else {
this.$window.location.href = "../initial_setup_wizard1_do";
}
};
angular
.module("whm.initialSetup.infoController", [
"ngSanitize",
"cjt2.validate",
"cjt2.directives.validationContainer",
"cjt2.directives.validationItem",
"whm.initialSetup.setupService",
])
.controller("infoController", InfoController);
return InfoController;
});