chat.coffee 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. define [
  2. "utils/staticLoader"
  3. "libs/underscore"
  4. "libs/backbone"
  5. "libs/jquery.storage"
  6. "models/room"
  7. "models/user"
  8. "views/chatWindowView"
  9. ], (staticLoader, _, Backbone, jqueryStorage, Room, User, ChatWindowView) ->
  10. staticLoader.appendAssets()
  11. _.templateSettings = escape : /\{\{(.+?)\}\}/g
  12. class GlobalNotificationManager
  13. constructor: (@chat) ->
  14. @focussed = true
  15. $(window).on "focus", () =>
  16. @clearNewMessageNotification()
  17. @focussed = true
  18. $(window).on "blur", () => @focussed = false
  19. @chat.on "joinedRoom", (room) =>
  20. notifyIfAppropriate = (message) =>
  21. if message.get("user") != @chat.user and !message.get("preloaded")
  22. @notifyAboutNewMessage()
  23. room.get("messages").on "add", notifyIfAppropriate
  24. room.on "disconnect", () ->
  25. room.get("messages").off "add", notifyIfAppropriate
  26. notifyAboutNewMessage: () ->
  27. if !@focussed and !@newMessageNotificationTimeout?
  28. @originalTitle ||= window.document.title
  29. do changeTitle = () =>
  30. if window.document.title == @originalTitle
  31. window.document.title = "New Message"
  32. else
  33. window.document.title = @originalTitle
  34. @newMessageNotificationTimeout = setTimeout changeTitle, 800
  35. clearNewMessageNotification: () ->
  36. clearTimeout @newMessageNotificationTimeout
  37. delete @newMessageNotificationTimeout
  38. if @originalTitle?
  39. window.document.title = @originalTitle
  40. class Chat
  41. constructor: (options) ->
  42. _.extend(@, Backbone.Events)
  43. window.chat = @
  44. @rooms = {}
  45. project_id = window.location.pathname.split( '/' )[2]
  46. @socket = socket = io.connect options.url, {
  47. resource: "chat/socket.io",
  48. "force new connection": true
  49. query:"project_id=#{project_id}"
  50. }
  51. @socket.on "connect", () =>
  52. @connected = true
  53. @getAuthToken (error, auth_token) =>
  54. return @handleError(error) if error?
  55. @socket.emit "auth", {auth_token: auth_token}, (error, user_info) =>
  56. return @handleError(error) if error?
  57. @user = User.findOrCreate(user_info)
  58. @joinProjectRoom(options.room.project_id)
  59. @trigger "authed"
  60. @socket.on "disconnect", () =>
  61. @connected = false
  62. @trigger "disconnected"
  63. @socket.on "messageReceived", (data) =>
  64. @getRoom(data.message.room.id)?.onMessageReceived(data)
  65. @socket.on "userJoined", (data) =>
  66. @getRoom(data.room.id).addConnectedUser(data.user)
  67. @socket.on "userLeft", (data) =>
  68. @getRoom(data.room.id)?.removeConnectedUser(data.user)
  69. @globalNotificationManager = new GlobalNotificationManager(@)
  70. getRoom: (room_id) ->
  71. @rooms[room_id]
  72. joinProjectRoom: (project_id) ->
  73. if !@room?
  74. @room = new Room(
  75. project_id: project_id
  76. chat: @
  77. )
  78. @window = new ChatWindowView({
  79. room: @room
  80. chat: @
  81. })
  82. @room.on "joined", => @trigger("joinedRoom", @room)
  83. getAuthToken: (callback = (error, auth_token) ->) ->
  84. $.ajax "/user/auth_token", {
  85. success: (data, status, xhr) ->
  86. callback null, data
  87. error: (xhr, status, error) ->
  88. callback error
  89. }
  90. handleError: (error) ->
  91. console.error error