|
| 1 | +<template> |
| 2 | + <el-dialog |
| 3 | + width="80%" |
| 4 | + :title="title" |
| 5 | + :visible.sync="dialogFormVisible" |
| 6 | + :before-close="closeDialog" |
| 7 | + :close-on-click-modal="false" |
| 8 | + class="sql-editor" |
| 9 | + append-to-body |
| 10 | + > |
| 11 | + <el-card v-if="record" shadow="never"> |
| 12 | + <el-form> |
| 13 | + <el-row> |
| 14 | + <el-col :span="24"> |
| 15 | + <el-form-item label="SQL Query" :label-width="formLabelWidth"> |
| 16 | + <editor |
| 17 | + v-if="sqlQuery" |
| 18 | + :key="editorKey" |
| 19 | + ref="aceEditorComponent" |
| 20 | + v-model="sqlQuery" |
| 21 | + id="sqlEditor" |
| 22 | + @init="editorInit" |
| 23 | + lang="sql" |
| 24 | + theme="chrome" |
| 25 | + width="100%" |
| 26 | + height="200" |
| 27 | + style="border: solid 1px #c0c4cc;" |
| 28 | + ></editor> |
| 29 | + </el-form-item> |
| 30 | + </el-col> |
| 31 | + </el-row> |
| 32 | + </el-form> |
| 33 | + </el-card> |
| 34 | + |
| 35 | + <span slot="footer" class="dialog-footer"> |
| 36 | + <el-button @click="closeDialog">Cancel</el-button> |
| 37 | + <el-button |
| 38 | + type="primary" |
| 39 | + :disabled="!hasChanged" |
| 40 | + @click="saveRecord()" |
| 41 | + >Save</el-button> |
| 42 | + </span> |
| 43 | + </el-dialog> |
| 44 | +</template> |
| 45 | + |
| 46 | +<script> |
| 47 | +export default { |
| 48 | + name: "SQLEditor", |
| 49 | + data: () => ({ |
| 50 | + dialogFormVisible: false, |
| 51 | + formLabelWidth: "120px", |
| 52 | + sqlQuery: "", |
| 53 | + originalQuery: "", |
| 54 | + queryFieldPath: null, // Store the path to the query field |
| 55 | + _keyListener: null, |
| 56 | + aceEditor: null, // Store ace editor instance |
| 57 | + editorKey: 0 // Key to force re-render |
| 58 | + }), |
| 59 | + components: { |
| 60 | + editor: require("vue2-ace-editor") |
| 61 | + }, |
| 62 | + computed: { |
| 63 | + hasChanged: function() { |
| 64 | + return this.sqlQuery !== this.originalQuery; |
| 65 | + }, |
| 66 | + title: function() { |
| 67 | + if (this.record && this.record._id) { |
| 68 | + return `SQL Query Editor (${this.record._id})`; |
| 69 | + } |
| 70 | + return "SQL Query Editor"; |
| 71 | + } |
| 72 | + }, |
| 73 | + props: { |
| 74 | + record: { |
| 75 | + type: Object, |
| 76 | + required: true |
| 77 | + }, |
| 78 | + config: { |
| 79 | + type: Object |
| 80 | + }, |
| 81 | + editMode: { |
| 82 | + type: String |
| 83 | + } |
| 84 | + }, |
| 85 | + watch: { |
| 86 | + record: { |
| 87 | + handler: function() { |
| 88 | + this.prepareData(); |
| 89 | + }, |
| 90 | + deep: true, |
| 91 | + immediate: true |
| 92 | + }, |
| 93 | + aceEditor: { |
| 94 | + handler: function(newEditor) { |
| 95 | + if (newEditor && this.sqlQuery) { |
| 96 | + this.$nextTick(() => { |
| 97 | + newEditor.setValue(this.sqlQuery, -1); |
| 98 | + }); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + }, |
| 103 | + created: function() { |
| 104 | + this.dialogFormVisible = true; |
| 105 | + }, |
| 106 | + mounted: function() { |
| 107 | + // Load data after component is fully mounted |
| 108 | + this.$nextTick(() => { |
| 109 | + this.prepareData(); |
| 110 | + }); |
| 111 | + |
| 112 | + // Fallback: Try to access editor from refs if @init didn't fire |
| 113 | + setTimeout(() => { |
| 114 | + if (!this.aceEditor && this.sqlQuery && this.$refs.aceEditorComponent) { |
| 115 | + const editorComponent = this.$refs.aceEditorComponent; |
| 116 | + if (editorComponent && editorComponent.editor) { |
| 117 | + this.aceEditor = editorComponent.editor; |
| 118 | + this.aceEditor.setValue(this.sqlQuery, -1); |
| 119 | + } |
| 120 | + } |
| 121 | + }, 500); |
| 122 | + |
| 123 | + this._keyListener = function(e) { |
| 124 | + if (e.key === "s" && (e.ctrlKey || e.metaKey)) { |
| 125 | + e.preventDefault(); |
| 126 | + if (this.hasChanged) { |
| 127 | + this.saveRecord(); |
| 128 | + } |
| 129 | + } |
| 130 | + }; |
| 131 | + document.addEventListener("keydown", this._keyListener.bind(this)); |
| 132 | + }, |
| 133 | + beforeDestroy: function() { |
| 134 | + if (this._keyListener) { |
| 135 | + document.removeEventListener("keydown", this._keyListener); |
| 136 | + } |
| 137 | + }, |
| 138 | + methods: { |
| 139 | + editorInit: function(editor) { |
| 140 | + require("brace/ext/language_tools"); |
| 141 | + require("brace/mode/sql"); |
| 142 | + require("brace/theme/chrome"); |
| 143 | + require("brace/snippets/sql"); |
| 144 | + editor.setOptions({ |
| 145 | + enableBasicAutocompletion: true, |
| 146 | + enableSnippets: true, |
| 147 | + enableLiveAutocompletion: true |
| 148 | + }); |
| 149 | + |
| 150 | + // Store editor reference |
| 151 | + this.aceEditor = editor; |
| 152 | + |
| 153 | + // Set initial value if already loaded |
| 154 | + if (this.sqlQuery) { |
| 155 | + editor.setValue(this.sqlQuery, -1); |
| 156 | + } |
| 157 | + |
| 158 | + // Set up change listener to update v-model |
| 159 | + editor.on('change', () => { |
| 160 | + this.sqlQuery = editor.getValue(); |
| 161 | + }); |
| 162 | + }, |
| 163 | + prepareData: function() { |
| 164 | + // Try different possible locations for the query field |
| 165 | + let query = ""; |
| 166 | + this.queryFieldPath = null; |
| 167 | + |
| 168 | + if (!this.record) { |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + // Try _source.query (most common case) |
| 173 | + if (this.record._source && this.record._source.query !== undefined) { |
| 174 | + query = this.record._source.query; |
| 175 | + this.queryFieldPath = "_source.query"; |
| 176 | + } |
| 177 | + // Try direct query property |
| 178 | + else if (this.record.query !== undefined) { |
| 179 | + query = this.record.query; |
| 180 | + this.queryFieldPath = "query"; |
| 181 | + } |
| 182 | + // Try looking in all _source fields for anything with 'query' in the name |
| 183 | + else if (this.record._source) { |
| 184 | + for (let key in this.record._source) { |
| 185 | + if (key.toLowerCase().includes('query') || key.toLowerCase().includes('sql')) { |
| 186 | + query = this.record._source[key]; |
| 187 | + this.queryFieldPath = `_source.${key}`; |
| 188 | + break; |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + // Try looking in root level for any field with query or sql |
| 194 | + if (!this.queryFieldPath) { |
| 195 | + for (let key in this.record) { |
| 196 | + if (key !== '_source' && key !== '_id' && key !== '_index' && |
| 197 | + (key.toLowerCase().includes('query') || key.toLowerCase().includes('sql'))) { |
| 198 | + query = this.record[key]; |
| 199 | + this.queryFieldPath = key; |
| 200 | + break; |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + // Handle multi-line queries (with >- YAML format) |
| 206 | + if (typeof query === 'string') { |
| 207 | + query = query.trim(); |
| 208 | + } |
| 209 | + |
| 210 | + this.sqlQuery = query || ""; |
| 211 | + this.originalQuery = query || ""; |
| 212 | + |
| 213 | + // Force editor re-render with new data |
| 214 | + this.editorKey++; |
| 215 | + |
| 216 | + // Manually update ace editor if it's already initialized |
| 217 | + if (this.aceEditor && this.sqlQuery) { |
| 218 | + this.$nextTick(() => { |
| 219 | + this.aceEditor.setValue(this.sqlQuery, -1); |
| 220 | + }); |
| 221 | + } |
| 222 | + }, |
| 223 | + closeDialog: function() { |
| 224 | + if (this.hasChanged) { |
| 225 | + this.$confirm( |
| 226 | + "You have unsaved changes. Are you sure you want to close?", |
| 227 | + "Warning", |
| 228 | + { |
| 229 | + confirmButtonText: "OK", |
| 230 | + cancelButtonText: "Cancel", |
| 231 | + type: "warning" |
| 232 | + } |
| 233 | + ) |
| 234 | + .then(() => { |
| 235 | + this.dialogFormVisible = false; |
| 236 | + this.$emit("dialogclose"); |
| 237 | + }) |
| 238 | + .catch(() => {}); |
| 239 | + } else { |
| 240 | + this.dialogFormVisible = false; |
| 241 | + this.$emit("dialogclose"); |
| 242 | + } |
| 243 | + }, |
| 244 | + saveRecord: function() { |
| 245 | + // Update the record with the new query |
| 246 | + let updatedRecord = JSON.parse(JSON.stringify(this.record)); |
| 247 | + |
| 248 | + // Use the stored field path to save to the correct location |
| 249 | + if (this.queryFieldPath) { |
| 250 | + if (this.queryFieldPath.startsWith("_source.")) { |
| 251 | + const fieldName = this.queryFieldPath.substring(8); // Remove "_source." |
| 252 | + if (!updatedRecord._source) updatedRecord._source = {}; |
| 253 | + updatedRecord._source[fieldName] = this.sqlQuery; |
| 254 | + } else { |
| 255 | + updatedRecord[this.queryFieldPath] = this.sqlQuery; |
| 256 | + } |
| 257 | + } else { |
| 258 | + // Fallback to default behavior |
| 259 | + if (updatedRecord._source) { |
| 260 | + updatedRecord._source.query = this.sqlQuery; |
| 261 | + } else { |
| 262 | + updatedRecord.query = this.sqlQuery; |
| 263 | + } |
| 264 | + } |
| 265 | +
|
| 266 | + this.$store.commit({ |
| 267 | + type: "updateRecord", |
| 268 | + data: updatedRecord |
| 269 | + }); |
| 270 | +
|
| 271 | + this.$notify({ |
| 272 | + title: "Success", |
| 273 | + message: "SQL query updated successfully", |
| 274 | + type: "success", |
| 275 | + position: "bottom-right" |
| 276 | + }); |
| 277 | +
|
| 278 | + this.originalQuery = this.sqlQuery; |
| 279 | + this.dialogFormVisible = false; |
| 280 | + this.$emit("dialogcloseupdated"); |
| 281 | + } |
| 282 | + } |
| 283 | +}; |
| 284 | +</script> |
| 285 | + |
| 286 | +<style> |
| 287 | +.sql-editor .el-dialog__body { |
| 288 | + padding: 20px; |
| 289 | +} |
| 290 | +.sql-editor .el-form-item__label { |
| 291 | + color: #464646; |
| 292 | + font-weight: 600; |
| 293 | +} |
| 294 | +</style> |
0 commit comments