<commit_before>
import Ember from 'ember';
import WidgetCollection from 'ember-eureka/widget-collection';

export default WidgetCollection.extend({

    /** update the filterTerm if there is a queryParam
     */
    filterTerm: Ember.computed.oneWay('queryParam'),

    filterEnabled: Ember.computed.bool('config.filter'),

    /** if there is a `queryParam` specified on the widget's config,
     * use. Otherwise,
     */
     queryParam: function(key, value) {
        var queryParam = this.get('config.filter.queryParam');

        if (queryParam) {
            key = 'currentController.'+queryParam;
        } else {
            key = '_queryParam';
        }

        if (arguments.length === 1) {
            return this.get(key);
        } else {
            this.set(key, value);
            return value;
        }

     }.property('config.filter.queryParam', 'currentController'),


    updateCollection: function() {
        var filterTerm = this.get('filterTerm');
        var query;

        if (filterTerm != null) {
            query = {'title[$iregex]': '^'+filterTerm};
        } else {
            query = {};
        }

        this.set('routeModel.query', query);
        this.set('queryParam', filterTerm);
    }.on('init'),


    label: function() {
        var _label = this.get('config.label');
        if (_label === 'auto') {
            return this.get('modelType');
        }
        return _label;
    }.property('config.label', 'modelType'),


    queryConfig: function() {
        var query = this.get('config.query');
        if (query) {
            query = JSON.parse(query);
        } else {
            query = {};
        }
        return query;
    }.property('config.query'),


    collection: function() {
        var queryConfig = this.get('queryConfig');
        var routeQuery = this.get('routeModel.query') || {};
        routeQuery = JSON.parse(JSON.stringify(routeQuery));
        Ember.setProperties(routeQuery, queryConfig);
        return this.get('modelStore').find(routeQuery);
    }.property('routeModel.query', 'queryConfig'),


    keyPress: function(e) {
        if (e.keyCode === 13) {
            this.updateCollection();
        }
    },


    actions: {
        clear: function() {
            this.set('filterTerm', null);
            this.updateCollection();
        }
    }


});<commit_msg>make query param working \o/<commit_after>
import Ember from 'ember';
import WidgetCollection from 'ember-eureka/widget-collection';



export default WidgetCollection.extend({

    /** add the `controllerQueryParam` as an alias of the
     *  controller's queryParam. The name of the attribute is
     * taken from `config.queryParam`
     */
    _addControllerQueryParamProperty: function() {
        var queryParam = this.get('config.queryParam');
        if (queryParam) {
            this.reopen({
                controllerQueryParam: Ember.computed.alias('currentController.'+queryParam)
            });
            this.set('queryParam', this.get('controllerQueryParam'));
            this.updateQuery();
        }
     }.on('init'),


    /** if the `controllerQueryParam` changed (url is modified via the back button),
     * update the filterTerm to match the changes and update the query
     */
    _controllerQueryParamObserver: function() {
        var controllerQueryParam = this.get('controllerQueryParam');
        if (this.get('queryParam') !== controllerQueryParam) {
            this.set('queryParam', controllerQueryParam);
            this.updateQuery();
        }
    }.observes('controllerQueryParam'),


    /** when the query has been update,
     *  if the filterTerm doesn't match the
     *  controllerQueryParam, update the later.
     */
    _routeModelQueryObserver: function() {
        var queryParam = this.get('queryParam');
        if (queryParam !== this.get('controllerQueryParam')) {
            this.set('controllerQueryParam', queryParam);
        }
    }.observes('routeModel.query.hasChanged'),



    /** update the filterTerm if there is a queryParam
     */
    filterTerm: Ember.computed.alias('queryParam'),

    filterEnabled: Ember.computed.bool('config.filter'),


    updateQuery: function() {
        var filterTerm = this.get('filterTerm');
        var query = this.get('routeModel.query.raw');

        if (filterTerm) {
            query['title[$iregex]'] = '^'+filterTerm;
        } else {
            query['title[$iregex]'] = undefined;
            filterTerm = null;
        }

        this.set('routeModel.query.raw', query);
    },



    label: function() {
        var _label = this.get('config.label');
        if (_label === 'auto') {
            return this.get('modelType');
        }
        return _label;
    }.property('config.label', 'modelType'),


    queryConfig: function() {
        var query = this.get('config.query');
        if (query) {
            query = JSON.parse(query);
        } else {
            query = {};
        }
        return query;
    }.property('config.query'),


    collection: function() {
        var queryConfig = this.get('queryConfig');
        var routeQuery = this.get('routeModel.query.raw');
        Ember.setProperties(routeQuery, queryConfig);
        return this.get('modelStore').find(routeQuery);
    }.property('routeModel.query.hasChanged', 'queryConfig'),


    keyPress: function(e) {
        if (e.keyCode === 13) {
            this.updateQuery();
        }
    },


    actions: {
        clear: function() {
            this.set('filterTerm', null);
            this.updateQuery();
        }
    }


});
{'fraction_non_alphanumeric': 0.13843123209169053, 'fraction_numerical': 0.0008954154727793697, 'mean_word_length': 2.202408256880734, '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': '1248663', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1497, 'n_words': 332}