﻿Ext.ns("OXX");



OXX.Scheme = Ext.extend(Ext.util.Observable, {

    constructor: function(config) {
        OXX.Scheme.superclass.constructor.call(this, config);
        this.els = {};
        Ext.apply(this, config);
    },

    schemeTpl: [],

    rowTpl: [
            '<div class="scheme-row">',
                '<div class="scheme-label">{labelText}</div>',
                '<div class="scheme-input">',
                '<input class="input_{schemeId}" style="{inputWidth}px" id="input_{inputId}" name="{inputId}" value="{inputValue}">',
                '</div>',
            '</div>'
            ],

    actionTpl: [],
    
    // when viewed by non-anonymous persons, the values will contain some values
    inputValues: {},

    addValue: function(inputId, inputValue) {
        this.inputValues[inputId] = inputValue;
    },

    getValue: function(inputId) {
        return (Ext.isPrimitive(this.inputValues[inputId])) ? this.inputValues[inputId] : "";

    },

    actions: {},

    addAction: function(action, fn) {
        this.actions[action] = fn;
    },

    // clear all elements below <el>......</el>
    clear: function(el) {
        Ext.select('div', true, el).remove();
    },


    render: function(clear) {
        if (clear)
            this.clear(this.contentEl);

        this.renderTo(this.contentEl);
    },

    renderTo: function(el) {

        if (!Ext.isObject(el)) return;

        var t = new Ext.Template(this.schemeTpl);
        t.append(el, this);
        t = new Ext.Template(this.rowTpl);

        Ext.each(this.items, function(o, i, a) {

             o.schemeId = this.schemeId;


            if (Ext.isPrimitive(o.inputId)) {
                if (Ext.isPrimitive(this.inputValues[o.inputId])) {
                    o.inputValue = this.inputValues[o.inputId];
                }
            }

            var xEl = (Ext.isEmpty(o.destId)) ? Ext.get('default_' + this.schemeId) : Ext.get(o.destId + '_' + this.schemeId);
            if (Ext.isEmpty(o.template)) {
                t.append(xEl, o);


            } else {
                var newT = new Ext.Template(o.template);
                newT.append(xEl, o);
            }

        }, this);

        var at = new Ext.Template(this.actionTpl);
        el = Ext.fly('actions_' + this.schemeId);
        at.append(el, this);

        Ext.fly('master_' + this.schemeId).on('click', function(e, t) {

            if (Ext.isFunction(this.actions[t.name])) {
                var values = {};
                var els = Ext.query('.input_' + this.schemeId);

                Ext.each(els, function(o, i, a) {
                    values[o.name] = o.value;

                });

                this.actions[t.name].call(this, values);

            }
        }, this);

    }




});


 
 
 
 
 