Handle save request as insert when there is no ID
[misc/kostenrechnung] / lib / functions.js
index d575cb6..1d7aa0b 100644 (file)
@@ -31,6 +31,7 @@ function ajax_request(func,params,oncomplete)
     if (typeof oncomplete == 'function')
        req.oncomplete = oncomplete;
     req.send(params);
+    info('');
 }
 
 function info(msg)
@@ -65,10 +66,12 @@ function set_value(id, value)
        obj.value = value;
     else if (obj.nodeName.toLowerCase() == 'span')
        obj.innerHTML = value;
-    else if (obj.nodeName.toLowerCase() == 'select')
+    else if (obj.nodeName.toLowerCase() == 'select') {
        for (var i=0; i < obj.options.length; i++)
            if (obj.options[i].value == value)
                obj.selectedIndex = i;
+    } else
+       obj.innerHTML = value;
 }
 
 function setvar(obj, name, callback, status)
@@ -90,6 +93,19 @@ function setvar(obj, name, callback, status)
        status(obj,value,obj.options[obj.selectedIndex].innerHTML);
 }
 
+function get_info(name, values, callback)
+{
+    var source = document.getElementById('source');
+
+    if (!source) return false;
+
+    var parms = 'source=' + source.innerHTML + '&name=' + name;
+    for (key in values)
+       parms += '&' + key + '=' + values[key];
+
+    ajax_request('info', parms, callback);
+}
+
 /*
  * Form functions
  */
@@ -119,6 +135,11 @@ function delete_callback(data)
 
 function form_save(obj)
 {
+    var id = document.getElementById('edit_id');
+
+    if (!id.value.length)
+       return form_insert(obj);
+
     info('');
     ajax_request('save', Form.serialize(obj.form), save_callback);
     return false;
@@ -134,6 +155,9 @@ function form_insert(obj)
 function form_delete(obj)
 {
     var id = document.getElementById('edit_id');
+
+    if (!id.value.length) return;
+
     var source = document.getElementById('edit_source');
     info('');
     var params = 'id='+id.value + '&source='+source.value;
@@ -208,6 +232,25 @@ function grid_update(grid, filter, value)
     grid.buffer.fetch(-1);
 }
 
+function grid_update_filters(grid)
+{
+    var todo = false;
+    for (var c=0; c < grid.headerColCnt; c++) {
+       var fmt = grid.columns[c].format;
+       if (typeof fmt.filterUI != 'string') continue;
+       if (fmt.filterUI != 's') continue;
+       $(grid.filterId(c)).options.length = 1;
+
+       var options = {};
+       Object.extend(options, grid.buffer.ajaxOptions);
+       var colnum = typeof(fmt.filterCol)=='number' ? fmt.filterCol : c;
+
+       options.parameters = 'id='+grid.tableId+'&distinct='+colnum;
+       options.onComplete = grid.filterValuesUpdate.bind(grid,c);
+       new Ajax.Request(grid.buffer.dataSource, options);
+    }
+}
+
 var calendars = new Array();
 function calendar_callback(value)
 {