PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/client/web/app/scripts/models/room.js

https://gitlab.com/ChristopherFunk/Whiteboard
JavaScript | 128 lines | 116 code | 12 blank | 0 comment | 14 complexity | bf6a8b116f806e7cf3079df3d55d1f25 MD5 | raw file
  1. define([
  2. 'backbone',
  3. './DrawModel',
  4. './User',
  5. 'collections/chatMessages'
  6. ], function (
  7. Backbone,
  8. DrawModel,
  9. User,
  10. ChatMessagesList
  11. ) {
  12. "use strict";
  13. var DrawModelsList = Backbone.Collection.extend({
  14. model: DrawModel
  15. });
  16. var UsersList = Backbone.Collection.extend({
  17. model: User
  18. });
  19. var MeetingRoomModel = Backbone.Model.extend({
  20. defaults: {
  21. name: 'Untitled Room',
  22. },
  23. idAttribute: "_id",
  24. url: "/api/room",
  25. initialize: function() {
  26. this._messages = new ChatMessagesList();
  27. this._drawModels = new DrawModelsList();
  28. this._users = new UsersList();
  29. this.set("userInvites", []);
  30. },
  31. getListOfUsersActiveInRoom: function(){
  32. return this._users;
  33. },
  34. setName: function(newName) {
  35. if(!newName || newName.length === 0) {
  36. throw "Invalid Room Name: Name cannot be blank/empty";
  37. }
  38. if (!this.isNew()) {
  39. return this.save({
  40. name: newName
  41. },{
  42. wait: true
  43. });
  44. }else{
  45. this.set("name", newName);
  46. }
  47. },
  48. getName: function() {
  49. return this.get("name");
  50. },
  51. setType: function(newType) {
  52. if(!newType || (newType !== "public" && newType !== "private")) {
  53. throw "Invalid Room Type";
  54. }
  55. if (!this.isNew()) {
  56. return this.save({
  57. type: newType
  58. },{
  59. wait: true
  60. });
  61. }else{
  62. this.set("type", newType);
  63. }
  64. },
  65. getType: function() {
  66. return this.get("type");
  67. },
  68. setAllowAnon: function(newAllowAnon) {
  69. if(typeof newAllowAnon !== "boolean") {
  70. throw "Invalid Allow Anonymous: Must be boolean";
  71. }
  72. if (!this.isNew()) {
  73. return this.save({
  74. type: newAllowAnon
  75. },{
  76. wait: true
  77. });
  78. }else{
  79. this.set("allowAnon", newAllowAnon);
  80. }
  81. },
  82. getAllowAnon: function() {
  83. return this.get("allowAnon");
  84. },
  85. addUserInvite: function(userId) {
  86. var invites = this.getUserInvites();
  87. var idObj = {id: userId};
  88. for(var i = 0; i < invites.length; i++){
  89. var invite = invites[i];
  90. if(invite.id === userId){
  91. throw "User is alreayd invited";
  92. }
  93. }
  94. if (!this.isNew()) {
  95. }else{
  96. invites.push(idObj);
  97. }
  98. },
  99. getUserInvites: function() {
  100. return this.get("userInvites");
  101. },
  102. removeAllInvites: function() {
  103. this.set("userInvites", []);
  104. },
  105. removeUserInvite: function(userId) {
  106. var invites = this.getUserInvites();
  107. for(var i = 0; i < invites.length; i++) {
  108. var invite = invites[i];
  109. if(invite.id === userId){
  110. invites.splice(i, 1);
  111. }
  112. }
  113. }
  114. });
  115. return MeetingRoomModel;
  116. });