Rushabh Mehta | 29a75c4 | 2011-08-25 19:17:44 +0530 | [diff] [blame] | 1 | pscript.onload_dashboard = function() { |
| 2 | // load jqplot |
| 3 | $.scriptPath = 'js/' |
Rushabh Mehta | 764fdb9 | 2012-01-20 12:07:56 +0530 | [diff] [blame] | 4 | wn.require('lib/css/jqpot.css'); |
Rushabh Mehta | 5caedff | 2011-09-08 15:45:37 +0530 | [diff] [blame] | 5 | wn.require('lib/js/legacy/jquery/jquery.jqplot.min.js'); |
| 6 | wn.require('lib/js/legacy/jquery/jqplot-plugins/jqplot.barRenderer.js'); |
| 7 | wn.require('lib/js/legacy/jquery/jqplot-plugins/jqplot.canvasAxisTickRenderer.min.js'); |
| 8 | wn.require('lib/js/legacy/jquery/jqplot-plugins/jqplot.canvasTextRenderer.min.js'); |
| 9 | wn.require('lib/js/legacy/jquery/jqplot-plugins/jqplot.categoryAxisRenderer.min.js'); |
Rushabh Mehta | 29a75c4 | 2011-08-25 19:17:44 +0530 | [diff] [blame] | 10 | |
| 11 | |
| 12 | pscript.dashboard_settings = { |
| 13 | company: sys_defaults.company, |
Anand Doshi | 130ae98 | 2011-12-12 20:24:06 +0530 | [diff] [blame] | 14 | start: (function() { |
| 15 | var start_date = dateutil.add_days(new Date(), -180); |
| 16 | var year_start_date = dateutil.str_to_obj(sys_defaults.year_start_date); |
| 17 | if (start_date < year_start_date) { start_date = year_start_date; } |
| 18 | console.log(start_date); |
| 19 | return dateutil.obj_to_str(start_date); |
| 20 | })(), |
| 21 | end: (function() { |
| 22 | var end_date = new Date(); |
| 23 | var year_end_date = dateutil.str_to_obj(sys_defaults.year_end_date); |
| 24 | if (end_date > year_end_date) { end_date = year_end_date; } |
| 25 | console.log(end_date); |
| 26 | return dateutil.obj_to_str(end_date); |
| 27 | })(), |
Rushabh Mehta | ae5cdeb | 2011-08-30 13:24:49 +0530 | [diff] [blame] | 28 | interval: 30 |
Rushabh Mehta | 29a75c4 | 2011-08-25 19:17:44 +0530 | [diff] [blame] | 29 | } |
| 30 | |
Rushabh Mehta | ae5cdeb | 2011-08-30 13:24:49 +0530 | [diff] [blame] | 31 | var ph = new PageHeader($('.dashboard .header').get(0), 'Dashboard'); |
Rushabh Mehta | 29a75c4 | 2011-08-25 19:17:44 +0530 | [diff] [blame] | 32 | var db = new Dashboard(); |
| 33 | |
| 34 | ph.add_button('Settings', db.show_settings); |
| 35 | |
| 36 | db.refresh(); |
| 37 | |
| 38 | } |
| 39 | |
| 40 | Dashboard = function() { |
| 41 | var me = this; |
| 42 | $.extend(me, { |
| 43 | refresh: function() { |
| 44 | $('.dashboard .help_box').css('display', 'block'); |
| 45 | $c_page('home', 'dashboard', 'load_dashboard', JSON.stringify(pscript.dashboard_settings), function(r,rt) { |
| 46 | $('.dashboard .help_box').css('display', 'none'); |
| 47 | me.render(r.message); |
| 48 | }) |
| 49 | }, |
| 50 | |
| 51 | render: function(data) { |
| 52 | $('.dashboard_table').html(''); |
| 53 | var t = make_table($('.dashboard_table').get(0), 4, 2, '100%', ['50%', '50%'], {padding: '5px'}); |
| 54 | var ridx=0; var cidx=0; |
| 55 | for(var i=0; i< data.length; i++) { |
| 56 | // switch columns and rows |
| 57 | if(cidx==2) { cidx=0; ridx++} |
| 58 | |
| 59 | // give an id! |
| 60 | var cell = $td(t,ridx,cidx); |
| 61 | var title = $a(cell, 'div', 'dashboard-title', '', data[i][0].title); |
Rushabh Mehta | ae5cdeb | 2011-08-30 13:24:49 +0530 | [diff] [blame] | 62 | var parent = $a(cell, 'div', 'dashboard-graph'); |
| 63 | if(data[i][0].comment); |
| 64 | var comment = $a(cell, 'div', 'comment', '', data[i][0].comment) |
Rushabh Mehta | 29a75c4 | 2011-08-25 19:17:44 +0530 | [diff] [blame] | 65 | |
| 66 | parent.id = '_dashboard' + ridx + '-' + cidx; |
| 67 | |
| 68 | // render graph |
Rushabh Mehta | ae5cdeb | 2011-08-30 13:24:49 +0530 | [diff] [blame] | 69 | me.render_graph(parent.id, data[i][1], data[i][0].fillColor); |
Rushabh Mehta | 29a75c4 | 2011-08-25 19:17:44 +0530 | [diff] [blame] | 70 | cidx++; |
| 71 | } |
| 72 | }, |
| 73 | |
Rushabh Mehta | ae5cdeb | 2011-08-30 13:24:49 +0530 | [diff] [blame] | 74 | render_graph: function(parent, values, fillColor) { |
Rushabh Mehta | 29a75c4 | 2011-08-25 19:17:44 +0530 | [diff] [blame] | 75 | var vl = []; |
| 76 | $.each(values, function(i,v) { |
| 77 | vl.push([dateutil.str_to_user(v[0]), v[1]]); |
| 78 | }); |
| 79 | $.jqplot(parent, [vl], { |
| 80 | seriesDefaults:{ |
| 81 | renderer:$.jqplot.BarRenderer, |
| 82 | rendererOptions: {fillToZero: true}, |
| 83 | }, |
| 84 | axes: { |
| 85 | // Use a category axis on the x axis and use our custom ticks. |
| 86 | xaxis: { |
| 87 | min: 0, |
| 88 | renderer: $.jqplot.CategoryAxisRenderer, |
| 89 | tickRenderer: $.jqplot.CanvasAxisTickRenderer, |
| 90 | tickOptions: { |
| 91 | angle: -30, |
| 92 | fontSize: '8pt' |
| 93 | } |
| 94 | }, |
| 95 | // Pad the y axis just a little so bars can get close to, but |
| 96 | // not touch, the grid boundaries. 1.2 is the default padding. |
| 97 | yaxis: { |
| 98 | min: 0, |
| 99 | pad: 1.05, |
| 100 | tickOptions: {formatString: '%d'} |
| 101 | } |
Rushabh Mehta | ae5cdeb | 2011-08-30 13:24:49 +0530 | [diff] [blame] | 102 | }, |
| 103 | seriesColors: [fillColor] |
Rushabh Mehta | 29a75c4 | 2011-08-25 19:17:44 +0530 | [diff] [blame] | 104 | }); |
| 105 | }, |
| 106 | |
| 107 | show_settings: function() { |
| 108 | var d = new wn.widgets.Dialog({ |
| 109 | title: 'Set Company Settings', |
| 110 | width: 500, |
| 111 | fields: [ |
| 112 | { |
| 113 | label:'Company', |
| 114 | reqd: 1, |
| 115 | fieldname:'company', |
| 116 | fieldtype:'Link', |
| 117 | options: 'Company' |
| 118 | }, |
| 119 | { |
| 120 | label:'Start Date', |
| 121 | reqd: 1, |
| 122 | fieldname:'start', |
| 123 | fieldtype:'Date', |
| 124 | }, |
| 125 | { |
| 126 | label:'End Date', |
| 127 | reqd: 1, |
| 128 | fieldname:'end', |
| 129 | fieldtype:'Date', |
| 130 | }, |
| 131 | { |
| 132 | label:'Interval', |
| 133 | reqd: 1, |
| 134 | fieldname:'interval', |
| 135 | fieldtype:'Int' |
| 136 | }, |
| 137 | { |
| 138 | label:'Regenerate', |
| 139 | fieldname:'refresh', |
| 140 | fieldtype:'Button' |
| 141 | } |
| 142 | ] |
| 143 | }); |
| 144 | d.onshow = function() { |
| 145 | d.set_values(pscript.dashboard_settings); |
| 146 | } |
| 147 | d.fields_dict.refresh.input.onclick = function() { |
| 148 | pscript.dashboard_settings = d.get_values(); |
| 149 | me.refresh(); |
| 150 | d.hide(); |
| 151 | } |
| 152 | d.show(); |
| 153 | } |
| 154 | }) |
| 155 | } |