Don't like ads? PRO users don't see any ads ;-)
Guest

zzz

By: a guest on Jun 4th, 2012  |  syntax: None  |  size: 10.34 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <style type="text/css">
  2. .gads-kpi-editor div {
  3.   margin: 5px 7px;
  4. }
  5. .gads-kpi-editor .edit div {
  6.   margin: 0;
  7.   margin-top: 5px;
  8. }
  9. .gads-kpi-editor b {
  10.   color: red;
  11. }
  12. .gads-kpi-editor em {
  13.   font-style: normal;
  14.   font-weight: bold;
  15.   color: #bbb;
  16. }
  17. .gads-kpi-editor expr * {
  18.   display: inline;
  19.   font-family: monospace;
  20.   font-weight: bold;
  21. }
  22. .gads-kpi-editor expr {
  23.   font-weight: bold;
  24.   color: #aaf;
  25. }
  26. .gads-kpi-editor expr op name {
  27.   color: #777;
  28. }
  29. .gads-kpi-editor expr num {
  30.   color: #f77;
  31. }
  32. .gads-kpi-editor expr args:before {
  33.   content: "(";
  34. }
  35. .gads-kpi-editor expr args:after {
  36.   content: ")";
  37. }
  38. .gads-kpi-editor expr args > *:after {
  39.   color: #aaf;
  40.   content: ",\a0";
  41. }
  42. .gads-kpi-editor expr args > *:last-child:after {
  43.   content: "";
  44. }
  45. .gads-kpi-editor expr plus > *:first-child:after {
  46.   content: "+";
  47.   color: #383;
  48. }
  49. .gads-kpi-editor expr minus > *:first-child:after {
  50.   content: "-";
  51.   color: #383;
  52. }
  53. .gads-kpi-editor expr mul > *:first-child:after {
  54.   content: "*";
  55.   color: #383;
  56. }
  57. .gads-kpi-editor expr div > *:first-child:after {
  58.   content: "/";
  59.   color: #383;
  60. }
  61. </style>
  62. <script type="text/javascript">
  63. function gads_kpi_editor_changed_kpi() {
  64.   alert('1');
  65.   var $w=$('#window{{window.id}}');
  66.   alert('2');
  67.   var val=$w.find('select.name').val();
  68.   alert('3');
  69.   switch (val) {
  70.   case 'new':
  71.     alert('4');
  72.     $w.find('.newkpi').show();
  73.     alert('5');
  74.     $w.find('.edit').show();
  75.     alert('6');
  76.     $w.find('.edit select.type').attr('disabled',false);
  77.     alert('7');
  78.     $w.find('button.delete').hide();
  79.     alert('8');
  80.     $w.find('.edit input').val('');
  81.     alert('9');
  82.     $w.find('.edit textarea').val('');
  83.     alert('10');
  84.     gads_kpi_editor_changed_type();
  85.     alert('11');
  86.     break;
  87.   case 'none':
  88.     $w.find('.newkpi').hide();
  89.     $w.find('.edit').hide();
  90.     break;
  91.   default:  // already created KPI
  92.     $w.find('.newkpi').hide();
  93.     $w.find('.edit').show();
  94.     $w.find('.edit select.type').attr('disabled',true);
  95.     $w.find('button.delete').show();
  96.     $o=$w.find("select.name option[value='"+val+"']");
  97.     gads_kpi_editor_set_form($w, $o.attr('_type'), $o.attr('_expr'),
  98.                              $o.attr('_desc'), $o.attr('_uom'), $o.attr('_agg'));
  99.     gads_kpi_editor_changed_type();
  100.     break;
  101.   }
  102. }
  103.  
  104. function gads_kpi_editor_set_form($w, tp, ex, desc, uom, agg) {
  105.   $w.find('.edit select.type').val(tp);
  106.   $w.find('.edit input.desc').val(desc);
  107.   $w.find('.edit select.unit_of_measure').val(uom);
  108. //  $w.find('.edit select.aggregate_function').val(agg);
  109.   switch (tp) {
  110.   case 'p':
  111. //    $w.find('div.pname input').val(ex);
  112.     break;
  113.   case 'e':
  114.     $w.find('div.expr textarea').val(ex);
  115.     break;
  116.   }
  117. }
  118.  
  119. function gads_kpi_editor_validate_new(no_ajax) {
  120.   var $w=$('#window{{window.id}}');
  121.   var val=$w.find('.newkpi input').val();
  122.   var $t=$w.find('.newkpi .isvalid');
  123.  
  124.   if (val=='') {
  125.     $t.html('<b>This field is mandatory</b>');
  126.     return false;
  127.   }
  128.   if (!no_ajax) {
  129.     $t.html('...');
  130.     gads_ajax($w, 'isKPINameValid', {name:val})
  131.     .done(function(response) {
  132.       if (response) {
  133.         $t.html('OK');
  134.       } else {
  135.         $t.html('<b>Name already in use!</b>');
  136.       }
  137.     });
  138.   }
  139.   return true;  // superficial check, don't wait for AJAX
  140. }
  141.  
  142. function gads_kpi_editor_validate_expr(no_ajax) {
  143.   var $w=$('#window{{window.id}}');
  144.   var val=$w.find('.expr textarea').val();
  145.   var $t=$w.find('.expr .isvalid');
  146.  
  147.   if (val=='') {
  148.     $t.html('<b>This field is mandatory</b>');
  149.     return false;
  150.   }
  151.   if (!no_ajax) {
  152.     $t.html('...');
  153.     gads_ajax($w, 'isKPIDefValid', {expr:val})
  154.     .done(function(response) {  // TODO
  155.       if (!response[0]) {
  156.         $t.html('<b>Error: '+response[1]+'</b>');
  157.       } else {
  158.         $t.html('Definition OK, <expr>'+response[1]+'</expr>');
  159.       }
  160.     });
  161.   }
  162.   return true;  // superficial check, don't wait for AJAX
  163. }
  164.  
  165. function gads_kpi_editor_changed_type() {
  166.   var $w=$('#window{{window.id}}');
  167.   $w.find('.edit div.pane').hide();
  168.   switch ($w.find('.edit select.type').val()) {
  169.   case 'p':
  170. //    $w.find('.edit div.pname').show();
  171.     break;
  172.   case 'e':
  173.     $w.find('.edit div.expr').show();
  174.     break;
  175.   }
  176. }
  177.  
  178. function gads_kpi_editor_validate_pname() {
  179.   var val=$('#window{{window.id}} .pname input').val();
  180.   if (val!='') {
  181.     $('#window{{window.id}} .pname .isvalid').html('OK');
  182.     return true;
  183.   } else {
  184.     $('#window{{window.id}} .pname .isvalid').html('<b>This field is mandatory</b>');
  185.     return false;
  186.   }
  187. }
  188.  
  189. function gads_kpi_editor_validate_fields() {
  190.   var result=true;
  191.   if ($('#window{{window.id}} select.name').val()=='new') {
  192.     result&=gads_kpi_editor_validate_new(true);
  193.   }
  194.   switch ($('#window{{window.id}} select.type').val()) {
  195.   case 'p':
  196. //    result&=gads_kpi_editor_validate_pname();
  197.     break;
  198.   case 'e':
  199.     result&=gads_kpi_editor_validate_expr(true);
  200.     break;
  201.   }
  202.   return result;
  203. }
  204.  
  205. function gads_kpi_editor_gather_data() {
  206.   var $e=$('#window{{window.id}} .edit');
  207.   var data={};
  208.   switch ($e.find('select.type').val()) {
  209.   case 'p':
  210. //    data['expr']=$e.find('.pname input').val();
  211.     break;
  212.   case 'e':
  213.     data['expr']=$e.find('.expr textarea').val();
  214.     break;
  215.   }
  216.   data['type']=$e.find('select.type').val();
  217.   data['desc']=$e.find('input.desc').val();
  218.   data['unit_of_measure']=parseInt($e.find('select.unit_of_measure').val());
  219. //  data['aggregate_function']=$e.find('select.aggregate_function').val();
  220.   return data;
  221. }
  222.  
  223. function gads_kpi_editor_save() {
  224.   if (!gads_kpi_editor_validate_fields()) return;
  225.   var $w=$('#window{{window.id}}');
  226.   var sel_id=$w.find('select.name').val();
  227.   var action, name;
  228.   var data=gads_kpi_editor_gather_data();
  229.   switch (sel_id) {
  230.   case 'new':
  231.     action='createKPI';
  232.     action_txt='created';
  233.     data['name']=$('#window{{window.id}} .newkpi input')[0].value;
  234.     name=data['name'];
  235.     break;
  236.   case 'none':  // essentially impossible
  237.     alert('internal error');
  238.     break;
  239.   default:  // existing KPI
  240.     action='modifyKPI';
  241.     action_txt='modified';
  242.     data['id']=parseInt(sel_id);
  243.     name=$w.find('select.name option:selected').text();
  244.     break;
  245.   }
  246.   gads_ajax($w, action, data)
  247.     .done(function(response) {  // TODO
  248.       if (!response[0]) {
  249.         alert('Error: '+response[1]);
  250.       } else {
  251.         success_msg($w, "KPI '"+name+"' "+action_txt);
  252.         reloadWindowView($w);
  253.       }
  254.     });
  255. }
  256.  
  257. function gads_kpi_editor_delete() {
  258.   var $w=$('#window{{window.id}}');
  259.   var name=$w.find('select.name option:selected').text();
  260.   var kpi_id=$w.find('select.name').val();
  261.   var ans=confirm("Are you sure you want to delete KPI '"+name+"'?");
  262.   if (ans) {
  263.     gads_ajax($w, 'deleteKPI', {id:kpi_id})
  264.     .done(function(response) {
  265.       if (response[0]) {
  266.         success_msg($w, "KPI '"+name+"' deleted");
  267.       } else {
  268.         showWindowError($w, "KPI '"+name+"' not deleted: "+response[1]);
  269.       }
  270.     });
  271.     reloadWindowView($w);
  272.   }
  273. }
  274.  
  275. function gads_kpi_editor_dbg_show_exp() {
  276.   var $w=$('#window{{window.id}}');
  277.   var kpi_id=$w.find('select.name').val();
  278.   var unit_id=$w.find('select.dbg_unit').val();
  279.   if (kpi_id=='none'||kpi_id=='new') {
  280.     alert('Select an existing KPI!');
  281.     return;
  282.   }
  283.   gads_ajax($w, 'debug_getExpansion', {kpi_id:kpi_id, unit_id:unit_id})
  284.   .done(function(response) {
  285.     if (response[0]) {
  286.       $w.find('.dbg_exp_resp').text(response[1]);
  287.     } else {
  288.       showWindowError($w, "Error: "+response[1]);
  289.     }  
  290.   });
  291. }
  292. </script>
  293. <div class="gads-kpi-editor">
  294.   <div>
  295.     KPI: <select onchange="gads_kpi_editor_changed_kpi()" class="name" name="name">
  296.       <option value="none">- select KPI expression -</option>
  297.       <option value="new">- Create new -</option>
  298.       {% for kpi in kpi_list %}
  299.       <option value="{{kpi.id}}" _type="{{kpi.type}}" _expr="{{kpi.expr}}" _desc="{{kpi.desc|default:""}}" _uom="{{kpi.unit_of_measure.id}}" _agg="{{kpi.aggregate_function}}">{{kpi.name}} ({{kpi.desc}})</option>
  300.       {% endfor %}
  301.     </select>
  302.   </div>
  303.   <div class="newkpi" style="display:none">
  304.   New KPI name: <input name="newname" type="text" onchange="gads_kpi_editor_validate_new()" /> <span class="isvalid">...</span>
  305.   <!-- TODO: img instead of span? -->
  306.   </div>
  307.   <div class="edit" style="display:none">
  308.     <div>
  309.       Description: <input class="desc" style="width: 25em" /> (not required)
  310.     </div>
  311.     <div>
  312.       KPI type:
  313.       <select onchange="gads_kpi_editor_changed_type()" name="type" class="type">
  314.         <option value="p">EDS point</option>
  315.         <option value="e">KPI expression</option>
  316.       </select>
  317.     </div>
  318.     <div>
  319.       Unit of measure:
  320.       <select name="unit_of_measure" class="unit_of_measure">
  321.         <option value="">(undefined)</option>
  322.       {% for uom in uoms %}
  323.         <option value="{{uom.id}}">{{uom.name}}</option>
  324.       {% endfor %}
  325.       </select> (only required for KPIs that will be shown in reports)
  326.     </div>
  327. <!--
  328.     <div>
  329.       Temporal aggregation mode:
  330.       <select name="aggregate_function" class="aggregate_function">
  331.         <option value="sum">Sum</option>
  332.         <option value="avg">Average</option>
  333.       </select>
  334.     </div>
  335. -->
  336.     <div class="expr pane" style="display:none">
  337.       KPI expression:
  338.       <textarea style="width:100%;height:3em" name="exprdef" onchange="gads_kpi_editor_validate_expr()"></textarea>
  339.       <span class="isvalid">...</span><br />
  340. <!--      <a href="#">Click here</a> for help on KPI expression language.-->
  341.     </div>
  342. <!--
  343.     <div class="pname pane" style="display:none">
  344.       EDS point name: <input type="text" name="pname" onchange="gads_kpi_editor_validate_pname()" /> <span class="isvalid">...</span>
  345.     </div>
  346. -->
  347.     <div class="controls">
  348.       <button class="save" onclick="gads_kpi_editor_save()">Save</button>
  349.       <button class="delete" onclick="gads_kpi_editor_delete()">Delete</button>
  350.     </div>
  351.   </div>
  352. <!--
  353.   <div class="debug">
  354.     <button class="show_exp" onclick="gads_kpi_editor_dbg_show_exp()">Show expansion</button>
  355.     <select name="dbg_unit" class="dbg_unit">
  356.     {% for unit in dbg_units %}
  357.       <option value="{{unit.id}}">{{unit.name}}@{{unit.plant}}</option>
  358.     {% endfor %}
  359.     </select>
  360.     <pre class="dbg_exp_resp">...</pre>
  361.   </div>
  362. -->
  363.   <!-- FIXME: name, type, newname and exprdef should be unique per page! -->
  364. </div>