jquery.slapos.js 5.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
(function ($) {
    'use strict';
    var methods = {
        init: function (options) {
            return this.each(function () {
                var settings = $.extend({
                    storage: 'localstorage'
                }, options),
                    setting;
                methods.store = settings.storage === 'localstorage' ? methods.lStore : methods.cStore;
                for (setting in settings) {
                    if (settings.hasOwnProperty(setting)) {
                        $(this).slapos('store', setting, settings[setting]);
                    }
                }
            });
        },

        /* Getters & Setters shortcuts */
        access_token: function (value) {
            return $(this).slapos('store', 'access_token', value);
        },
        host: function (value) {
            return $(this).slapos('store', 'host', value);
        },
        appid: function (value) {
            return $(this).slapos('store', 'appid', value);
        },

        deleteStore: function (name) {
            delete window.localStorage[name];
        },

        /* Local storage method */
        lStore: function (name, value) {
            if (Modernizr.localstorage) {
                return value === undefined ? window.localStorage[name] : window.localStorage[name] = value;
            }
            return false;
        },

        /* Cookie storage method */
        cStore: function (name, value) {
            if (value !== undefined) {
                document.cookie = name + '=' + value + ';domain=' + window.location.hostname + ';path=' + window.location.pathname;
            } else {
                var i, x, y, cookies = document.cookie.split(';');
                for (i = 0; i < cookies.length; i += 1) {
                    x = cookies[i].substr(0, cookies[i].indexOf('='));
                    y = cookies[i].substr(cookies[i].indexOf('=') + 1);
                    x = x.replace(/^\s+|\s+$/g, '');
                    if (x === name) {
                        return unescape(y);
                    }
                }
            }
        },

        statusDefault: function () {
            return {
                0: function () { console.error('status error code: 0'); },
                404: function () { console.error('status error code: Not Found !'); },
                500: function () { console.error('Server error !'); }
            };
        },

        request: function (type, method, args) {
68
            var statusCode, data, ajaxOptions;
69 70 71 72 73 74 75 76 77 78 79 80
            if (args.hasOwnProperty('statusCode')) {
                statusCode = args.statusCode || methods.statusDefault();
            } else {
                statusCode = methods.statusDefault();
            }
            if (args.hasOwnProperty('data')) {
                data = args.data || undefined;
            } else {
                data = undefined;
            }
            delete args.data;
            $.extend(args, {statusCode: statusCode});
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
            ajaxOptions = {
                type: type,
                url: $(this).slapos('host') + method,
                contentType: 'application/json',
                data: JSON.stringify(data),
                datatype: 'json',
                context: $(this),
                headers: {
                    'Accept': 'application/json',
                },
                beforeSend: function (xhr) {
                    if ($(this).slapos('access_token')) {
                        xhr.setRequestHeader('Authorization', $(this).slapos('store', 'token_type') + ' ' + $(this).slapos('access_token'));
                    }
                }
            };
            $.extend(ajaxOptions, args);
            return $.ajax(ajaxOptions);
99 100 101 102 103 104 105 106 107 108 109 110 111 112
        },

        instanceList: function (args) {
            return $(this).slapos('request', 'GET', '/instance', args);
        },

        instanceInfo: function (url, args) {
            $.extend(args, {url: url});
            return $(this).slapos('request', 'GET', '', args);
        },

        instanceRequest: function (args) {
            return $(this).slapos('request', 'POST', '/instance', args);
        },
113

114 115 116 117 118 119 120 121 122 123
        instanceBang: function (url, args) {
            $.extend(args, {url: url + '/bang'});
            return $(this).slapos('request', 'POST', '', args);
        },

        instanceCertificate: function (url, args) {
            $.extend(args, {url: url + '/certificate'});
            return $(this).slapos('request', 'GET', '', args);
        },

124 125 126
        softwareList: function (args) {
            return $(this).slapos('request', 'GET', '/software', args);
        },
127

128 129 130 131
        softwareInfo: function (url, args) {
            $.extend(args, {url: url});
            return $(this).slapos('request', 'GET', '', args);
        },
132

133
        computerList: function (args) {
134
            return $(this).slapos('request', 'GET', '/computer', args);
135
        },
136 137 138 139 140 141 142 143

        computerInfo: function (url, args) {
            $.extend(args, {url: url});
            return $(this).slapos('request', 'GET', '', args);
        }
    };

    $.fn.slapos = function (method) {
144
        var r;
145
        if (methods[method]) {
146
            r = methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
147
        } else if (typeof method === 'object' || !method) {
148
            r = methods.init.apply(this, arguments);
149 150 151
        } else {
            $.error('Method ' +  method + ' does not exist on jQuery.slapos');
        }
152
        return r;
153 154
    };
}(jQuery));