2014-05-18 18:51:05 -04:00
|
|
|
#jQuery for node, reimplementated for compatibility purposes. Poorly.
|
|
|
|
#Leaves out all the dome stuff but allows ajax.
|
|
|
|
_ = require 'lodash'
|
|
|
|
request = require 'request'
|
2014-06-30 22:16:26 -04:00
|
|
|
Deferred = require 'JQDeferred'
|
2014-05-18 18:51:05 -04:00
|
|
|
module.exports = $ = (input) ->
|
|
|
|
console.log 'Ignored jQuery: ', input if $._debug
|
|
|
|
append: (input)-> exports: ()->
|
|
|
|
|
|
|
|
# Non-standard jQuery stuff. Don't use outside of server.
|
|
|
|
$._debug = false
|
2015-02-12 19:06:27 -05:00
|
|
|
$._server = 'http://direct.codecombat.com'
|
2014-05-18 18:51:05 -04:00
|
|
|
$._cookies = request.jar()
|
|
|
|
|
|
|
|
$.when = Deferred.when
|
|
|
|
$.ajax = (options) ->
|
|
|
|
responded = false
|
|
|
|
url = options.url
|
|
|
|
if url.indexOf('http')
|
|
|
|
url = '/' + url unless url[0] is '/'
|
|
|
|
url = $._server + url
|
|
|
|
|
|
|
|
data = options.data
|
2014-06-30 22:16:26 -04:00
|
|
|
console.log 'Requesting: ' + JSON.stringify options if $._debug
|
|
|
|
console.log 'URL: ' + url if $._debug
|
2014-05-18 18:51:05 -04:00
|
|
|
deferred = Deferred()
|
|
|
|
request
|
|
|
|
url: url
|
|
|
|
jar: $._cookies
|
|
|
|
json: options.parse
|
|
|
|
method: options.type
|
|
|
|
body: data
|
|
|
|
, (error, response, body) ->
|
2014-06-30 22:16:26 -04:00
|
|
|
console.log 'HTTP Request:' + JSON.stringify options if $._debug and not error
|
2014-05-18 18:51:05 -04:00
|
|
|
if responded
|
2014-06-30 22:16:26 -04:00
|
|
|
console.log '\t↳Already returned before.' if $._debug
|
2014-05-18 18:51:05 -04:00
|
|
|
return
|
|
|
|
if (error)
|
|
|
|
console.warn "\t↳Returned: error: #{error}"
|
|
|
|
deferred.reject(error)
|
|
|
|
else
|
|
|
|
console.log "\t↳Returned: statusCode #{response.statusCode}: #{if options.parse then JSON.stringify body else body}" if $._debug
|
|
|
|
deferred.resolve(body, response, status: response.statusCode)
|
|
|
|
|
|
|
|
statusCode = response.statusCode if response?
|
|
|
|
options.complete(status: statusCode) if options.complete?
|
|
|
|
responded = true
|
|
|
|
deferred.promise().done(options.success).fail(options.error)
|
|
|
|
|
|
|
|
$.extend = (deep, into, from) ->
|
|
|
|
copy = _.clone(from, deep);
|
|
|
|
if into
|
|
|
|
_.assign into, copy
|
|
|
|
copy = into
|
|
|
|
copy
|
|
|
|
|
|
|
|
$.isArray = (object) ->
|
|
|
|
_.isArray object
|
|
|
|
|
|
|
|
$.isPlainObject = (object) ->
|
|
|
|
_.isPlainObject object
|