main.js 4.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
(function (window, $) {
    'use strict';
    var getDevice = function (w) {
        if (w < 500) {
            return 'mobile';
        }
        if (w < 900) {
            return 'tablet';
        }
        return 'desktop';
11
    },
12
        body = $("body");
13

14 15 16 17
    $.vifib.device = getDevice($(window).width());
    $.vifib.devices = {
        "mobile": function (url) {
            $('body')
18 19 20 21 22 23 24 25 26 27 28
                .route('add', $.vifib.pages.overview.url)
                .done($.vifib.pages.overview.action);
            //$('body')
                //.route('add', '')
                //.done($.vifib.mobile.overview);
            //$('body')
                //.route('add', '/login/facebook')
                //.done($.vifib.login.facebook);
            //$('body')
                //.route('add', '/login/google')
                //.done($.vifib.login.google);
29
            // when Google send back the token, it reset hashtag from url
30 31 32 33 34 35 36 37 38
            //$('body')
                //.route('add', '/overview')
                //.done($.vifib.mobile.overview);
            //$('body')
                //.route('add', '/library<path:url>')
                //.done($.vifib.mobile.library.dispatch);
            //$('body')
                //.route('add', '/dashboard<path:url>')
                //.done($.vifib.mobile.dashboard.dispatch);
39 40 41
        },
        "tablet": function () {
            $('body')
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
                .route('add', $.vifib.pages.overview.url)
                .done($.vifib.pages.overview.action);
            //$('body')
                //.route('add', '/login/facebook')
                //.done($.vifib.login.facebook);
            //$('body')
                //.route('add', '/login/google')
                //.done($.vifib.login.google);
             //when Google send back the token, it reset hashtag from url
            //$('body')
                //.route('add', 'access_token=<path:path>')
                //.done($.vifib.login.googleRedirect);
            //$('body')
                //.route('add', '/overview')
                //.done($.vifib.tablet.overview);
            //$('body')
                //.route('add', '/library<path:url>')
                //.done($.vifib.tablet.library.dispatch);
            //$('body')
                //.route('add', '/dashboard<path:url>')
                //.done($.vifib.tablet.dashboard.dispatch);
63 64 65
        },
        "desktop": function () {
            $('body')
66 67 68 69 70
                .route('add', $.vifib.pages.overview.url)
                .done($.vifib.pages.overview.action);
            //$('body')
                //.route('add', '<path:url>')
                //.done($.vifib.desktop.dispatch);
71 72
        }
    };
73

74 75
    $.vifib.startrouter = function () {
        $('body')
76 77
            .route('go', $.url.getPath());
            //.fail($.vifib.mobile.nopage);
78
    };
79

80 81 82 83 84 85
    /* Thanks to Ben Alman
     * https://raw.github.com/cowboy/jquery-misc/master/jquery.ba-serializeobject.js
     */
    $.fn.serializeObject = function () {
        var obj = {};
        $.each(this.serializeArray(), function (i, o) {
86 87 88
            var n = o.name,
                v = o.value;
            obj[n] = obj[n] === undefined ? v
89 90
                : $.isArray(obj[n]) ? obj[n].concat(v)
                    : [obj[n], v];
91
        });
92 93
        return obj;
    };
94

95 96 97 98 99 100 101
    $.fn.spin = function (opts) {
        this.each(function () {
            var $this = $(this),
                data = $this.data();
            if (data.spinner) {
                data.spinner.stop();
                delete data.spinner;
102
            }
103 104 105 106 107 108
            if (opts !== false) {
                data.spinner = new Spinner($.extend({color: $this.css('color')}, opts)).spin(this);
            }
        });
        return this;
    };
109

110 111 112
    $(document).ready(function () {
        // bind on resize screen event
        $(window).resize(function () {
113 114 115 116
            if ($(window).data('resizing')) {
                clearTimeout($(window).data('resizing'));
            }
            $(window).data('resizing', setTimeout(function () {
117 118 119 120
                var curdevice = getDevice($(window).width());
                if ($.vifib.device !== curdevice) {
                    $.vifib.device = curdevice;
                    $.routereset();
121
                    $.vifib.initroutes();
122 123
                    $.vifib.startrouter();
                }
124
            }, 800));
125 126 127 128 129 130 131 132
        });

        // Url change event
        $.url.onhashchange(function () {
            var options = $.url.getOptions();
            if (options.hasOwnProperty('access_token')) {
                $(document).slapos('access_token', options.access_token);
            }
133
            $.vifib.initroutes();
134 135
            $.vifib.startrouter();
        });
136
    });
137
}(window, jQuery));