1
///<reference name="MicrosoftAjax.js"/>
Type.registerNamespace("AmpUI.Form");

AmpUI.Form.FBuilder = function (element) {
    AmpUI.Form.FBuilder.initializeBase(this, [element]);
    this._questions = null;
    this._formID = null;
    this._submitted = null;
    this._facID = null;
    this._formType = null;
    this._autoSaveID = null;
    this._typeName = null;
    this._enableAutoSave = true;
    this._autoSaveInterval = 30000;
    this._template = null;
    this._btnsave = null;
    this._btnsubmit = null;
    this._btnsavebottom = null;
    this._btnsubmitbottom = null;
    this._saveDelegate = null;
    this._submitDelegate = null;
    this._isautosave = null;
    this._saveCallback = null;
    this._submitCallback = null;
};

//define proto
AmpUI.Form.FBuilder.prototype = {
    initialize: function () {
        AmpUI.Form.FBuilder.callBaseMethod(this, "initialize");

        //wire up save/submit events
        if (this._submitted) {
            this.GetResponses(this);
        }
        else {

            //register the dependencies
            Array.forEach(this.get_qrefs(), function (e, i, a) {
                e.registerDepedency(a);
            });
            //start autosave
            if (this._enableAutoSave && !this._submitted) {
                this._autoSaveID = setInterval("$find('" + this._element.id + "').SaveForm(true);", this._autoSaveInterval);
            }
            if (this._saveDelegate === null) {
                this._saveDelegate = Function.createDelegate(this, this.Save);
            }
            Sys.UI.DomEvent.addHandler($get(this._btnsave), 'click', this._saveDelegate);
            Sys.UI.DomEvent.addHandler($get(this._btnsavebottom), 'click', this._saveDelegate);

            if (this._submitDelegate === null) {
                this._submitDelegate = Function.createDelegate(this, this.SubmitForm);
            }
            Sys.UI.DomEvent.addHandler($get(this._btnsubmit), 'click', this._submitDelegate);
            Sys.UI.DomEvent.addHandler($get(this._btnsubmitbottom), 'click', this._submitDelegate);
        }
        this._template = "<div class='form_question'><span>{2}</span><div class='form_answer'>{3}</div></div>";
    },
    dispose: function () {
        //init
        AmpUI.Form.FBuilder.callBaseMethod(this, "dispose");
    },
    SaveForm: function (is_auto) {
        this._isautosave = is_auto;
        var questions = this.get_json();
        var sa = new ampWeb.FormService();
        sa.SaveFormQuestions(Sys.Serialization.JavaScriptSerializer.serialize(this.get_json()), false, this._typeName, this.onSuccess, this.onFail, this);
    },
    Save: function () {
        this.SaveForm(false);
    },
    SubmitForm: function () {
        var useValidation = true;
        var valid = false;
        try {
            valid = Page_ClientValidate();
        }
        catch (e) {
            useValidation = false;
        }
        if (valid || !useValidation) {
            //stop autosave
            clearInterval(this._autoSaveID);
            autosaveEnabled = false;
            var sa = new ampWeb.FormService();
            sa.SaveFormQuestions(Sys.Serialization.JavaScriptSerializer.serialize(this.get_json()), true, this._typeName, this.onSubmitComplete, this.onFail, this);
        } else {
            $('#' + me._element.id + ' .fbuilder-result').html('Invalid responses were found.  Please make all required corrections before submitting.');
        }
    },
    onSuccess: function (s, me) {
        $('#' + me._element.id + ' .fbuilder-result').fadeOut();
        if (me._isautosave) {
            var now = new Date();
            $('#' + me._element.id + ' .fbuilder-result').html('Last autosave:' + now.format('hh:mm tt'));
        } else {
            $('#' + me._element.id + ' .fbuilder-result').html('Form has been saved.');
            me._save();
            if (me._saveCallback)
                me._saveCallback.call(me);
        }
        $('#' + me._element.id + ' .fbuilder-result').fadeIn();
    },
    onFail: function (r, me) {
        $('#' + me._element.id + ' .fbuilder-result').html(r.get_message());
        //var s = new ampWeb.FormService();
        //s.ReportAjaxError(r.get_message());
    },
    onSubmitComplete: function (r, me) {
        $('#' + me._element.id + ' .fbuilder-result').html('Your responses have been submitted.');
        $('#' + me._element.id).fadeOut('fast', function () {
            $('#' + me._element.id).html('<h1>Thank you for submitting.</h1>');
            clearInterval(me._autoSaveID);
            me._submitted = true;
            me.GetResponses(me);
            if (me._submitCallback)
                me._submitCallback.call(me);
        });
    },
    GetResponses: function (me) {
        var sa = new ampWeb.FormService();
        sa.GetFormResponses(me._formID, me.WriteToForm, me.onFail, me);
    },
    WriteToForm: function (answers, me) {
        var ar = Sys.Serialization.JavaScriptSerializer.deserialize(answers);
        //recreate if missing
        if ($('#' + me._element.id + ' #submitted').length == 0) { $('#' + me._element.id).append("<div id='submitted'/>"); }
        Array.forEach(ar, function (a) {
            $('#' + me._element.id + ' #submitted').append(String.format(me._template, null, null, a.question, a.response));
        });
        $('#' + me._element.id).fadeIn('fast');
        me._submit();
    },
    resetInterval: function () {
        if (this._enableAutoSave) {
            clearInterval(this._autoSaveID);
            this._autoSaveID = this._autoSaveID = setInterval("$find('" + this._element.id + "').SaveForm(true);", 30000);
        }
    },
    get_questions: function () {
        return Sys.Serialization.JavaScriptSerializer.serialize(this._questions);
    },
    set_questions: function (q) {
        this._questions = Sys.Serialization.JavaScriptSerializer.deserialize(q);
    },
    get_qrefs: function () {
        var a = new Array();
        Array.forEach(this._questions, function (i) {
            a.push($find(i));
        });
        return a;
    },
    get_json: function () {
        var a = new Array();
        Array.forEach(this._questions, function (i) {
            a.push($find(i));
        });
        var b = new Array();
        Array.forEach(a, function (i) {
            b.push(i.get_json());
        });
        return b;
    },
    get_formID: function () {
        return this._formID;
    },
    set_formID: function (value) {
        this._formID = value;
    },
    get_submitted: function () {
        return this._submitted;
    },
    set_submitted: function (value) {
        this._submitted = value;
    },
    set_facID: function (value) {
        this._facID = value;
    },
    get_facID: function () {
        return this._facID;
    },
    set_formType: function (value) {
        this._formType = value;
    },
    get_formType: function () {
        return this._formType;
    },
    set_typeName: function (value) {
        this._typeName = value;
    },
    get_typeName: function () {
        return this._typeName;
    },
    set_enableAutoSave: function (value) {
        this._enableAutoSave = value;
    },
    get_enableAutoSave: function () {
        return this._enableAutoSave;
    },
    set_autoSaveInterval: function (value) {
        if (!isNaN(value))
            this._autoSaveInterval = value;
    },
    get_autoSaveInterval: function () {
        return this._autoSaveInterval;
    },
    set_template: function (value) {
        this._template = value;
    },
    get_template: function () {
        return this._template;
    },
    set_btnsave: function (v) {
        this._btnsave = v;
    },
    get_btnsave: function () {
        return this._btnsave;
    },
    get_btnsubmit: function () {
        return this._btnsubmit;
    },
    set_btnsubmit: function (v) {
        this._btnsubmit = v;
    },
    set_btnsave: function (v) {
        this._btnsave = v;
    },
    get_btnsavebottom: function () {
        return this._btnsavebottom;
    },
    get_btnsubmitbottom: function () {
        return this._btnsubmitbottom;
    },
    set_btnsubmitbottom: function (v) {
        this._btnsubmitbottom = v;
    },
    set_btnsavebottom: function (v) {
        this._btnsavebottom = v;
    },
    get_saveCallback: function () {
        return this._saveCallback;
    },
    set_saveCallback: function (f) {
        var fn = window[f];
        if (fn && typeof (fn) === 'function')
            this._saveCallback = fn;
    },
    get_submitCallback: function (f) {
        return this._submitCallback;
    },
    set_submitCallback: function (f) {
        var fn = window[f];
        if (fn && typeof (fn) === 'function')
            this._submitCallback = fn;
    },
    add_save: function (handler) {
        this.get_events().addHandler("save", handler);
    },
    remove_save: function (handler) {
        this.get_events().removeHandler("save", handler);
    },
    _save: function () {
        var handler = this.get_events().getHandler("save");
        if (handler) handler(this, Sys.EventArgs.Empty);
    },
    add_submit: function (handler) {
        this.get_events().addHandler("submit", handler);
    },
    remove_submit: function (handler) {
        this.get_events().removeHandler("submit", handler);
    },
    _submit: function () {
        var handler = this.get_events().getHandler("submit");
        if (handler) handler(this, Sys.EventArgs.Empty);
    }
}
//register in the namespace
AmpUI.Form.FBuilder.registerClass("AmpUI.Form.FBuilder", Sys.UI.Control);
if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

特別ですか?たとえば、Javascript 関数の addHandlder と removeHandler を理解できず、registerNamespace とは何か、Sys.Application.notifyScriptLoaded(); とは何か?

特別ですか?たとえば、Javascript 関数の addHandlder と removeHandler を理解できず、registerNamespace とは何か、Sys.Application.notifyScriptLoaded(); とは何か?

4

0 に答える 0