import View from 'girder/views/View';
import { AccessType } from 'girder/constants';

import VegaWidgetTemplate from '../templates/vegaWidget.pug';
import '../stylesheets/vegaWidget.styl';

import vg from 'vega';

var VegaWidget = View.extend({
    initialize: function (settings) {
        this.item = settings.item;
        this.accessLevel = settings.accessLevel;
        this.item.on('change', function () {
            this.render();
        }, this);
        this.render();
    },

    render: function () {
        var meta = this.item.get('meta');

        if (this.accessLevel >= AccessType.READ && meta && meta.vega) {
            $('#g-app-body-container')
                .append(VegaWidgetTemplate());
            $.ajax({
                url: '/api/v1/item/' + this.item.get('_id') + '/download',
                type: 'GET',
                dataType: 'json',
                success: function (spec) {
                    vg.parse.spec(spec, function (chart) {
                        chart({
                            el: '.g-item-vega-vis',
                            renderer: 'svg'
                        }).update();
                    });
                }
            });
        } else {
            $('.g-item-vega')
                .remove();
        }
    }
});

export default VegaWidget;

@@ -77,16 +77,59 @@
 stants';
+%0Aimport %7B restRequest %7D from 'girder/rest';
 %0A%0Aimport
@@ -767,14 +767,19 @@
     
-$.ajax
+restRequest
 (%7B%0A 
@@ -797,11 +797,12 @@
     
-url
+path
 : '/
@@ -869,24 +869,10 @@
     
-    type: 'GET',
+%7D)
 %0A   
@@ -888,51 +888,14 @@
     
-dataType: 'json',%0A                success: 
+.done(
 func
@@ -1164,22 +1164,8 @@
     
-            %7D%0A
     

Use restRequest for Ajax requests to Girder servers