<commit_before>// bootstrap 3.1.1
// + datetimepicker 3.0.0 https://github.com/Eonasdan/bootstrap-datetimepicker http://eonasdan.github.io/bootstrap-datetimepicker
// + typeahead https://github.com/bassjobsen/Bootstrap-3-Typeahead
//
//
//= require app/settings
//= require jquery/jquery-${org.codehaus.groovy.grails.plugins.jquery.JQueryConfig.SHIPPED_VERSION}.min
//= require bootstrap3-typeahead
//= require imms.backbone.pack
//= require_self

(function($, Backbone, _, App){

    App.view.TypeAhead = App.View.extend({
        publishSearch : false,
        events : {
            "change" : "onSelect"
        },
        reset : function() { //TODO: test
//            this.$el.data('typeahead').$menu.removeClass(".active");
            this.$el.removeData("selected-value");
            this.$el.val('').change();
            if(this.$values != undefined) {
                this.$values.children("input").each(function(){
                    var $this = $(this);
                    if($this.data("field")) $this.val('');
                });
            }
        },
        onSelect : function() {
            var item = this.$el.data("selected-value");
            if(this.$values != undefined) {
                this.$values.children("input").each(function(){
                    var $this = $(this);
                    if($this.data("field")) {
                        var val = item ? item[$this.data("field")] : null;
                        if(val) {
                            $this.val(val);
                        }
                        else {
                            if(item) App.logErr("Warning. data not exist for field: " + $this.data("field"));
                        }
                    }
                });
            }
            if(item) {
                this.onValidSelectedItem(item);
                if(this.publishSearch) this.publishEvt("ta:search:" + this.field, item);
            }
        },
        onValidSelectedItem : function(item) {
            // to override from opt
        },
        // <input class=".type-ahead" id="assetInstance-type" data-field="type" data-domain="assetType" data-display-key='value' data-items='all' data-minLength='2'/>
        // <div id="type-values">
        //      <input type="hidden" name="typeId" data-field="id">
        //      <input type="hidden" name="typeCd" data=field="code">
        // </div>
        initialize: function(opt) {
            this.field = this.$el.data("field"); // fieldName
            this.key = this.$el.data("domain"); //domain class
            this.$values = this.$el.parent().find("#"+this.field + "-values");
            this.displayKey = this.$el.data("display-key") || "value";
            this.name = opt.name || this.key;
            this.onSelect = opt.onSelect || this.onSelect;
            this.onValidSelectedItem = opt.onValidSelectedItem || this.onValidSelectedItem;
            this.$el.attr('autocomplete', 'off');
            //http://stackoverflow.com/questions/14901535/bootstrap-typeahead-ajax-result-format-example/14959406#14959406
            //http://tatiyants.com/how-to-use-json-objects-with-twitter-bootstrap-typeahead/
            var typeAheadOpt = {};
            if(this.$el.data('items')) { typeAheadOpt.items = this.$el.data('items') }
            if(this.$el.data('minlength')) { typeAheadOpt.minLength = this.$el.data('minlength') }
            if(!this.$el.data("source")) {
                $.extend(typeAheadOpt, {
                    isObjectItem : true, displayKey : this.displayKey, autoSelect : false,
                    remoteUrl : this.$el.data("sourceurl") || App.url + "/typeAhead/" + this.key,
                    remoteDefaultOpts : $.extend(opt.filter || {}, { max : 50})
                })
            }
            this.$el.typeahead(typeAheadOpt);
            if(this.$el.data("publishevt")) {
                App.logDebug(this.field + " is set to publish event.");
                this.publishSearch = true;
            }
            if(opt.publishSearch) {
                this.publishSearch = opt.publishSearch;
                App.logDebug(this.field + " is set to publish event. override : " + this.publishSearch);
            }
        },
        remove: function() {
            if(this.$el.data('typeahead')) {
                this.$el.data('typeahead').destroy();
            }
            return App.View.prototype.remove.apply(this, arguments);
        }
    });

})(jQuery, Backbone, _, App);

<commit_msg>Handle reset of typeAhead<commit_after>// bootstrap 3.1.1
// + datetimepicker 3.0.0 https://github.com/Eonasdan/bootstrap-datetimepicker http://eonasdan.github.io/bootstrap-datetimepicker
// + typeahead https://github.com/bassjobsen/Bootstrap-3-Typeahead
//
//
//= require app/settings
//= require jquery/jquery-${org.codehaus.groovy.grails.plugins.jquery.JQueryConfig.SHIPPED_VERSION}.min
//= require bootstrap3-typeahead
//= require imms.backbone.pack
//= require_self

(function($, Backbone, _, App){

    App.view.TypeAhead = App.View.extend({
        publishSearch : false,
        events : {
            "change" : "onSelect"
        },
        reset : function() { //TODO: test
            App.logDebug("reset value on " + this.key);
            this.$el.removeData("selected-value");
            this.$el.val('').change();
            if(this.$values != undefined) {
                this.$values.children("input").each(function(){
                    var $this = $(this);
                    if($this.data("field")) $this.val('');
                });
            }
        },
        onSelect : function() {
            var item = this.$el.data("selected-value");
            if(this.$values != undefined) {
                this.$values.children("input").each(function(){
                    var $this = $(this);
                    if($this.data("field")) {
                        var val = item ? item[$this.data("field")] : null;
                        if(val) {
                            $this.val(val);
                        }
                        else {
                            if(item) App.logErr("Warning. data not exist for field: " + $this.data("field"));
                        }
                    }
                });
            }
            if(item) {
                this.onValidSelectedItem(item);
                if(this.publishSearch) this.publishEvt("ta:search:" + this.field, item);
            }
        },
        onValidSelectedItem : function(item) {
            // to override from opt
        },
        // <input class=".type-ahead" id="assetInstance-type" data-field="type" data-domain="assetType" data-display-key='value' data-items='all' data-minLength='2'/>
        // <div id="type-values">
        //      <input type="hidden" name="typeId" data-field="id">
        //      <input type="hidden" name="typeCd" data=field="code">
        // </div>
        initialize: function(opt) {
            this.field = this.$el.data("field"); // fieldName
            this.key = this.$el.data("domain"); //domain class
            this.$values = this.$el.parent().find("#"+this.field + "-values");
            this.displayKey = this.$el.data("display-key") || "value";
            this.name = opt.name || this.key;
            this.onSelect = opt.onSelect || this.onSelect;
            this.onValidSelectedItem = opt.onValidSelectedItem || this.onValidSelectedItem;
            this.$el.attr('autocomplete', 'off');
            //http://stackoverflow.com/questions/14901535/bootstrap-typeahead-ajax-result-format-example/14959406#14959406
            //http://tatiyants.com/how-to-use-json-objects-with-twitter-bootstrap-typeahead/
            var typeAheadOpt = {};
            if(this.$el.data('items')) { typeAheadOpt.items = this.$el.data('items') }
            if(this.$el.data('minlength')) { typeAheadOpt.minLength = this.$el.data('minlength') }
            if(!this.$el.data("source")) {
                $.extend(typeAheadOpt, {
                    isObjectItem : true, displayKey : this.displayKey, autoSelect : false,
                    remoteUrl : this.$el.data("sourceurl") || App.url + "/typeAhead/" + this.key,
                    remoteDefaultOpts : $.extend(opt.filter || {}, { max : 50})
                })
            }
            this.$el.typeahead(typeAheadOpt);
            if(this.$el.data("publishevt")) {
                App.logDebug(this.field + " is set to publish event.");
                this.publishSearch = true;
            }
            if(opt.publishSearch) {
                this.publishSearch = opt.publishSearch;
                App.logDebug(this.field + " is set to publish event. override : " + this.publishSearch);
            }
            this.$el.on('keyup', $.proxy(this.checkOnDelete, this)); // make sure it's reset.
        },
        checkOnDelete : function(e) {
            switch(e.keyCode) {
                case 8: // backspace
                case 46: // delete
                var $el = $(e.currentTarget);
                    if($el.val() == "") {
                        this.reset();
                    }
                break;
                default:
                break;
            }
        },
        remove: function() {
            if(this.$el.data('typeahead')) {
                this.$el.data('typeahead').destroy();
            }
            return App.View.prototype.remove.apply(this, arguments);
        }
    });

})(jQuery, Backbone, _, App);


{'fraction_non_alphanumeric': 0.15766670189157772, 'fraction_numerical': 0.007714255521504808, 'mean_word_length': 1.6347438752783965, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 4, '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': '1456432', 'n_tokens_mistral': 2724, 'n_tokens_neox': 2575, 'n_words': 402}