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