]> git.proxmox.com Git - proxmox-widget-toolkit.git/blobdiff - src/window/Edit.js
edit window: url is optional if both loadUrl and submitUrl are set
[proxmox-widget-toolkit.git] / src / window / Edit.js
index c0e305b9af1ba3c7bfa556358f2ab2929fee7c70..e952362ae9b2a84466d82529ec2fb99bc3648fc6 100644 (file)
@@ -7,14 +7,16 @@ Ext.define('Proxmox.window.Edit', {
     // set extra options like params for the load request
     autoLoadOptions: undefined,
 
+    // to submit extra params on load and submit, useful, e.g., if not all ID
+    // parameters are included in the URL
+    extraRequestParams: {},
+
     resizable: false,
 
-    // use this tio atimatically generate a title like
-    // Create: <subject>
+    // use this to atimatically generate a title like `Create: <subject>`
     subject: undefined,
 
-    // set isCreate to true if you want a Create button (instead
-    // OK and RESET)
+    // set isCreate to true if you want a Create button (instead OK and RESET)
     isCreate: false,
 
     // set to true if you want an Add button (instead of Create)
@@ -28,6 +30,14 @@ Ext.define('Proxmox.window.Edit', {
 
     backgroundDelay: 0,
 
+    // string or function, called as (url, values) - useful if the ID of the
+    // new object is part of the URL, or that URL differs from GET/PUT URL
+    submitUrl: Ext.identityFn,
+
+    // string or function, called as (url, initialConfig) - mostly for
+    // consistency with submitUrl existing. If both are set `url` gets optional
+    loadUrl: Ext.identityFn,
+
     // needed for finding the reference to submitbutton
     // because we do not have a controller
     referenceHolder: true,
@@ -63,15 +73,16 @@ Ext.define('Proxmox.window.Edit', {
     getValues: function(dirtyOnly) {
        let me = this;
 
-        let values = {};
+       let values = {};
+       Ext.apply(values, me.extraRequestParams);
 
        let form = me.formPanel.getForm();
 
-        form.getFields().each(function(field) {
-            if (!field.up('inputpanel') && (!dirtyOnly || field.isDirty())) {
-                Proxmox.Utils.assemble_field_data(values, field.getSubmitData());
-            }
-        });
+       form.getFields().each(function(field) {
+           if (!field.up('inputpanel') && (!dirtyOnly || field.isDirty())) {
+               Proxmox.Utils.assemble_field_data(values, field.getSubmitData());
+           }
+       });
 
        Ext.Array.each(me.query('inputpanel'), function(panel) {
            Proxmox.Utils.assemble_field_data(values, panel.getValues(dirtyOnly));
@@ -129,7 +140,9 @@ Ext.define('Proxmox.window.Edit', {
            values.background_delay = me.backgroundDelay;
        }
 
-       let url = me.url;
+       let url = Ext.isFunction(me.submitUrl)
+           ? me.submitUrl(me.url, values)
+           : me.submitUrl || me.url;
        if (me.method === 'DELETE') {
            url = url + "?" + Ext.Object.toQueryString(values);
            values = undefined;
@@ -190,9 +203,19 @@ Ext.define('Proxmox.window.Edit', {
            waitMsgTarget: me,
        }, options);
 
+       if (Object.keys(me.extraRequestParams).length > 0) {
+           let params = newopts.params || {};
+           Ext.applyIf(params, me.extraRequestParams);
+           newopts.params = params;
+       }
+
+       let url = Ext.isFunction(me.loadUrl)
+           ? me.loadUrl(me.url, me.initialConfig)
+           : me.loadUrl || me.url;
+
        let createWrapper = function(successFn) {
            Ext.apply(newopts, {
-               url: me.url,
+               url: url,
                method: 'GET',
                success: function(response, opts) {
                    form.clearInvalid();
@@ -223,8 +246,12 @@ Ext.define('Proxmox.window.Edit', {
     initComponent: function() {
        let me = this;
 
-       if (!me.url) {
-           throw "no url specified";
+       if (!me.url && (
+               !me.submitUrl || !me.loadUrl || me.submitUrl === Ext.identityFn ||
+               me.loadUrl === Ext.identityFn
+           )
+       ) {
+           throw "neither 'url' nor both, submitUrl and loadUrl specified";
        }
 
        if (me.create) {throw "deprecated parameter, use isCreate";}
@@ -234,7 +261,7 @@ Ext.define('Proxmox.window.Edit', {
        me.items = undefined;
 
        me.formPanel = Ext.create('Ext.form.Panel', {
-           url: me.url,
+           url: me.url, // FIXME: not in 'form' class, safe to remove??
            method: me.method || 'PUT',
            trackResetOnLoad: true,
            bodyPadding: me.bodyPadding !== undefined ? me.bodyPadding : 10,