- <style type="text/css">
- .gads-kpi-editor div {
- margin: 5px 7px;
- }
- .gads-kpi-editor .edit div {
- margin: 0;
- margin-top: 5px;
- }
- .gads-kpi-editor b {
- color: red;
- }
- .gads-kpi-editor em {
- font-style: normal;
- font-weight: bold;
- color: #bbb;
- }
- .gads-kpi-editor expr * {
- display: inline;
- font-family: monospace;
- font-weight: bold;
- }
- .gads-kpi-editor expr {
- font-weight: bold;
- color: #aaf;
- }
- .gads-kpi-editor expr op name {
- color: #777;
- }
- .gads-kpi-editor expr num {
- color: #f77;
- }
- .gads-kpi-editor expr args:before {
- content: "(";
- }
- .gads-kpi-editor expr args:after {
- content: ")";
- }
- .gads-kpi-editor expr args > *:after {
- color: #aaf;
- content: ",\a0";
- }
- .gads-kpi-editor expr args > *:last-child:after {
- content: "";
- }
- .gads-kpi-editor expr plus > *:first-child:after {
- content: "+";
- color: #383;
- }
- .gads-kpi-editor expr minus > *:first-child:after {
- content: "-";
- color: #383;
- }
- .gads-kpi-editor expr mul > *:first-child:after {
- content: "*";
- color: #383;
- }
- .gads-kpi-editor expr div > *:first-child:after {
- content: "/";
- color: #383;
- }
- </style>
- <script type="text/javascript">
- function gads_kpi_editor_changed_kpi() {
- alert('1');
- var $w=$('#window{{window.id}}');
- alert('2');
- var val=$w.find('select.name').val();
- alert('3');
- switch (val) {
- case 'new':
- alert('4');
- $w.find('.newkpi').show();
- alert('5');
- $w.find('.edit').show();
- alert('6');
- $w.find('.edit select.type').attr('disabled',false);
- alert('7');
- $w.find('button.delete').hide();
- alert('8');
- $w.find('.edit input').val('');
- alert('9');
- $w.find('.edit textarea').val('');
- alert('10');
- gads_kpi_editor_changed_type();
- alert('11');
- break;
- case 'none':
- $w.find('.newkpi').hide();
- $w.find('.edit').hide();
- break;
- default: // already created KPI
- $w.find('.newkpi').hide();
- $w.find('.edit').show();
- $w.find('.edit select.type').attr('disabled',true);
- $w.find('button.delete').show();
- $o=$w.find("select.name option[value='"+val+"']");
- gads_kpi_editor_set_form($w, $o.attr('_type'), $o.attr('_expr'),
- $o.attr('_desc'), $o.attr('_uom'), $o.attr('_agg'));
- gads_kpi_editor_changed_type();
- break;
- }
- }
- function gads_kpi_editor_set_form($w, tp, ex, desc, uom, agg) {
- $w.find('.edit select.type').val(tp);
- $w.find('.edit input.desc').val(desc);
- $w.find('.edit select.unit_of_measure').val(uom);
- // $w.find('.edit select.aggregate_function').val(agg);
- switch (tp) {
- case 'p':
- // $w.find('div.pname input').val(ex);
- break;
- case 'e':
- $w.find('div.expr textarea').val(ex);
- break;
- }
- }
- function gads_kpi_editor_validate_new(no_ajax) {
- var $w=$('#window{{window.id}}');
- var val=$w.find('.newkpi input').val();
- var $t=$w.find('.newkpi .isvalid');
- if (val=='') {
- $t.html('<b>This field is mandatory</b>');
- return false;
- }
- if (!no_ajax) {
- $t.html('...');
- gads_ajax($w, 'isKPINameValid', {name:val})
- .done(function(response) {
- if (response) {
- $t.html('OK');
- } else {
- $t.html('<b>Name already in use!</b>');
- }
- });
- }
- return true; // superficial check, don't wait for AJAX
- }
- function gads_kpi_editor_validate_expr(no_ajax) {
- var $w=$('#window{{window.id}}');
- var val=$w.find('.expr textarea').val();
- var $t=$w.find('.expr .isvalid');
- if (val=='') {
- $t.html('<b>This field is mandatory</b>');
- return false;
- }
- if (!no_ajax) {
- $t.html('...');
- gads_ajax($w, 'isKPIDefValid', {expr:val})
- .done(function(response) { // TODO
- if (!response[0]) {
- $t.html('<b>Error: '+response[1]+'</b>');
- } else {
- $t.html('Definition OK, <expr>'+response[1]+'</expr>');
- }
- });
- }
- return true; // superficial check, don't wait for AJAX
- }
- function gads_kpi_editor_changed_type() {
- var $w=$('#window{{window.id}}');
- $w.find('.edit div.pane').hide();
- switch ($w.find('.edit select.type').val()) {
- case 'p':
- // $w.find('.edit div.pname').show();
- break;
- case 'e':
- $w.find('.edit div.expr').show();
- break;
- }
- }
- function gads_kpi_editor_validate_pname() {
- var val=$('#window{{window.id}} .pname input').val();
- if (val!='') {
- $('#window{{window.id}} .pname .isvalid').html('OK');
- return true;
- } else {
- $('#window{{window.id}} .pname .isvalid').html('<b>This field is mandatory</b>');
- return false;
- }
- }
- function gads_kpi_editor_validate_fields() {
- var result=true;
- if ($('#window{{window.id}} select.name').val()=='new') {
- result&=gads_kpi_editor_validate_new(true);
- }
- switch ($('#window{{window.id}} select.type').val()) {
- case 'p':
- // result&=gads_kpi_editor_validate_pname();
- break;
- case 'e':
- result&=gads_kpi_editor_validate_expr(true);
- break;
- }
- return result;
- }
- function gads_kpi_editor_gather_data() {
- var $e=$('#window{{window.id}} .edit');
- var data={};
- switch ($e.find('select.type').val()) {
- case 'p':
- // data['expr']=$e.find('.pname input').val();
- break;
- case 'e':
- data['expr']=$e.find('.expr textarea').val();
- break;
- }
- data['type']=$e.find('select.type').val();
- data['desc']=$e.find('input.desc').val();
- data['unit_of_measure']=parseInt($e.find('select.unit_of_measure').val());
- // data['aggregate_function']=$e.find('select.aggregate_function').val();
- return data;
- }
- function gads_kpi_editor_save() {
- if (!gads_kpi_editor_validate_fields()) return;
- var $w=$('#window{{window.id}}');
- var sel_id=$w.find('select.name').val();
- var action, name;
- var data=gads_kpi_editor_gather_data();
- switch (sel_id) {
- case 'new':
- action='createKPI';
- action_txt='created';
- data['name']=$('#window{{window.id}} .newkpi input')[0].value;
- name=data['name'];
- break;
- case 'none': // essentially impossible
- alert('internal error');
- break;
- default: // existing KPI
- action='modifyKPI';
- action_txt='modified';
- data['id']=parseInt(sel_id);
- name=$w.find('select.name option:selected').text();
- break;
- }
- gads_ajax($w, action, data)
- .done(function(response) { // TODO
- if (!response[0]) {
- alert('Error: '+response[1]);
- } else {
- success_msg($w, "KPI '"+name+"' "+action_txt);
- reloadWindowView($w);
- }
- });
- }
- function gads_kpi_editor_delete() {
- var $w=$('#window{{window.id}}');
- var name=$w.find('select.name option:selected').text();
- var kpi_id=$w.find('select.name').val();
- var ans=confirm("Are you sure you want to delete KPI '"+name+"'?");
- if (ans) {
- gads_ajax($w, 'deleteKPI', {id:kpi_id})
- .done(function(response) {
- if (response[0]) {
- success_msg($w, "KPI '"+name+"' deleted");
- } else {
- showWindowError($w, "KPI '"+name+"' not deleted: "+response[1]);
- }
- });
- reloadWindowView($w);
- }
- }
- function gads_kpi_editor_dbg_show_exp() {
- var $w=$('#window{{window.id}}');
- var kpi_id=$w.find('select.name').val();
- var unit_id=$w.find('select.dbg_unit').val();
- if (kpi_id=='none'||kpi_id=='new') {
- alert('Select an existing KPI!');
- return;
- }
- gads_ajax($w, 'debug_getExpansion', {kpi_id:kpi_id, unit_id:unit_id})
- .done(function(response) {
- if (response[0]) {
- $w.find('.dbg_exp_resp').text(response[1]);
- } else {
- showWindowError($w, "Error: "+response[1]);
- }
- });
- }
- </script>
- <div class="gads-kpi-editor">
- <div>
- KPI: <select onchange="gads_kpi_editor_changed_kpi()" class="name" name="name">
- <option value="none">- select KPI expression -</option>
- <option value="new">- Create new -</option>
- {% for kpi in kpi_list %}
- <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>
- {% endfor %}
- </select>
- </div>
- <div class="newkpi" style="display:none">
- New KPI name: <input name="newname" type="text" onchange="gads_kpi_editor_validate_new()" /> <span class="isvalid">...</span>
- <!-- TODO: img instead of span? -->
- </div>
- <div class="edit" style="display:none">
- <div>
- Description: <input class="desc" style="width: 25em" /> (not required)
- </div>
- <div>
- KPI type:
- <select onchange="gads_kpi_editor_changed_type()" name="type" class="type">
- <option value="p">EDS point</option>
- <option value="e">KPI expression</option>
- </select>
- </div>
- <div>
- Unit of measure:
- <select name="unit_of_measure" class="unit_of_measure">
- <option value="">(undefined)</option>
- {% for uom in uoms %}
- <option value="{{uom.id}}">{{uom.name}}</option>
- {% endfor %}
- </select> (only required for KPIs that will be shown in reports)
- </div>
- <!--
- <div>
- Temporal aggregation mode:
- <select name="aggregate_function" class="aggregate_function">
- <option value="sum">Sum</option>
- <option value="avg">Average</option>
- </select>
- </div>
- -->
- <div class="expr pane" style="display:none">
- KPI expression:
- <textarea style="width:100%;height:3em" name="exprdef" onchange="gads_kpi_editor_validate_expr()"></textarea>
- <span class="isvalid">...</span><br />
- <!-- <a href="#">Click here</a> for help on KPI expression language.-->
- </div>
- <!--
- <div class="pname pane" style="display:none">
- EDS point name: <input type="text" name="pname" onchange="gads_kpi_editor_validate_pname()" /> <span class="isvalid">...</span>
- </div>
- -->
- <div class="controls">
- <button class="save" onclick="gads_kpi_editor_save()">Save</button>
- <button class="delete" onclick="gads_kpi_editor_delete()">Delete</button>
- </div>
- </div>
- <!--
- <div class="debug">
- <button class="show_exp" onclick="gads_kpi_editor_dbg_show_exp()">Show expansion</button>
- <select name="dbg_unit" class="dbg_unit">
- {% for unit in dbg_units %}
- <option value="{{unit.id}}">{{unit.name}}@{{unit.plant}}</option>
- {% endfor %}
- </select>
- <pre class="dbg_exp_resp">...</pre>
- </div>
- -->
- <!-- FIXME: name, type, newname and exprdef should be unique per page! -->
- </div>