forked from defunkt/hurl
/
hurl.js
261 lines (223 loc) · 6.89 KB
/
hurl.js
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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
var Hurl = {
// apply label hints to inputs based on their
// title attribute
labelHints: function(el) {
$(el).each(function() {
var self = $(this), title = self.attr('title')
// indicate inputs using defaults
self.addClass('defaulted')
if (self.val() === '' || self.val() === title) {
self.val(title).css('color', '#E9EAEA')
} else {
self.addClass('focused')
}
self.focus(function() {
if (self.val() === title) {
self.val('').addClass('focused').css('color', '#333')
}
})
self.blur(function() {
if (self.val() === '') {
self.val(title).removeClass('focused').css('color', '#E9EAEA')
}
})
})
},
removeEmptyData: function(data) {
var keepers = [], value
// remove empty arrays and any default titular data
for (key in data) {
if (value = data[key].value) {
if ($('input[name=' + data[key].name +'].defaulted:not(.focused)').val() != value) {
keepers.push(data[key])
}
}
}
data.splice(0, data.length)
for (key in keepers)
data.push( keepers[key] )
return true
},
pony: function() {
if (!this.ponyLoaded) return this.loadPony()
if (this.ponying) return
this.ponying = true
var width = 668
var pony = $("<div />").css({
width: width,
height: 422,
background: 'url(/img/pony.png) top center',
position: 'fixed',
bottom: 0,
right: 0-width,
"z-index": 1000,
cursor: 'pointer'
}).appendTo($("body"))
pony.show().animate({right: 0}, 1500, function() {
setTimeout(function() {
pony.css('background', 'url(/img/pony-hurl.png) top center')
setTimeout(function() {
pony.animate({right: 0-width}, 1500, function() {
Hurl.ponying = false
})
}, 500)
}, 1000)
})
},
loadPony: function() {
$(new Image()).load(function() {
Hurl.loadOtherPony()
}).attr('src', '/img/pony.png');
},
loadOtherPony: function() {
$(new Image()).load(function() {
Hurl.ponyLoaded = true
Hurl.pony()
}).attr('src', '/img/pony-hurl.png');
}
}
$.fn.hurlAjaxSubmit = function(callback) {
return $(this).ajaxSubmit({
beforeSubmit: Hurl.removeEmptyData,
success: callback
})
}
$(document).ready(function() {
// select method
$('#select-method').change(function() {
$('#select-method option:selected').each(function() {
var method = $(this).attr('value')
if (method == 'POST' || method == 'PUT'){
$('#post-params').show()
} else {
$('#post-params').hide()
}
})
})
$('#select-method').change()
// add auth
$('input[name=auth]').change(function() {
if ($(this).attr('value') == 'basic') {
$('#basic-auth-fields').show()
$('#basic-auth-fields .form-alpha').focus()
} else {
$('#basic-auth-fields').hide()
}
})
$('#auth-selection :checked').change()
// add post param
$('#add-param').click(function() {
// toggle if post body is being shown
if ( $('#set-post-body .link-icon').text() == '-' ) {
$('#set-post-body').click()
return false
}
var newField = $('#param-fields').clone()
newField.toggle().attr('id', '')
newField.addClass('param-field')
newField.find('.form-alpha').attr('title', 'name')
newField.find('.form-beta').attr('title', 'value')
Hurl.labelHints( newField.find('input[title]') )
registerRemoveHandlers( newField, '.param-delete' )
$(this).parent().append( newField )
return false
})
// set post body
$('#set-post-body').click(function() {
var icon = $(this).find('.link-icon')
if ( icon.text() == '+' ) {
icon.text('-')
$('.param-field').hide()
$('#post-body').show().find('textarea').attr('disabled', false)
} else {
icon.text('+')
$('.param-field').show()
$('#post-body').hide().find('textarea').attr('disabled', true)
}
return false
})
if ( $('#post-body').is(':visible') ) {
$('#set-post-body').click()
}
// add header
$('#add-header').click(function() {
var newField = $('#header-fields').clone()
newField.toggle().attr('id', '')
Hurl.autocompleteHeaders( newField.find('.form-alpha') )
newField.find('.form-alpha').attr('title', 'name')
newField.find('.form-beta').attr('title', 'value')
Hurl.labelHints( newField.find('input[title]') )
registerRemoveHandlers( newField, '.header-delete' )
$(this).parent().append( newField )
return false
})
// remove header / param
function registerRemoveHandlers(el, klass) {
$(el).find(klass).click(function() {
$(this).parents('p:first').remove()
return false
})
}
registerRemoveHandlers( document, '.header-delete' )
registerRemoveHandlers( document, '.param-delete' )
// hurl it!
$('#hurl-form').submit(function() {
$('#send-wrap').children().toggle()
$('.flash-error, .flash-notice').fadeOut()
$('#request-and-response').hide()
$(this).hurlAjaxSubmit(function(res) {
var data = JSON.parse(res)
if (data.error) {
$('#flash-error-msg').html(data.error)
$('.flash-error').show()
} else if (/hurl/.test(location.pathname) && data.hurl_id && data.view_id) {
window.location = '/hurls/' + data.hurl_id + '/' + data.view_id
} else if (data.header && data.body && data.request) {
if ( /railsrumble/.test($('input[name=url]').val()) ) Hurl.pony()
if (data.prev_hurl) {
$('#page-prev').attr('href', '/hurls/' + data.prev_hurl).show()
$('#page-next').attr('href', '/').show()
}
$('.permalink').attr('href', '/hurls/'+data.hurl_id+'/'+data.view_id)
$('.full-size-link').attr('href', '/views/' + data.view_id)
$('#request').html(data.request)
$('#response').html('<pre>' + data.header + '</pre>' + data.body)
$('.help-blurb').hide()
$('#request-and-response').show()
} else {
$('#flash-error-msg').html("Weird response. Sorry.")
$('.flash-error').show()
}
$('#send-wrap').children().toggle()
})
return false
})
// delete hurl
$('.hurl-delete').click(function() {
$(this).parents('tr:first').remove()
$.ajax({type: 'DELETE', url: $(this).attr('href')})
return false
})
// toggle request/response display
$('.toggle-reqres-link').click(function(){
$('.toggle-reqres').toggle()
$('#code-request').toggle()
$('#code-response').toggle()
return false
})
// flash close
$('.flash-close').click(function (){
$(this).parent().fadeOut()
return false
})
// facebox
$('a[rel*=facebox]').facebox({ opacity: 0.4 })
$(document).bind('reveal.facebox', function() {
Hurl.labelHints('#facebox input[title]')
$('#facebox .footer').remove()
})
// in-field labels
Hurl.labelHints('input[title]')
// relatize dates
$('.relatize').relatizeDate()
})