<commit_before>/**
 * Developer: <NAME>
 * Date: 1/28/2015
 * Copyright: 2009-2015 Comindware®
 *       All Rights Reserved
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Comindware
 *       The copyright notice above does not evidence any
 *       actual or intended publication of such source code.
 */

/* global define, require, Handlebars, Backbone, Marionette, $, _ */

define([
        'module/lib',
        'core/dropdown/dropdownApi',
        'core/utils/utilsApi',
        'text!./templates/memberSelectEditor.html',
        './base/BaseLayoutEditorView',
        'core/serviceLocator',
        './impl/memberSelect/views/DefaultButtonView',
        './impl/memberSelect/views/PanelView',
        './impl/common/members/models/MemberModel',
        './impl/common/members/collections/MembersCollection'
    ],
    function (
        lib,
        dropdown,
        utils,
        template,
        BaseLayoutEditorView,
        serviceLocator,
        DefaultButtonView,
        PanelView,
        MemberModel,
        MembersCollection
    ) {
        'use strict';

        var defaultOptions = {
            dropdownOptions: {
                buttonView: DefaultButtonView,
                popoutFlow: 'right',
                customAnchor: true
            }
        };

        var ButtonModel = Backbone.AssociatedModel.extend({
            relations: [
                {
                    type: Backbone.One,
                    key: 'member',
                    relatedModel: MemberModel
                }
            ]
        });

        Backbone.Form.editors.MemberSelect = BaseLayoutEditorView.extend({
            initialize: function (options) {
                var defOpts = _.cloneDeep(defaultOptions);
                if (options.schema) {
                    _.extend(this.options, defOpts, _.pick(options.schema, _.keys(defOpts)));
                } else {
                    _.extend(this.options, defOpts, _.pick(options || {}, _.keys(defOpts)));
                }
                this.options.dropdownOptions = _.extend(defOpts.dropdownOptions, this.options.dropdownOptions);

                this.controller = options.controller;

                this.reqres = new Backbone.Wreqr.RequestResponse();
                this.reqres.setHandler('value:clear', this.onValueClear, this);
                this.reqres.setHandler('value:set', this.onValueSet, this);
                this.reqres.setHandler('value:navigate', this.onValueNavigate, this);
                this.reqres.setHandler('filter:text', this.onFilterText, this);
                this.reqres.setHandler('panel:open', this.onPanelOpen, this);

                this.viewModel = new Backbone.Model({
                    button: new ButtonModel({
                        enabled: this.getEnabled() && !this.getReadonly()
                    }),
                    panel: new Backbone.Model({
                    })
                });

                this.__initCollection();
            },

            focusElement: null,

            attributes: {
                tabindex: 0
            },

            regions: {
                dropdownRegion: '.js-dropdown-region'
            },

            className: 'users-list',

            template: Handlebars.compile(template),

            setValue: function (value) {
                this.__value(_.isArray(value) ? (value.length ? value[0] : null) : value, false);
            },

            onRender: function () {
                // dropdown
                var dropdownOptions = _.extendDeep({
                    buttonViewOptions: {
                        model: this.viewModel.get('button'),
                        reqres: this.reqres
                    },
                    panelView: PanelView,
                    panelViewOptions: {
                        model: this.viewModel.get('panel'),
                        reqres: this.reqres
                    },
                    autoOpen: false
                }, this.options.dropdownOptions);
                this.dropdownView = dropdown.factory.createPopout(dropdownOptions);
                this.dropdownRegion.show(this.dropdownView);
                // hotkeys
                if (this.keyListener) {
                    this.keyListener.reset();
                }
                this.keyListener = new lib.keypress.Listener(this.el);
                _.each('down,enter,num_enter'.split(','), function (key) {
                    this.keyListener.simple_combo(key, function () {
                        if (this.getEnabled() && !this.getReadonly()) {
                            this.dropdownView.open();
                        }
                    }.bind(this));
                }, this);
            },

            __value: function (value, triggerChange) {
                if (this.value === value) {
                    return;
                }
                this.value = value;
                this.viewModel.get('button').set('member', this.__findModel(value));
                if (triggerChange) {
                    this.__triggerChange();
                }
            },

            onValueClear: function () {
                this.__value(null, true);
            },

            onValueSet: function (value) {
                this.__value(value, true);
                this.dropdownView.close();
                this.$el.focus();
            },

            onValueNavigate: function () {
            },

            onFilterText: function (options) {
                var deferred = $.Deferred();
                var text = options.text.toLocaleLowerCase();
                this.collection.unhighlight();
                if (text === '') {
                    this.collection.filter(null);
                } else {
                    this.collection.filter(function (model) {
                        var fullName = (model.get('fullName') || '').toLocaleLowerCase();
                        return fullName.indexOf(text) !== -1;
                    });
                    this.collection.highlight(text);
                }
                deferred.resolve();
                return deferred.promise();
            },

            onPanelOpen: function () {
                if (this.getEnabled() && !this.getReadonly()) {
                    this.dropdownView.open();
                }
            },

            __initCollection: function () {
                serviceLocator.cacheService.ListUsers().then(function (users) {
                    this.collection = new MembersCollection(new Backbone.Collection(users, {
                        model: MemberModel
                    }), {
                        comparator: utils.helpers.comparatorFor(utils.comparators.stringComparator2Asc, 'fullName')
                    });
                    this.viewModel.get('button').set('member', this.__findModel(this.getValue()));
                    this.viewModel.get('panel').set('collection', this.collection);
                }.bind(this));
            },

            __findModel: function (value) {
                return this.collection.findWhere({ id: value });
            },

            __setEnabled: function (enabled) {
                BaseLayoutEditorView.prototype.__setEnabled.call(this, enabled);
                this.viewModel.get('button').set('enabled', this.getEnabled() && !this.getReadonly());
            },

            __setReadonly: function (readonly) {
                BaseLayoutEditorView.prototype.__setReadonly.call(this, readonly);
                this.viewModel.get('button').set('enabled', this.getEnabled() && !this.getReadonly());
            }
        });

        return Backbone.Form.editors.MemberSelect;
    });
<commit_msg>case management:subtasks<commit_after>/**
 * Developer: <NAME>
 * Date: 1/28/2015
 * Copyright: 2009-2015 Comindware®
 *       All Rights Reserved
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Comindware
 *       The copyright notice above does not evidence any
 *       actual or intended publication of such source code.
 */

/* global define, require, Handlebars, Backbone, Marionette, $, _ */

define([
        'module/lib',
        'core/dropdown/dropdownApi',
        'core/utils/utilsApi',
        'text!./templates/memberSelectEditor.html',
        './base/BaseLayoutEditorView',
        'core/serviceLocator',
        './impl/memberSelect/views/DefaultButtonView',
        './impl/memberSelect/views/PanelView',
        './impl/common/members/models/MemberModel',
        './impl/common/members/collections/MembersCollection'
    ],
    function (
        lib,
        dropdown,
        utils,
        template,
        BaseLayoutEditorView,
        serviceLocator,
        DefaultButtonView,
        PanelView,
        MemberModel,
        MembersCollection
    ) {
        'use strict';

        var defaultOptions = {
            dropdownOptions: {
                buttonView: DefaultButtonView,
                popoutFlow: 'right',
                customAnchor: true
            }
        };

        var ButtonModel = Backbone.AssociatedModel.extend({
            relations: [
                {
                    type: Backbone.One,
                    key: 'member',
                    relatedModel: MemberModel
                }
            ]
        });

        Backbone.Form.editors.MemberSelect = BaseLayoutEditorView.extend({
            initialize: function (options) {
                var defOpts = _.cloneDeep(defaultOptions);
                if (options.schema) {
                    _.extend(this.options, defOpts, _.pick(options.schema, _.keys(defOpts)));
                } else {
                    _.extend(this.options, defOpts, _.pick(options || {}, _.keys(defOpts)));
                }
                this.options.dropdownOptions = _.extend(defOpts.dropdownOptions, this.options.dropdownOptions);

                this.controller = options.controller;

                this.reqres = new Backbone.Wreqr.RequestResponse();
                this.reqres.setHandler('value:clear', this.onValueClear, this);
                this.reqres.setHandler('value:set', this.onValueSet, this);
                this.reqres.setHandler('value:navigate', this.onValueNavigate, this);
                this.reqres.setHandler('filter:text', this.onFilterText, this);
                this.reqres.setHandler('panel:open', this.onPanelOpen, this);

                this.viewModel = new Backbone.Model({
                    button: new ButtonModel({
                        enabled: this.getEnabled() && !this.getReadonly()
                    }),
                    panel: new Backbone.Model({
                    })
                });

                this.__initCollection();
            },

            focusElement: null,

            attributes: {
                tabindex: 0
            },

            regions: {
                dropdownRegion: '.js-dropdown-region'
            },

            className: 'users-list',

            template: Handlebars.compile(template),

            setValue: function (value) {
                this.__value(_.isArray(value) ? (value.length ? value[0] : null) : value, false);
            },

            onRender: function () {
                // dropdown
                var dropdownOptions = _.extendDeep({
                    buttonViewOptions: {
                        model: this.viewModel.get('button'),
                        reqres: this.reqres
                    },
                    panelView: PanelView,
                    panelViewOptions: {
                        model: this.viewModel.get('panel'),
                        reqres: this.reqres
                    },
                    autoOpen: false
                }, this.options.dropdownOptions);
                this.dropdownView = dropdown.factory.createPopout(dropdownOptions);
                this.dropdownRegion.show(this.dropdownView);
                // hotkeys
                if (this.keyListener) {
                    this.keyListener.reset();
                }
                this.keyListener = new lib.keypress.Listener(this.el);
                _.each('down,enter,num_enter'.split(','), function (key) {
                    this.keyListener.simple_combo(key, function () {
                        if (this.getEnabled() && !this.getReadonly()) {
                            this.dropdownView.open();
                        }
                    }.bind(this));
                }, this);
            },

            __value: function (value, triggerChange) {
                if (this.value === value) {
                    return;
                }
                this.value = value;
                this.viewModel.get('button').set('member', this.__findModel(value));
                if (triggerChange) {
                    this.__triggerChange();
                }
            },

            onValueClear: function () {
                this.__value(null, true);
            },

            onValueSet: function (value) {
                this.__value(value, true);
                this.dropdownView.close();
                this.$el.focus();
            },

            onValueNavigate: function () {
            },

            onFilterText: function (options) {
                var deferred = $.Deferred();
                var text = options.text.toLocaleLowerCase();
                this.collection.unhighlight();
                if (text === '') {
                    this.collection.filter(null);
                } else {
                    this.collection.filter(function (model) {
                        var fullName = (model.get('fullName') || '').toLocaleLowerCase();
                        return fullName.indexOf(text) !== -1;
                    });
                    this.collection.highlight(text);
                }
                deferred.resolve();
                return deferred.promise();
            },

            onPanelOpen: function () {
                if (this.getEnabled() && !this.getReadonly()) {
                    this.dropdownView.open();
                }
            },

            __initCollection: function() {
                var users = serviceLocator.cacheService.ListUsers();
                this.collection = new MembersCollection(new Backbone.Collection(users, {
                    model: MemberModel
                }), {
                    comparator: utils.helpers.comparatorFor(utils.comparators.stringComparator2Asc, 'fullName')
                });
                this.viewModel.get('button').set('member', this.__findModel(this.getValue()));
                this.viewModel.get('panel').set('collection', this.collection);
            },

            __findModel: function (value) {
                return this.collection.findWhere({ id: value });
            },

            __setEnabled: function (enabled) {
                BaseLayoutEditorView.prototype.__setEnabled.call(this, enabled);
                this.viewModel.get('button').set('enabled', this.getEnabled() && !this.getReadonly());
            },

            __setReadonly: function (readonly) {
                BaseLayoutEditorView.prototype.__setReadonly.call(this, readonly);
                this.viewModel.get('button').set('enabled', this.getEnabled() && !this.getReadonly());
            }
        });

        return Backbone.Form.editors.MemberSelect;
    });

{'fraction_non_alphanumeric': 0.10990682639285035, 'fraction_numerical': 0.0024085694365215187, 'mean_word_length': 1.2899854862119013, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3938146', 'n_tokens_mistral': 3964, 'n_tokens_neox': 3566, 'n_words': 613}