PageRenderTime 28ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/static-data/static/js/superadmin/models.js

https://bitbucket.org/caret/timetables-2
JavaScript | 257 lines | 188 code | 44 blank | 25 comment | 8 complexity | 51fbdd7caa4bd27cef12f6030eb57b8d MD5 | raw file
Possible License(s): Apache-2.0
  1. var console;
  2. define(["jquery",
  3. "underscore",
  4. "backbone",
  5. "assert",
  6. "backbone-utils",
  7. "validate"],
  8. function($, _, Backbone, assert, backbone_utils, validate) {
  9. "use strict";
  10. var superclass = backbone_utils.superclass;
  11. var superproto = backbone_utils.superproto;
  12. var ValidityReportingModel = Backbone.Model.extend({
  13. constructor: function TimetablesModel() {
  14. superclass(TimetablesModel).apply(this, arguments);
  15. },
  16. initialize: function() {
  17. _.bindAll(this, "recheckValidity");
  18. this._isValid = this.isValid();
  19. this.on("change", this.recheckValidity);
  20. },
  21. recheckValidity: function() {
  22. var wasValid = this._isValid;
  23. this._isValid = this.isValid();
  24. if(wasValid !== this._isValid) {
  25. this.trigger("validity_change", this._isValid);
  26. }
  27. },
  28. // Provide a default implementation of validate.
  29. validate: function(){}
  30. });
  31. var Classifier = ValidityReportingModel.extend({
  32. constructor: function Classifier() {
  33. superclass(Classifier).apply(this, arguments);
  34. },
  35. defaults: {
  36. id: null,
  37. value: null
  38. },
  39. validate: function(attrs) {
  40. return validate.notEmpty("Value", attrs.value);
  41. }
  42. });
  43. var Classifiers = Backbone.Collection.extend({
  44. constructor: function Classifiers() {
  45. superclass(Classifiers).apply(this, arguments);
  46. },
  47. model: Classifier
  48. });
  49. var ClassifierGroup = ValidityReportingModel.extend({
  50. constructor: function ClassifierGroup() {
  51. superclass(ClassifierGroup).apply(this, arguments);
  52. },
  53. defaults: function() {
  54. return {
  55. id: null,
  56. name: "",
  57. type: null,
  58. classifiers: new Classifiers()
  59. };
  60. },
  61. initialize: function() {
  62. superproto(ClassifierGroup).initialize.apply(this, arguments);
  63. this.classifiers().on("validity_change add remove",
  64. this.recheckValidity);
  65. },
  66. classifiers: function() {
  67. return this.get("classifiers");
  68. },
  69. addClassifier: function(classifier) {
  70. this.get("classifiers").add(classifier);
  71. },
  72. toJSON: function() {
  73. return _.extend(Backbone.Model.prototype.toJSON.call(this), {
  74. classifiers: this.classifiers().toJSON()
  75. });
  76. },
  77. validate: function(attrs) {
  78. var error = validate.notEmpty("Name", attrs.name) ||
  79. validate.notEmpty("Type", attrs.type);
  80. if(error) return error;
  81. if(attrs.classifiers.length == 0)
  82. return "classifiers is empty";
  83. if(attrs.classifiers.any(function(c){
  84. return !(c.isValid()); }))
  85. return "All classifiers must be valid.";
  86. }
  87. });
  88. var ClassifierGroups = Backbone.Collection.extend({
  89. constructor: function ClassifierGroups() {
  90. superclass(ClassifierGroups).apply(this, arguments);
  91. },
  92. model: ClassifierGroup
  93. });
  94. var Faculty = ValidityReportingModel.extend({
  95. constructor: function Faculty() {
  96. superclass(Faculty).apply(this, arguments);
  97. },
  98. defaults: function() {
  99. return {
  100. id: null,
  101. name: "",
  102. code: "",
  103. year: null,
  104. classifier_groups: new ClassifierGroups()
  105. };
  106. },
  107. initialize: function() {
  108. superproto(Faculty).initialize.apply(this, arguments);
  109. this.classifierGroups().on("validity_change add remove",
  110. this.recheckValidity);
  111. },
  112. classifierGroups: function() {
  113. return this.get("classifier_groups");
  114. },
  115. addClassifierGroup: function(group) {
  116. this.classifierGroups().add(group);
  117. },
  118. toJSON: function() {
  119. var attrs = this.attributes;
  120. return {
  121. id: attrs.id,
  122. name: attrs.name,
  123. code: attrs.code,
  124. year: attrs.year.get("start_year"),
  125. classifier_groups: this.classifierGroups().toJSON()
  126. };
  127. },
  128. validate: function(attrs) {
  129. var groups = attrs.classifier_groups;
  130. var error = validate.notEmpty("Name", attrs.name) ||
  131. validate.notEmpty("Code", attrs.code);
  132. if(error) return error;
  133. if(groups.length < 2)
  134. return "At least two classifier groups are required.";
  135. if(groups.any(function(g) { return !(g.isValid()); }))
  136. return "Classifier groups must be valid."
  137. //if(!groups.filter(function(group){ return group.get("type") === "level" }))
  138. }
  139. });
  140. /**
  141. * A FacultyAffiliation represents an association between an Administrator
  142. * and a Faculty. This can confer upon the admin permission to manage the
  143. * Faculty.
  144. */
  145. var FacultyAffiliation = Backbone.Model.extend({
  146. constructor: function FacultyAffiliation() {
  147. superclass(FacultyAffiliation).apply(this, arguments);
  148. },
  149. defaults: function() {
  150. return {
  151. /** The Faculty this affiliation is with. */
  152. faculty: null,
  153. /** The Admin this affiliation is with. */
  154. administrator: null,
  155. /** Whether the admin is a key administrator of this Faculty. */
  156. is_key_admin: false,
  157. /**
  158. * Whether the admin is permitted to edit administrators of
  159. * the faculty associated with this affiliation.
  160. */
  161. can_edit_admins: false,
  162. /**
  163. * The set of permissions held by the associated admin with the
  164. * associated faculty. The set may contain any of:
  165. * read, write, publish, add_classifications
  166. */
  167. permission_set: [],
  168. /**
  169. * The set of Classifiers this admin has permission in.
  170. */
  171. classifier_set: new Classifiers()
  172. };
  173. },
  174. initialize: function() {
  175. },
  176. validate: function() {
  177. return !this.get("faculty") ? "No faculty" :
  178. !this.get("administrator") ? "No administrator" :
  179. undefined;
  180. }
  181. });
  182. /**
  183. * Represents a member of Administration staff in the Timetables system.
  184. */
  185. var Administrator = Backbone.Model.extend({
  186. constructor: function Administrator() {
  187. superclass(Administrator).apply(this, arguments);
  188. },
  189. defaults: function() {
  190. return {
  191. name: "",
  192. email: "",
  193. is_superadmin: false,
  194. is_disabled: false,
  195. affiliations: new Backbone.Collection()
  196. };
  197. },
  198. initialize: function() {
  199. },
  200. validate: function() {
  201. return validate.notEmpty("Name", name) ||
  202. validate.notEmpty("Email", email) ||
  203. validate.looksLikeEmail("Email", email);
  204. }
  205. });
  206. return {
  207. Faculty: Faculty,
  208. ClassifierGroup: ClassifierGroup,
  209. Classifier: Classifier,
  210. Administrator: Administrator,
  211. FacultyAffiliation: FacultyAffiliation
  212. };
  213. });