PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/javascript/shared-package/scratchpads.js

https://gitlab.com/gregtyka/KhanLatest
JavaScript | 125 lines | 87 code | 17 blank | 21 comment | 3 complexity | 6aebc98be7bf9c2c3046975d839a65c2 MD5 | raw file
  1. window.ScratchpadRevision = Backbone.Model.extend({
  2. parse: function(resp, xhr) {
  3. // resp.created comes down from the server as an RFC date string (see
  4. // api/jsonify.py). We want it as a native JS Date object.
  5. resp.created = new Date(resp.created);
  6. return resp;
  7. }
  8. });
  9. window.Scratchpad = Backbone.Model.extend({
  10. urlRoot: "/api/labs/scratchpads",
  11. showUrl: function() {
  12. return "/explore/" + this.get("slug") + "/" + this.get("id");
  13. },
  14. // This is called whenever data is pulled down via fetch or save.
  15. //
  16. // We want to add a revision key with another backbone model (since backbone
  17. // doesn't support nested .set and .get out of the box
  18. parse: function(resp, xhr) {
  19. resp.revision = new ScratchpadRevision(resp.revision, {parse: true});
  20. return resp;
  21. },
  22. // Returns a copy of the Scratchpad with origin_scratchpad_id and
  23. // origin_revision_id set, and with the id stripped out
  24. fork: function() {
  25. // Clone the scratchpad and revision, but strip out the ids, and
  26. // all developer-only attributes
  27. var forkedScratchpad = this.clone()
  28. .unset("id")
  29. .unset("category")
  30. .unset("difficulty")
  31. .unset("youtube_id")
  32. .set({
  33. revision: this.get("revision").clone().unset("id"),
  34. origin_scratchpad_id: this.get("id"),
  35. origin_revision_id: this.get("revision").get("id")
  36. });
  37. return forkedScratchpad;
  38. },
  39. save: function(attributes, options) {
  40. // TODO(jlfwong): Fix these issues on the serverside instead
  41. // (this is gross)
  42. // These properties are not assignable properties of Scratchpads, so we
  43. // want to strip them out before saving
  44. this
  45. .unset("kind")
  46. .unset("slug")
  47. .get("revision")
  48. .unset("created")
  49. .unset("kind")
  50. .unset("scratchpad_id");
  51. if (!this.isNew()) {
  52. // These properties are immutable once set, so we
  53. // want to strip them out before trying to do an update
  54. this
  55. .unset("origin_revision_id")
  56. .unset("origin_scratchpad_id")
  57. .unset("user_id")
  58. .get("revision")
  59. .unset("id");
  60. }
  61. return Scratchpad.__super__.save.call(this, attributes, options);
  62. }
  63. }, {
  64. // Static Properties
  65. difficultyMapping: {
  66. "10": "Getting Started",
  67. "20": "Easy",
  68. "30": "Intermediate",
  69. "40": "Expert"
  70. }
  71. });
  72. window.ScratchpadList = Backbone.Collection.extend({
  73. model: Scratchpad,
  74. fetchForUser: function(options) {
  75. return this.fetch($.extend({
  76. url: "/api/labs/user/scratchpads"
  77. }, options));
  78. }
  79. });
  80. window.ScratchpadListView = Backbone.View.extend({
  81. template: Templates.get("shared.scratchpad-list"),
  82. render: function() {
  83. var scratchpads = this.collection
  84. .chain()
  85. // Sort using the provided key function, or just use the
  86. // scratchpads' natural sort order if none is provided.
  87. .sortBy(this.options.sortBy || _.identity)
  88. .map(function(scratchpad) {
  89. var difficulty = scratchpad.get("difficulty");
  90. var category = scratchpad.get("category");
  91. return {
  92. // TODO(jlfwong): Switch this to use the published flag
  93. // See: http://phabricator.khanacademy.org/T44
  94. displayDifficulty: (difficulty !== -1 &&
  95. (category === "tutorial" || category === "official")),
  96. difficulty: difficulty,
  97. difficultyText: Scratchpad.difficultyMapping[difficulty],
  98. imageUrl: scratchpad.showUrl() + "/image.png",
  99. showUrl: scratchpad.showUrl(),
  100. title: scratchpad.get("title")
  101. };
  102. })
  103. .value();
  104. this.$el.html(this.template({
  105. scratchpads: scratchpads
  106. }));
  107. }
  108. });