jquery.slapos.js 5.7 KB
Newer Older
1
(function ($) {
2 3 4 5 6
    'use strict';
    var methods = {
        init: function (options) {
            var settings = $.extend({
            }, options);
7

8 9 10 11 12 13 14 15 16 17
            return this.each(function () {
                var setting;
                methods.store = Modernizr.localstorage ? methods.lStore : methods.cStore;
                for (setting in settings) {
                    if (settings.hasOwnProperty(setting)) {
                        $(this).slapos('store', setting, settings[setting]);
                    }
                }
            });
        },
18

19 20 21 22 23 24 25 26 27 28
        /* Getters & Setters shortcuts */
        access_token: function (value) {
            return $(this).slapos('store', 'access_token', value);
        },
        host: function (value) {
            return $(this).slapos('store', 'host', value);
        },
        clientID: function (value) {
            return $(this).slapos('store', 'clientID', value);
        },
29

30 31 32 33 34 35 36
        /* Local storage method */
        lStore: function (name, value) {
            if (Modernizr.localstorage) {
                return value === undefined ? window.localStorage[name] : window.localStorage[name] = value;
            }
            return false;
        },
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
        /* 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);
                    }
                }
            }
        },
54

55 56 57 58 59 60 61
        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 !'); }
            };
        },
62

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        request: function (type, authentication, args) {
            var statusCode, data;
            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});
            return this.each(function () {
                var ajaxOptions = {
                    type: type,
                    contentType: 'application/json',
                    data: JSON.stringify(data),
                    datatype: 'json',
                    context: $(this),
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader('REMOTE_USER', 'test_vifib_customer');
                        xhr.setRequestHeader('Accept', 'application/json');
                        if ($(this).slapos('access_token') && authentication) {
                            //xhr.setRequestHeader('Authorization', $(this).slapos('store', 'token_type') + ' ' + $(this).slapos('access_token'));
                            //xhr.setRequestHeader('Accept', 'application/json');
                        }
                    }
                };
                $.extend(ajaxOptions, args);
                $.ajax(ajaxOptions);
            });
        },
97

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
        prepareRequest: function (methodName, args) {
            var $this = $(this);
            args = args || {};
            return this.each(function () {
                $(this).slapos('discovery', function (access) {
                    if (access.hasOwnProperty(methodName)) {
                        var url = args.url || access[methodName].url;
                        $.extend(args, {'url': url});
                        $this.slapos('request',
                            access[methodName].method,
                            access[methodName].authentication,
                            args);
                    }
                });
            });
        },
114

115 116 117 118 119 120 121 122 123 124 125 126
        discovery: function (callback) {
            return this.each(function () {
                $.ajax({
                    url: 'http://10.8.2.34:12006/erp5/portal_vifib_rest_api_v1',
                    dataType: 'json',
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader('Accept', 'application/json');
                    },
                    success: callback
                });
            });
        },
127

128 129 130
        instanceList: function (args) {
            return $(this).slapos('prepareRequest', 'instance_list', args);
        },
131

132 133 134 135 136
        instanceInfo: function (url, args) {
            url = decodeURIComponent(url);
            $.extend(args, {'url': url});
            return $(this).slapos('prepareRequest', 'instance_info', args);
        },
137

138 139 140
        instanceRequest: function (args) {
            return $(this).slapos('prepareRequest', 'request_instance', args);
        }
141

142
    };
143

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