if (typeof(PhpDebugBar) == 'undefined') {
    // namespace
    var PhpDebugBar = {};
    PhpDebugBar.$ = jQuery;
}
(function($) {
    var csscls = function(cls) {
        return PhpDebugBar.utils.csscls(cls, 'phpdebugbar-openhandler-');
    };
    PhpDebugBar.OpenHandler = PhpDebugBar.Widget.extend({
        className: 'phpdebugbar-openhandler',
        defaults: {
            items_per_page: 20
        },
        render: function() {
            var self = this;
            this.$el.appendTo('body').hide();
            this.$closebtn = $('');
            this.$table = $('
');
            $('PHP DebugBar | Open
').addClass(csscls('header')).append(this.$closebtn).appendTo(this.$el);
            $('| Date | Method | URL | IP | Filter data | 
|---|
').append(this.$table).appendTo(this.$el);
            this.$actions = $('').addClass(csscls('actions')).appendTo(this.$el);
            this.$closebtn.on('click', function() {
                self.hide();
            });
            this.$loadmorebtn = $('Load more')
                .appendTo(this.$actions)
                .on('click', function() {
                    self.find(self.last_find_request, self.last_find_request.offset + self.get('items_per_page'), self.handleFind.bind(self));
                });
            this.$showonlycurrentbtn = $('Show only current URL')
                .appendTo(this.$actions)
                .on('click', function() {
                    self.$table.empty();
                    self.find({uri: window.location.pathname}, 0, self.handleFind.bind(self));
                });
            this.$showallbtn = $('Show all')
                .appendTo(this.$actions)
                .on('click', function() {
                    self.refresh();
                });
            this.$clearbtn = $('Delete all')
                .appendTo(this.$actions)
                .on('click', function() {
                    self.clear(function() {
                        self.hide();
                    });
                });
            this.addSearch();
            this.$overlay = $('').addClass(csscls('overlay')).hide().appendTo('body');
            this.$overlay.on('click', function() {
                self.hide();
            });
        },
        refresh: function() {
            this.$table.empty();
            this.$loadmorebtn.show();
            this.find({}, 0, this.handleFind.bind(this));
        },
        addSearch: function(){
            var self = this;
            var searchBtn = $('')
                .text('Search')
                .attr('type', 'submit')
                .on('click', function(e) {
                    self.$table.empty();
                    var search = {};
                    var a = $(this).parent().serializeArray();
                    $.each(a, function() {
                        if(this.value){
                            search[this.name] = this.value;
                        }
                    });
                    self.find(search, 0, self.handleFind.bind(self));
                    e.preventDefault();
                });
            $('')
                .append('
Filter results
')
                .append('Method: 
')
                .append('Uri: 
')
                .append('IP: 
')
                .append(searchBtn)
                .appendTo(this.$actions);
        },
        handleFind: function(data) {
            var self = this;
            $.each(data, function(i, meta) {
               var a = $('')
                    .text('Load dataset')
                    .on('click', function(e) {
                       self.hide();
                       self.load(meta['id'], function(data) {
                           self.callback(meta['id'], data);
                       });
                       e.preventDefault();
                    });
                var method = $('')
                    .text(meta['method'])
                    .on('click', function(e) {
                        self.$table.empty();
                        self.find({method: meta['method']}, 0, self.handleFind.bind(self));
                        e.preventDefault();
                    });
                var uri = $('')
                    .text(meta['uri'])
                    .on('click', function(e) {
                        self.hide();
                        self.load(meta['id'], function(data) {
                            self.callback(meta['id'], data);
                        });
                        e.preventDefault();
                    });
                var ip = $('')
                    .text(meta['ip'])
                    .on('click', function(e) {
                        self.$table.empty();
                        self.find({ip: meta['ip']}, 0, self.handleFind.bind(self));
                        e.preventDefault();
                    });
                var search = $('')
                    .text('Show URL')
                    .on('click', function(e) {
                        self.$table.empty();
                        self.find({uri: meta['uri']}, 0, self.handleFind.bind(self));
                        e.preventDefault();
                    });
                $('|
')
                    .append('' + meta['datetime'] + '')
                    .append(' | ' + meta['method'] + '')
                    .append($(' | ').append(uri))
                    .append($(' | ').append(ip))
                    .append($(' | ').append(search))
                    .appendTo(self.$table);
            });
            if (data.length < this.get('items_per_page')) {
                this.$loadmorebtn.hide();
            }
        },
        show: function(callback) {
            this.callback = callback;
            this.$el.show();
            this.$overlay.show();
            this.refresh();
        },
        hide: function() {
            this.$el.hide();
            this.$overlay.hide();
        },
        find: function(filters, offset, callback) {
            var data = $.extend({}, filters, {max: this.get('items_per_page'), offset: offset || 0});
            this.last_find_request = data;
            this.ajax(data, callback);
        },
        load: function(id, callback) {
            this.ajax({op: "get", id: id}, callback);
        },
        clear: function(callback) {
            this.ajax({op: "clear"}, callback);
        },
        ajax: function(data, callback) {
            var url = this.get('url');
            if (data) {
                url = url + '?' + new URLSearchParams(data);
            }
            fetch(url, {
                method: "GET",
                headers: {
                    "Accept": "application/json",
                },
            })
                .then((data) => data.json())
                .then(callback);
        }
    });
})(PhpDebugBar.$); |