Browse Source

Update `ISettingRegistry.ISettings` interface to support querying of default values for root schema keys.

Afshin Darian 7 years ago
parent
commit
429212deae

+ 66 - 7
packages/coreutils/src/settingregistry.ts

@@ -254,16 +254,13 @@ namespace ISettingRegistry {
     readonly user: JSONObject;
 
     /**
-     * Remove a single setting.
-     *
-     * @param key - The name of the setting being removed.
+     * Calculate the default value of a setting by iterating through the schema.
      *
-     * @returns A promise that resolves when the setting is removed.
+     * @param key - The name of the setting whose default value is calculated.
      *
-     * #### Notes
-     * This function is asynchronous because it writes to the setting registry.
+     * @returns A calculated default JSON value for a specific setting.
      */
-    remove(key: string): Promise<void>;
+    default(key: string): JSONValue;
 
     /**
      * Get an individual setting.
@@ -274,6 +271,18 @@ namespace ISettingRegistry {
      */
     get(key: string): { composite: JSONValue, user: JSONValue };
 
+    /**
+     * Remove a single setting.
+     *
+     * @param key - The name of the setting being removed.
+     *
+     * @returns A promise that resolves when the setting is removed.
+     *
+     * #### Notes
+     * This function is asynchronous because it writes to the setting registry.
+     */
+    remove(key: string): Promise<void>;
+
     /**
      * Save all of the plugin's user settings at once.
      */
@@ -705,6 +714,17 @@ class Settings implements ISettingRegistry.ISettings {
    */
   readonly registry: SettingRegistry;
 
+  /**
+   * Calculate the default value of a setting by iterating through the schema.
+   *
+   * @param key - The name of the setting whose default value is calculated.
+   *
+   * @returns A calculated default JSON value for a specific setting.
+   */
+  default(key: string): JSONValue {
+    return Private.reifyDefault(this.schema, key);
+  }
+
   /**
    * Dispose of the plugin settings resources.
    */
@@ -875,4 +895,43 @@ namespace Private {
     }
   };
   /* tslint:enable */
+
+  /**
+   * Create a fully extrapolated default value for a root key in a schema.
+   */
+  export
+  function reifyDefault(schema: ISettingRegistry.ISchema, root?: string): JSONValue | undefined {
+    // If the root level is not an object or is not further defined downward
+    // return its default value, which may be `undefined`.
+    if (schema.type !== 'object' || !schema.properties) {
+      return schema.default;
+    }
+
+    const property = root ? schema.properties[root] : schema;
+
+    // If the property is not an object or is not further defined downward
+    // return its default value, which may be `undefined`.
+    if (property.type !== 'object' || !property.properties) {
+      return property.default;
+    }
+
+    const properties = property.properties;
+    const result: JSONObject = property.default || { };
+
+    // Iterate through the schema and populate the default values for each
+    // property that is defined in the schema.
+    for (let prop in properties) {
+      if ('default' in properties[prop]) {
+        const reified = reifyDefault(properties[prop]);
+
+        if (reified === undefined) {
+          continue;
+        }
+
+        result[prop] = reified;
+      }
+    }
+
+    return result;
+  }
 }

+ 0 - 1
packages/settingeditor-extension/package.json

@@ -16,7 +16,6 @@
     "@jupyterlab/application": "^0.9.0",
     "@jupyterlab/apputils": "^0.9.0",
     "@jupyterlab/codeeditor": "^0.9.0",
-    "@jupyterlab/coreutils": "^0.9.0",
     "@phosphor/coreutils": "^1.2.0",
     "@phosphor/messaging": "^1.2.1",
     "@phosphor/signaling": "^1.2.1",

+ 3 - 45
packages/settingeditor-extension/src/settingeditor.ts

@@ -15,10 +15,6 @@ import {
   ICON_CLASS_KEY, ICON_LABEL_KEY, ISettingRegistry, IStateDB, ObservableJSON
 } from '@jupyterlab/coreutils';
 
-import {
-  JSONObject, JSONValue
-} from '@phosphor/coreutils';
-
 import {
   Message
 } from '@phosphor/messaging';
@@ -958,9 +954,9 @@ namespace Private {
       const field = properties[key];
       const { type } = field;
       const exists = key in user;
-      const reified = reifyDefault(schema, key);
-      const value = JSON.stringify(reified) || '';
-      const valueTitle = JSON.stringify(reified, null, 4);
+      const defaultValue = settings.default(key);
+      const value = JSON.stringify(defaultValue) || '';
+      const valueTitle = JSON.stringify(defaultValue, null, 4);
 
       fields[key] = h.tr(
         h.td({ className: FIELDSET_ADD_CLASS },
@@ -983,44 +979,6 @@ namespace Private {
     }
   }
 
-  /**
-   * Create a fully extrapolated default value for a root key in a schema.
-   */
-  function reifyDefault(schema: ISettingRegistry.ISchema, root?: string): JSONValue | undefined {
-    // If the root level is not an object or is not further defined downward
-    // return its default value, which may be `undefined`.
-    if (schema.type !== 'object' || !schema.properties) {
-      return schema.default;
-    }
-
-    const property = root ? schema.properties[root] : schema;
-
-    // If the property is not an object or is not further defined downward
-    // return its default value, which may be `undefined`.
-    if (property.type !== 'object' || !property.properties) {
-      return property.default;
-    }
-
-    const properties = property.properties;
-    const result: JSONObject = property.default || { };
-
-    // Iterate through the schema and populate the default values for each
-    // property that is defined in the schema.
-    for (let prop in properties) {
-      if ('default' in properties[prop]) {
-        const reified = reifyDefault(properties[prop]);
-
-        if (reified === undefined) {
-          continue;
-        }
-
-        result[prop] = reified;
-      }
-    }
-
-    return result;
-  }
-
   /**
    * Sort a list of plugins by ID.
    */