<commit_before>/**
 * Developer: <NAME>
 * Date: 10/13/2014
 * Copyright: 2009-2014 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',
        'text!./templates/referenceEditor.html',
        './base/BaseLayoutEditorView',
        './impl/reference/views/ReferenceButtonView',
        './impl/reference/views/ReferencePanelView',
        './impl/reference/collections/BaseReferenceCollection',
        './impl/reference/models/SearchMoreModel',
        './impl/reference/models/DefaultReferenceModel'
    ],
    function (
        lib,
        dropdown,
        template,
        BaseLayoutEditorView,
        ReferenceButtonView,
        ReferencePanelView,
        ReferenceCollection,
        SearchMoreModel,
        DefaultReferenceModel
    ) {
        'use strict';

        var classes = {
        };

        var defaultOptions = {
            'controller': null
        };

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

                this.reqres = new Backbone.Wreqr.RequestResponse();

                this.controller = this.options.controller;

                this.value = this.__adjustValue(this.value);


                this.reqres.setHandler('panel:open', this.onPanelOpen, this);
                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('search:more', this.onSearchMore, this);
                this.reqres.setHandler('filter:text', this.onFilterText, this);

                this.viewModel = new Backbone.Model({
                    button: new Backbone.Model({
                        value: this.getValue(),
                        state: 'view',
                        enabled: this.getEnabled() && !this.getReadonly()
                    }),
                    panel: new Backbone.Model({
                        value: this.getValue(),
                        collection: this.controller.collection,
                        totalCount: this.controller.totalCount || 0
                    })
                });
            },

            focusElement: null,

            attributes: {
                tabindex: 0
            },

            ui: {
                searchMore: '.js-search-more-button'
            },

            events: {
                'click @ui.searchMore': '__searchMore'
            },

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

            className: 'pr-reference-editor',

            template: Handlebars.compile(template),

            setValue: function (value) {
                value = this.__adjustValue(value);
                this.__value(value, false);
            },

            onRender: function () {
                // dropdown
                this.dropdownView = dropdown.factory.createDropdown({
                    buttonView: ReferenceButtonView,
                    buttonViewOptions: {
                        model: this.viewModel.get('button'),
                        reqres: this.reqres
                    },
                    panelView: ReferencePanelView,
                    panelViewOptions: {
                        model: this.viewModel.get('panel'),
                        reqres: this.reqres
                    },
                    panelPosition: 'down-over',
                    autoOpen: false
                });
                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);
                this.$el.toggleClass('pr-empty', _.isEmpty(this.value));
            },

            __adjustValue: function (value) {
                if (!value || !value.id) {
                    return null;
                }
                if (value instanceof DefaultReferenceModel) {
                    return value;
                }
                if (value instanceof Backbone.Model) {
                    value = value.attributes;
                }

                return new DefaultReferenceModel(value);
            },

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

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

            onValueSet: function (model) {
                this.__value(model, true);
                this.dropdownView.close();
                this.$el.toggleClass('pr-empty', _.isEmpty(this.value));
                this.$el.focus();
            },

            onValueNavigate: function () {
                this.controller.navigate(this.getValue());
            },

            onSearchMore: function () {
                // TODO: Not implemented in Release 1
                this.dropdownView.close();
            },

            onFilterText: function (options) {
                var deferred = $.Deferred();
                this.controller.fetch(options).then(function () {
                    this.viewModel.get('panel').set('totalCount', this.controller.totalCount);
                    deferred.resolve();
                }.bind(this));
                return deferred.promise();
            },

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

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

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

            getDisplayAttribute: function() {
                return this.controller.originalCollection.options.displayAttribute;
            }
        });

        return Backbone.Form.editors.Reference;
    });
<commit_msg>Added reference field property "show as link".<commit_after>/**
 * Developer: <NAME>
 * Date: 10/13/2014
 * Copyright: 2009-2014 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',
        'text!./templates/referenceEditor.html',
        './base/BaseLayoutEditorView',
        './impl/reference/views/ReferenceButtonView',
        './impl/reference/views/ReferencePanelView',
        './impl/reference/collections/BaseReferenceCollection',
        './impl/reference/models/SearchMoreModel',
        './impl/reference/models/DefaultReferenceModel'
    ],
    function (
        lib,
        dropdown,
        template,
        BaseLayoutEditorView,
        ReferenceButtonView,
        ReferencePanelView,
        ReferenceCollection,
        SearchMoreModel,
        DefaultReferenceModel
    ) {
        'use strict';

        var classes = {
        };

        var defaultOptions = {
            'controller': null
        };

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

                this.reqres = new Backbone.Wreqr.RequestResponse();

                this.controller = this.options.controller;

                this.value = this.__adjustValue(this.value);


                this.reqres.setHandler('panel:open', this.onPanelOpen, this);
                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('search:more', this.onSearchMore, this);
                this.reqres.setHandler('filter:text', this.onFilterText, this);

                this.viewModel = new Backbone.Model({
                    button: new Backbone.Model({
                        value: this.getValue(),
                        state: 'view',
                        enabled: this.getEnabled() && !this.getReadonly()
                    }),
                    panel: new Backbone.Model({
                        value: this.getValue(),
                        collection: this.controller.collection,
                        totalCount: this.controller.totalCount || 0
                    })
                });
            },

            focusElement: null,

            attributes: {
                tabindex: 0
            },

            ui: {
                searchMore: '.js-search-more-button'
            },

            events: {
                'click @ui.searchMore': '__searchMore'
            },

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

            className: 'pr-reference-editor',

            template: Handlebars.compile(template),

            setValue: function (value) {
                value = this.__adjustValue(value);
                this.__value(value, false);
            },

            onRender: function () {
                // dropdown
                this.dropdownView = dropdown.factory.createDropdown({
                    buttonView: ReferenceButtonView,
                    buttonViewOptions: {
                        model: this.viewModel.get('button'),
                        reqres: this.reqres
                    },
                    panelView: ReferencePanelView,
                    panelViewOptions: {
                        model: this.viewModel.get('panel'),
                        reqres: this.reqres
                    },
                    panelPosition: 'down-over',
                    autoOpen: false
                });
                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);
                this.$el.toggleClass('pr-empty', _.isEmpty(this.value));
            },

            __adjustValue: function (value) {
                if (!value || !value.id) {
                    return null;
                }
                if (value instanceof DefaultReferenceModel) {
                    return value;
                }
                if (value instanceof Backbone.Model) {
                    value = value.attributes;
                }

                return new DefaultReferenceModel(value);
            },

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

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

            onValueSet: function (model) {
                this.__value(model, true);
                this.dropdownView.close();
                this.$el.toggleClass('pr-empty', _.isEmpty(this.value));
                this.$el.focus();
            },

            onValueNavigate: function () {
                if (!this.schema.showAsLink) {
                    return;
                }
                this.controller.navigate(this.getValue());
            },

            onSearchMore: function () {
                // TODO: Not implemented in Release 1
                this.dropdownView.close();
            },

            onFilterText: function (options) {
                var deferred = $.Deferred();
                this.controller.fetch(options).then(function () {
                    this.viewModel.get('panel').set('totalCount', this.controller.totalCount);
                    deferred.resolve();
                }.bind(this));
                return deferred.promise();
            },

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

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

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

            getDisplayAttribute: function() {
                return this.controller.originalCollection.options.displayAttribute;
            }
        });

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

{'fraction_non_alphanumeric': 0.11157881103484005, 'fraction_numerical': 0.002460821137158399, 'mean_word_length': 1.348745247148289, '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': '4873700', 'n_tokens_mistral': 3828, 'n_tokens_neox': 3510, 'n_words': 627}