PageRenderTime 79ms CodeModel.GetById 41ms RepoModel.GetById 1ms app.codeStats 0ms

/cms/static/js/certificates/collections/certificates.js

https://gitlab.com/unofficial-mirrors/edx-platform
JavaScript | 81 lines | 62 code | 9 blank | 10 comment | 8 complexity | 6b031139c89fba20bf3db6649e5a76e4 MD5 | raw file
  1. // Backbone.js Application Collection: Certificates
  2. define([
  3. 'backbone',
  4. 'gettext',
  5. 'js/certificates/models/certificate'
  6. ],
  7. function(Backbone, gettext, Certificate) {
  8. 'use strict';
  9. var CertificateCollection = Backbone.Collection.extend({
  10. model: Certificate,
  11. /**
  12. * It represents the maximum number of certificates that a user can create. default set to 1.
  13. */
  14. maxAllowed: 1,
  15. initialize: function(attr, options) {
  16. // Set up the attributes for this collection instance
  17. this.url = options.certificateUrl;
  18. this.bind('remove', this.onModelRemoved, this);
  19. this.bind('add', this.onModelAdd, this);
  20. },
  21. certificate_array: function(certificate_info) {
  22. var return_array;
  23. try {
  24. return_array = JSON.parse(certificate_info);
  25. } catch (ex) {
  26. // If it didn't parse, and `certificate_info` is an object then return as it is
  27. // otherwise return empty array
  28. if (typeof certificate_info === 'object') {
  29. return_array = certificate_info;
  30. }
  31. else {
  32. console.error(
  33. interpolate(
  34. gettext('Could not parse certificate JSON. %(message)s'), {message: ex.message}, true
  35. )
  36. );
  37. return_array = [];
  38. }
  39. }
  40. return return_array;
  41. },
  42. onModelRemoved: function() {
  43. // remove the certificate web preview UI.
  44. if (window.certWebPreview && this.length === 0) {
  45. window.certWebPreview.remove();
  46. }
  47. this.toggleAddNewItemButtonState();
  48. },
  49. onModelAdd: function() {
  50. this.toggleAddNewItemButtonState();
  51. },
  52. toggleAddNewItemButtonState: function() {
  53. // user can create a new item e.g certificate; if not exceeded the maxAllowed limit.
  54. if (this.length >= this.maxAllowed) {
  55. $('.action-add').addClass('action-add-hidden');
  56. } else {
  57. $('.action-add').removeClass('action-add-hidden');
  58. }
  59. },
  60. parse: function(certificatesJson) {
  61. // Transforms the provided JSON into a Certificates collection
  62. var modelArray = this.certificate_array(certificatesJson);
  63. for (var i in modelArray) {
  64. if (modelArray.hasOwnProperty(i)) {
  65. this.push(modelArray[i]);
  66. }
  67. }
  68. return this.models;
  69. }
  70. });
  71. return CertificateCollection;
  72. });