dynamic_dashboard.js 8.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
odoo.define('odoo_dynamic_dashboard.Dashboard', function (require) {
"use strict";

var AbstractAction = require('web.AbstractAction');
var ajax = require('web.ajax');
var core = require('web.core');
var rpc = require('web.rpc');
var session = require('web.session');
var web_client = require('web.web_client');
var _t = core._t;
var QWeb = core.qweb;

var DynamicDashboard = AbstractAction.extend({
    template: 'dynamic_dashboard',
    events: {
         'click .add_block': '_onClick_add_block',
         'click .add_grapgh': '_onClick_add_grapgh',
         'click .block_setting': '_onClick_block_setting',
         'click .tile': '_onClick_tile',
    },

    init: function(parent, context) {
        this.action_id = context['id'];
        this._super(parent, context);
        this.block_ids = []
    },

    start: function() {
        var self = this;
        this.set("title", 'Dashboard');
        return this._super().then(function() {
            self.render_dashboards();
        });
    },

    willStart: function() {
        var self = this;
        return $.when(ajax.loadLibs(this), this._super()).then(function() {
             return self.fetch_data();
        });
    },

     fetch_data: function() {
        var self = this;
        var def1 =  this._rpc({
                model: 'dashboard.block',
                method: 'get_dashboard_vals',
                args: [[],this.action_id]
            }).then(function(result) {
                self.block_ids = result;
        });
        return $.when(def1);
    },

    get_colors : function(x_axis) {
        var color = []
        for (var j = 0; j < x_axis.length; j++) {
            var r = Math.floor(Math.random() * 255);
            var g = Math.floor(Math.random() * 255);
            var b = Math.floor(Math.random() * 255);
            color.push("rgb(" + r + "," + g + "," + b + ")");
         }
         return color
    },

    get_values_bar : function(block){
        var labels = block['x_axis']
        var data = {
          labels: labels,
          datasets: [{
            label: "",
            data: block['y_axis'],
                    backgroundColor: this.get_colors(block['x_axis']),
                    borderColor: 'rgba(200, 200, 200, 0.75)',
                    borderWidth: 1
                  }]
                };

        var options = {
                scales: {
                  y: {
                    beginAtZero: true
                  }
                }
            },
        bar_data = [data,options]
        return bar_data;
        },

    get_values_pie : function(block){
        var data = {
        labels: block['x_axis'],
          datasets: [{
            label: '',
            data: block['y_axis'],
            backgroundColor: this.get_colors(block['x_axis']),
            hoverOffset: 4
          }]
        };
        var options = { },
        pie_data = [data,options]
        return pie_data;
    },

    get_values_line : function(block){
        var labels = block['x_axis']
        var data = {
          labels: labels,
          datasets: [{
            label: '',
            data: block['y_axis'],
            fill: false,
            borderColor: 'rgb(75, 192, 192)',
            tension: 0.1
          }]
        };
        var options = { },
        line_data = [data,options]
        return line_data;

    },

    get_values_doughnut : function(block){
        var data = {
        labels: block['x_axis'],
          datasets: [{
            label: '',
            data: block['y_axis'],
            backgroundColor: this.get_colors(block['x_axis']),
            hoverOffset: 4
          }]
        };
        var options = { },
        doughnut_data = [data,options]
        return doughnut_data;
    },

    get_values_radar : function(block){
          var data = {
          labels: block['x_axis'],
          datasets: [{
            label: '',
            data: block['y_axis'],
            fill: true,
            backgroundColor: 'rgba(255, 99, 132, 0.2)',
            borderColor: 'rgb(255, 99, 132)',
            pointBackgroundColor: 'rgb(255, 99, 132)',
            pointBorderColor: '#fff',
            pointHoverBackgroundColor: '#fff',
            pointHoverBorderColor: 'rgb(255, 99, 132)'
          }]
        };
        var options = {
            elements: {
              line: {
                borderWidth: 3
              }
            }
          },
        radar_data = [data,options]
        return radar_data;
    },

    render_dashboards: function() {
        var self = this;
        _.each(this.block_ids, function(block) {
                if (block['type'] == 'tile') {
                    console.log('INSIDE IF');
                    self.$('.o_dynamic_dashboard').append(QWeb.render('DynamicDashboardTile', {widget: block}));
                }
                else{
                    console.log('INSIDE ELSE');
                    self.$('.o_dynamic_chart').append(QWeb.render('DynamicDashboardChart', {widget: block}));
                    var element = $('[data-id=' + block['id'] + ']')
                    console.log('INSIDE ELSE 2');
                    if (!('x_axis' in block)){
                        return false
                    }
                    var ctx =self.$('.chart_graphs').last()
                    var type = block['graph_type']
                    var chart_type = 'self.get_values_' + `${type}(block)`
                    var data = eval(chart_type)
                    console.log('chart_type: ', chart_type);
                  //create Chart class object
                  var chart = new Chart(ctx, {
                    type: type,
                    data: data[0],
                    options: data[1]
                  });
                }
        });
    },

    _onClick_block_setting : function(event){
        event.stopPropagation();
        var self = this;
        var id = $(event.currentTarget).closest('.block').attr('data-id');
        this.do_action({
            type: 'ir.actions.act_window',
            res_model: 'dashboard.block',
            view_mode: 'form',
            res_id: parseInt(id),
            views: [[false,'form']],
            context: {'form_view_initial_mode': 'edit'},
        });
    },

    _onClick_add_block : function(e){
         var self = this;
         var type = $(e.currentTarget).attr('data-type');
         ajax.jsonRpc('/create/tile', 'call', {
            'type' : type,
            'action_id' : self.action_id
                }).then(function (result) {
                    if(result['type'] == 'tile'){
                        self.$('.o_dynamic_dashboard').append(QWeb.render('DynamicDashboardTile', {widget: result}));
                    }
                    else{
                        self.$('.o_dynamic_chart').append(QWeb.render('DynamicDashboardChart', {widget: result}));
                        var element = $('[data-id=' + result['id'] + ']')
                        var ctx =self.$('.chart_graphs').last()
                        var options = {
                          type: 'bar',
                          data: {
                            labels: [],
                            datasets: [
                                {
                                  data: [],
                                borderWidth: 1
                                },
                                ]
                          },
                        }
                        var chart = new Chart(ctx, {
                            type: "bar",
                            data: options
                          });
                    }
                });
    },

    _onClick_tile : function(e){
        e.stopPropagation();
        var self = this;
        var id = $(e.currentTarget).attr('data-id');
        ajax.jsonRpc('/tile/details', 'call', {
           'id': id
        }).then(function (result) {
                self.do_action({
                name : result['model_name'],
                type: 'ir.actions.act_window',
                res_model:result['model'] ,
                view_mode: 'tree,form',
                views: [[false, 'list'], [false, 'form']],
                domain: result['filter']
                });
        });
    },



});


core.action_registry.add('dynamic_dashboard', DynamicDashboard);

return DynamicDashboard;

});