<commit_before>/*jslint nomen:true*/
/*global define*/
define([
    'backbone',
    'underscore',
    'orotranslation/js/translator',
    './collection',
    './model',
    'oroui/js/modal',
    'oroui/js/mediator'
], function (Backbone, _, __, GridViewsCollection, GridViewModel, Modal, mediator) {
    'use strict';
    var $, GridViewsView;
    $ = Backbone.$;

    /**
     * Datagrid views widget
     *
     * @export  orodatagrid/js/datagrid/grid-views/view
     * @class   orodatagrid.datagrid.GridViewsView
     * @extends Backbone.View
     */
    GridViewsView = Backbone.View.extend({
        className: 'btn-toolbar grid-views pull-left',

        /** @property */
        events: {
            "click .views-group a": "onChange",
            "click a.save": "onSave",
            "click a.save_as": "onSaveAs",
            "click a.share": "onShare",
            "click a.delete": "onDelete"
        },

        /** @property */
        template: _.template(
            '<div class="btn-group views-group">' +
                '<button data-toggle="dropdown" class="btn dropdown-toggle <% if (disabled) { %>disabled<% } %>">' +
                    '<%=  current %>' + '<span class="caret"></span>' +
                '</button>' +
                '<ul class="dropdown-menu pull-right">' +
                    '<% _.each(choices, function (choice) { %>' +
                        '<li><a href="#" data-value="' + '<%= choice.value %>' + '">' + '<%= choice.label %>' + '</a></li>' +
                    '<% }); %>' +
                '</ul>' +
            '</div>' +
            '<% if (showActions) { %>' +
                '<div class="btn-group actions-group">' +
                    '<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">' +
                        '<%= actionsLabel %><span class="caret">' +
                    '</a>' +
                    '<ul class="dropdown-menu">' +
                        '<% _.each(actions, function(action) { %>' +
                            '<% if (action.enabled) { %>' +
                                '<li><a href="#" class="<%= action.name %>"><%= action.label %></a></li>' +
                            '<% } %>' +
                        '<% }); %>' +
                    '</ul>' +
                '</div>' +
            '<% } %>'
        ),

        /** @property */
        enabled: true,

        /** @property */
        choices: [],

        /** @property */
        permissions: {
            CREATE: false,
            EDIT: false,
            DELETE: false,
            SHARE: false,
            EDIT_SHARED: false
        },

        /** @property */
        viewsCollection: GridViewsCollection,

        /**
         * Initializer.
         *
         * @param {Object} options
         * @param {Backbone.Collection} options.collection
         * @param {Boolean} [options.enable]
         * @param {Array}   [options.choices]
         * @param {Array}   [options.views]
         */
        initialize: function (options) {
            options = options || {};

            if (!options.collection) {
                throw new TypeError("'collection' is required");
            }

            if (options.choices) {
                this.choices = _.union(this.choices, options.choices);
            }

            if (options.permissions) {
                this.permissions = _.extend(this.permissions, options.permissions);
            }

            this.collection = options.collection;
            this.enabled = options.enable != false;

            this.listenTo(this.collection, "updateState", this.render);
            this.listenTo(this.collection, "beforeFetch", this.render);

            options.views = options.views || [];
            _.each(options.views, function(view) {
                view.grid_name = options.collection.inputName;
            });

            this.viewsCollection = new this.viewsCollection(options.views);

            this.listenTo(this.viewsCollection, 'add', this._onModelAdd);
            this.listenTo(this.viewsCollection, 'remove', this._onModelRemove);

            GridViewsView.__super__.initialize.call(this, options);
        },

        /**
         * @inheritDoc
         */
        dispose: function () {
            if (this.disposed) {
                return;
            }
            this.viewsCollection.dispose();
            delete this.viewsCollection;
            GridViewsView.__super__.dispose.call(this);
        },

        /**
         * Disable view selector
         *
         * @return {*}
         */
        disable: function () {
            this.enabled = false;
            this.render();

            return this;
        },

        /**
         * Enable view selector
         *
         * @return {*}
         */
        enable: function () {
            this.enabled = true;
            this.render();

            return this;
        },

        /**
         * Select change event handler
         *
         * @param {Event} e
         */
        onChange: function (e) {
            e.preventDefault();
            var value = $(e.target).data('value');
            if (value !== this.collection.state.gridView) {
                this.changeView(value);
            }
        },

        /**
         * @param {Event} e
         */
        onSave: function(e) {
            var model = this._getCurrentViewModel();

            model.save({
                label: this._getCurrentViewLabel(),
                filters: this.collection.state.filters,
                sorters: this.collection.state.sorters
            }, {
                wait: true
            });

            model.once('sync', function() {
                mediator.execute('showFlashMessage', 'success', __('oro.datagrid.gridView.updated'));
            });
        },

        /**
         * @param {Event} e
         */
        onSaveAs: function(e) {
            var modal = new Modal({
                title: 'Filter configuration',
                content: '<label for="gridViewName"><strong>' + __('oro.datagrid.gridView.name') + ':</strong></label>' +
                         '<input id="gridViewName" name="name" type="text">'
            });

            var self = this;
            modal.on('ok', function(e) {
                var model = new GridViewModel({
                    label: this.$('input[name=name]').val(),
                    type: 'private',
                    grid_name: self.collection.inputName,
                    filters: self.collection.state.filters,
                    sorters: self.collection.state.sorters
                });
                model.save(null, {
                    wait: true
                });
                model.once('sync', function(model) {
                    this.viewsCollection.add(model);
                }, self);
            });

            modal.open();
        },
<commit_msg>BAP-7673: Use horizontal form<commit_after>/*jslint nomen:true*/
/*global define*/
define([
    'backbone',
    'underscore',
    'orotranslation/js/translator',
    './collection',
    './model',
    'oroui/js/modal',
    'oroui/js/mediator'
], function (Backbone, _, __, GridViewsCollection, GridViewModel, Modal, mediator) {
    'use strict';
    var $, GridViewsView;
    $ = Backbone.$;

    /**
     * Datagrid views widget
     *
     * @export  orodatagrid/js/datagrid/grid-views/view
     * @class   orodatagrid.datagrid.GridViewsView
     * @extends Backbone.View
     */
    GridViewsView = Backbone.View.extend({
        className: 'btn-toolbar grid-views pull-left',

        /** @property */
        events: {
            "click .views-group a": "onChange",
            "click a.save": "onSave",
            "click a.save_as": "onSaveAs",
            "click a.share": "onShare",
            "click a.delete": "onDelete"
        },

        /** @property */
        template: _.template(
            '<div class="btn-group views-group">' +
                '<button data-toggle="dropdown" class="btn dropdown-toggle <% if (disabled) { %>disabled<% } %>">' +
                    '<%=  current %>' + '<span class="caret"></span>' +
                '</button>' +
                '<ul class="dropdown-menu pull-right">' +
                    '<% _.each(choices, function (choice) { %>' +
                        '<li><a href="#" data-value="' + '<%= choice.value %>' + '">' + '<%= choice.label %>' + '</a></li>' +
                    '<% }); %>' +
                '</ul>' +
            '</div>' +
            '<% if (showActions) { %>' +
                '<div class="btn-group actions-group">' +
                    '<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">' +
                        '<%= actionsLabel %><span class="caret">' +
                    '</a>' +
                    '<ul class="dropdown-menu">' +
                        '<% _.each(actions, function(action) { %>' +
                            '<% if (action.enabled) { %>' +
                                '<li><a href="#" class="<%= action.name %>"><%= action.label %></a></li>' +
                            '<% } %>' +
                        '<% }); %>' +
                    '</ul>' +
                '</div>' +
            '<% } %>'
        ),

        /** @property */
        enabled: true,

        /** @property */
        choices: [],

        /** @property */
        permissions: {
            CREATE: false,
            EDIT: false,
            DELETE: false,
            SHARE: false,
            EDIT_SHARED: false
        },

        /** @property */
        viewsCollection: GridViewsCollection,

        /**
         * Initializer.
         *
         * @param {Object} options
         * @param {Backbone.Collection} options.collection
         * @param {Boolean} [options.enable]
         * @param {Array}   [options.choices]
         * @param {Array}   [options.views]
         */
        initialize: function (options) {
            options = options || {};

            if (!options.collection) {
                throw new TypeError("'collection' is required");
            }

            if (options.choices) {
                this.choices = _.union(this.choices, options.choices);
            }

            if (options.permissions) {
                this.permissions = _.extend(this.permissions, options.permissions);
            }

            this.collection = options.collection;
            this.enabled = options.enable != false;

            this.listenTo(this.collection, "updateState", this.render);
            this.listenTo(this.collection, "beforeFetch", this.render);

            options.views = options.views || [];
            _.each(options.views, function(view) {
                view.grid_name = options.collection.inputName;
            });

            this.viewsCollection = new this.viewsCollection(options.views);

            this.listenTo(this.viewsCollection, 'add', this._onModelAdd);
            this.listenTo(this.viewsCollection, 'remove', this._onModelRemove);

            GridViewsView.__super__.initialize.call(this, options);
        },

        /**
         * @inheritDoc
         */
        dispose: function () {
            if (this.disposed) {
                return;
            }
            this.viewsCollection.dispose();
            delete this.viewsCollection;
            GridViewsView.__super__.dispose.call(this);
        },

        /**
         * Disable view selector
         *
         * @return {*}
         */
        disable: function () {
            this.enabled = false;
            this.render();

            return this;
        },

        /**
         * Enable view selector
         *
         * @return {*}
         */
        enable: function () {
            this.enabled = true;
            this.render();

            return this;
        },

        /**
         * Select change event handler
         *
         * @param {Event} e
         */
        onChange: function (e) {
            e.preventDefault();
            var value = $(e.target).data('value');
            if (value !== this.collection.state.gridView) {
                this.changeView(value);
            }
        },

        /**
         * @param {Event} e
         */
        onSave: function(e) {
            var model = this._getCurrentViewModel();

            model.save({
                label: this._getCurrentViewLabel(),
                filters: this.collection.state.filters,
                sorters: this.collection.state.sorters
            }, {
                wait: true
            });

            model.once('sync', function() {
                mediator.execute('showFlashMessage', 'success', __('oro.datagrid.gridView.updated'));
            });
        },

        /**
         * @param {Event} e
         */
        onSaveAs: function(e) {
            var modal = new Modal({
                title: 'Filter configuration',
                content: '<div class="form-horizontal">' +
                            '<div class="control-group">' +
                                '<label class="control-label" for="gridViewName">' + __('oro.datagrid.gridView.name') + ':</label>' +
                                '<div class="controls">' +
                                    '<input id="gridViewName" name="name" type="text">' +
                                '</div>' +
                            '</div>' +
                         '</div>'
            });

            var self = this;
            modal.on('ok', function(e) {
                var model = new GridViewModel({
                    label: this.$('input[name=name]').val(),
                    type: 'private',
                    grid_name: self.collection.inputName,
                    filters: self.collection.state.filters,
                    sorters: self.collection.state.sorters
                });
                model.save(null, {
                    wait: true
                });
                model.once('sync', function(model) {
                    this.viewsCollection.add(model);
                }, self);
            });

            modal.open();
        },

{'fraction_non_alphanumeric': 0.1498193667209747, 'fraction_numerical': 0.0002833463200396685, 'mean_word_length': 1.306109114668409, 'pattern_counts': {'":': 10, '<': 95, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6746492', 'n_tokens_mistral': 3802, 'n_tokens_neox': 3651, 'n_words': 655}