From 2dd1901649bd794e20310c5c1305b7452c84fac4 Mon Sep 17 00:00:00 2001 From: Michael Abashian Date: Mon, 10 Apr 2017 11:28:54 -0400 Subject: [PATCH] Set up dynamic inventory org lookup state --- .../add/smart-inventory-add.controller.js | 2 +- .../smart-inventory/smart-inventory.form.js | 117 +----------------- .../inventories/inventory-manage.service.js | 60 +++++++++ awx/ui/client/src/inventories/main.js | 70 +++++++++-- 4 files changed, 127 insertions(+), 122 deletions(-) create mode 100644 awx/ui/client/src/inventories/inventory-manage.service.js diff --git a/awx/ui/client/src/inventories/hosts/smart-inventory/add/smart-inventory-add.controller.js b/awx/ui/client/src/inventories/hosts/smart-inventory/add/smart-inventory-add.controller.js index a97ea886d0..5ccac44ddc 100644 --- a/awx/ui/client/src/inventories/hosts/smart-inventory/add/smart-inventory-add.controller.js +++ b/awx/ui/client/src/inventories/hosts/smart-inventory/add/smart-inventory-add.controller.js @@ -51,7 +51,7 @@ function SmartInventoryAdd($scope, $location, scope: $scope, variable: 'variables', parse_variable: 'parseType', - field_id: 'inventory_variables' + field_id: 'smartinventory_variables' }); } diff --git a/awx/ui/client/src/inventories/hosts/smart-inventory/smart-inventory.form.js b/awx/ui/client/src/inventories/hosts/smart-inventory/smart-inventory.form.js index d467217078..4ba3d7e6d7 100644 --- a/awx/ui/client/src/inventories/hosts/smart-inventory/smart-inventory.form.js +++ b/awx/ui/client/src/inventories/hosts/smart-inventory/smart-inventory.form.js @@ -9,11 +9,10 @@ export default ['i18n', function(i18n) { addTitle: i18n._('NEW SMART INVENTORY'), editTitle: '{{ inventory_name }}', - name: 'inventory', + name: 'smartinventory', basePath: 'inventory', - // the top-most node of this generated state tree + breadcrumbName: 'SMART INVENTORY', stateTree: 'hosts', - tabs: true, fields: { inventory_name: { @@ -79,116 +78,6 @@ export default ['i18n', function(i18n) { ngShow: '(inventory_obj.summary_fields.user_capabilities.edit || canAdd)' } }, - related: { - permissions: { - name: 'permissions', - awToolTip: i18n._('Please save before assigning permissions'), - dataPlacement: 'top', - basePath: 'api/v1/inventories/{{$stateParams.inventory_id}}/access_list/', - type: 'collection', - title: i18n._('Permissions'), - iterator: 'permission', - index: false, - open: false, - search: { - order_by: 'username' - }, - actions: { - add: { - label: i18n._('Add'), - ngClick: "$state.go('.add')", - awToolTip: i18n._('Add a permission'), - actionClass: 'btn List-buttonSubmit', - buttonContent: '+ ADD', - ngShow: '(inventory_obj.summary_fields.user_capabilities.edit || canAdd)' - - } - }, - fields: { - username: { - label: i18n._('User'), - linkBase: 'users', - class: 'col-lg-3 col-md-3 col-sm-3 col-xs-4' - }, - role: { - label: i18n._('Role'), - type: 'role', - nosort: true, - class: 'col-lg-4 col-md-4 col-sm-4 col-xs-4', - }, - team_roles: { - label: i18n._('Team Roles'), - type: 'team_roles', - nosort: true, - class: 'col-lg-5 col-md-5 col-sm-5 col-xs-4', - } - } - }, - hosts: { - name: 'hosts', - // awToolTip: i18n._('Please save before assigning permissions'), - // dataPlacement: 'top', - basePath: 'api/v2/inventories/{{$stateParams.inventory_id}}/hosts/', - type: 'collection', - title: i18n._('Hosts'), - iterator: 'host', - index: false, - open: false, - // search: { - // order_by: 'username' - // }, - actions: { - add: { - label: i18n._('Add'), - ngClick: "$state.go('.add')", - awToolTip: i18n._('Add a permission'), - actionClass: 'btn List-buttonSubmit', - buttonContent: '+ ADD', - // ngShow: '(inventory_obj.summary_fields.user_capabilities.edit || canAdd)' - - } - }, - fields: { - name: { - label: i18n._('Name'), - // linkBase: 'users', - class: 'col-lg-3 col-md-3 col-sm-3 col-xs-4' - } - } - }, - //this is a placeholder for when we're ready for completed jobs - completed_jobs: { - name: 'completed_jobs', - // awToolTip: i18n._('Please save before assigning permissions'), - // dataPlacement: 'top', - basePath: 'api/v2/inventories/{{$stateParams.inventory_id}}/completed_jobs/', - type: 'collection', - title: i18n._('Completed Jobs'), - iterator: 'completed_job', - index: false, - open: false, - // search: { - // order_by: 'username' - // }, - actions: { - add: { - label: i18n._('Add'), - ngClick: "$state.go('.add')", - awToolTip: i18n._('Add a permission'), - actionClass: 'btn List-buttonSubmit', - buttonContent: '+ ADD', - // ngShow: '(inventory_obj.summary_fields.user_capabilities.edit || canAdd)' - - } - }, - fields: { - name: { - label: i18n._('Name'), - // linkBase: 'users', - class: 'col-lg-3 col-md-3 col-sm-3 col-xs-4' - } - } - } - } + related: {} };}]; diff --git a/awx/ui/client/src/inventories/inventory-manage.service.js b/awx/ui/client/src/inventories/inventory-manage.service.js new file mode 100644 index 0000000000..710c494aeb --- /dev/null +++ b/awx/ui/client/src/inventories/inventory-manage.service.js @@ -0,0 +1,60 @@ +/************************************************* + * Copyright (c) 2016 Ansible, Inc. + * + * All Rights Reserved + *************************************************/ + + export default + ['$rootScope', 'Rest', 'GetBasePath', 'ProcessErrors', 'Wait', + function($rootScope, Rest, GetBasePath, ProcessErrors, Wait){ + return { + // cute abstractions via fn.bind() + url: function(){ + return ''; + }, + error: function(data, status) { + ProcessErrors($rootScope, data, status, null, { hdr: 'Error!', + msg: 'Call to ' + this.url + '. GET returned: ' + status }); + }, + success: function(data){ + return data; + }, + // data getters + getInventory: function(id){ + Wait('start'); + this.url = GetBasePath('inventory') + id; + Rest.setUrl(this.url); + return Rest.get() + .success(this.success.bind(this)) + .error(this.error.bind(this)) + .finally(Wait('stop')); + }, + getBreadcrumbs: function(groups){ + Wait('start'); + this.url = GetBasePath('groups') + '?' + _.map(groups, function(item){ + return '&or__id=' + item; + }).join(''); + Rest.setUrl(this.url); + return Rest.get() + .success(this.success.bind(this)) + .error(this.error.bind(this)) + .finally(Wait('stop')); + }, + rootHostsUrl: function(id){ + var url = GetBasePath('inventory') + id + '/hosts'; + return url; + }, + childHostsUrl: function(id){ + var url = GetBasePath('groups') + id + '/all_hosts'; + return url; + }, + childGroupsUrl: function(id){ + var url = GetBasePath('groups') + id + '/children'; + return url; + }, + rootGroupsUrl: function(id){ + var url = GetBasePath('inventory') + id+ '/root_groups'; + return url; + } + }; + }]; diff --git a/awx/ui/client/src/inventories/main.js b/awx/ui/client/src/inventories/main.js index 3f71dee845..3ae1d9d7cf 100644 --- a/awx/ui/client/src/inventories/main.js +++ b/awx/ui/client/src/inventories/main.js @@ -12,6 +12,7 @@ import { templateUrl } from '../shared/template-url/template-url.factory'; import { N_ } from '../i18n'; import InventoryList from './inventory.list'; import InventoryForm from './inventory.form'; +import InventoryManageService from './inventory-manage.service'; export default angular.module('inventory', [ host.name, @@ -21,14 +22,15 @@ angular.module('inventory', [ ]) .factory('InventoryForm', InventoryForm) .factory('InventoryList', InventoryList) - .config(['$stateProvider', '$stateExtenderProvider', 'stateDefinitionsProvider', - function($stateProvider, $stateExtenderProvider, stateDefinitionsProvider) { + .service('InventoryManageService', InventoryManageService) + .config(['$stateProvider', 'stateDefinitionsProvider', '$stateExtenderProvider', + function($stateProvider, stateDefinitionsProvider, $stateExtenderProvider) { // When stateDefinition.lazyLoad() resolves, states matching name.** or /url** will be de-registered and replaced with resolved states // This means inventoryManage states will not be registered correctly on page refresh, unless they're registered at the same time as the inventories state tree let stateDefinitions = stateDefinitionsProvider.$get(), stateExtender = $stateExtenderProvider.$get(); - function foobar() { + function generateHostStates() { let smartInventoryAdd = { name: 'hosts.addSmartInventory', @@ -50,14 +52,67 @@ angular.module('inventory', [ } }; + let smartInventoryAddOrgLookup = { + searchPrefix: 'organization', + name: 'hosts.addSmartInventory.organization', + url: '/organization', + data: { + formChildState: true + }, + params: { + organization_search: { + value: { + page_size: '5' + }, + squash: true, + dynamic: true + } + }, + ncyBreadcrumb: { + skip: true + }, + views: { + 'related': { + templateProvider: function(ListDefinition, generateList) { + let list_html = generateList.build({ + mode: 'lookup', + list: ListDefinition, + input_type: 'radio' + }); + return `${list_html}`; + + } + } + }, + resolve: { + ListDefinition: ['OrganizationList', function(OrganizationList) { + let list = _.cloneDeep(OrganizationList); + list.lookupConfirmText = 'SELECT'; + return list; + }], + Dataset: ['ListDefinition', 'QuerySet', '$stateParams', 'GetBasePath', + (list, qs, $stateParams, GetBasePath) => { + let path = GetBasePath(list.name) || GetBasePath(list.basePath); + return qs.search(path, $stateParams[`${list.iterator}_search`]); + } + ] + }, + onExit: function($state) { + if ($state.transition) { + $('#form-modal').modal('hide'); + $('.modal-backdrop').remove(); + $('body').removeClass('modal-open'); + } + }, + }; + let hosts = stateDefinitions.generateTree({ parent: 'hosts', // top-most node in the generated tree (will replace this state definition) - modes: ['add', 'edit'], + modes: ['edit'], list: 'HostsList', form: 'HostsForm', controllers: { list: 'HostListController', - add: 'HostAddController', edit: 'HostEditController' }, urls: { @@ -101,7 +156,8 @@ angular.module('inventory', [ states: _.reduce(generated, (result, definition) => { return result.concat(definition.states); }, [ - stateExtender.buildDefinition(smartInventoryAdd) + stateExtender.buildDefinition(smartInventoryAdd), + stateExtender.buildDefinition(smartInventoryAddOrgLookup) ]) }; }); @@ -148,7 +204,7 @@ angular.module('inventory', [ $stateProvider.state({ name: 'hosts', url: '/hosts', - lazyLoad: () => foobar() + lazyLoad: () => generateHostStates() }); } ]);