123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- "use strict";
- /**
- * Licensed Materials - Property of IBM
- * IBM Cognos Products: admin
- * Copyright IBM Corp. 2015, 2018
- * US Government Users Restricted Rights - Use, duplication or
- * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- */
- define(['jquery', 'underscore', 'doT', 'bi/commons/utils/BidiUtil', 'bi/commons/i18n/Formatter', 'bi/glass/app/ContentView', 'bi/admin/system/services/ApiSvc', 'bi/admin/nls/StringResource', 'text!bi/admin/system/templates/BackupTabTemplate.html'], function ($, _, dot, BidiUtil, Formatter, View, Api, StringResource, template) {
- 'use strict'; //NOSONAR: meant to be strict
- var BackupTab = View.extend({
- init: function init(options) {
- BackupTab.inherited('init', this, arguments);
- _.extend(this, options);
- },
- _getNewDate: function _getNewDate(sDate) {
- return new Date(sDate);
- },
- render: function render() {
- return Promise.try(function () {
- var backupItem = _.max(this.items, function (item) {
- var date = new Date(item.lastModified);
- return date;
- });
- var lastModified = backupItem.lastModified ? this._formatDate(backupItem.lastModified) : StringResource.get('invalidDate');
- var templateData = {
- existingLastBackup: !_.isEmpty(this.items),
- strings: {
- lastModified: lastModified,
- name: backupItem.name || '',
- typeNameToBackup: StringResource.get('typeNameToBackup'),
- typePassToEncry: StringResource.get('typePassToEncry'),
- confirmPass: StringResource.get('confirmPass'),
- success: StringResource.get('success'),
- lastBackup: StringResource.get('lastBackup'),
- backupBtn: StringResource.get('backupBtn'),
- backupNameLabel: StringResource.get('backupNameLabel'),
- newBackupLabel: StringResource.get('newBackupLabel')
- }
- };
- this.$el.html(dot.template(template)(templateData));
- BidiUtil.initElementForBidi(this.$el.find('#backupName')[0]);
- this._bindEvents();
- }.bind(this));
- },
- _bindEvents: function _bindEvents() {
- this.$el.find('#backupBtn').on('primaryaction', this._backupAction.bind(this));
- this.$el.find('[required]').on({
- keyup: function keyup(e) {
- $(e.target.parentNode).find('.invalid').remove();
- }
- });
- },
- _formatDate: function _formatDate(date) {
- return Formatter.format(this._getNewDate(date), {
- type: 'datetime',
- formatLength: 'short',
- locale: this.glassContext.services.userProfile.preferences.contentLocale,
- timezone: this.glassContext.services.userProfile.preferences.timeZoneID
- });
- },
- _backupAction: function _backupAction() {
- this.$el.find('.invalid').remove();
- if (this.validate()) {
- this.glassContext.appController.showToast(StringResource.get('backupInprogress'), {
- 'type': 'info',
- 'timeOut': 3000
- });
- this.parentView.hide();
- var backupData = {
- password: this.$el.find('#backupPwd').val(),
- type: 'exports',
- packageName: this.$el.find('#backupName').val().replace(/\\/g, '').replace(/\//g, '')
- };
- return Api.generateExportImportDefinition('exports', backupData).then(function (response) {
- backupData = response;
- return Api.executeExportImportDefinition('exports', backupData.id);
- }.bind(this)).then(function () {
- var dfd = $.Deferred();
- var int;
- var func = function () {
- Api.queryStatus('exports', backupData.id).then(function (data) {
- clearInterval(int);
- this.glassContext.appController.showToast(StringResource.get('backedupSucceeded', {
- 'name': data.packageName
- }), {
- 'type': 'success',
- 'timeOut': 3000
- });
- dfd.resolve();
- }.bind(this)).fail(function (failDfd, jqXHR) {
- clearInterval(int);
- dfd.reject(failDfd, jqXHR);
- }.bind(this));
- }.bind(this);
- int = setInterval(func, 10000);
- return dfd.promise();
- }.bind(this)).fail(function (dfd, jqXHR) {
- this.logger.error(jqXHR);
- this.glassContext.appController.showErrorMessage(jqXHR.responseJSON.messages, StringResource.get('error'));
- this.glassContext.appController.showToast(StringResource.get('backedupFailed', {
- 'name': backupData.packageName
- }), {
- 'type': 'fail',
- 'timeOut': 3000
- });
- }.bind(this));
- }
- },
- _checkSpace: function _checkSpace(el) {
- var reg = /^\s*$/;
- $(el.parentNode).find('.invalid').remove();
- var value = $(el).val();
- return !(value && reg.test(value));
- },
- _checkPassword: function _checkPassword() {
- this.$el.find('.invalid').remove();
- var passWords = this.$el.find(':password');
- $(passWords[1].parentNode).find('.invalid').remove();
- var returnValue = true;
- if (passWords[0].value !== passWords[1].value && passWords[0].value !== '' && passWords[1].value !== '') {
- $(passWords[1].parentNode).append($("<label class='invalid'/>").text(StringResource.get('passwordNotMatchError')));
- $(passWords[1].parentNode).children('label.invalid').prepend($('<svg class="svgIcon bi-admin-warning-svgIcon invalid"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#common-warning"></use></svg>'));
- returnValue = false;
- } else if (passWords[0].value.length < 8) {
- $(passWords[1].parentNode).append($("<label class='invalid'/>").text(StringResource.get('passwordLessThanEight')));
- $(passWords[1].parentNode).children('label.invalid').prepend($('<svg class="svgIcon bi-admin-warning-svgIcon invalid"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#common-warning"></use></svg>'));
- returnValue = false;
- }
- return returnValue;
- },
- validate: function validate() {
- var valid = true;
- var resetFocus;
- var temp = true;
- this.$el.find('[required]').each(function (index, anInput) {
- if (anInput.checkValidity()) {
- if (!this._checkSpace(anInput)) {
- $(anInput.parentNode).append($("<label class='invalid'/>").text(StringResource.get('SpaceInputError')));
- $(anInput.parentNode).children('label.invalid').prepend($('<svg class="svgIcon bi-admin-warning-svgIcon invalid"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#common-warning"></use></svg>'));
- valid = false;
- }
- } else {
- $(anInput.parentNode).append($("<label class='invalid'/>").text(StringResource.get('requiredError')));
- $(anInput.parentNode).children('label.invalid').prepend($('<svg class="svgIcon bi-admin-warning-svgIcon invalid"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#common-warning"></use></svg>'));
- valid = false;
- }
- if (!valid && temp) {
- resetFocus = $(anInput.parentNode).find('input');
- temp = false;
- }
- }.bind(this));
- valid = valid ? this._checkPassword() : false;
- if (!valid) {
- if (!resetFocus) {
- resetFocus = this.$el.find('.bi-admin-content-inputLine #backupPwd');
- }
- resetFocus.focus();
- }
- return valid;
- }
- });
- return BackupTab;
- });
|