PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/webapp/resources/js/wss-model.js

https://github.com/Absor/wss
JavaScript | 167 lines | 150 code | 8 blank | 9 comment | 34 complexity | c71bbd1cadbcbdac9a11c6cfbfae2028 MD5 | raw file
  1. // Backbone models
  2. wss.model.Week = Backbone.Model.extend({
  3. initialize: function() {
  4. this.on("change", this.checkYearAndWeek, this);
  5. },
  6. week: null,
  7. year: null,
  8. url: "wss/plannedshifts/weekinfo",
  9. last: function() {
  10. this.checkYearAndWeek();
  11. if (this.week && this.year) {
  12. this.week--;
  13. if (this.week < 1) {
  14. this.week = 52;
  15. this.year--;
  16. }
  17. this.setUrlandFetch();
  18. }
  19. },
  20. thisWeek: function() {
  21. this.week = null;
  22. this.year = null;
  23. this.url = "wss/plannedshifts/weekinfo";
  24. this.fetch();
  25. },
  26. next: function() {
  27. this.checkYearAndWeek();
  28. if (this.week && this.year) {
  29. this.week++;
  30. if (this.week > 52) {
  31. this.week = 1;
  32. this.year++;
  33. }
  34. this.setUrlandFetch();
  35. }
  36. },
  37. checkYearAndWeek: function() {
  38. this.week = this.attributes.weekNumber;
  39. this.year = this.attributes.year;
  40. },
  41. setUrlandFetch: function() {
  42. this.url = "wss/plannedshifts/weekinfo/" + this.year + "/" + this.week;
  43. this.fetch();
  44. }
  45. });
  46. wss.model.LoggedUser = Backbone.Model.extend({
  47. url: "wss/users/loggedin"
  48. });
  49. wss.model.WorkShift = Backbone.Model.extend({
  50. idAttribute: "id",
  51. validate: function(attrs) {
  52. var attrsToUse = attrs;
  53. if (!attrsToUse) {
  54. attrsToUse = this.attributes;
  55. }
  56. if (!attrsToUse.shiftName) {
  57. return {field: "shiftName", error: "Invalid name."};
  58. }
  59. if (attrsToUse.shiftName && attrsToUse.shiftName.length < 3) {
  60. return {field: "shiftName", error: "Name too short."};
  61. }
  62. if (!attrsToUse.startTime) {
  63. return {field: "startTime", error: "Invalid start time."};
  64. }
  65. if (!attrsToUse.endTime) {
  66. return {field: "endTime", error: "Invalid end time."};
  67. }
  68. }
  69. });
  70. wss.model.ShiftList = Backbone.Collection.extend({
  71. model: wss.model.WorkShift,
  72. url: "wss/shifts",
  73. comparator: function(shift1, shift2) {
  74. // sorts by start time
  75. var value = wss.timeInt(shift1.get("startTime")) - wss.timeInt(shift2.get("startTime"));
  76. if (value < 0) {
  77. return -1;
  78. }
  79. if (value === 0) {
  80. return 0;
  81. }
  82. if (value > 0) {
  83. return 1;
  84. }
  85. }
  86. });
  87. wss.model.PlannedShift = Backbone.Model.extend({
  88. idAttribute: "id",
  89. // custom create and delete functionality for planned shifts
  90. sync: function(method, model, options) {
  91. if (method === "create") {
  92. options.url = "wss/plannedshifts/" + model.get("bareEmployee") + "/" + model.get("shift");
  93. options.data = JSON.stringify(model.get("shiftDate"));
  94. options.contentType = "application/json";
  95. }
  96. if (method === "delete") {
  97. options.url = "wss/plannedshifts/" + model.id;
  98. }
  99. return Backbone.sync(method, model, options);
  100. }
  101. });
  102. wss.model.PlannedShiftList = Backbone.Collection.extend({
  103. initialize: function() {
  104. // on week changes set url
  105. this.listenTo(wss.week, "change", this.setUrl);
  106. },
  107. setUrl: function() {
  108. if (wss.week.year && wss.week.week) {
  109. this.url = "wss/plannedshifts/" + wss.week.year + "/" + wss.week.week;
  110. } else {
  111. this.url = "wss/plannedshifts";
  112. }
  113. this.fetch();
  114. },
  115. model: wss.model.PlannedShift,
  116. url: "wss/plannedshifts"
  117. });
  118. wss.model.User = Backbone.Model.extend({
  119. idAttribute: "id",
  120. validate: function(attrs) {
  121. var attrsToUse = attrs;
  122. if (!attrsToUse) {
  123. attrsToUse = this.attributes;
  124. }
  125. // username
  126. if (!attrsToUse.username) {
  127. return {field: "username", error: "Invalid username."};
  128. }
  129. if (attrsToUse.username && attrsToUse.username.length < 3) {
  130. return {field: "username", error: "Username too short."};
  131. }
  132. // password
  133. if (!attrsToUse.barePassword) {
  134. return {field: "password", error: "Invalid password."};
  135. }
  136. if (attrsToUse.barePassword && attrsToUse.barePassword.length < 8) {
  137. return {field: "password", error: "Password too short."};
  138. }
  139. // email
  140. if (!attrsToUse.email) {
  141. return {field: "email", error: "Invalid email."};
  142. }
  143. if (!wss.emailRegExp.test(attrsToUse.email)) {
  144. return {field: "email", error: "Email is not valid."};
  145. }
  146. // role
  147. if (!attrsToUse.role || (attrsToUse.role !== "employee" && attrsToUse.role !== "employer")) {
  148. return {field: "role", error: "Invalid role."};
  149. }
  150. // role
  151. if (attrsToUse.enabled !== true && attrsToUse.enabled !== false) {
  152. return {field: "enabled", error: "Invalid state."};
  153. }
  154. }
  155. });
  156. wss.model.UserList = Backbone.Collection.extend({
  157. model: wss.model.User,
  158. url: "wss/users"
  159. });