artboxModal.js 4.8 KB
/**
 * первая ";" нужна и это не глюк.
 * Если будет сжатие и объединение нескольких файлов и предыдущий код
 * закончится на var a = 5 без ";", то мы получим следующий код :
 * var a=5(function($){...}(jQuery)), а это приведет к ошибке
 */
;(function($) {

    $.fn.artboxModal = function() {

        if (!Array.isArray) {
            Array.isArray = function(arg) {
                return Object.prototype.toString.call(arg) === '[object Array]';
            };
        }

        if (!Object.isObject) {
            Object.isObject = function(arg) {
                return Object.prototype.toString.call(arg) === '[object Object]';
            };
        }

        var that = this; // jq object from which the method was called => $("#asdas")

        return {
            open : function(jqSelector) {

//                var pos = ($(window).scrollTop()+30+50);
                //                 console.log(pos);

                if (
                    jqSelector !== undefined &&
                    jqSelector !== null &&
                    jqSelector !== '' &&
                    typeof(jqSelector) === "string"
                ){

                    $('#overlay').fadeIn(400,
                        function(){
                            $(jqSelector).first()
                                .css('display', 'block')
                                .animate({opacity: 1, top: 0}, 200);
                        });

                    $("body").addClass("off-scroll");
                    return $(jqSelector);
                }
                else{

                    if (that.selector !== ""){
                        $('#overlay').fadeIn(400,
                            function(){
                                console.log("that",that);
                                that
                                    .css('display', 'block')
                                    .animate({opacity: 1, top: 0}, 200);
                            });
                        $("body").addClass("off-scroll");
                        return that;
                    }
                    else {
                        throw 'Don\'t call this function from $.fn without selector in argument';
                    }
                }

            },

            close : function() {
                return this.closeAll();
            },

            closeAll : function() {
                $('.forms_').animate({opacity: 0, top: '0'}, 200,function(){
                    $(this).css('display', 'none');
                    $('#overlay').fadeOut(400);
                });
                $('#success_modal').animate({opacity: 0, top: '0'}, 200,function(){
                    $(this).css('display', 'none');
                    $(this).css({top:'50%'});
                    $('#overlay').fadeOut(400);
                });

                $("body").removeClass("off-scroll");

                return that;
            },

            openSuccessModal : function(arg) {
                try {
                    if (arguments.length === 1){
                        if (
                            Object.isObject(arg) &&
                            arg.hasOwnProperty("header") &&
                            arg.hasOwnProperty("message")
                        ){
                            $("#Success").text(arg.header);
                            $("#success-body").text(arg.message);
                        }
                        else {
                            throw new SyntaxError("Argument must be an object with properties: 'header' and 'message'");
                        }
                    }
                    else if (arguments.length === 2){
                        if (
                            typeof arguments[0] === "string" &&
                            typeof arguments[1] === "string"
                        ){
                            var header = arguments[0],
                                message = arguments[1];

                            $("#Success").text(header);
                            $("#success-body").text(message);
                        }
                        else {
                            throw new SyntaxError("Both params (header and message) must be a string");
                        }
                    }
                    else {
                        throw new SyntaxError("You can specify only 1(as object) or 2(as strings) arguments");
                    }

//                    $('#success-modal').modal('show');
                    that.artboxModal().open("#success-modal");
                    return $('#success-modal');

                }
                catch (err){
                    console.log(err);
                }
            }
        };
    }
}(jQuery));