Skip to content

Commit

Permalink
Refactor graphql adapter to abstract and adding panel exists check
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Jan 19, 2024
1 parent 5919e64 commit 512eb9b
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 100 deletions.
1 change: 1 addition & 0 deletions src/PimcoreDataHubBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function getJsPaths(): array
return [
'/bundles/pimcoredatahub/js/datahub.js',
'/bundles/pimcoredatahub/js/config.js',
'/bundles/pimcoredatahub/js/adapter/abstract.js',
'/bundles/pimcoredatahub/js/adapter/graphql.js',
'/bundles/pimcoredatahub/js/configuration/graphql/configItem.js',
'/bundles/pimcoredatahub/js/fieldConfigDialog.js',
Expand Down
119 changes: 119 additions & 0 deletions src/Resources/public/js/adapter/abstract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

pimcore.registerNS("pimcore.plugin.datahub.adapter.abstract");
pimcore.plugin.datahub.adapter.abstract = Class.create({
initialize: function (configPanel) {
this.configPanel = configPanel;
},

addConfiguration: function (type) {
Ext.MessageBox.prompt(t('plugin_pimcore_datahub_configpanel_enterkey_title'), t('plugin_pimcore_datahub_configpanel_enterkey_prompt'), this.addConfigurationComplete.bind(this, type), null, null, "");
},

addConfigurationComplete: function (type, button, value, object) {
var regresult = value.match(/[a-zA-Z0-9_\-]+/);
if (button == "ok" && value.length > 2 && value.length <= 80 && regresult == value) {
Ext.Ajax.request({
url: "/admin/pimcoredatahub/config/add",
params: {
name: value,
type: type
},
success: function (response) {
var data = Ext.decode(response.responseText);
this.configPanel.refreshTree();

if (!data || !data.success) {
pimcore.helpers.showNotification(t("error"), t("plugin_pimcore_datahub_configpanel_error_adding_config") + ': <br/>' + data.message, "error");
} else {
this.openConfiguration(data.name);
}

}.bind(this)
});
}
else if (button == "cancel") {
return;
}
else {
Ext.Msg.alert(t("plugin_pimcore_datahub_configpanel"), value.length <= 80 ? t("plugin_pimcore_datahub_configpanel_invalid_name") : t("plugin_pimcore_datahub_configpanel_invalid_length"));
}
},

openConfiguration: function (id) {
this.checkIfPanelExists(id);
},

cloneConfiguration: function (tree, record) {
Ext.MessageBox.prompt(t('plugin_pimcore_datahub_configpanel_enterclonekey_title'), t('plugin_pimcore_datahub_configpanel_enterclonekey_enterclonekey_prompt'),
this.cloneConfigurationComplete.bind(this, tree, record), null, null, "");
},

cloneConfigurationComplete: function (tree, record, button, value, object) {

var regresult = value.match(/[a-zA-Z0-9_\-]+/);
if (button == "ok" && value.length > 2 && value.length <= 80 && regresult == value) {
Ext.Ajax.request({
url: "/admin/pimcoredatahub/config/clone",
params: {
name: value,
originalName: record.data.id
},
success: function (response) {
var data = Ext.decode(response.responseText);

this.configPanel.refreshTree();

if (!data || !data.success) {
pimcore.helpers.showNotification(t("error"), t("plugin_pimcore_datahub_configpanel_error_cloning_config") + ': <br/>' + data.message, "error");
} else {
this.openConfiguration(data.name, tree, record);
}

}.bind(this)
});
}
else if (button == "cancel") {
return;
}
else {
Ext.Msg.alert(t("plugin_pimcore_datahub_configpanel"), value.length <= 80 ? t("plugin_pimcore_datahub_configpanel_invalid_name") : t("plugin_pimcore_datahub_configpanel_invalid_length"));
}
},

deleteConfiguration: function (tree, record) {
Ext.Msg.confirm(t('delete'), t('delete_message'), function (btn) {
if (btn == 'yes') {
Ext.Ajax.request({
url: "/admin/pimcoredatahub/config/delete",
params: {
name: record.data.id
}
});

this.configPanel.getEditPanel().removeAll();
record.remove();
}
}.bind(this));
},

checkIfPanelExists: function(id) {
let existingPanel = Ext.getCmp("plugin_pimcore_datahub_configpanel_panel_" + id);
if(existingPanel) {
this.configPanel.editPanel.setActiveTab(existingPanel);
return true;
}
return false;
}
});
108 changes: 9 additions & 99 deletions src/Resources/public/js/adapter/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,9 @@
*/

pimcore.registerNS("pimcore.plugin.datahub.adapter.graphql");
pimcore.plugin.datahub.adapter.graphql = Class.create({

initialize: function (configPanel) {
this.configPanel = configPanel;
},

addConfiguration: function (type) {
Ext.MessageBox.prompt(t('plugin_pimcore_datahub_configpanel_enterkey_title'), t('plugin_pimcore_datahub_configpanel_enterkey_prompt'), this.addConfigurationComplete.bind(this, type), null, null, "");
},

addConfigurationComplete: function (type, button, value, object) {
var regresult = value.match(/[a-zA-Z0-9_\-]+/);
if (button == "ok" && value.length > 2 && value.length <= 80 && regresult == value) {
Ext.Ajax.request({
url: "/admin/pimcoredatahub/config/add",
params: {
name: value,
type: type
},
success: function (response) {
var data = Ext.decode(response.responseText);
this.configPanel.refreshTree();

if (!data || !data.success) {
pimcore.helpers.showNotification(t("error"), t("plugin_pimcore_datahub_configpanel_error_adding_config") + ': <br/>' + data.message, "error");
} else {
this.openConfiguration(data.name);
}

}.bind(this)
});
}
else if (button == "cancel") {
return;
}
else {
Ext.Msg.alert(t("plugin_pimcore_datahub_configpanel"), value.length <= 80 ? t("plugin_pimcore_datahub_configpanel_invalid_name") : t("plugin_pimcore_datahub_configpanel_invalid_length"));
}
},

pimcore.plugin.datahub.adapter.graphql = Class.create(pimcore.plugin.datahub.adapter.abstract, {
openConfiguration: function (id) {
var existingPanel = Ext.getCmp("plugin_pimcore_datahub_configpanel_panel_" + id);
if(existingPanel) {
this.configPanel.editPanel.setActiveTab(existingPanel);
if(this.checkIfPanelExists(id)) {
return;
}

Expand All @@ -65,7 +24,12 @@ pimcore.plugin.datahub.adapter.graphql = Class.create({
name: id
},
success: function (response) {
var data = Ext.decode(response.responseText);
// check again here to prevent double click problem
if(this.checkIfPanelExists(id)) {
return;
}

let data = Ext.decode(response.responseText);

pimcore.plugin.datahub.graphql = pimcore.plugin.datahub.graphql || {};
pimcore.plugin.datahub.graphql.supportedQueryDataTypes = data.supportedGraphQLQueryDataTypes;
Expand All @@ -75,59 +39,5 @@ pimcore.plugin.datahub.adapter.graphql = Class.create({
pimcore.layout.refresh();
}.bind(this)
});
},

cloneConfiguration: function (tree, record) {
Ext.MessageBox.prompt(t('plugin_pimcore_datahub_configpanel_enterclonekey_title'), t('plugin_pimcore_datahub_configpanel_enterclonekey_enterclonekey_prompt'),
this.cloneConfigurationComplete.bind(this, tree, record), null, null, "");
},

cloneConfigurationComplete: function (tree, record, button, value, object) {

var regresult = value.match(/[a-zA-Z0-9_\-]+/);
if (button == "ok" && value.length > 2 && value.length <= 80 && regresult == value) {
Ext.Ajax.request({
url: "/admin/pimcoredatahub/config/clone",
params: {
name: value,
originalName: record.data.id
},
success: function (response) {
var data = Ext.decode(response.responseText);

this.configPanel.refreshTree();

if (!data || !data.success) {
pimcore.helpers.showNotification(t("error"), t("plugin_pimcore_datahub_configpanel_error_cloning_config") + ': <br/>' + data.message, "error");
} else {
this.openConfiguration(data.name, tree, record);
}

}.bind(this)
});
}
else if (button == "cancel") {
return;
}
else {
Ext.Msg.alert(t("plugin_pimcore_datahub_configpanel"), value.length <= 80 ? t("plugin_pimcore_datahub_configpanel_invalid_name") : t("plugin_pimcore_datahub_configpanel_invalid_length"));
}
},

deleteConfiguration: function (tree, record) {
Ext.Msg.confirm(t('delete'), t('delete_message'), function (btn) {
if (btn == 'yes') {
Ext.Ajax.request({
url: "/admin/pimcoredatahub/config/delete",
params: {
name: record.data.id
}
});

this.configPanel.getEditPanel().removeAll();
record.remove();
}
}.bind(this));
},

}
});
2 changes: 1 addition & 1 deletion src/Resources/public/js/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pimcore.plugin.datahub.config = Class.create({
let firstHandler;

for (let key in pimcore.plugin.datahub.adapter) {
if( pimcore.plugin.datahub.adapter.hasOwnProperty( key ) && this.userIsAllowedToCreate(key)) {
if( key !== 'abstract' && pimcore.plugin.datahub.adapter.hasOwnProperty( key ) && this.userIsAllowedToCreate(key)) {
let adapter = new pimcore.plugin.datahub.adapter[key](this);

if (!firstHandler) {
Expand Down

0 comments on commit 512eb9b

Please sign in to comment.