PageRenderTime 25ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/cms/static/js/collections/group.js

https://gitlab.com/unofficial-mirrors/edx-platform
JavaScript | 111 lines | 63 code | 13 blank | 35 comment | 9 complexity | 8e71b16bbcf847e66c70659601954fd8 MD5 | raw file
  1. define([
  2. 'underscore', 'underscore.string', 'backbone', 'gettext', 'js/models/group'
  3. ],
  4. function(_, str, Backbone, gettext, GroupModel) {
  5. 'use strict';
  6. var GroupCollection = Backbone.Collection.extend({
  7. model: GroupModel,
  8. comparator: 'order',
  9. /*
  10. * Return next index for the model.
  11. * @return {Number}
  12. */
  13. nextOrder: function() {
  14. if (!this.length) {
  15. return 0;
  16. }
  17. return this.last().get('order') + 1;
  18. },
  19. /**
  20. * Indicates if the collection is empty when all the models are empty
  21. * or the collection does not include any models.
  22. **/
  23. isEmpty: function() {
  24. return this.length === 0 || this.every(function(m) {
  25. return m.isEmpty();
  26. });
  27. },
  28. /*
  29. * Return default name for the group.
  30. * @return {String}
  31. * @examples
  32. * Group A, Group B, Group AA, Group ZZZ etc.
  33. */
  34. getNextDefaultGroupName: function() {
  35. var index = this.nextOrder(),
  36. usedNames = _.pluck(this.toJSON(), 'name'),
  37. name = '';
  38. do {
  39. name = str.sprintf(gettext('Group %s'), this.getGroupId(index));
  40. index ++;
  41. } while (_.contains(usedNames, name));
  42. return name;
  43. },
  44. /*
  45. * Return group id for the default name of the group.
  46. * @param {Number} number Current index of the model in the collection.
  47. * @return {String}
  48. * @examples
  49. * A, B, AA in Group A, Group B, ..., Group AA, etc.
  50. */
  51. getGroupId: (function() {
  52. /*
  53. Translators: Dictionary used for creation ids that are used in
  54. default group names. For example: A, B, AA in Group A,
  55. Group B, ..., Group AA, etc.
  56. */
  57. var dict = gettext('ABCDEFGHIJKLMNOPQRSTUVWXYZ').split(''),
  58. len = dict.length,
  59. divide;
  60. divide = function(numerator, denominator) {
  61. if (!_.isNumber(numerator) || !denominator) {
  62. return null;
  63. }
  64. return {
  65. quotient: numerator / denominator,
  66. remainder: numerator % denominator
  67. };
  68. };
  69. return function getId(number) {
  70. var accumulatedValues = '',
  71. result = divide(number, len),
  72. index;
  73. if (result) {
  74. // subtract 1 to start the count with 0.
  75. index = Math.floor(result.quotient) - 1;
  76. // Proceed by dividing the non-remainder part of the
  77. // dividend by the desired base until the result is less
  78. // than one.
  79. if (index < len) {
  80. // if index < 0, we do not need an additional power.
  81. if (index > -1) {
  82. // Get value for the next power.
  83. accumulatedValues += dict[index];
  84. }
  85. } else {
  86. // If we need more than 1 additional power.
  87. // Get value for the next powers.
  88. accumulatedValues += getId(index);
  89. }
  90. // Accumulated values + the current reminder
  91. return accumulatedValues + dict[result.remainder];
  92. }
  93. return String(number);
  94. };
  95. }())
  96. });
  97. return GroupCollection;
  98. });