<commit_before>NEWSBLUR.Models.FeedOrFolder = Backbone.Model.extend({
    
    initialize: function(model) {
        if (_.isNumber(model) || model['feed_id']) {
            this.feed = NEWSBLUR.assets.feeds.get(model['feed_id'] || model);

            // The feed needs to exists as a model as well. Otherwise, toss it.
            if (this.feed) {
                this.set('is_feed', true);
            }
        } else if (model && model.fake) {
            this.folders = model.folders;
        } else if (model) {
            var title = _.keys(model)[0];
            var children = model[title];
            this.set('is_folder', true);
            this.set('folder_title', title);
            this.folder_views = [];
            this.folders = new NEWSBLUR.Collections.Folders([], {
                title: title,
                parent_folder: this.collection,
                parse: true
            });
            this.folders.reset(_.compact(children), {parse: true});
        }
    },
    
    parse: function(attrs) {
        if (_.isNumber(attrs)) {
            attrs = {'feed_id': attrs};
        }
        return attrs;
    },
    
    is_feed: function() {
        return !!this.get('is_feed');
    },
    
    is_folder: function() {
        return !!this.get('is_folder');
    },
    
    get_view: function($folder) {
        var view = _.detect(this.folder_views, function(view) {
            if ($folder) {
                return view.el == $folder.get(0);
            }
        });
        if (!view && this.folders.length) {
            view = this.folders.get_view($folder);
        }
        return view;
    },
    
    feed_ids_in_folder: function() {
        if (this.is_feed() && this.feed.get('active')) {
            return this.feed.id;
        } else if (this.is_folder()) {
            return this.folders.feed_ids_in_folder();
        }
    },
    
    feeds_with_unreads: function(options) {
        if (this.is_feed()) {
            return this.feed.has_unreads(options) && this.feed;
        } else if (this.is_folder()) {
            return this.folders.feeds_with_unreads(options);
        }
    },
    
    move_to_folder: function(to_folder, options) {
        options = options || {};
        var view = options.view || this.get_view();
        var in_folder = this.collection.options.title;
        var folder_title = this.get('folder_title');
        if (in_folder == to_folder) return false;
        
        NEWSBLUR.reader.flags['reloading_feeds'] = true;
        NEWSBLUR.assets.move_folder_to_folder(folder_title, in_folder, to_folder, function() {
            NEWSBLUR.reader.flags['reloading_feeds'] = false;
            _.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;
    },
    
    rename: function(new_folder_name) {
        var folder_title = this.get('folder_title');
        var in_folder = this.collection.options.title;
        NEWSBLUR.assets.rename_folder(folder_title, new_folder_name, in_folder);
        this.set('folder_title', new_folder_name);
    },
    
    delete_folder: function() {
        var folder_title = this.get('folder_title');
        var in_folder = this.collection.options.title;
        var feed_ids_in_folder = this.feed_ids_in_folder();
        
        NEWSBLUR.reader.flags['reloading_feeds'] = true;
        NEWSBLUR.assets.delete_folder(folder_title, in_folder, feed_ids_in_folder, function() {
            NEWSBLUR.reader.flags['reloading_feeds'] = false;
        });
        this.trigger('delete');
    },
    
    has_unreads: function(options) {
        options = options || {};

        if (options.include_selected && this.get('selected')) {
            return true;
        }
        
        return this.folders.has_unreads(options);
    },
    
    view_setting: function(setting) {
        if (this.is_folder()) {
            return NEWSBLUR.assets.view_setting('river:' + this.get('folder_title'), setting);
        } else {
            return NEWSBLUR.assets.view_setting(this.id, setting);
        }
    }
    
});

NEWSBLUR.Collections.Folders = Backbone.Collection.extend({
    
    options: {
        title: ''
    },
    
    initialize: function(models, options) {
        _.bindAll(this, 'propagate_feed_selected');
        this.options = options || {};
        this.parent_folder = options && options.parent_folder;
        this.comparator = NEWSBLUR.Collections.Folders.comparator;
        this.bind('change:feed_selected', this.propagate_feed_selected);
        this.bind('change:counts', this.propagate_change_counts);
        this.bind('reset', this.reset_folder_views);
    },
    
    model: NEWSBLUR.Models.FeedOrFolder,
    
    reset_folder_views: function() {
        this.each(function(item) {
            if (item.is_feed()) {
                item.feed.views = [];
                item.feed.folders = [];
            }
        });        
    },
    
    folders: function() {
        return this.select(function(item) {
            return item.is_folder();
        });
    },
    
    find_folder: function(folder_name) {
        var found_folder;
        this.any(function(folder) {
            if (folder.is_folder()) {
                if (folder.get('folder_title').toLowerCase() == folder_name) {
                    found_folder = folder;
                    return found_folder;
                }
                found_folder = folder.folders.find_folder(folder_name);
                return found_folder;
            }
        });
        return found_folder;
    },
    
    get_view: function($folder) {
        var view;
        this.any(function(item) {
            if (item.is_folder()) {
                view = item.get_view($folder);
                return view;
            }
        });
        if (view) {
            return view;
        }
    },
    
    child_folder_names: function() {
        var names = [];
        this.each(function(item) {
            if (item.is_folder()) {
                names.push(item.get('folder_title'));
                _.each(item.folders.child_folder_names(), function(name) {
                    names.push(name);
                });<commit_msg>Fixing urls for folders with dashes in the title.<commit_after>NEWSBLUR.Models.FeedOrFolder = Backbone.Model.extend({
    
    initialize: function(model) {
        if (_.isNumber(model) || model['feed_id']) {
            this.feed = NEWSBLUR.assets.feeds.get(model['feed_id'] || model);

            // The feed needs to exists as a model as well. Otherwise, toss it.
            if (this.feed) {
                this.set('is_feed', true);
            }
        } else if (model && model.fake) {
            this.folders = model.folders;
        } else if (model) {
            var title = _.keys(model)[0];
            var children = model[title];
            this.set('is_folder', true);
            this.set('folder_title', title);
            this.folder_views = [];
            this.folders = new NEWSBLUR.Collections.Folders([], {
                title: title,
                parent_folder: this.collection,
                parse: true
            });
            this.folders.reset(_.compact(children), {parse: true});
        }
    },
    
    parse: function(attrs) {
        if (_.isNumber(attrs)) {
            attrs = {'feed_id': attrs};
        }
        return attrs;
    },
    
    is_feed: function() {
        return !!this.get('is_feed');
    },
    
    is_folder: function() {
        return !!this.get('is_folder');
    },
    
    get_view: function($folder) {
        var view = _.detect(this.folder_views, function(view) {
            if ($folder) {
                return view.el == $folder.get(0);
            }
        });
        if (!view && this.folders.length) {
            view = this.folders.get_view($folder);
        }
        return view;
    },
    
    feed_ids_in_folder: function() {
        if (this.is_feed() && this.feed.get('active')) {
            return this.feed.id;
        } else if (this.is_folder()) {
            return this.folders.feed_ids_in_folder();
        }
    },
    
    feeds_with_unreads: function(options) {
        if (this.is_feed()) {
            return this.feed.has_unreads(options) && this.feed;
        } else if (this.is_folder()) {
            return this.folders.feeds_with_unreads(options);
        }
    },
    
    move_to_folder: function(to_folder, options) {
        options = options || {};
        var view = options.view || this.get_view();
        var in_folder = this.collection.options.title;
        var folder_title = this.get('folder_title');
        if (in_folder == to_folder) return false;
        
        NEWSBLUR.reader.flags['reloading_feeds'] = true;
        NEWSBLUR.assets.move_folder_to_folder(folder_title, in_folder, to_folder, function() {
            NEWSBLUR.reader.flags['reloading_feeds'] = false;
            _.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;
    },
    
    rename: function(new_folder_name) {
        var folder_title = this.get('folder_title');
        var in_folder = this.collection.options.title;
        NEWSBLUR.assets.rename_folder(folder_title, new_folder_name, in_folder);
        this.set('folder_title', new_folder_name);
    },
    
    delete_folder: function() {
        var folder_title = this.get('folder_title');
        var in_folder = this.collection.options.title;
        var feed_ids_in_folder = this.feed_ids_in_folder();
        
        NEWSBLUR.reader.flags['reloading_feeds'] = true;
        NEWSBLUR.assets.delete_folder(folder_title, in_folder, feed_ids_in_folder, function() {
            NEWSBLUR.reader.flags['reloading_feeds'] = false;
        });
        this.trigger('delete');
    },
    
    has_unreads: function(options) {
        options = options || {};

        if (options.include_selected && this.get('selected')) {
            return true;
        }
        
        return this.folders.has_unreads(options);
    },
    
    view_setting: function(setting) {
        if (this.is_folder()) {
            return NEWSBLUR.assets.view_setting('river:' + this.get('folder_title'), setting);
        } else {
            return NEWSBLUR.assets.view_setting(this.id, setting);
        }
    }
    
});

NEWSBLUR.Collections.Folders = Backbone.Collection.extend({
    
    options: {
        title: ''
    },
    
    initialize: function(models, options) {
        _.bindAll(this, 'propagate_feed_selected');
        this.options = options || {};
        this.parent_folder = options && options.parent_folder;
        this.comparator = NEWSBLUR.Collections.Folders.comparator;
        this.bind('change:feed_selected', this.propagate_feed_selected);
        this.bind('change:counts', this.propagate_change_counts);
        this.bind('reset', this.reset_folder_views);
    },
    
    model: NEWSBLUR.Models.FeedOrFolder,
    
    reset_folder_views: function() {
        this.each(function(item) {
            if (item.is_feed()) {
                item.feed.views = [];
                item.feed.folders = [];
            }
        });        
    },
    
    folders: function() {
        return this.select(function(item) {
            return item.is_folder();
        });
    },
    
    find_folder: function(folder_name) {
        var found_folder;
        this.any(function(folder) {
            if (folder.is_folder()) {
                if (folder.get('folder_title').toLowerCase() == folder_name ||
                    folder.get('folder_title').toLowerCase().replace(/-/g, ' ') == folder_name) {
                    found_folder = folder;
                    return found_folder;
                }
                found_folder = folder.folders.find_folder(folder_name);
                return found_folder;
            }
        });
        return found_folder;
    },
    
    get_view: function($folder) {
        var view;
        this.any(function(item) {
            if (item.is_folder()) {
                view = item.get_view($folder);
                return view;
            }
        });
        if (view) {
            return view;
        }
    },
    
    child_folder_names: function() {
        var names = [];
        this.each(function(item) {
            if (item.is_folder()) {
                names.push(item.get('folder_title'));
                _.each(item.folders.child_folder_names(), function(name) {
                    names.push(name);
                });
{'fraction_non_alphanumeric': 0.12928516525749423, 'fraction_numerical': 0.0015372790161414297, 'mean_word_length': 1.7449367088607595, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '347607', 'n_tokens_mistral': 3871, 'n_tokens_neox': 3779, 'n_words': 604}