diff --git a/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 b/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 index 8a43665..e307996 100644 --- a/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-plugins-explorer.js.es6 @@ -2,23 +2,24 @@ import showModal from 'discourse/lib/show-modal'; import Query from 'discourse/plugins/discourse-data-explorer/discourse/models/query'; export default Ember.ArrayController.extend({ - selectedItem: null, + selectedQueryId: null, dirty: false, loading: false, - markDirty: function() { - this.set('dirty', true); - }.observes('selectedItem.name', 'selectedItem.description', 'selectedItem.sql'), + explain: false, + + saveDisabled: Ember.computed.not('selectedItem.dirty'), + runDisabled: Ember.computed.alias('selectedItem.dirty'), + + selectedItem: function() { + const _id = this.get('selectedQueryId'); + const id = parseInt(_id); + return this.get('content').find(function(q) { + return q.get('id') === id; + }); + }.property('selectedQueryId'), actions: { - selectItem(item) { - this.setProperties({ - selectedItem: item, - editName: false - }); - this.set('dirty', false); - }, - dummy() {}, create() { @@ -28,7 +29,7 @@ export default Ember.ArrayController.extend({ newQuery.save().then(function(result) { self.pushObject(result.target); self.set('selectedItem', result.target); - self.set('dirty', false); + self.set('selectedItem.dirty', false); }).finally(function() { self.set('loading', false); }); @@ -39,17 +40,16 @@ export default Ember.ArrayController.extend({ }, editName() { - this.setProperties({ - editName: true, - dirty: true - }); + this.set('editName', true); }, save() { const self = this; this.set('loading', true); - this.get('selectedItem').save().then(function(result) { - debugger; + this.get('selectedItem').save().then(function() { + const query = self.get('selectedItem'); + query.markNotDirty(); + self.set('editName', false); }).finally(function() { self.set('loading', false); }); @@ -58,21 +58,25 @@ export default Ember.ArrayController.extend({ discard() { const self = this; this.set('loading', true); - this.store.find('query', this.selectedItem.id).then(function(result) { - self.set('selectedItem', result); + this.store.find('query', this.get('selectedItem.id')).then(function(result) { + debugger; + const query = self.get('selectedItem'); + query.setProperties(result.getProperties(Query.updatePropertyNames)); + query.markNotDirty(); + self.set('editName', false); }).finally(function() { self.set('loading', false); }); }, run() { - if (this.get('dirty')) { + if (this.get('selectedItem.dirty')) { self.set('results', {errors: [I18n.t('errors.explorer.dirty')]}); return; } const self = this; this.set('loading', true); - Discourse.ajax("/admin/plugins/explorer/query/" + this.get('selectedItem.id') + "/run", { + Discourse.ajax("/admin/plugins/explorer/queries/" + this.get('selectedItem.id') + "/run", { type: "POST", data: { params: JSON.stringify({foo: 34}), diff --git a/assets/javascripts/discourse/models/query.js.es6 b/assets/javascripts/discourse/models/query.js.es6 index ac00458..63d95c2 100644 --- a/assets/javascripts/discourse/models/query.js.es6 +++ b/assets/javascripts/discourse/models/query.js.es6 @@ -1,12 +1,30 @@ import RestModel from 'discourse/models/rest'; -const Query = RestModel.extend({ +let Query; +Query = RestModel.extend({ + dirty: false, + + markDirty: function() { + this.set('dirty', true); + }.observes('name', 'description', 'sql', 'defaults'), + + markNotDirty() { + this.set('dirty', false); + }, + + listName: function() { + if (this.get('dirty')) { + return this.get('name') + " (*)"; + } + return this.get('name'); + }.property('name', 'dirty'), + createProperties() { return this.getProperties("name"); }, updateProperties() { - return this.getProperties("name", "description", "sql", "defaults"); + return this.getProperties(Query.updatePropertyNames); }, run() { @@ -14,4 +32,8 @@ const Query = RestModel.extend({ } }); +Query.reopenClass({ + updatePropertyNames: ["name", "description", "sql", "defaults"] +}); + export default Query; diff --git a/assets/javascripts/discourse/templates/admin/plugins-explorer-show.hbs b/assets/javascripts/discourse/templates/admin/plugins-explorer-show.hbs index 17d39a6..317d31a 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-explorer-show.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-explorer-show.hbs @@ -4,7 +4,7 @@ {{text-field value=selectedItem.name}} {{else}}
Name | -Description | -
---|---|
{{query.name}} | -{{query.description}} | -