| 
									
										
										
										
											2018-01-20 00:46:47 -08:00
										 |  |  | import Component from '@ember/component'; | 
					
						
							|  |  |  | import { computed } from '@ember/object'; | 
					
						
							| 
									
										
										
										
											2017-02-13 14:49:06 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-20 00:46:47 -08:00
										 |  |  | export default Component.extend({ | 
					
						
							| 
									
										
										
										
											2017-02-13 14:49:06 -08:00
										 |  |  |   random: Math.floor(Math.random() * 10000), | 
					
						
							| 
									
										
										
										
											2018-01-20 00:46:47 -08:00
										 |  |  |   content: computed({ | 
					
						
							|  |  |  |     get: function() { | 
					
						
							| 
									
										
										
										
											2017-02-13 14:49:06 -08:00
										 |  |  |       if (!this.editor) { | 
					
						
							|  |  |  |         return undefined; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (arguments.length == 1) { | 
					
						
							|  |  |  |         return this.editor.getSession().getValue(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       return undefined; | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2018-01-20 00:46:47 -08:00
										 |  |  |     set: function(key, val) { | 
					
						
							| 
									
										
										
										
											2017-02-13 14:49:06 -08:00
										 |  |  |       if (!this.editor) { | 
					
						
							|  |  |  |         this.preset = val; | 
					
						
							|  |  |  |         return val; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       var cursor = this.editor.getCursorPosition(); | 
					
						
							|  |  |  |       this.editor.getSession().setValue(val); | 
					
						
							|  |  |  |       this.editor.moveCursorToPosition(cursor); | 
					
						
							|  |  |  |       return val; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }), | 
					
						
							| 
									
										
										
										
											2018-01-20 00:46:47 -08:00
										 |  |  |   didInsertElement: function() { | 
					
						
							| 
									
										
										
										
											2017-02-13 14:49:06 -08:00
										 |  |  |     this.editor = window.ace.edit(this.random + '_editor'); | 
					
						
							|  |  |  |     this.editor.$blockScrolling = Infinity; | 
					
						
							|  |  |  |     this.editor.setTheme('ace/theme/github'); | 
					
						
							|  |  |  |     this.editor.getSession().setMode('ace/mode/sql'); | 
					
						
							|  |  |  |     this.editor.setReadOnly(false); | 
					
						
							|  |  |  |     var self = this; | 
					
						
							| 
									
										
										
										
											2018-01-20 00:46:47 -08:00
										 |  |  |     this.editor.on('change', function() { | 
					
						
							| 
									
										
										
										
											2017-02-13 14:49:06 -08:00
										 |  |  |       self.notifyPropertyChange('content'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (this.preset) { | 
					
						
							|  |  |  |       this.set('content', this.preset); | 
					
						
							|  |  |  |       this.preset = null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   actions: { | 
					
						
							| 
									
										
										
										
											2018-01-20 00:46:47 -08:00
										 |  |  |     save: function() { | 
					
						
							| 
									
										
										
										
											2017-02-13 14:49:06 -08:00
										 |  |  |       var url = this.get('savePath'); | 
					
						
							| 
									
										
										
										
											2018-01-20 00:46:47 -08:00
										 |  |  |       url = url.replace(/\{.\w+\}/, this.get('itemId')); | 
					
						
							| 
									
										
										
										
											2017-02-13 14:49:06 -08:00
										 |  |  |       var method = 'POST'; | 
					
						
							| 
									
										
										
										
											2017-05-15 12:45:28 -07:00
										 |  |  |       var data = {}; | 
					
						
							| 
									
										
										
										
											2018-01-20 00:46:47 -08:00
										 |  |  |       data[this.get('saveParam')] = this.editor.getSession().getValue(); | 
					
						
							| 
									
										
										
										
											2017-02-13 14:49:06 -08:00
										 |  |  |       $.ajax({ | 
					
						
							|  |  |  |         url: url, | 
					
						
							|  |  |  |         method: method, | 
					
						
							|  |  |  |         dataType: 'json', | 
					
						
							|  |  |  |         data: data | 
					
						
							|  |  |  |       }) | 
					
						
							| 
									
										
										
										
											2018-01-20 00:46:47 -08:00
										 |  |  |         .done(function(data, txt, xhr) { | 
					
						
							|  |  |  |           if (data && data.status && data.status != 'success') { | 
					
						
							|  |  |  |             Notify.toast('Failed to update data', 'Failed to update data within editor', 'error'); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         .fail(function(xhr, txt, err) { | 
					
						
							|  |  |  |           Notify.toast('Failed to update data', 'Failed to update data within editor', 'error'); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-02-13 14:49:06 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }); |