Tuesday, October 29, 2013

jquery extends attempt, 2009

var _objects = {
    interface0: {
        data: new Array(),
        getString: function () { return JSON.stringify(this); },
        extend: function (args) {
            var obj = (typeof args == 'string') ? eval('(' + args + ')') : args;
            $.extend(this, obj, true);
            return this;
        },
        parse: function (args) {
            var i = args.toString().indexOf('{');
            if (i == -1) i = args.toString().indexOf('}');
            if (i == -1) { return { data: args.toString() }; }
            return (typeof args == 'string') ? eval('(' + args + ')') : args;
        }
    },
    create: function () {
        var o = {};
        if (arguments.length == 1) {
            try {
                //Long l = Long.parseLong(arguments[0]);
                o = $.extend({}, { id: arguments[0] }, true);
            } catch (ex) {
                o = $.extend({}, { id: 0 }, true);
                var obj = (typeof arguments[0] == 'string') ? eval('(' + arguments[0] + ')') : arguments[0];
                o = $.extend({}, obj, true);
            }
        } else if (arguments.length == 2) {
            var obj = (typeof arguments[1] == 'string') ? eval('(' + arguments[1] + ')') : arguments[1];
            o = $.extend({}, obj, true);
            o = $.extend(o, { id: arguments[0] }, true);
        } else
            o = $.extend({}, { id: 0 }, true);
        o = $.extend(o, this.interface0, true);
        o.data = new Array();
        //usage::
        //var n = new MyEntity(10);
        //n.hi = "hi boroo";
        //n.extend({name:"boroo"});
        //var obj = new MyEntity(20, n.getString());
        //var n = new MyEntity(10, "{name:'baatar'}");
        //n.data["a"]="b";
        //alert( n.data["a"] );
        return o;
    }
    //var a = _objects.create(22);
    //var b = _objects.create(33);
    //b.hi = "welcome, boroo";
    //b.id="1";
    //a.extend(b.getString());
    //alert(a.data.length);
};

(function ($) {
   
    $._Calendar = new Date();
   
    $.extend($._Calendar, {
        month: function() { return $._Calendar.getMonth() + 1; },
        months: function() { return ("" + ($._Calendar.getMonth() + 1)).length == 1 ? "0" + ($._Calendar.getMonth() + 1) : "" + ($._Calendar.getMonth() + 1); },
        year: function() { return $._Calendar.getFullYear(); },
        dow: function() { return $._Calendar.getDay(); },
        dom: function() { return $._Calendar.getDate() },
        doms: function() { return ("" + $._Calendar.getDate()).length == 1 ? "0" + $._Calendar.getDate() : "" + $._Calendar.getDate(); }
    }, true);
   
    $.extend($._Calendar, {
        dateValue: function () { return $._Calendar.year() + "." + $._Calendar.months() + "." + $._Calendar.doms(); },
        nowString: function () {
            var d = new Date();
            return d.toGMTString();
        },
        dateString: function () {
            var d = new Date();
            return d.getFullYear() + "-" + d.getMonth() + "-" + d.getDate();
        },
        timeString: function () {
            var d = new Date();
            return d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
        }
    }, true);

    $.extend($.expr[':'], {
        'containsi': function (elem, i, match, array) {
            //(elem.textContent || elem.innerText || '')
            var val = $(elem).html().toLowerCase();
            var str = (match[3] || "").toLowerCase();
            return (val.indexOf(str) >= 0);
        }
    });

    $.centerPopup = function() { 
        //request data for centering 
        var windowWidth = document.documentElement.clientWidth; 
        var windowHeight = document.documentElement.clientHeight; 
        var popupHeight = $("#popupContent").height(); 
        var popupWidth = $("#popupContent").width(); 
        //centering 
        $("#popupContent").css({ 
            "position": "absolute", 
            "top": windowHeight/2-popupHeight/2, 
            "left": windowWidth/2-popupWidth/2 
        });
        //only need force for IE6 
        $("#backgroundPopup").css({ 
            "height": windowHeight 
        }); 
    };
   
    $.filterTable = function(text, dataTableSelector, targetTableSelector) {
       
        if (typeof $(dataTableSelector).data("Table-Tr-Data") == 'undefined' || $(dataTableSelector).data("Table-Tr-Data") == null) {
            $(dataTableSelector).data("Table-Tr-Data", $(dataTableSelector).find("> tbody > tr"));
        }
       
        var plus = (text.indexOf('+') > -1);
        var trs = $(dataTableSelector).data("Table-Tr-Data");
        var target = $(targetTableSelector).find(">tbody");
       
        $(target).children().remove();

        if (plus) {
            var txts = text.split('+');
            for (var i = 0; i < txts.length; i++) {
                var txt = txts[i];
                trs = $(trs).filter(":containsi('" + txt + "')");
            }
            $(target).append(trs);
        } else {
            var txts = text.split(' ');
            for (var i = 0; i < txts.length; i++) {
                var txt = txts[i];
                var trs0 = $(trs).filter(":containsi('" + txt + "')");
                $(target).append(trs0);
            }
        }
        return trs;
    };

    $.fn.makeDatePicker = function (method, format) {
        var calendarButtonGif = "/Scripts/jquery/images/calendar.gif";
       
        var target = $(this).datepicker({
            showOn: "both",
            buttonImage: calendarButtonGif,
            buttonImageOnly: true,
            showOtherMonths: true,
            selectOtherMonths: true,
            changeMonth: true,
            changeYear: true,
            onSelect: method,
            dateFormat: ((format == null || !format || typeof format == 'undefined') ? $.datepicker.ATOM : format),
            yearRange: 'c-100:c+20'
            //constrainInput: false
        }).change(method);
       
        var inst = $.datepicker._getInst(target[0]);
        var dateFormat = $.datepicker._get(inst, 'dateFormat');
        var settings = $.datepicker._getFormatConfig(inst);
       
        if(target.val())
        {
            //alert($.datepicker.parseDate(dateFormat, new Date(target.val()), settings));
            $.datepicker._selectDate("#" + target.attr("id"), $.datepicker._formatDate(inst, new Date(target.val())));
        }
        return target;
    };
   
    $.fn.createDropDownMenu = function (options) {
        var a, closetimerHandle = 0, settings = jQuery.extend({
            timeout: 100,
            item: "li",
            query: "> li > a",
            subQuery: "ul",
            hoverClass: "hoverClass"
        }, options),
        closeMethod = function () { // close
            $(a).removeClass(settings.hoverClass).parent(settings.item).find(settings.subQuery)
            .css("visibility", "hidden");
        },
        closetimer = function () { // go close timer
            cancelclosetimer();
            closetimerHandle = window.setTimeout(closeMethod, settings.timeout);
        },
        cancelclosetimer = function () { // cancel close timer
            if (closetimerHandle) {
                window.clearTimeout(closetimerHandle);
                closetimerHandle = null;
            }
        },
        aopen = function () {
            cancelclosetimer();
            closeMethod();

            $(this).addClass(settings.hoverClass).parent(settings.item).find(settings.subQuery)
            .mouseover(cancelclosetimer)
            .mouseout(closetimer)
            .css("visibility", "visible");
        };

        a = $(this).find(settings.query);
        $(a).mouseover(aopen).mouseout(closetimer);

        return this;
    };

    $.fn.ajaxQuery = function (url, callback, options) {
        var loadingImageGif = "/Content/admin/images/loading.gif",
        params, off = url.indexOf("?"), setting = $.extend({
            loadingImage: loadingImageGif,
            loadingImageWidth: 12,
            loadingImageHeight: 12
        }, options);

        if (off >= 0) {
            params = url.slice(off + 1, url.length);
            url = url.slice(0, off);
        }
       
        var type = "GET";
        var self = this;
       
        // Request the remote document
        $.ajax({
            url: url,
            type: type,
            dataType: "html",
            data: params,
            beforeSend: function () {
                self.html($("<img width='" + setting.loadingImageWidth + "' height='" + setting.loadingImageHeight + "'>")
                    .attr("src", setting.loadingImage)
                    .css("border", "none"));
            },
            success: function (data) {
                alert(data.toLowerCase() == "true" ? "Амжилттай" : (data.toLowerCase() == "false" ? "Амжилтгүй !!!" : data));
            }
        });

        return this;
    };

    $.fn.ajaxGet = function (url, options) {
        var loadingImageGif = "/Content/admin/images/loading.gif",
        params, off = url.indexOf("?"), setting = $.extend({
            loadingImage: loadingImageGif,
            loadingImageWidth: 36,
            loadingImageHeight: 36
        }, options);

        if (off >= 0) {
            params = url.slice(off + 1, url.length);
            url = url.slice(0, off);
        }
       
        var self = this;
       
        // Request the remote document
        $.ajax({
            url: url,
            type: "GET",
            dataType: "html",
            data: params,
            beforeSend: function () {
                self.html($("<img width='" + setting.loadingImageWidth + "' height='" + setting.loadingImageHeight + "'>")
                    .attr("src", setting.loadingImage)
                    .css("border", "none"));
            },
            success: function (data) {
                self.html(data);
            }
        });

        return this;
    };

    $.fn.ajaxPost = function (formIndex, options) {
        var loadingImageGif = "/Content/admin/images/loading.gif",
        setting = $.extend({
            loadingImage: loadingImageGif,
            loadingImageWidth: 36,
            loadingImageHeight: 36
        }, options);

        var self = this;
        var form = $($(self).find("form").get(formIndex));
       
        // Request the remote document
        $.ajax({
            url: $(form).attr("action"),
            type: "POST",
            dataType: "html",
            data: $(form).serialize(),
            beforeSend: function () {
                $(self).html($("<img width='" + setting.loadingImageWidth + "' height='" + setting.loadingImageHeight + "'>")
                    .attr("src", setting.loadingImage)
                    .css("border", "none"));
            },
            success: function (data) {
                self.html(data);
//                responseText = jqXHR.responseText;
//                if (jqXHR.isResolved()) {
//                    jqXHR.done(function (r) {
//                        responseText = r;
//                    });
//                    self.html(responseText);
//                }
//                if (callback) {
//                    self.each(callback, [responseText, status, jqXHR]);
//                }
            }
        });

        return this;
    };
})( jQuery );

No comments: