2012-05-17 18:40:46 -07:00
|
|
|
NEWSBLUR.Models.Feed = Backbone.Model.extend({
|
|
|
|
|
2012-05-22 17:39:21 -07:00
|
|
|
initialize: function() {
|
2012-06-07 13:56:54 -07:00
|
|
|
_.bindAll(this, 'on_change', 'delete_feed', 'update_folder_counts');
|
2012-06-14 15:30:34 -07:00
|
|
|
// this.bind('change', this.on_change);
|
2013-03-26 18:54:32 -07:00
|
|
|
this.bind('change:ps', this.change_counts);
|
|
|
|
this.bind('change:nt', this.change_counts);
|
|
|
|
this.bind('change:ng', this.change_counts);
|
2013-01-02 17:44:14 -08:00
|
|
|
this.bind('change:selected', this.update_folder_visibility);
|
2012-05-23 10:02:30 -07:00
|
|
|
this.views = [];
|
2012-06-07 13:56:54 -07:00
|
|
|
this.folders = [];
|
2012-05-22 17:39:21 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
on_change: function() {
|
2012-05-24 13:31:23 -07:00
|
|
|
if (!('selected' in this.changedAttributes())) {
|
|
|
|
NEWSBLUR.log(['Feed Change', this.changedAttributes(), this.previousAttributes()]);
|
|
|
|
}
|
2012-05-22 17:39:21 -07:00
|
|
|
},
|
|
|
|
|
2013-03-26 18:54:32 -07:00
|
|
|
change_counts: function(data, count, options) {
|
|
|
|
options = options || {};
|
|
|
|
console.log(["change_counts", data, count, options]);
|
|
|
|
this.update_folder_counts();
|
|
|
|
|
|
|
|
if (this.get('selected') && options.refresh_feeds) {
|
|
|
|
console.log(["Selected feed count change", this]);
|
|
|
|
NEWSBLUR.reader.feed_unread_count(this.id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
force_update_counts: function() {
|
|
|
|
NEWSBLUR.reader.feed_unread_count(this.id);
|
|
|
|
},
|
|
|
|
|
2012-06-07 13:56:54 -07:00
|
|
|
update_folder_counts: function() {
|
|
|
|
_.each(this.folders, function(folder) {
|
|
|
|
folder.trigger('change:counts');
|
2013-01-02 17:44:14 -08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
update_folder_visibility: function() {
|
|
|
|
_.each(this.folders, function(folder) {
|
|
|
|
folder.trigger('change:feed_selected');
|
2012-06-07 13:56:54 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-05-23 12:10:35 -07:00
|
|
|
delete_feed: function(options) {
|
|
|
|
options = options || {};
|
|
|
|
var view = options.view || this.get_view();
|
2013-09-13 16:35:48 -07:00
|
|
|
|
|
|
|
NEWSBLUR.reader.flags['reloading_feeds'] = true;
|
|
|
|
NEWSBLUR.assets.delete_feed(this.id, view.options.folder_title, function() {
|
|
|
|
NEWSBLUR.reader.flags['reloading_feeds'] = false;
|
|
|
|
});
|
2012-05-23 12:10:35 -07:00
|
|
|
view.delete_feed();
|
|
|
|
},
|
|
|
|
|
2012-05-23 17:21:06 -07:00
|
|
|
move_to_folder: function(to_folder, options) {
|
|
|
|
options = options || {};
|
|
|
|
var view = options.view || this.get_view();
|
|
|
|
var in_folder = view.options.folder_title;
|
|
|
|
|
|
|
|
if (in_folder == to_folder) return false;
|
2013-09-13 16:35:48 -07:00
|
|
|
|
|
|
|
NEWSBLUR.reader.flags['reloading_feeds'] = true;
|
2012-05-23 17:21:06 -07:00
|
|
|
NEWSBLUR.assets.move_feed_to_folder(this.id, in_folder, to_folder, function() {
|
2013-09-13 16:35:48 -07:00
|
|
|
NEWSBLUR.reader.flags['reloading_feeds'] = false;
|
2012-05-23 17:21:06 -07:00
|
|
|
_.delay(function() {
|
|
|
|
NEWSBLUR.reader.$s.$feed_list.css('opacity', 1).animate({'opacity': 0}, {
|
|
|
|
'duration': 100,
|
|
|
|
'complete': function() {
|
|
|
|
NEWSBLUR.app.feed_list.make_feeds();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 250);
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2013-08-07 10:56:51 -07:00
|
|
|
parent_folder_names: function() {
|
|
|
|
var names = _.compact(_.flatten(_.map(this.folders, function(folder) {
|
|
|
|
return folder.parent_folder_names();
|
|
|
|
})));
|
|
|
|
|
|
|
|
return names;
|
|
|
|
},
|
|
|
|
|
2014-06-27 16:53:01 -07:00
|
|
|
in_folders: function() {
|
|
|
|
var in_folders = _.pluck(_.pluck(this.folders, 'options'), 'title');
|
|
|
|
|
|
|
|
return in_folders;
|
|
|
|
},
|
|
|
|
|
2012-05-23 12:27:59 -07:00
|
|
|
rename: function(new_title) {
|
|
|
|
this.set('feed_title', new_title);
|
|
|
|
NEWSBLUR.assets.rename_feed(this.id, new_title);
|
|
|
|
},
|
|
|
|
|
2012-07-18 18:34:19 -07:00
|
|
|
get_view: function($feed, fallback) {
|
|
|
|
var found_view = _.detect(this.views, function(view) {
|
2012-05-23 12:10:35 -07:00
|
|
|
if ($feed) {
|
|
|
|
return view.el == $feed.get(0);
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2012-07-18 18:34:19 -07:00
|
|
|
|
|
|
|
if (!found_view && fallback && this.views.length) {
|
|
|
|
found_view = this.views[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return found_view;
|
2012-05-23 12:10:35 -07:00
|
|
|
},
|
|
|
|
|
2012-05-18 16:59:39 -07:00
|
|
|
is_social: function() {
|
|
|
|
return false;
|
2012-05-22 17:39:21 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
is_feed: function() {
|
|
|
|
return true;
|
2012-05-24 17:32:01 -07:00
|
|
|
},
|
|
|
|
|
2013-08-16 16:02:45 -07:00
|
|
|
is_starred: function() {
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2012-05-24 17:32:01 -07:00
|
|
|
is_light: function() {
|
2012-05-29 11:48:40 -07:00
|
|
|
var is_light = this._is_light;
|
2012-05-24 17:32:01 -07:00
|
|
|
if (!_.isUndefined(is_light)) {
|
|
|
|
return is_light;
|
|
|
|
}
|
|
|
|
var color = this.get('favicon_color');
|
|
|
|
if (!color) return false;
|
|
|
|
|
|
|
|
var r = parseInt(color.substr(0, 2), 16) / 255.0;
|
|
|
|
var g = parseInt(color.substr(2, 2), 16) / 255.0;
|
|
|
|
var b = parseInt(color.substr(4, 2), 16) / 255.0;
|
|
|
|
|
|
|
|
is_light = $.textColor({r: r, g: g, b: b}) != 'white';
|
2012-05-29 11:48:40 -07:00
|
|
|
this._is_light = is_light;
|
2012-05-24 17:32:01 -07:00
|
|
|
return is_light;
|
2012-06-07 13:56:54 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
unread_counts: function() {
|
2014-05-28 16:54:51 -07:00
|
|
|
var starred_feed = NEWSBLUR.assets.starred_feeds.get_feed(this.id);
|
|
|
|
|
2012-06-07 13:56:54 -07:00
|
|
|
return {
|
2013-08-05 13:26:51 -07:00
|
|
|
ps: this.get('ps') || 0,
|
|
|
|
nt: this.get('nt') || 0,
|
2014-05-28 16:54:51 -07:00
|
|
|
ng: this.get('ng') || 0,
|
|
|
|
st: starred_feed && starred_feed.get('count') || 0
|
2012-06-07 13:56:54 -07:00
|
|
|
};
|
2013-01-02 17:44:14 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
has_unreads: function(options) {
|
|
|
|
options = options || {};
|
|
|
|
var unread_view = NEWSBLUR.assets.preference('unread_view');
|
|
|
|
|
|
|
|
if (options.include_selected && this.get('selected')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-26 10:26:11 -07:00
|
|
|
if (!this.get('active')) return false;
|
|
|
|
|
2013-01-02 17:44:14 -08:00
|
|
|
if (unread_view <= -1) {
|
|
|
|
return !!(this.get('ng') || this.get('nt') || this.get('ps'));
|
|
|
|
} else if (unread_view == 0) {
|
|
|
|
return !!(this.get('nt') || this.get('ps'));
|
2014-05-28 17:35:51 -07:00
|
|
|
} else if (unread_view >= 2) {
|
|
|
|
var starred_feed = NEWSBLUR.assets.starred_feeds.get_feed(this.id);
|
|
|
|
return starred_feed && starred_feed.get('count');
|
2013-01-02 17:44:14 -08:00
|
|
|
} else if (unread_view > 0) {
|
|
|
|
return !!(this.get('ps'));
|
|
|
|
}
|
2012-05-18 16:59:39 -07:00
|
|
|
}
|
|
|
|
|
2012-05-17 18:40:46 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
NEWSBLUR.Collections.Feeds = Backbone.Collection.extend({
|
|
|
|
|
|
|
|
model: NEWSBLUR.Models.Feed,
|
|
|
|
|
|
|
|
url: '/reader/feeds',
|
|
|
|
|
2012-05-25 18:54:04 -07:00
|
|
|
active_feed: null,
|
|
|
|
|
|
|
|
initialize: function() {
|
|
|
|
this.bind('change', this.detect_active_feed);
|
|
|
|
},
|
|
|
|
|
|
|
|
// ===========
|
|
|
|
// = Actions =
|
|
|
|
// ===========
|
|
|
|
|
2012-05-17 18:40:46 -07:00
|
|
|
fetch: function(options) {
|
2012-05-26 22:14:34 -07:00
|
|
|
var data = {
|
|
|
|
'v': 2
|
|
|
|
};
|
|
|
|
|
2012-05-17 18:40:46 -07:00
|
|
|
options = _.extend({
|
2012-05-26 22:14:34 -07:00
|
|
|
data: data,
|
2012-05-17 20:50:08 -07:00
|
|
|
silent: true
|
2012-05-17 18:40:46 -07:00
|
|
|
}, options);
|
|
|
|
return Backbone.Collection.prototype.fetch.call(this, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
parse: function(data) {
|
2012-05-23 12:10:35 -07:00
|
|
|
_.each(data.feeds, function(feed) {
|
|
|
|
feed.selected = false;
|
|
|
|
});
|
2012-05-17 18:40:46 -07:00
|
|
|
return data.feeds;
|
2012-05-18 18:13:45 -07:00
|
|
|
},
|
2012-05-17 18:40:46 -07:00
|
|
|
|
2012-05-25 18:54:04 -07:00
|
|
|
deselect: function() {
|
|
|
|
this.each(function(feed){
|
|
|
|
feed.set('selected', false);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// ==================
|
|
|
|
// = Model Managers =
|
|
|
|
// ==================
|
|
|
|
|
2012-05-23 12:10:35 -07:00
|
|
|
selected: function() {
|
|
|
|
return this.detect(function(feed) { return feed.get('selected'); });
|
|
|
|
},
|
|
|
|
|
2012-12-10 16:25:21 -08:00
|
|
|
active: function() {
|
|
|
|
return this.select(function(feed) { return feed.get('active'); });
|
|
|
|
},
|
|
|
|
|
2012-05-18 18:13:45 -07:00
|
|
|
has_chosen_feeds: function() {
|
|
|
|
return this.any(function(feed) {
|
|
|
|
return feed.get('active');
|
|
|
|
});
|
2012-05-21 20:08:27 -07:00
|
|
|
},
|
|
|
|
|
2012-06-18 15:59:31 -07:00
|
|
|
has_unfetched_feeds: function() {
|
|
|
|
return this.any(function(feed) {
|
|
|
|
return feed.get('not_yet_fetched');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-04-22 18:33:08 -07:00
|
|
|
// ============
|
|
|
|
// = Counters =
|
|
|
|
// ============
|
|
|
|
|
|
|
|
search_indexed: function() {
|
|
|
|
var indexed = this.select(function(feed) {
|
|
|
|
return feed.get('search_indexed');
|
|
|
|
}).length;
|
|
|
|
|
|
|
|
return indexed;
|
|
|
|
},
|
|
|
|
|
2012-05-25 18:54:04 -07:00
|
|
|
// ==========
|
|
|
|
// = Events =
|
|
|
|
// ==========
|
|
|
|
|
|
|
|
detect_active_feed: function() {
|
|
|
|
this.active_feed = this.detect(function(feed) {
|
|
|
|
return feed.get('selected');
|
2012-05-21 20:08:27 -07:00
|
|
|
});
|
2012-05-18 18:13:45 -07:00
|
|
|
}
|
2012-05-17 18:40:46 -07:00
|
|
|
|
|
|
|
});
|