Add XHR CBI helper
[project/luci.git] / libs / cbi / htdocs / luci-static / resources / cbi.js
index 33a328c..e9a8011 100644 (file)
@@ -1,3 +1,18 @@
+/*
+       LuCI - Lua Configuration Interface
+
+       Copyright 2008 Steven Barth <steven@midlink.org>
+       Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
+
+       Licensed under the Apache License, Version 2.0 (the "License");
+       you may not use this file except in compliance with the License.
+       You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+       $Id$
+*/
+
 var cbi_d = [];
 
 function cbi_d_add(field, dep, next) {
@@ -19,27 +34,25 @@ function cbi_d_add(field, dep, next) {
                                "deps": []
                        };
                        cbi_d.unshift(entry);
-               }       
+               }
                entry.deps.push(dep)
        }
 }
 
 function cbi_d_checkvalue(target, ref) {
        var t = document.getElementById(target);
-       var value
-       
-       if (!t) {
-               return true
-       } else if (!t.value) {
+       var value;
+
+       if (!t || !t.value) {
                value = "";
        } else {
                value = t.value;
-               
+
                if (t.type == "checkbox") {
                        value = t.checked ? value : "";
                }
        }
-       
+
        return (value == ref)
 }
 
@@ -52,7 +65,7 @@ function cbi_d_check(deps) {
                if (istat) {
                        return true
                }
-       }               
+       }
 }
 
 function cbi_d_update() {
@@ -71,7 +84,7 @@ function cbi_d_update() {
                                parent.appendChild(entry.node);
                        } else {
                                next.parentNode.insertBefore(entry.node, next);
-                       }               
+                       }
                        state = (state || (node && node.parentNode))
                }
        }
@@ -102,10 +115,17 @@ function cbi_bind(obj, type, callback, mode) {
 }
 
 function cbi_combobox(id, values, def, man) {
+       var selid = "cbi.combobox." + id;
+       if (document.getElementById(selid)) {
+               return
+       }
+
        var obj = document.getElementById(id)
        var sel = document.createElement("select");
+       sel.id = selid;
+       sel.className = 'cbi-input-select';
        if (obj.nextSibling) {
-               obj.parentNode.insertBefore(sel, obj.nextSibling);      
+               obj.parentNode.insertBefore(sel, obj.nextSibling);
        } else {
                obj.parentNode.appendChild(sel);
        }
@@ -161,4 +181,36 @@ function cbi_combobox_init(id, values, def, man) {
                cbi_combobox(id, values, def, man)
        });
        cbi_combobox(id, values, def, man);
-}
\ No newline at end of file
+}
+
+function cbi_filebrowser(id, url, defpath) {
+       var field   = document.getElementById(id);
+       var browser = window.open(
+               url + ( field.value || defpath || '' ) + '?field=' + id,
+               "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
+       );
+
+       browser.focus();
+}
+
+//Hijacks the CBI form to send via XHR (requires Prototype)
+function cbi_hijack_forms(layer, win, fail, load) {
+       layer.select('form').each(function(form) {
+               form.observe('submit', function(event) {
+                       // Prevent the form from also submitting the regular way
+                       event.stop();
+
+                       var form = event.element();
+
+                       if (load) {
+                               load();
+                       }
+
+                       // Submit via XHR
+                       form.request({
+                               onSuccess: win,
+                               onFailure: fail
+                       });
+               });
+       });
+}