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