///////////////////////////////////////////////////////////// // // pgAdmin 4 - PostgreSQL Tools // // Copyright (C) 2013 - 2021, The pgAdmin Development Team // This software is released under the PostgreSQL Licence // ////////////////////////////////////////////////////////////// import gettext from 'sources/gettext'; import pgAdmin from 'sources/pgadmin'; import Backform from 'pgadmin.backform'; import Backgrid from 'pgadmin.backgrid'; import url_for from 'sources/url_for'; import $ from 'jquery'; import _ from 'underscore'; import Alertify from 'pgadmin.alertifyjs'; export default function macroModel(transId) { let MacroModel = pgAdmin.Browser.DataModel.extend({ idAttribute: 'id', defaults: { id: undefined, key: undefined, name: undefined, sql: undefined, key_label: undefined, }, schema: [{ id: 'key_label', name: 'key_label', label: gettext('Key'), type: 'text', cell: 'string', editable: false, cellHeaderClasses: 'width_percent_10', headerCell: Backgrid.Extension.CustomHeaderCell, disabled: false, }, { id: 'name', name: 'name', label: gettext('Name'), cell: 'string', type: 'text', editable: true, cellHeaderClasses: 'width_percent_20', headerCell: Backgrid.Extension.CustomHeaderCell, disabled: false, }, { id: 'sql', name: 'sql', label: gettext('SQL'), cell: Backgrid.Extension.SqlCell, type: 'multiline', control: Backform.SqlCodeControl, editable: true, cellHeaderClasses: 'width_percent_70', headerCell: Backgrid.Extension.CustomHeaderCell, disabled: false, }, ], validate: function() { let msg = null; this.errorModel.clear(); if (_.isEmpty(this.get('name')) && !(_.isEmpty(this.get('sql')))) { msg = gettext('Please enter macro name.'); this.errorModel.set('name', msg); return msg; } else if (_.isEmpty(this.get('sql')) && !(_.isEmpty(this.get('name')))) { msg = gettext('Please enter macro sql.'); this.errorModel.set('sql', msg); return msg; } return null; }, }); let MacroCollectionModel = pgAdmin.Browser.DataModel.extend({ defaults: { macro: undefined, }, urlRoot: url_for('sqleditor.get_macros', {'trans_id': transId}), schema: [{ id: 'macro', name: 'macro', label: gettext('Macros'), model: MacroModel, editable: true, type: 'collection', control: Backform.SubNodeCollectionControl.extend({ showGridControl: function(data) { var self = this, gridBody = $('
'); var subnode = data.subnode.schema ? data.subnode : data.subnode.prototype, gridSchema = Backform.generateGridColumnsFromModel( data.node_info, subnode, this.field.get('mode'), data.columns, data.schema_node ); // Clean up existing grid if any (in case of re-render) if (self.grid) { self.grid.remove(); } // Set visibility of Add button if (data.disabled || data.canAdd == false) { $(gridBody).find('button.add').remove(); } // Insert Clear Cell into Grid gridSchema.columns.unshift({ name: 'pg-backform-clear', label: '', cell: Backgrid.Extension.ClearCell, editable: false, cell_priority: -1, sortable: false, headerCell: Backgrid.Extension.CustomHeaderCell.extend({ className: 'header-icon-cell', events: { 'click': 'clearrAll', }, clearrAll: function(e) { e.preventDefault(); var that = this; // We will check if row is deletable or not let macros = that.collection.toJSON().filter(m => m.name !== undefined && m.name !== null); if (macros.length > 0) { Alertify.confirm( gettext('Clear All Rows'), gettext('Are you sure you wish to clear all rows?'), function() { _.each(that.collection.toJSON(), function(m) { that.collection.get(m.id).set({'name': null, 'sql': null}); }); }, function() { return true; } ); } }, render: function() { this.$el.empty(); var column = this.column; var label = $('