Completed
Push — master ( 840eee...2073f4 )
by Yaro
01:35
created

app.js ➔ removeNewHeaderInput   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
2
$(window).on('resize', function() {
3
    $('.global-headers-section').width($('.method-copy').width() - 30);
4
});
5
6
$(document).ready(function(){
7
    $('#documenter_nav').tendina({activeMenu: '.current'});
8
    
9
    var hash = window.location.hash;
10
    var a = $('a[href="' + hash + '"]', '#scroll');
11
    if (a.length) {
12
        $('a[href="'+ hash +'"]', '#scroll')[0].click();
13
    } else {
14
        window.location.hash = $($("a[href^='#']")[0]).attr('href');
15
    }
16
    $('section.method').each(function() {
17
        /** global: Waypoint */
18
        new Waypoint({
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new Waypoint({Identifier...alse,false,None,None)}) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
19
            element : this,
20
            handler : function() {
21
                var id = this.element.attributes['id'].nodeValue;
22
                //
23
                $('a', '#scroll').removeClass('current');
24
                $('ul', '#scroll').removeClass('open');
25
                var a = $('a[href="#' + id + '"]', '#scroll');
26
                a.addClass('current');
27
    
28
                var ul = a.closest('ul');
29
                if (ul.length) {
30
                    openUl(ul);
31
                }
32
                if (a.next().is('ul')) {
33
                    a.next().addClass('open');
34
                }
35
                window.history.replaceState(null, null, document.location.pathname + '#' + id);
36
            }
37
        })
38
    });
39
    $("a[href^='#']").on('click', function() {
40
        var a = this; 
41
        $('html, body').animate({
42
            scrollTop: $($(a).attr('href').replace(new RegExp(/\./, 'g'), '\\.').replace(new RegExp(/\:/, 'g'), '\\:')).offset().top 
43
        });
44
        return false;
45
    });
46
    
47
    autocompleteHeaders();
48
49
    setHeadersCheckboxEvents();
50
}); 
51
52
function setHeadersCheckboxEvents()
53
{
54
	$('input[type=checkbox]', '.global-headers-form').change(function() {
55
	    recalculateGlobalHeaders();
56
	});
57
}
58
59
function autocompleteHeaders()
60
{
61
	$('.req-header').easyAutocomplete({
62
		data: [
63
			'Accept',
64
			'Accept-Charset',
65
			'Accept-Encoding',
66
			'Accept-Language',
67
			'Accept-Datetime',
68
			'Access-Control-Request-Method',
69
			'Access-Control-Request-Headers',
70
			'Authorization',
71
			'Cache-Control',
72
			'Connection',
73
			'Cookie',
74
			'Content-Length',
75
			'Content-MD5',
76
			'Content-Type',
77
			'Content-Disposition',
78
			'Date',
79
			'Expect',
80
			'Forwarded',
81
			'From',
82
			'Host',
83
			'If-Match',
84
			'If-Modified-Since',
85
			'If-None-Match',
86
			'If-Range',
87
			'If-Unmodified-Since',
88
			'Max-Forwards',
89
			'Origin',
90
			'Pragma',
91
			'Proxy-Authorization',
92
			'Range',
93
			'Referer',
94
			'TE',
95
			'User-Agent',
96
			'Upgrade',
97
			'Via',
98
			'Warning',
99
			'X-Requested-With',
100
			'DNT',
101
			'X-Forwarded-For',
102
			'X-Forwarded-Host',
103
			'X-Forwarded-Proto',
104
			'Front-End-Https',
105
			'X-Http-Method-Override',
106
			'X-ATT-DeviceId',
107
			'X-Wap-Profile',
108
			'Proxy-Connection',
109
			'X-UIDH',
110
			'X-Csrf-Token',
111
			'X-Request-ID',
112
			'X-Correlation-ID',
113
		],
114
		list: {
115
	        match: {
116
	            enabled: true
117
	        }
118
	    },
119
	});
120
}
121
122
function openUl(ul)
123
{
124
    ul.addClass('open');
125
    var ul = ul.parent().closest('ul');
126
    if (ul.length) {
127
        openUl(ul);
128
    }
129
}
130
131
function sendRequest(form) 
132
{
133
    $('#toggle-lang-response').trigger('click');
134
    
135
    var $form = $(form);
136
    var $section = $form.closest('section');
137
    
138
    var $btn = $form.find('button[type="submit"]');
139
    $btn.html($('#preloader-template').html()).attr('disabled', true);
140
    
141
    var headers = {};
142
    $section.find('.headers-form .form-group').not('.except').each(function(key, element) {
143
        var $el = $(element);
144
        if ($el.find('.req-header-active').is(':checked')) {
145
            var header = $el.find('.req-header').val();
146
            headers[header] = $el.find('.req-header-value').val();
147
        }
148
    });
149
    $('.global-headers-form .form-group').not('.except').each(function(key, element) {
150
        var $el = $(element);
151
        if ($el.find('.req-header-active').is(':checked')) {
152
            var header = $el.find('.req-header').val();
153
            headers[header] = $el.find('.req-header-value').val();
154
        }
155
    });
156
    
157
    $.ajax({
158
        url : $section.find('.action-url').val(),
159
        headers: headers,
160
        type : $form.attr('method'),
161
        data : $form.serializeArray(),
162
        success : function(response, status, xhr) {
163
            $btn.text('Send').attr('disabled', false);
164
            $section.find('.method-example-endpoint code.response-content.response-highlighted').jsonViewer(response); 
165
            $section.find('.method-example-endpoint code.response-content.response-raw').text(typeof response == 'object' ? JSON.stringify(response): String(response));  
166
            $section.find('.method-example-endpoint code.response-headers').text(xhr.getAllResponseHeaders());
167
        },
168
        error : function(xhr) {
169
            $btn.text('Send').attr('disabled', false);
170
            var content = xhr.responseText;
171
            if (xhr.statusText && !content) {
172
                $.notify({
173
                    message: xhr.statusText
174
                },{
175
                    type: 'danger'
176
                });
177
            }
178
            if (IsJsonString(content)) {
179
                $section.find('.method-example-endpoint code.response-content').jsonViewer(content); 
180
                return;
181
            }
182
            var $frame = $('<iframe class="supa" style="width:100%; height:350px;">');
183
            $section.find('.method-example-endpoint code.response-content.response-highlighted').html($frame);
184
            $section.find('.method-example-endpoint code.response-content.response-raw').text(typeof content == 'object' ? JSON.stringify(content): String(content)); 
185
            setTimeout(function() {
186
                var doc = $frame[0].contentWindow.document;
187
                var $body = $('body', doc);
188
                $body.html(content);
189
            }, 1);
190
            
191
            $section.find('.method-example-endpoint code.response-headers').text(xhr.getAllResponseHeaders());
192
        }
193
    });
194
    
195
    return false;
196
}
197
198
function IsJsonString(str) {
199
    try {
200
        JSON.parse(str);
201
    } catch (e) {
202
        return false;
203
    }
204
    return true;
205
}
206
207
function changeApiUrl(input)
208
{
209
    var $input = $(input);
210
    var $form = $input.closest('form');
211
    var $urlInput = $form.find('.action-url');
212
    var original = $urlInput.data('original');
213
    
214
    $form.find('input').not('.action-url').each(function(key, input) {
215
        if (!input.value) {
216
            return;
217
        }
218
219
        var regexp = new RegExp('{'+ input.name +'}', "g");
220
        original = original.replace(regexp, input.value);
221
    });
222
    
223
    $urlInput.val(original);
224
}
225
226
function changeTab(ident)
227
{
228
    $('.method-tab').hide();
229
    $('.method-tab.'+ ident).show();
230
}
231
232
function changeSourceView(ctx)
233
{
234
    var $a = $(ctx);
235
    var $parent = $a.parent();
236
    $parent.find('pre.language-none').hide();
237
    if ($a.hasClass('show-source-block')) {
238
        $a.removeClass('show-source-block').addClass('show-highlighted-block');
239
        $parent.find('.response-highlighted').parent().show();
240
        $a.find('.fa-eye-slash').removeClass('fa-eye-slash').addClass('fa-eye');
241
    } else {
242
        $a.removeClass('show-highlighted-block').addClass('show-source-block');
243
        $parent.find('.response-raw').parent().show();
244
        $a.find('.fa-eye').removeClass('fa-eye').addClass('fa-eye-slash');
245
    }
246
}
247
248
function addNewHeaderInput(ctx)
249
{
250
	$(ctx).closest('.form-group').before($('#header-row-template').html());
251
	setHeadersCheckboxEvents();
252
	autocompleteHeaders();
253
	recalculateGlobalHeaders();
254
}
255
256
function removeNewHeaderInput(ctx)
257
{
258
	$(ctx).closest('.form-group').remove();
259
	recalculateGlobalHeaders();
260
}
261
262
function recalculateGlobalHeaders()
263
{
264
	var count = 0;
265
	$('.global-headers-form .form-group').not('.except').each(function(key, element) {
266
        var $el = $(element);
267
        if ($el.find('.req-header-active').is(':checked')) {
268
            count++;
269
        }
270
    });
271
    
272
	$('.global-headers-count').text(count);
273
}
274
275
function showGlobalHeaders(ctx)
276
{
277
    var $li = $(ctx).parent();
278
    if ($li.hasClass('dx-nav-active')) {
279
        $li.removeClass('dx-nav-active');
280
        $('.global-headers-section').slideUp();
281
    } else {
282
        $li.addClass('dx-nav-active');
283
        $(window).trigger('resize')
284
        $('.global-headers-section').slideDown();
285
    }
286
}
287
288