"use strict"; /** * Licensed Materials - Property of IBM * IBM Cognos Products: Cognos Analytics * Copyright IBM Corp. 2015, 2018 * US Government Users Restricted Rights - * Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['underscore', 'q', 'bi/admin/common/actions/ListAction', 'bi/commons/utils/BidiUtil', 'bi/admin/nls/StringResource', 'bacontentnav/utils/UIHelper', 'bi/admin/common/utils/AJAXUtils', 'ba-react-admin/ba-react-admin.min'], function (_, Q, listAction, BidiUtil, StringResource, UIHelper, AJAXUtils, AdminReact) { 'use strict'; //NOSONAR var AddInputRow = listAction.extend({ init: function init(options) { AddInputRow.inherited('init', this, options); _.extend(this, options); }, execute: function execute(type, pid) { var listControl = this.getListControl(); var rowProperties = this._getInputRowProperties(type); listControl.insertToTable({ "defaultName": rowProperties.defaultName, "type": rowProperties.type, "uid": rowProperties.uid }); this._createInput(rowProperties.className, rowProperties.defaultName, rowProperties.ajaxRequestType, pid); }, _getInputRowProperties: function _getInputRowProperties(type) { var rowObj; switch (type) { case "namespaceFolder": rowObj = { "defaultName": StringResource.get('addNewFolder'), "type": 'folder', "uid": _.uniqueId('new_folder_'), "className": "newFolderRow", "ajaxRequestType": "namespaceFolder" }; break; case "group": rowObj = { "defaultName": StringResource.get('addNewGroup'), "type": 'group', "uid": _.uniqueId('new_group_'), "className": "newGroupRow", "ajaxRequestType": "group" }; break; case "nonBrowsableGroup": rowObj = { "defaultName": StringResource.get('addNewGroup'), "type": 'nonBrowsableGroup', "uid": _.uniqueId('new_group_'), "className": "newGroupRow", "ajaxRequestType": "nonBrowsableGroup" }; break; case "role": rowObj = { "defaultName": StringResource.get('addNewRole'), "type": 'role', "uid": _.uniqueId('new_role_'), "className": "newRoleRow", "ajaxRequestType": "role" }; break; default: break; } ; return rowObj; }, _createInput: function _createInput(className, label, objectType, pid) { var listControl = this.getListControl(); $('.dataTables_scrollBody').scrollTop(0); //the new row will initially be added to the bottom, so we move it to the top var listOfTrs = listControl.$el.find('.dataTables_scrollBody').find('tbody').find('tr'); var $newTr = $(listOfTrs[listOfTrs.length - 1]).clone(); var $theDiv = $($newTr.find('.nameColumnDiv')[0]); $theDiv.addClass(className); var tdEllipsisCell = $theDiv.parents("td"); tdEllipsisCell.empty(); //insert the row object $newTr.insertAfter(listOfTrs[0]); $(listOfTrs[listOfTrs.length - 1]).remove(); var $theInput = $(document.createElement('input')); $theInput.attr('type', 'text'); $theInput.attr('id', 'inputBox'); $theInput.attr('value', label); tdEllipsisCell.append($theInput); BidiUtil.initElementForBidi($theInput[0]); $theInput[0].select(); $theInput[0].focus(); listControl.removeEmptyTableMessage(); this._addInputHandlers($theInput, objectType, pid); }, _handleBlur: function _handleBlur(evt, objectType, pid) { var deferred = Q.defer(); var listControl = this.getListControl(); // Detach the scroll event listControl.$el.find('.dataTables_scrollBody').off('scroll', null, this._onScroll); var checkList = { 'script': true }; var closeValidationErrorDialogCallback = function closeValidationErrorDialogCallback() { $(evt.currentTarget).select(); $(evt.currentTarget).focus(); }; var objectInfo = this._getInputText(listControl, objectType); var isValid = UIHelper.validateInput(objectInfo.defaultName, checkList, closeValidationErrorDialogCallback); if (isValid) { var objURL; this._handledSave = true; switch (objectType) { case 'group': objURL = AJAXUtils.getPath('createGroup', pid); break; case 'nonBrowsableGroup': objURL = AJAXUtils.getPath('createGroup', pid); break; case 'role': objURL = AJAXUtils.getPath('createRole', pid); break; case 'namespaceFolder': objURL = AJAXUtils.getPath('createNamespaceFolder', pid); break; default: objURL = ""; break; } var options = { dataType: 'json', type: 'POST', contentType: 'application/json; charset=utf-8', data: JSON.stringify(objectInfo), url: objURL, cache: false }; switch (objectType) { case 'groups': this.glassContext.appController.showToast(StringResource.get("groupToastCreateMsg", { 'name': objectInfo.defaultName })); break; case 'roles': this.glassContext.appController.showToast(StringResource.get("roleToastCreateMsg", { 'name': objectInfo.defaultName })); break; case 'folders': this.glassContext.appController.showToast(StringResource.get("folderToastCreateMsg", { 'name': objectInfo.defaultName })); break; default: break; } return this.glassContext.services.ajax.ajax(options); } return deferred.promise; }, _addInputHandlers: function _addInputHandlers(inputElement, type, pid) { var self = this; inputElement.on('keydown', function (evt) { // call the blur event when the Enter key is pressed if (evt.keyCode === 13) { $(evt.currentTarget).blur(); evt.stopPropagation(); return false; } }); inputElement.on('blur', function (evt) { this._handleBlur(evt, type, pid).then(function () { self._reloadListView(); }, function (ajaxOptions, err) { if (err.responseText) { var responseText = JSON.parse(err.responseText); self.glassContext.appController.showMessage(responseText.messages); } self._reloadListView(); }); }.bind(this)); }, _reloadListView: function _reloadListView() { if (this.accountExplorer.pagingEnabled) { var listControl = this.getListControl(); AdminReact.PaginationHelper.reloadListViewinPagingContext(this.glassContext, this.accountExplorer, listControl, this.listAdaptor, true); } else { this._reloadListViewWithouPaging(); } }, _reloadListViewWithouPaging: function _reloadListViewWithouPaging() { var listControl = this.getListControl(); listControl.dataAdaptor.sortChanged = true; listControl.reload(false).done(function () { listControl.activeInputForm = null; //this instance of AddInputRow is finished, remove its reference so that a new instance can be spawned }); }, _generateOriginalName: function _generateOriginalName(input, type) { var inputName = input; var nameCounter = 1; while (this._duplicatesExist(inputName, type) === true) { inputName = input + " (" + nameCounter + ")"; nameCounter++; } return inputName; }, _duplicatesExist: function _duplicatesExist(input) { var sameNameObjects = _.filter(this.data, function (obj) { return input.toUpperCase() === obj.defaultName.toUpperCase(); }); if (sameNameObjects.length !== 0) { return true; } return false; }, _getInputText: function _getInputText(listControl, type) { var inputValue = listControl.$el.find("#inputBox").val(); //look for duplicate objects and differentiate the name of this object if so var name = this._generateOriginalName(inputValue, type); return type === 'nonBrowsableGroup' ? { "defaultName": name } : { "defaultName": name, "defaultDescription": "" }; } }); return AddInputRow; });