Completed
Push — master ( 21ab2c...a38f7a )
by Yaro
01:32
created

app.js ➔ changeSourceView   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 15
rs 9.4285
1
2
$(document).ready(function(){
3
    $('#documenter_nav').tendina({activeMenu: '.current'});
4
    
5
    var hash = window.location.hash;
6
    var a = $('a[href="' + hash + '"]', '#scroll');
7
    if (a.length) {
8
        $('a[href="'+ hash +'"]', '#scroll')[0].click();
9
    } else {
10
        window.location.hash = $($("a[href^='#']")[0]).attr('href');
11
    }
12
    $('section.method').each(function() {
13
        var waypoint = new Waypoint({
0 ignored issues
show
Unused Code introduced by
The variable waypoint seems to be never used. Consider removing it.
Loading history...
Bug introduced by
The variable Waypoint seems to be never declared. If this is a global, consider adding a /** global: Waypoint */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
14
            element : this,
15
            handler : function() {
16
                var id = this.element.attributes['id'].nodeValue;
17
                //
18
                $('a', '#scroll').removeClass('current');
19
                $('ul', '#scroll').removeClass('open');
20
                var a = $('a[href="#' + id + '"]', '#scroll');
21
                a.addClass('current');
22
    
23
                var ul = a.closest('ul');
24
                if (ul.length) {
25
                    openUl(ul);
26
                }
27
                if (a.next().is('ul')) {
28
                    a.next().addClass('open');
29
                }
30
                window.history.replaceState(null, null, document.location.pathname + '#' + id);
31
            }
32
        })
33
    });
34
    $("a[href^='#']").on('click', function() {
35
        var a = this; 
36
        console.log($(a).attr('href').replace('.', '\\.').replace(':', '\\:'));
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
37
        $('html, body').animate({
38
            scrollTop: $($(a).attr('href').replace(new RegExp(/\./, 'g'), '\\.').replace(new RegExp(/\:/, 'g'), '\\:')).offset().top 
39
        });
40
        return false;
41
    });
42
});
43
44
function openUl(ul)
45
{
46
    ul.addClass('open');
47
    var ul = ul.parent().closest('ul');
48
    if (ul.length) {
49
        openUl(ul);
50
    }
51
}
52
53
function sendRequest(form) 
54
{
55
    $('#toggle-lang-response').trigger('click');
56
    
57
    var $form = $(form);
58
    var $section = $form.closest('section');
59
    
60
    var $btn = $form.find('button[type="submit"]');
61
    $btn.html($('#preloader-template').html()).attr('disabled', true);
62
    
63
    var headers = {};
64
    $section.find('.headers-form .form-group').not('.except').each(function(key, element) {
65
        var $el = $(element);
66
        if ($el.find('.req-header-active').is(':checked')) {
67
            var header = $el.find('.req-header').val();
68
            headers[header] = $el.find('.req-header-value').val();
69
        }
70
    });
71
    
72
    $.ajax({
73
        url : $section.find('.action-url').val(),
74
        headers: headers,
75
        type : $form.attr('method'),
76
        data : $form.serializeArray(),
77
        success : function(response, status, xhr) {
78
            $btn.text('Send').attr('disabled', false);
79
            $section.find('.method-example-endpoint code.response-content.response-highlighted').jsonViewer(response); 
80
            $section.find('.method-example-endpoint code.response-content.response-raw').html(typeof response == 'object' ? JSON.stringify(response): String(response));  
81
            $section.find('.method-example-endpoint code.response-headers').text(xhr.getAllResponseHeaders());
82
        },
83
        error : function(xhr) {
84
            $btn.text('Send').attr('disabled', false);
85
            var content = xhr.responseText;
86
            if (xhr.statusText && !content) {
87
                $.notify({
88
                    message: xhr.statusText
89
                },{
90
                    type: 'danger'
91
                });
92
            }
93
            if (IsJsonString(content)) {
94
                $section.find('.method-example-endpoint code.response-content').jsonViewer(content); 
95
                return;
96
            }
97
            var $frame = $('<iframe class="supa" style="width:100%; height:350px;">');
98
            $section.find('.method-example-endpoint code.response-content.response-highlighted').html($frame);
99
            $section.find('.method-example-endpoint code.response-content.response-raw').html(typeof content == 'object' ? JSON.stringify(content): String(content)); 
100
            setTimeout(function() {
101
                var doc = $frame[0].contentWindow.document;
102
                var $body = $('body', doc);
103
                $body.html(content);
104
            }, 1);
105
            
106
            $section.find('.method-example-endpoint code.response-headers').text(xhr.getAllResponseHeaders());
107
        }
108
    });
109
    
110
    return false;
111
}
112
113
function IsJsonString(str) {
114
    try {
115
        JSON.parse(str);
116
    } catch (e) {
117
        return false;
118
    }
119
    return true;
120
}
121
122
function changeApiUrl(input)
123
{
124
    var $input = $(input);
125
    var $form = $input.closest('form');
126
    var $urlInput = $form.find('.action-url');
127
    var original = $urlInput.data('original');
128
    
129
    $form.find('input').not('.action-url').each(function(key, input) {
130
        if (!input.value) {
131
            return;
132
        }
133
134
        var regexp = new RegExp('{'+ input.name +'}', "g");
135
        original = original.replace(regexp, input.value);
136
    });
137
    
138
    $urlInput.val(original);
139
}
140
141
function changeTab(ident)
142
{
143
    $('.method-tab').hide();
144
    $('.method-tab.'+ ident).show();
145
}
146
147
function changeSourceView(ctx)
148
{
149
    var $a = $(ctx);
150
    var $parent = $a.parent();
151
    $parent.find('pre.language-none').hide();
152
    if ($a.hasClass('show-source-block')) {
153
        $a.removeClass('show-source-block').addClass('show-highlighted-block');
154
        $parent.find('.response-highlighted').parent().show();
155
        $a.find('.fa-eye-slash').removeClass('fa-eye-slash').addClass('fa-eye');
156
    } else {
157
        $a.removeClass('show-highlighted-block').addClass('show-source-block');
158
        $parent.find('.response-raw').parent().show();
159
        $a.find('.fa-eye').removeClass('fa-eye').addClass('fa-eye-slash');
160
    }
161
}
162