<commit_before>Template.Collection.created = function() {
    var self = this;
    self._selected = new ReactiveVar();
    self._loading = new ReactiveVar();
    self._progress = new ReactiveVar();
    self.load = function(song, action) {
        options = {};
        if(!_.isEqual(action, undefined)) {
            self._collection = self.data.collection;
            if(_.isEqual(action, true)) {
                _.extend(options, {
                    action: 'download',
                    progress: function(data, n, total) {
                        self._progress.set({ value: n, max: total });
                    }
                });
            }
        }
        self._loading.set(song);
        AudioPlayer.load(song, function() {
            self._loading.set(null);
            self._progress.set(null);
        }, function(error) {
            UIkit.notify(error.message, 'warning');
        }, options);
    }
    self.loadNext = function() {
        var song = Session.get('currentSong');
        if(!song) {
        } else {
            self.load(self.findNext(song, 1));
        }
    },
    self.loadPrevious = function() {
        var song = Session.get('currentSong');
        if(!song) {
        } else {
            self.load(self.findNext(song, -1));
        }
    },
    self.selectNext = function() {
        var song = self._selected.get();
        if(!song) {
        } else {
            self._selected.set(self.findNext(song, 1));
        }
    }
    self.selectPrevious = function() {
        var song = self._selected.get();
        if(!song) {
        } else {
            self._selected.set(self.findNext(song, -1));
        }
    }
    self.findNext = function(song, order, sort) {
        if(!order) order = 1;
        var query = {}, sort = _.extend({}, sort ? sort : self.data.sort);
        var index = 0, lastIndex = _.size(sort) -1;
        var field;
        for(field in sort) {
            var value = song[field];
            var operator = sort[field] == order ? '$gt' : '$lt';
            if(!value) {
                continue; // TODO
            } else if(++index < lastIndex) {
                query[field] = value; 
            } else {
                query[field] = {};
                query[field][operator] = value;
            }
        }
        var collection = null;
        if(song != self._selected.get()) {
            collection = self._collection;
        } else {
            collection = self.data.collection;
            _.extend(query, self.searchQuery());
        }
        var nextSong = collection.findOne(query, { sort: _.object(_.map(self.data.sort, function(value, key) {
            return [key, value * order];
        })), fields: self.data.fields });
        if(!nextSong) {
            delete sort[field];
            nextSong = self.findNext(song, order, sort);
        }
        return nextSong;
    }
    self.searchQuery = function() {
        var query = {};
        var search = Session.get('search');
        if(typeof search == 'string') {
            var regexp = new RegExp(search.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'), 'i');
            _.extend(query, { '$or': [{ 'artist': regexp }, { 'album': regexp }, { 'title': regexp }]});
        }
        return query;
    }
    _.extend(AudioPlayer, { loadNext: self.loadNext, loadPrevious: self.loadPrevious });
}

Template.Collection.rendered = function() {
    var self = this;
    self.$('table').focus();
}

Template.Collection.helpers({
    'model': function() {
        var self = Template.instance();
        return this.collection.find(self.searchQuery(), { fields: this.fields, sort: this.sort });
    },
    'rowAttributes': function() {
        var self = Template.instance();
        var attributes = {};
        if(!AudioPlayer.canPlay(this)) {
            _.extend(attributes, { disabled: 'disabled' });
        }
        if(_.isEqual(self._selected.get(), this)) {
            _.extend(attributes, { selected: 'selected' });
        }
        if(_.isEqual(self._loading.get(), this)) {
            _.extend(attributes, { role: 'loading' });
        }
        if(_.isEqual(Session.get('currentSong'), this)) {
            _.extend(attributes, { class: 'uk-active' });
        }
        return attributes;
    },
    'progress': function() {
        var self = Template.instance();
        var progress = self._progress.get();
        if(_.isEqual(self._loading.get(), this) && progress) {
            return progress;
        }

    },
    'search': function() {
        var self = Template.instance();
        return Session.get('search');
    }
});

Template.Collection.events({
    'input aside input[type="search"]': function(event, template) {
        var self = template;
        Session.set('search', event.target.value);
    },
    'change table thead tr th input[type="checkbox"]': function(event, template) {
        var self = template;
        var checked = self.$(event.target).prop('checked');
        self.$('td input[type="checkbox"]').each(function() {<commit_msg>Block form submition in collection template helper<commit_after>Template.Collection.created = function() {
    var self = this;
    self._selected = new ReactiveVar();
    self._loading = new ReactiveVar();
    self._progress = new ReactiveVar();
    self.load = function(song, action) {
        options = {};
        if(!_.isEqual(action, undefined)) {
            self._collection = self.data.collection;
            if(_.isEqual(action, true)) {
                _.extend(options, {
                    action: 'download',
                    progress: function(data, n, total) {
                        self._progress.set({ value: n, max: total });
                    }
                });
            }
        }
        self._loading.set(song);
        AudioPlayer.load(song, function() {
            self._loading.set(null);
            self._progress.set(null);
        }, function(error) {
            UIkit.notify(error.message, 'warning');
        }, options);
    }
    self.loadNext = function() {
        var song = Session.get('currentSong');
        if(!song) {
        } else {
            self.load(self.findNext(song, 1));
        }
    },
    self.loadPrevious = function() {
        var song = Session.get('currentSong');
        if(!song) {
        } else {
            self.load(self.findNext(song, -1));
        }
    },
    self.selectNext = function() {
        var song = self._selected.get();
        if(!song) {
        } else {
            self._selected.set(self.findNext(song, 1));
        }
    }
    self.selectPrevious = function() {
        var song = self._selected.get();
        if(!song) {
        } else {
            self._selected.set(self.findNext(song, -1));
        }
    }
    self.findNext = function(song, order, sort) {
        if(!order) order = 1;
        var query = {}, sort = _.extend({}, sort ? sort : self.data.sort);
        var index = 0, lastIndex = _.size(sort) -1;
        var field;
        for(field in sort) {
            var value = song[field];
            var operator = sort[field] == order ? '$gt' : '$lt';
            if(!value) {
                continue; // TODO
            } else if(++index < lastIndex) {
                query[field] = value; 
            } else {
                query[field] = {};
                query[field][operator] = value;
            }
        }
        var collection = null;
        if(song != self._selected.get()) {
            collection = self._collection;
        } else {
            collection = self.data.collection;
            _.extend(query, self.searchQuery());
        }
        var nextSong = collection.findOne(query, { sort: _.object(_.map(self.data.sort, function(value, key) {
            return [key, value * order];
        })), fields: self.data.fields });
        if(!nextSong) {
            delete sort[field];
            nextSong = self.findNext(song, order, sort);
        }
        return nextSong;
    }
    self.searchQuery = function() {
        var query = {};
        var search = Session.get('search');
        if(typeof search == 'string') {
            var regexp = new RegExp(search.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'), 'i');
            _.extend(query, { '$or': [{ 'artist': regexp }, { 'album': regexp }, { 'title': regexp }]});
        }
        return query;
    }
    _.extend(AudioPlayer, { loadNext: self.loadNext, loadPrevious: self.loadPrevious });
}

Template.Collection.rendered = function() {
    var self = this;
    self.$('table').focus();
}

Template.Collection.helpers({
    'model': function() {
        var self = Template.instance();
        return this.collection.find(self.searchQuery(), { fields: this.fields, sort: this.sort });
    },
    'rowAttributes': function() {
        var self = Template.instance();
        var attributes = {};
        if(!AudioPlayer.canPlay(this)) {
            _.extend(attributes, { disabled: 'disabled' });
        }
        if(_.isEqual(self._selected.get(), this)) {
            _.extend(attributes, { selected: 'selected' });
        }
        if(_.isEqual(self._loading.get(), this)) {
            _.extend(attributes, { role: 'loading' });
        }
        if(_.isEqual(Session.get('currentSong'), this)) {
            _.extend(attributes, { class: 'uk-active' });
        }
        return attributes;
    },
    'progress': function() {
        var self = Template.instance();
        var progress = self._progress.get();
        if(_.isEqual(self._loading.get(), this) && progress) {
            return progress;
        }

    },
    'search': function() {
        var self = Template.instance();
        return Session.get('search');
    }
});

Template.Collection.events({
    'submit form': function(event, template) {
        return false;
    },
    'input aside input[type="search"]': function(event, template) {
        var self = template;
        Session.set('search', event.target.value);
    },
    'change table thead tr th input[type="checkbox"]': function(event, template) {
        var self = template;
        var checked = self.$(event.target).prop('checked');
        self.$('td input[type="checkbox"]').each(function() {
{'fraction_non_alphanumeric': 0.16054821341164954, 'fraction_numerical': 0.0013705335291238376, 'mean_word_length': 1.8182068965517242, 'pattern_counts': {'":': 0, '<': 5, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5971279', 'n_tokens_mistral': 2919, 'n_tokens_neox': 2844, 'n_words': 580}