Add CSV download for compare (change backing service, too)
This commit is contained in:
		
							parent
							
								
									7c194447f7
								
							
						
					
					
						commit
						da4ebc9b29
					
				
					 3 changed files with 34 additions and 32 deletions
				
			
		| 
						 | 
					@ -3,6 +3,13 @@ import Ember from 'ember';
 | 
				
			||||||
export default Ember.Controller.extend({
 | 
					export default Ember.Controller.extend({
 | 
				
			||||||
  queryParams: ['strain_ids', 'characteristic_ids'],
 | 
					  queryParams: ['strain_ids', 'characteristic_ids'],
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  csvLink: function() {
 | 
				
			||||||
 | 
					    let token = encodeURIComponent(this.get('session.secure.token'));
 | 
				
			||||||
 | 
					    return `${this.get('globals.apiURL')}/api/${this.get('globals.genus')}/` +
 | 
				
			||||||
 | 
					      `compare?token=${token}&strain_ids=${this.get('strain_ids')}&` +
 | 
				
			||||||
 | 
					      `characteristic_ids=${this.get('characteristic_ids')}&mimeType=csv`;
 | 
				
			||||||
 | 
					  }.property('strain_ids', 'characteristic_ids').readOnly(),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  strains: function() {
 | 
					  strains: function() {
 | 
				
			||||||
    let strains = [];
 | 
					    let strains = [];
 | 
				
			||||||
    let strain_ids = this.get('strain_ids').split(',');
 | 
					    let strain_ids = this.get('strain_ids').split(',');
 | 
				
			||||||
| 
						 | 
					@ -21,30 +28,4 @@ export default Ember.Controller.extend({
 | 
				
			||||||
    return characteristics;
 | 
					    return characteristics;
 | 
				
			||||||
  }.property('characteristic_ids'),
 | 
					  }.property('characteristic_ids'),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Set up data table matrix
 | 
					 | 
				
			||||||
  data: function() {
 | 
					 | 
				
			||||||
    let characteristics = this.get('characteristics');
 | 
					 | 
				
			||||||
    let strains = this.get('strains');
 | 
					 | 
				
			||||||
    let measurements = this.get('model');
 | 
					 | 
				
			||||||
    let data = Ember.A();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    characteristics.forEach((characteristic) => {
 | 
					 | 
				
			||||||
      let row = {
 | 
					 | 
				
			||||||
        characteristic: characteristic.get('characteristicName'),
 | 
					 | 
				
			||||||
      };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      strains.forEach((strain) => {
 | 
					 | 
				
			||||||
        let meas = measurements.filterBy('strain.id', strain.get('id'))
 | 
					 | 
				
			||||||
          .filterBy('characteristic.id', characteristic.get('id'));
 | 
					 | 
				
			||||||
        if (!Ember.isEmpty(meas)) {
 | 
					 | 
				
			||||||
          row[strain.get('id')] = meas[0].get('value');
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
          row[strain.get('id')] = '';
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      });
 | 
					 | 
				
			||||||
      data.pushObject(row);
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
    return data;
 | 
					 | 
				
			||||||
  }.property('characteristics', 'strains').readOnly(),
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,5 @@
 | 
				
			||||||
import Ember from 'ember';
 | 
					import Ember from 'ember';
 | 
				
			||||||
 | 
					import ajaxRequest from '../../../../utils/ajax-request';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default Ember.Route.extend({
 | 
					export default Ember.Route.extend({
 | 
				
			||||||
  queryParams: {
 | 
					  queryParams: {
 | 
				
			||||||
| 
						 | 
					@ -12,16 +13,35 @@ export default Ember.Route.extend({
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  beforeModel: function(transition) {
 | 
					  beforeModel: function(transition) {
 | 
				
			||||||
    this._super(transition);
 | 
					    this._super(transition);
 | 
				
			||||||
    if (Ember.$.isEmptyObject(transition.queryParams)) {
 | 
					    if (Ember.$.isEmptyObject(transition.queryParams.strain_ids) ||
 | 
				
			||||||
 | 
					        Ember.$.isEmptyObject(transition.queryParams.characteristic_ids)) {
 | 
				
			||||||
      this.transitionTo('protected.compare');
 | 
					      this.transitionTo('protected.compare');
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  model: function(params) {
 | 
					  model: function(params) {
 | 
				
			||||||
 | 
					    if (params.strain_ids === '' || params.characteristic_ids === '') {
 | 
				
			||||||
 | 
					      this.transitionTo('protected.compare');
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let compare = this.controllerFor('protected.compare');
 | 
					    let compare = this.controllerFor('protected.compare');
 | 
				
			||||||
    compare.set('selectedStrains', params.strain_ids);
 | 
					    compare.set('selectedStrains', params.strain_ids);
 | 
				
			||||||
    compare.set('selectedCharacteristics', params.characteristic_ids);
 | 
					    compare.set('selectedCharacteristics', params.characteristic_ids);
 | 
				
			||||||
    return this.store.query('measurement', params);
 | 
					
 | 
				
			||||||
 | 
					    let url = `${this.get('globals.apiURL')}/api/${this.get('globals.genus')}/compare`;
 | 
				
			||||||
 | 
					    let options = {
 | 
				
			||||||
 | 
					      method: 'GET',
 | 
				
			||||||
 | 
					      data: params,
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    return ajaxRequest(url, options);
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  setupController: function(controller, model) {
 | 
				
			||||||
 | 
					    model.forEach((m, i) => {
 | 
				
			||||||
 | 
					      let c = this.store.peekRecord('characteristic', m[0]);
 | 
				
			||||||
 | 
					      model[i][0] = c.get('characteristicName');
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					    controller.set('model', model);
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -13,14 +13,15 @@
 | 
				
			||||||
      </tr>
 | 
					      </tr>
 | 
				
			||||||
    </thead>
 | 
					    </thead>
 | 
				
			||||||
    <tbody>
 | 
					    <tbody>
 | 
				
			||||||
      {{#each data as |row|}}
 | 
					      {{#each model as |row|}}
 | 
				
			||||||
        <tr>
 | 
					        <tr>
 | 
				
			||||||
          <td>{{row.characteristic}}</td>
 | 
					          {{#each row key="@index" as |col|}}
 | 
				
			||||||
          {{#each strains as |strain|}}
 | 
					            <td>{{col}}</td>
 | 
				
			||||||
            <td>{{get-property row strain.id}}</td>
 | 
					 | 
				
			||||||
          {{/each}}
 | 
					          {{/each}}
 | 
				
			||||||
        </tr>
 | 
					        </tr>
 | 
				
			||||||
      {{/each}}
 | 
					      {{/each}}
 | 
				
			||||||
    </tbody>
 | 
					    </tbody>
 | 
				
			||||||
  </table>
 | 
					  </table>
 | 
				
			||||||
 | 
					  <hr>
 | 
				
			||||||
 | 
					  <a href="{{csvLink}}" download>Download as CSV</a>
 | 
				
			||||||
</div>
 | 
					</div>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Reference in a new issue