Просмотр исходного кода

Merge pull request #329 from smiclea/server-texts

Load field label and description from schema
Dorin Paslaru 7 лет назад
Родитель
Сommit
5387f6a7fe

+ 6 - 2
src/plugins/endpoint/default/ConnectionSchemaPlugin.js

@@ -14,12 +14,13 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 // @flow
 
+import LabelDictionary from '../../../utils/LabelDictionary'
 import type { Schema, SchemaProperties, SchemaDefinitions } from '../../../types/Schema'
 import type { Field } from '../../../types/Field'
 
 export const defaultSchemaToFields = (schema: SchemaProperties, schemaDefinitions?: ?SchemaDefinitions, parent?: string): any[] => {
   let fields = Object.keys(schema.properties).map(fieldName => {
-    let properties = schema.properties[fieldName]
+    let properties: any = schema.properties[fieldName]
 
     if (typeof schema.properties[fieldName].$ref === 'string' && schemaDefinitions) {
       const definitionName = schema.properties[fieldName].$ref.substr(schema.properties[fieldName].$ref.lastIndexOf('/') + 1)
@@ -31,9 +32,12 @@ export const defaultSchemaToFields = (schema: SchemaProperties, schemaDefinition
       }
     }
 
+    const name = parent ? `${parent}/${fieldName}` : fieldName
+    LabelDictionary.pushToCache({ name, title: properties.title, description: properties.description })
+
     return {
       ...properties,
-      name: parent ? `${parent}/${fieldName}` : fieldName,
+      name,
       required: schema.required && schema.required.find(k => k === fieldName) ? true : fieldName === 'username' || fieldName === 'password',
     }
   })

+ 2 - 0
src/types/Field.js

@@ -31,4 +31,6 @@ export type Field = {
   required?: boolean,
   useTextArea?: boolean,
   readOnly?: boolean,
+  title?: string,
+  description?: string,
 }

+ 65 - 44
src/utils/LabelDictionary.js

@@ -14,53 +14,63 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 // @flow
 
-class LabelDictionary {
-  // The word will be uppercased
-  static acronyms = ['id', 'api', 'url', 'vm', 'os', 'dhcp', 'sql', 'oci', 'sku']
-
-  // The word will be replaced
-  static abbreviations = {
-    migr: 'Migration',
-    auth: 'Authentication',
-    fip: 'Floating IP',
-  }
+import type { Field } from '../types/Field'
 
-  static dictionary = {
-    auth_url: 'Auth URL',
-    migr_image: 'Migration Image Name or Id',
-    migr_network: 'Migration Network Name or ID',
-    migr_worker_boot_from_volume: 'Boot Migration Workers from Volume',
-    volumes_are_zeroed: 'Volumes on destination are created zeroed',
-    keep_mac: 'Keep MAC address',
-    reuse_ports: 'Reuse Existing Ports',
-    replace_mac: 'Replace MAC Address',
-    migr_worker_use_config_drive: 'Migration Worker use ConfigDrive',
-    migr_worker_use_fip: 'Migration Worker use FIP',
-    aws: 'Amazon',
-    openstack: 'OpenStack',
-    opc: 'Oracle Cloud',
-    vmware_vsphere: 'VMware',
-    migr_subnet_id: 'Migration Subnet ID',
-    separate_vm: 'Separate Migration/VM?',
-    windows_migr_image: { label: 'Windows Migration Image', description: 'The Windows Migration Image information found on the Azure page' },
-    linux_migr_image: { label: 'Linux Migration Image', description: 'The Linux Migration Image information found on the Azure page' },
-    duplicate_to_project: { label: 'Project', description: 'Duplicate endpoint to selected project' },
-    // AzureStack suffixes
-    azure_datalake_analytics_catalog_and_job_endpoint: 'Azure Datalake Analytics Catalog And Job Endpoint Suffix',
-    azure_datalake_store_file_system_endpoint: 'Azure Datalake Store File System Endpoint Suffix',
-    keyvault_dns: 'Keyvault DNS Suffix',
-    sql_server_hostname: 'SQL Server Hostname Suffix',
-    storage_endpoint: 'Storage Endpoint Suffix',
-    preserve_nic_ips: 'Preserve NIC IPs',
-    openstack_use_current_user: 'Use Current User/Project/Domain for Authentification',
-  }
+// The word will be uppercased
+const acronyms = ['id', 'api', 'url', 'vm', 'os', 'dhcp', 'sql', 'oci', 'aws']
+
+// The word will be replaced
+const abbreviations = {
+  migr: 'Migration',
+  auth: 'Authentication',
+  fip: 'Floating IP',
+}
 
+const dictionary = {
+  migr_image: 'Migration Image Name or Id',
+  migr_network: 'Migration Network Name or ID',
+  migr_worker_boot_from_volume: 'Boot Migration Workers from Volume',
+  volumes_are_zeroed: 'Volumes on destination are created zeroed',
+  keep_mac: 'Keep MAC address',
+  reuse_ports: 'Reuse Existing Ports',
+  replace_mac: 'Replace MAC Address',
+  migr_worker_use_config_drive: 'Migration Worker use ConfigDrive',
+  migr_worker_use_fip: 'Migration Worker use FIP',
+  openstack: 'OpenStack',
+  opc: 'Oracle Cloud',
+  vmware_vsphere: 'VMware',
+  separate_vm: 'Separate Migration/VM?',
+  windows_migr_image: { description: 'The Windows Migration Image information found on the Azure page' },
+  linux_migr_image: { description: 'The Linux Migration Image information found on the Azure page' },
+  duplicate_to_project: { label: 'Project', description: 'Duplicate endpoint to selected project' },
+  // AzureStack suffixes
+  azure_datalake_analytics_catalog_and_job_endpoint: 'Azure Datalake Analytics Catalog And Job Endpoint Suffix',
+  azure_datalake_store_file_system_endpoint: 'Azure Datalake Store File System Endpoint Suffix',
+  keyvault_dns: 'Keyvault DNS Suffix',
+  sql_server_hostname: 'SQL Server Hostname Suffix',
+  storage_endpoint: 'Storage Endpoint Suffix',
+  preserve_nic_ips: 'Preserve NIC IPs',
+  openstack_use_current_user: 'Use Current User/Project/Domain for Authentification',
+}
+
+const cache: { name: string, label: ?string, description: ?string }[] = []
+
+class LabelDictionary {
   // Fields which have enums for which dictionary labels should be used.
   // If a field has enums and is not in this array, their values will be used as labels
   static enumFields = ['port_reuse_policy']
 
   static get(fieldName: ?string): string {
-    let labelInfo = fieldName ? this.dictionary[fieldName] : null
+    if (!fieldName) {
+      return ''
+    }
+
+    let cachItem = cache.find(i => i.name === fieldName)
+    if (cachItem && cachItem.label) {
+      return cachItem.label
+    }
+
+    let labelInfo = dictionary[fieldName]
     if (labelInfo) {
       if (typeof labelInfo === 'string') {
         return labelInfo
@@ -70,17 +80,22 @@ class LabelDictionary {
       }
     }
 
-    let words = fieldName ? fieldName.split('_') : []
+    let words = fieldName.split('_')
     words = words.map(word => {
-      let acronym = this.acronyms.find(a => a === word)
-      let newWord = acronym ? acronym.toUpperCase() : (this.abbreviations[word] || word)
+      let acronym = acronyms.find(a => a === word)
+      let newWord = acronym ? acronym.toUpperCase() : (abbreviations[word] || word)
       return newWord.charAt(0).toUpperCase() + newWord.substr(1)
     })
     return words.join(' ')
   }
 
   static getDescription(fieldName: string): string {
-    let labelInfo = this.dictionary[fieldName]
+    let cachItem = cache.find(i => i.name === fieldName)
+    if (cachItem && cachItem.description) {
+      return cachItem.description
+    }
+
+    let labelInfo = dictionary[fieldName]
 
     if (labelInfo && typeof labelInfo === 'object') {
       return labelInfo.description || ''
@@ -88,6 +103,12 @@ class LabelDictionary {
 
     return ''
   }
+
+  static pushToCache(field: Field) {
+    if ((field.title || field.description) && !cache.find(i => i.name === field.name)) {
+      cache.push({ label: field.title, description: field.description, name: field.name })
+    }
+  }
 }
 
 export default LabelDictionary