PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/media/js/newsblur/models/social_subscription.js

https://github.com/samuelclay/NewsBlur
JavaScript | 114 lines | 89 code | 23 blank | 2 comment | 17 complexity | 12bed8d5a6590d43f9f5685c5dae70e9 MD5 | raw file
Possible License(s): MIT, GPL-2.0, BSD-3-Clause
  1. NEWSBLUR.Models.SocialSubscription = Backbone.Model.extend({
  2. initialize: function() {
  3. if (!this.get('page_url')) {
  4. this.set('page_url', '/social/page/' + this.get('user_id'));
  5. }
  6. _.bindAll(this, 'on_change', 'on_remove', 'update_counts');
  7. // this.bind('change', this.on_change);
  8. this.bind('remove', this.on_remove);
  9. this.bind('change:ps', this.update_counts);
  10. this.bind('change:nt', this.update_counts);
  11. this.bind('change:ng', this.update_counts);
  12. this.views = [];
  13. },
  14. on_change: function() {
  15. NEWSBLUR.log(['Social Feed Change', this.changedAttributes(), this.previousAttributes()]);
  16. },
  17. on_remove: function() {
  18. NEWSBLUR.log(["Remove Feed", this, this.views]);
  19. _.each(this.views, function(view) { view.remove(); });
  20. },
  21. update_counts: function() {
  22. NEWSBLUR.assets.social_feeds.trigger('change:counts');
  23. },
  24. is_social: function() {
  25. return true;
  26. },
  27. is_feed: function() {
  28. return false;
  29. },
  30. is_starred: function() {
  31. return false;
  32. },
  33. unread_counts: function() {
  34. return {
  35. ps: this.get('ps') || 0,
  36. nt: this.get('nt') || 0,
  37. ng: this.get('ng') || 0
  38. };
  39. }
  40. });
  41. NEWSBLUR.Collections.SocialSubscriptions = Backbone.Collection.extend({
  42. model : NEWSBLUR.Models.SocialSubscription,
  43. parse: function(models) {
  44. _.each(models, function(feed) {
  45. feed.selected = false;
  46. });
  47. return models;
  48. },
  49. comparator: function(a, b) {
  50. var sort_order = NEWSBLUR.reader.model.preference('feed_order');
  51. var title_a = a.get('feed_title') || '';
  52. var title_b = b.get('feed_title') || '';
  53. title_a = title_a.toLowerCase();
  54. title_b = title_b.toLowerCase();
  55. if (sort_order == 'MOSTUSED') {
  56. var opens_a = a.get('feed_opens');
  57. var opens_b = b.get('feed_opens');
  58. if (opens_a > opens_b) return -1;
  59. if (opens_a < opens_b) return 1;
  60. }
  61. // if (!sort_order || sort_order == 'ALPHABETICAL')
  62. if (title_a > title_b) return 1;
  63. else if (title_a < title_b) return -1;
  64. return 0;
  65. },
  66. selected: function() {
  67. return this.detect(function(feed) { return feed.get('selected'); });
  68. },
  69. deselect: function() {
  70. this.chain().select(function(feed) {
  71. return feed.get('selected');
  72. }).each(function(feed){
  73. feed.set('selected', false);
  74. });
  75. },
  76. unread_counts: function(existing_counts) {
  77. existing_counts = existing_counts || {};
  78. var counts = this.reduce(function(counts, item) {
  79. var feed_counts = item.unread_counts();
  80. counts['ps'] += feed_counts['ps'];
  81. counts['nt'] += feed_counts['nt'];
  82. counts['ng'] += feed_counts['ng'];
  83. return counts;
  84. }, {
  85. ps: existing_counts['ps'] || 0,
  86. nt: existing_counts['nt'] || 0,
  87. ng: existing_counts['ng'] || 0
  88. });
  89. return counts;
  90. }
  91. });