<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 imms.datatable.pack
//= require imms.form.typeahead
//= require imms.form.datepicker
//= require select2.min
//= require_self

(function($, Backbone, _, App){
    App.template.ManyToManyItem = _.template("<div class='item col-md-2'><button type='button' class='btn btn-default' disabled='disabled'><span class='glyphicon glyphicon-ok'></span> <%= text %></button></div>");

    App.view.ManyToManyView = App.View.extend({
        searchView : null, searchEl : null, listEl : null, removeBtnEl : null,
        mappingClass : null, parentField : null, parentId : null, childField : null,
        items : [], toRemove : [],
        readOnly : true,
        localPubSub : _.extend({},Backbone.Events),
        remove: function() {
            if(this.searchView) this.searchView.remove();
            this.removeAllItemsFromView();
            return App.View.prototype.remove.apply(this, arguments);
        },
        initialize: function(opt) {
            if(opt.readOnly) this.readOnly = opt.readOnly || this.$el.data("readonly");
            this.mappingClass = opt.mappingClass || this.$el.data("mappingclass");
            this.parentField = opt.parentField || this.$el.data("parentfield"); this.parentId = opt.parentId || this.$el.data("parentid");
            this.childField = opt.childField || this.$el.data("childfield");
            this.listEl = opt.listEl || (this.$el.selector + " #" + this.$(".list-item").id);
            this.removeBtnEl = opt.removeBtnEl || (this.$el.selector + " .remove-item");
            if(!this.readOnly) {
                this.queryListUrl = opt.queryListUrl ||(App.url + '/manyToMany/list');
                this.addItemUrl = opt.addItemUrl ||(App.url + '/manyToMany/addItem');
                this.removeItemUrl = opt.removeItemUrl ||(App.url + '/manyToMany/removeItem');
                this.searchEl = opt.searchEl || (this.$el.selector + " #" + this.$(".type-ahead").id) ;
                this.searchView = new App.view.TypeAhead({ el : this.searchEl, pubSub : this.localPubSub, publishSearch : true });
                this.subscribeEvt("ta:search", this.onSelectedItem);
            }
            this.populateItems();
        },
        onRemoveItems : function() {
            this.$(this.removeBtnEl).hide();
        },
        onSelectedItem : function(item) {
            var index = this.getIndexOf(item);
            if(index >= 0) {
                //TODO: toggle selection, show delete button. hide search button
                this.$(this.listEl + " .item:eq(" + index + ")").toggleClass(".selected");
                if(this.items.length > 0) {
                    this.$(this.removeBtnEl).show();
                }
                else {
                    this.$(this.removeBtnEl).hide();
                }

            } else {
                this.items.push(item);
                this.renderItem(item);
            }
            this.searchView.reset();
        },
        populateItems : function() {
            var options = {parentField : this.parentField, parentId: this.parentId, mappingClass : this.mappingClass, childField : this.childField};
            this.getJSON(this.queryListUrl, options, function(allItems) {
                this.items = allItems || [];
                this.removeAllItemsFromView();
                for(var i=0;i<this.items.length;i++) {
                    this.renderItem(this.items[i]);
                }
            });
        },
        postJSON : function(url, option, callback) {
            return $.ajax({
                type: "POST",
                url: url,
                data: option || {},
                success: callback || function(){},
                dataType: "json",
                context : this // make sure this BB view is the context
            });
        },
        getJSON : function(url, option, callback) {
            return $.ajax({
                type: "GET",
                url: url,
                data: option || {},
                success: callback || function(){},
                dataType: "json",
                context : this // make sure this BB view is the context
            });
        },
        getIndexOf : function(item) {
            for(var i=0;i<this.items.length;i++) {
                if(item["id"] == this.items[i]["id"]) {
                    return i;
                }
            }
            return -1;
        },
        removeAllItemsFromView : function() {
            this.$(this.listEl + " .item").remove(); // TODO: make sure all item has class item
        },
        renderItem : function(item) {
            App.template.ManyToManyItem (item);
        }
    });

    App.view.ReadOnlyForm = App.View.extend({
        manyToManyFields : [], select2Els : [],
        events : {
            "submit form" : "ignoreSubmit",
            "click .buttons .btn" : "submitForm"
        },
        remove: function() {
            _.each(this.select2Els, function(elem){
                var $select2 = this.$(elem);
                if($select2) $select2.select2("destroy");
            }, this);
            return App.View.prototype.remove.apply(this, arguments);
        },
        ignoreSubmit : function() {
            var $btn = this.$(".buttons .btn:focus");
            if($btn && $btn.data("nojs")) {
                return true;
            }
            App.logDebug("ignoreSubmit");
            return false;
        },
        submitForm : function(evt) {
            var $btn = $(evt.currentTarget);
            $btn.attr('disabled','disabled');
            if($btn.data("nojs")) {
                var $form = $btn.closest("form");
                if($btn.data("url")) {
                    $form.attr("action", $btn.data("url"));
                }
                $form.submit();
                return false;
            }
            var form = this.serializeForm();
            if($btn.data("flag")) form._flag = $btn.data("flag");
            var actionUrl = $btn.data("url") || form.action;
            App.logDebug("actionUrl " + actionUrl +", action " + $btn.data("action"));
            if($btn.data("action") && actionUrl) { // not yet tested, must alter the UI<commit_msg>make sure disabled attr removed in all cases<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 imms.datatable.pack
//= require imms.form.typeahead
//= require imms.form.datepicker
//= require select2.min
//= require_self

(function($, Backbone, _, App){
    App.template.ManyToManyItem = _.template("<div class='item col-md-2'><button type='button' class='btn btn-default' disabled='disabled'><span class='glyphicon glyphicon-ok'></span> <%= text %></button></div>");

    App.view.ManyToManyView = App.View.extend({
        searchView : null, searchEl : null, listEl : null, removeBtnEl : null,
        mappingClass : null, parentField : null, parentId : null, childField : null,
        items : [], toRemove : [],
        readOnly : true,
        localPubSub : _.extend({},Backbone.Events),
        remove: function() {
            if(this.searchView) this.searchView.remove();
            this.removeAllItemsFromView();
            return App.View.prototype.remove.apply(this, arguments);
        },
        initialize: function(opt) {
            if(opt.readOnly) this.readOnly = opt.readOnly || this.$el.data("readonly");
            this.mappingClass = opt.mappingClass || this.$el.data("mappingclass");
            this.parentField = opt.parentField || this.$el.data("parentfield"); this.parentId = opt.parentId || this.$el.data("parentid");
            this.childField = opt.childField || this.$el.data("childfield");
            this.listEl = opt.listEl || (this.$el.selector + " #" + this.$(".list-item").id);
            this.removeBtnEl = opt.removeBtnEl || (this.$el.selector + " .remove-item");
            if(!this.readOnly) {
                this.queryListUrl = opt.queryListUrl ||(App.url + '/manyToMany/list');
                this.addItemUrl = opt.addItemUrl ||(App.url + '/manyToMany/addItem');
                this.removeItemUrl = opt.removeItemUrl ||(App.url + '/manyToMany/removeItem');
                this.searchEl = opt.searchEl || (this.$el.selector + " #" + this.$(".type-ahead").id) ;
                this.searchView = new App.view.TypeAhead({ el : this.searchEl, pubSub : this.localPubSub, publishSearch : true });
                this.subscribeEvt("ta:search", this.onSelectedItem);
            }
            this.populateItems();
        },
        onRemoveItems : function() {
            this.$(this.removeBtnEl).hide();
        },
        onSelectedItem : function(item) {
            var index = this.getIndexOf(item);
            if(index >= 0) {
                //TODO: toggle selection, show delete button. hide search button
                this.$(this.listEl + " .item:eq(" + index + ")").toggleClass(".selected");
                if(this.items.length > 0) {
                    this.$(this.removeBtnEl).show();
                }
                else {
                    this.$(this.removeBtnEl).hide();
                }

            } else {
                this.items.push(item);
                this.renderItem(item);
            }
            this.searchView.reset();
        },
        populateItems : function() {
            var options = {parentField : this.parentField, parentId: this.parentId, mappingClass : this.mappingClass, childField : this.childField};
            this.getJSON(this.queryListUrl, options, function(allItems) {
                this.items = allItems || [];
                this.removeAllItemsFromView();
                for(var i=0;i<this.items.length;i++) {
                    this.renderItem(this.items[i]);
                }
            });
        },
        postJSON : function(url, option, callback) {
            return $.ajax({
                type: "POST",
                url: url,
                data: option || {},
                success: callback || function(){},
                dataType: "json",
                context : this // make sure this BB view is the context
            });
        },
        getJSON : function(url, option, callback) {
            return $.ajax({
                type: "GET",
                url: url,
                data: option || {},
                success: callback || function(){},
                dataType: "json",
                context : this // make sure this BB view is the context
            });
        },
        getIndexOf : function(item) {
            for(var i=0;i<this.items.length;i++) {
                if(item["id"] == this.items[i]["id"]) {
                    return i;
                }
            }
            return -1;
        },
        removeAllItemsFromView : function() {
            this.$(this.listEl + " .item").remove(); // TODO: make sure all item has class item
        },
        renderItem : function(item) {
            App.template.ManyToManyItem (item);
        }
    });

    App.view.ReadOnlyForm = App.View.extend({
        manyToManyFields : [], select2Els : [],
        events : {
            "submit form" : "ignoreSubmit",
            "click .buttons .btn" : "submitForm"
        },
        remove: function() {
            _.each(this.select2Els, function(elem){
                var $select2 = this.$(elem);
                if($select2) $select2.select2("destroy");
            }, this);
            return App.View.prototype.remove.apply(this, arguments);
        },
        ignoreSubmit : function() {
            var $btn = this.$(".buttons .btn:focus");
            if($btn && $btn.data("nojs")) {
                return true;
            }
            App.logDebug("ignoreSubmit");
            return false;
        },
        submitForm : function(evt) {
            var $btn = $(evt.currentTarget);
            $btn.attr('disabled','disabled');
            if($btn.data("nojs")) {
                var $form = $btn.closest("form");
                if($btn.data("url")) {
                    $form.attr("action", $btn.data("url"));
                }
                $form.submit();
                $btn.removeAttr('disabled');
                return false;
            }
            var form = this.serializeForm();
            if($btn.data("flag")) form._flag = $btn.data("flag");
            var actionUrl = $btn.data("url") || form.action;
            App.logDebug("actionUrl " + actionUrl +", action " + $btn.data("action"));
            if($btn.data("action") && actionUrl) { // not yet tested, must alter the UI
{'fraction_non_alphanumeric': 0.15136265097553422, 'fraction_numerical': 0.003096934035305048, 'mean_word_length': 1.7216603455541508, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '970587', 'n_tokens_mistral': 3613, 'n_tokens_neox': 3483, 'n_words': 667}