2014-05-20 18:31:49 -04:00
|
|
|
Backbone.Mediator.setValidationEnabled false
|
2014-01-03 13:32:13 -05:00
|
|
|
app = require 'application'
|
|
|
|
|
2014-04-11 08:37:08 -04:00
|
|
|
channelSchemas =
|
2014-08-27 15:24:03 -04:00
|
|
|
'auth': require './schemas/subscriptions/auth'
|
2014-04-11 14:09:50 -04:00
|
|
|
'bus': require './schemas/subscriptions/bus'
|
|
|
|
'editor': require './schemas/subscriptions/editor'
|
|
|
|
'errors': require './schemas/subscriptions/errors'
|
2014-11-12 13:23:43 -05:00
|
|
|
'ipad': require './schemas/subscriptions/ipad'
|
2014-04-11 14:09:50 -04:00
|
|
|
'misc': require './schemas/subscriptions/misc'
|
2014-08-29 18:10:04 -04:00
|
|
|
'multiplayer': require './schemas/subscriptions/multiplayer'
|
2014-04-11 14:09:50 -04:00
|
|
|
'play': require './schemas/subscriptions/play'
|
|
|
|
'surface': require './schemas/subscriptions/surface'
|
|
|
|
'tome': require './schemas/subscriptions/tome'
|
2014-08-27 15:24:03 -04:00
|
|
|
'god': require './schemas/subscriptions/god'
|
|
|
|
'scripts': require './schemas/subscriptions/scripts'
|
2014-08-28 12:27:42 -04:00
|
|
|
'world': require './schemas/subscriptions/world'
|
2014-04-11 08:37:08 -04:00
|
|
|
|
|
|
|
definitionSchemas =
|
|
|
|
'bus': require './schemas/definitions/bus'
|
|
|
|
'misc': require './schemas/definitions/misc'
|
|
|
|
|
2014-02-24 14:12:52 -05:00
|
|
|
init = ->
|
2014-09-24 16:11:55 -04:00
|
|
|
setupConsoleLogging()
|
2014-07-13 11:37:39 -04:00
|
|
|
watchForErrors()
|
2014-09-06 22:50:31 -04:00
|
|
|
setUpIOSLogging()
|
2014-06-16 13:42:13 -04:00
|
|
|
path = document.location.pathname
|
2014-10-15 16:43:26 -04:00
|
|
|
app.testing = path.startsWith '/test'
|
|
|
|
app.demoing = path.startsWith '/demo'
|
|
|
|
initializeUtilityServices() unless app.testing or app.demoing
|
2014-09-06 22:50:31 -04:00
|
|
|
setUpBackboneMediator()
|
2014-01-03 13:32:13 -05:00
|
|
|
app.initialize()
|
|
|
|
Backbone.history.start({ pushState: true })
|
|
|
|
handleNormalUrls()
|
2014-07-30 17:19:21 -04:00
|
|
|
setUpMoment() # Set up i18n for moment
|
2014-01-03 13:32:13 -05:00
|
|
|
treemaExt = require 'treema-ext'
|
|
|
|
treemaExt.setup()
|
|
|
|
|
|
|
|
handleNormalUrls = ->
|
|
|
|
# http://artsy.github.com/blog/2012/06/25/replacing-hashbang-routes-with-pushstate/
|
2014-06-30 22:16:26 -04:00
|
|
|
$(document).on 'click', "a[href^='/']", (event) ->
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
href = $(event.currentTarget).attr('href')
|
|
|
|
|
|
|
|
# chain 'or's for other black list routes
|
|
|
|
passThrough = href.indexOf('sign_out') >= 0
|
|
|
|
|
|
|
|
# Allow shift+click for new tabs, etc.
|
|
|
|
if !passThrough && !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey
|
|
|
|
event.preventDefault()
|
|
|
|
|
|
|
|
# Remove leading slashes and hash bangs (backward compatablility)
|
|
|
|
url = href.replace(/^\//,'').replace('\#\!\/','')
|
|
|
|
|
|
|
|
# Instruct Backbone to trigger routing events
|
|
|
|
app.router.navigate url, { trigger: true }
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
2014-09-06 22:50:31 -04:00
|
|
|
setUpBackboneMediator = ->
|
|
|
|
Backbone.Mediator.addDefSchemas schemas for definition, schemas of definitionSchemas
|
|
|
|
Backbone.Mediator.addChannelSchemas schemas for channel, schemas of channelSchemas
|
|
|
|
Backbone.Mediator.setValidationEnabled document.location.href.search(/codecombat.com/) is -1
|
|
|
|
if webkit?.messageHandlers
|
2014-10-01 13:58:19 -04:00
|
|
|
window.iPadSubscriptions = 'application:error': true # We try to subscribe to this one before it's all set up, so just do it.
|
2014-09-06 22:50:31 -04:00
|
|
|
originalPublish = Backbone.Mediator.publish
|
|
|
|
Backbone.Mediator.publish = ->
|
|
|
|
originalPublish.apply Backbone.Mediator, arguments
|
2014-10-01 13:58:19 -04:00
|
|
|
if window.iPadSubscriptions[arguments[0]]
|
|
|
|
webkit.messageHandlers.backboneEventHandler?.postMessage channel: arguments[0], event: serializeForIOS(arguments[1] ? {})
|
2014-06-15 17:19:37 -04:00
|
|
|
|
2014-07-30 17:19:21 -04:00
|
|
|
setUpMoment = ->
|
|
|
|
{me} = require 'lib/auth'
|
2014-08-23 18:51:59 -04:00
|
|
|
moment.lang me.get('preferredLanguage', true), {}
|
|
|
|
me.on 'change:preferredLanguage', (me) ->
|
|
|
|
moment.lang me.get('preferredLanguage', true), {}
|
2014-07-30 17:19:21 -04:00
|
|
|
|
2014-10-15 16:43:26 -04:00
|
|
|
initializeUtilityServices = ->
|
2014-06-16 13:42:13 -04:00
|
|
|
services = [
|
2014-11-09 20:35:50 -05:00
|
|
|
#'./lib/services/filepicker' # Not until needed
|
2014-06-16 13:42:13 -04:00
|
|
|
'./lib/services/segmentio'
|
|
|
|
]
|
|
|
|
|
|
|
|
for service in services
|
|
|
|
service = require service
|
|
|
|
service()
|
2014-07-13 11:37:39 -04:00
|
|
|
|
2014-09-24 16:11:55 -04:00
|
|
|
setupConsoleLogging = ->
|
|
|
|
unless console.debug
|
|
|
|
# Needed for IE10 and earlier
|
|
|
|
console.debug = console.log
|
|
|
|
|
2014-07-13 11:37:39 -04:00
|
|
|
watchForErrors = ->
|
|
|
|
currentErrors = 0
|
|
|
|
window.onerror = (msg, url, line, col, error) ->
|
|
|
|
return if currentErrors >= 3
|
|
|
|
return unless me.isAdmin() or document.location.href.search(/codecombat.com/) is -1 or document.location.href.search(/\/editor\//) isnt -1
|
|
|
|
++currentErrors
|
2014-09-06 22:50:31 -04:00
|
|
|
message = "Error: #{msg}<br>Check the JS console for more."
|
2014-07-13 11:37:39 -04:00
|
|
|
#msg += "\nLine: #{line}" if line?
|
|
|
|
#msg += "\nColumn: #{col}" if col?
|
|
|
|
#msg += "\nError: #{error}" if error?
|
|
|
|
#msg += "\nStack: #{stack}" if stack = error?.stack
|
2014-10-02 01:02:52 -04:00
|
|
|
unless webkit?.messageHandlers # Don't show these notys on iPad
|
|
|
|
noty text: message, layout: 'topCenter', type: 'error', killer: false, timeout: 5000, dismissQueue: true, maxVisible: 3, callback: {onClose: -> --currentErrors}
|
2014-11-09 11:48:11 -05:00
|
|
|
Backbone.Mediator.publish 'application:error', message: "Line #{line} of #{url}:\n#{msg}" # For iOS app
|
2014-09-06 22:50:31 -04:00
|
|
|
|
2014-10-01 13:58:19 -04:00
|
|
|
window.addIPadSubscription = (channel) ->
|
|
|
|
window.iPadSubscriptions[channel] = true
|
|
|
|
|
|
|
|
window.removeIPadSubscription = (channel) ->
|
|
|
|
window.iPadSubscriptions[channel] = false
|
|
|
|
|
2014-09-06 22:50:31 -04:00
|
|
|
setUpIOSLogging = ->
|
|
|
|
return unless webkit?.messageHandlers
|
|
|
|
for level in ['debug', 'log', 'info', 'warn', 'error']
|
|
|
|
do (level) ->
|
|
|
|
originalLog = console[level]
|
|
|
|
console[level] = ->
|
|
|
|
originalLog.apply console, arguments
|
|
|
|
try
|
|
|
|
webkit?.messageHandlers?.consoleLogHandler?.postMessage level: level, arguments: (a?.toString?() ? ('' + a) for a in arguments)
|
|
|
|
catch e
|
|
|
|
webkit?.messageHandlers?.consoleLogHandler?.postMessage level: level, arguments: ['could not post log: ' + e]
|
|
|
|
|
|
|
|
# This is so hacky... hopefully it's restrictive enough to not be slow.
|
|
|
|
# We could also keep a list of events we are actually subscribed for and only try to send those over.
|
|
|
|
seen = null
|
|
|
|
window.serializeForIOS = serializeForIOS = (obj, depth=3) ->
|
|
|
|
return {} unless depth
|
|
|
|
root = not seen?
|
|
|
|
seen ?= []
|
|
|
|
clone = {}
|
|
|
|
keysHandled = 0
|
|
|
|
for own key, value of obj
|
2014-10-30 14:12:58 -04:00
|
|
|
continue if ++keysHandled > 50
|
2014-09-06 22:50:31 -04:00
|
|
|
if not value
|
|
|
|
clone[key] = value
|
|
|
|
else if value is window or value.firstElementChild or value.preventDefault
|
|
|
|
null # Don't include these things
|
|
|
|
else if value in seen
|
|
|
|
null # No circular references
|
|
|
|
else if _.isArray value
|
|
|
|
clone[key] = (serializeForIOS(child, depth - 1) for child in value)
|
|
|
|
seen.push value
|
|
|
|
else if _.isObject value
|
|
|
|
value = value.attributes if value.id and value.attributes
|
|
|
|
clone[key] = serializeForIOS value, depth - 1
|
|
|
|
seen.push value
|
|
|
|
else
|
|
|
|
clone[key] = value
|
|
|
|
seen = null if root
|
|
|
|
clone
|
2014-07-23 10:02:45 -04:00
|
|
|
|
2014-07-30 17:19:21 -04:00
|
|
|
$ -> init()
|