"use strict";

var m      = require("mithril"),
    assign = require("lodash.assign"),
    
    db = require("../lib/firebase");

module.exports = {
    display : {
        controller : function(options) {
            var ctrl  = this,
                field = db.child("fields/" + options.id),
                data  = db.child("data/" + options.id);
            
            ctrl.field = null;
            ctrl.data  = {
                value : ""
            };
            
            field.on("value", function(snap) {
                ctrl.field = snap.val();
                
                m.redraw();
            });

            data.on("value", function(snap) {
                ctrl.data = snap.val();
                
                m.redraw();
            });

            ctrl.oninput = function(value) {
                data.update({
                    value : value
                });
            };
        },

        view : function(ctrl) {
            console.log(ctrl);
            
            if(!ctrl.field) {
                return m("span", "Loading...");
            }

            return m("label", ctrl.field.name,
                m("input", assign({}, ctrl.field, ctrl.data, {
                    oninput : m.withAttr("value", ctrl.oninput)
                }))
            );
        }
    },

    edit : {
        controller : function(options) {
            var ctrl  = this,
                field = db.child("fields/" + options.id);;
            
            ctrl.options = options;
            ctrl.field   = null;
            
            field.on("value", function(snap) {
                ctrl.field = snap.val();
                
                m.redraw();
            });

            ctrl.nameChange = function(name) {
                field.update({
                    name : name
                });
            };

            ctrl.placeholderChange = function(placeholder) {
                field.update({
                    placeholder : placeholder
                });
            };

            ctrl.disabledChange = function(disabled) {
                field.update({
                    disabled : disabled
                });
            };

            ctrl.readonlyChange = function(readonly) {
                field.update({
                    readonly : readonly
                });
            };
        },

        view : function(ctrl) {
            if(!ctrl.field) {
                return m("span", "Loading...");
            }

            return m("div",
                m.component(module.exports.display, ctrl.options),
                m("ul",
                    m("li",
                        m("label",
                            "Name",
                            m("input", {
                                oninput : m.withAttr("value", ctrl.nameChange),
                                value   : ctrl.field.name || ""
                            })
                        )
                    ),
                    m("li",
                        m("label",
                            "Placeholder",
                            m("input", {
                                oninput : m.withAttr("value", ctrl.placeholderChange),
                                value   : ctrl.field.placeholder || ""
                            })
                        )
                    ),
                    m("li",
                        m("label",
                            m("input[type=checkbox]", {
                                onclick : m.withAttr("checked", ctrl.disabledChange),
                                checked : ctrl.field.disabled || false
                            }),
                            "Disabled"
                        )
                    ),
                    m("li",
                        m("label",
                            m("input[type=checkbox]", {
                                onclick : m.withAttr("checked", ctrl.readonlyChange),
                                checked : ctrl.field.readonly || false
                            }),
                            "Read-Only"
                        )
                    )
                )
            );
        }
    }
};

@@ -123,16 +123,124 @@
 ase%22);%0A%0A
+function update(db, name, val) %7B%0A    var args = %7B%7D;%0A    %0A    args%5Bname%5D = val;%0A    %0A    db.update(args);%0A%7D%0A%0A
 module.e
@@ -390,17 +390,20 @@
 options.
-i
+fiel
 d),%0A    
@@ -449,18 +449,20 @@
 options.
-i
 d
+ata
 );%0A     
@@ -1072,52 +1072,8 @@
 ) %7B%0A
-            console.log(ctrl);%0A            %0A
     
@@ -1529,13 +1529,15 @@
 ons.
-i
+fiel
 d);
-;
 %0A   
@@ -1810,621 +1810,277 @@
 nge 
-= function(name) %7B%0A                field.update(%7B%0A                    name : name%0A                %7D);%0A            %7D;%0A%0A            ctrl.placeholderChange = function(placeholder) %7B%0A                field.update(%7B%0A                    placeholder : placeholder%0A                %7D);%0A            %7D;%0A%0A            ctrl.disabledChange = function(disabled) %7B%0A                field.update(%7B%0A                    disabled : disabled%0A                %7D);%0A            %7D;%0A%0A            ctrl.readonlyChange = function(readonly) %7B%0A                field.update(%7B%0A                    readonly : readonly%0A                %7D);%0A            %7D
+       = update.bind(null, field, %22name%22);%0A            ctrl.placeholderChange = update.bind(null, field, %22placeholder%22);%0A            ctrl.disabledChange    = update.bind(null, field, %22disabled%22);%0A            ctrl.readonlyChange    = update.bind(null, field, %22readonly%22)
 ;%0A  

Streamline updating values some