<commit_before>
define([
    'flight/lib/component',
    'tpl!./form',
    'tpl!./shareRow',
    'tpl!./permissions',
    'util/users/userSelect',
    'util/withDataRequest'
], function(
    defineComponent,
    template,
    shareRowTemplate,
    permissionsTemplate,
    UserSelect,
    withDataRequest) {
    'use strict';

    return defineComponent(Form, withDataRequest);

    function Form() {

        this.defaultAttrs({
            titleSelector: '.workspace-title',
            shareListSelector: '.share-list',
            shareHeader: '.share-header',
            shareFormSelector: '.share-form',
            permissionsSelector: '.permissions',
            permissionsRadioSelector: '.popover input',
            permissionsRadioLabelSelector: '.popover label',
            deleteSelector: '.delete',
            removeAccessSelector: '.remove-access'
        });

        this.after('teardown', function() {
            $(document).off('click.permPopover');
        });

        this.after('initialize', function() {
            var self = this;

            this.on(document, 'userStatusChange', this.onUserStatusChange);

            this.editable = this.attr.data.editable;

            this.$node.html(template({
                workspace: this.attr.data,
                editable: this.editable
            }));
            this.loadUserPermissionsList();

            if (this.editable) {

                this.on('userSelected', function(event, data) {
                    if (data && data.user) {
                        self.trigger('shareWorkspaceWithUser', {
                            workspace: self.attr.data,
                            user: data.user
                        });
                        self.trigger(this.select('shareFormSelector'), 'clearUser');
                    }
                });

                UserSelect.attachTo(this.select('shareFormSelector'), {
                    filterUserIds: _.pluck(self.attr.data.users, 'userId'),
                    placeholder: i18n('workspaces.form.sharing.placeholder')
                });

                $(document).on('click.permPopover', function(event) {
                    var $target = $(event.target);

                    if ($target.closest('.permissions-list').length === 0) {
                        self.$node.find('.permissions-list').popover('hide');
                    }
                });
                this.on('shareWorkspaceWithUser', this.onShareWorkspaceWithUser);
                this.on('click', {
                    deleteSelector: this.onDelete,
                    removeAccessSelector: this.onRevokeAccess,
                    permissionsRadioLabelSelector: function(e) {
                        e.stopPropagation();
                    }
                });
                this.on('change', {
                    permissionsRadioSelector: this.onPermissionsChange
                });
                this.select('titleSelector').on('change keyup paste', this.onChangeTitle.bind(this));
            }
        });

        this.onUserStatusChange = function(event, user) {
            this.$node.find('.share-list > .user-row').each(function() {
                var $this = $(this);
                if ($this.data('userId') === user.id) {
                    $this.find('.user-status')
                        .removeClass('active idle offline unknown')
                        .addClass((user.status && user.status.toLowerCase()) || 'unknown');
                }
            })
        };

        var timeout;
        this.saveWorkspace = function(immediate, options) {
            var self = this,
                changes = options.changes,
                revert = options.revert,
                d = $.Deferred(),
                save = function() {
                    self.trigger(document, 'workspaceSaving', self.attr.data);

                    self.dataRequest('workspace', 'save', self.attr.data.workspaceId, changes)
                        .then(function(workspace) {
                            self.trigger(document, 'workspaceSaved', self.attr.data);
                            d.resolve({ workspace: self.attr.data });
                        })
                        .catch(function() {
                            self.attr.data = revert;
                            self.trigger(document, 'workspaceSaved', revert);
                            d.reject();
                        })
                }

            if (immediate) {
                save();
            } else {
                clearTimeout(timeout);
                timeout = setTimeout(function() {
                    save();
                }, 1000);
            }

            return d;
        };

        this.loadUserPermissionsList = function() {
            var self = this,
                workspace = this.attr.data,
                workspaceUsers = workspace.users || (workspace.users = []),
                userIds = _.pluck(workspaceUsers, 'userId'),
                html = $();

            this.dataRequest('user', 'search', { userIds: userIds })
                .done(function(users) {
                    var usersById = _.indexBy(users, 'id');
                    self.currentUsers = usersById;

                    _.sortBy(workspaceUsers, function(userPermission) {
                        var user = usersById[userPermission.userId];
                        return user && user.displayName || 1;
                    }).forEach(function(userPermission) {
                        if (userPermission.userId != lumifyData.currentUser.id) {
                            var data = self.shareRowDataForPermission(userPermission);
                            if (data) {
                                html = html.add(shareRowTemplate(data));
                            }
                        }
                    });
                    self.select('shareHeader').after(html).find('.loading').remove();
                    if (self.editable) {
                        self.select('shareFormSelector').show();
                        self.updatePopovers();
                    }

                })
        };
<commit_msg>Don't re-request for current user when opening form<commit_after>
define([
    'flight/lib/component',
    'tpl!./form',
    'tpl!./shareRow',
    'tpl!./permissions',
    'util/users/userSelect',
    'util/withDataRequest'
], function(
    defineComponent,
    template,
    shareRowTemplate,
    permissionsTemplate,
    UserSelect,
    withDataRequest) {
    'use strict';

    return defineComponent(Form, withDataRequest);

    function Form() {

        this.defaultAttrs({
            titleSelector: '.workspace-title',
            shareListSelector: '.share-list',
            shareHeader: '.share-header',
            shareFormSelector: '.share-form',
            permissionsSelector: '.permissions',
            permissionsRadioSelector: '.popover input',
            permissionsRadioLabelSelector: '.popover label',
            deleteSelector: '.delete',
            removeAccessSelector: '.remove-access'
        });

        this.after('teardown', function() {
            $(document).off('click.permPopover');
        });

        this.after('initialize', function() {
            var self = this;

            this.on(document, 'userStatusChange', this.onUserStatusChange);

            this.editable = this.attr.data.editable;

            this.$node.html(template({
                workspace: this.attr.data,
                editable: this.editable
            }));
            this.loadUserPermissionsList();

            if (this.editable) {

                this.on('userSelected', function(event, data) {
                    if (data && data.user) {
                        self.trigger('shareWorkspaceWithUser', {
                            workspace: self.attr.data,
                            user: data.user
                        });
                        self.trigger(this.select('shareFormSelector'), 'clearUser');
                    }
                });

                UserSelect.attachTo(this.select('shareFormSelector'), {
                    filterUserIds: _.pluck(self.attr.data.users, 'userId'),
                    placeholder: i18n('workspaces.form.sharing.placeholder')
                });

                $(document).on('click.permPopover', function(event) {
                    var $target = $(event.target);

                    if ($target.closest('.permissions-list').length === 0) {
                        self.$node.find('.permissions-list').popover('hide');
                    }
                });
                this.on('shareWorkspaceWithUser', this.onShareWorkspaceWithUser);
                this.on('click', {
                    deleteSelector: this.onDelete,
                    removeAccessSelector: this.onRevokeAccess,
                    permissionsRadioLabelSelector: function(e) {
                        e.stopPropagation();
                    }
                });
                this.on('change', {
                    permissionsRadioSelector: this.onPermissionsChange
                });
                this.select('titleSelector').on('change keyup paste', this.onChangeTitle.bind(this));
            }
        });

        this.onUserStatusChange = function(event, user) {
            this.$node.find('.share-list > .user-row').each(function() {
                var $this = $(this);
                if ($this.data('userId') === user.id) {
                    $this.find('.user-status')
                        .removeClass('active idle offline unknown')
                        .addClass((user.status && user.status.toLowerCase()) || 'unknown');
                }
            })
        };

        var timeout;
        this.saveWorkspace = function(immediate, options) {
            var self = this,
                changes = options.changes,
                revert = options.revert,
                d = $.Deferred(),
                save = function() {
                    self.trigger(document, 'workspaceSaving', self.attr.data);

                    self.dataRequest('workspace', 'save', self.attr.data.workspaceId, changes)
                        .then(function(workspace) {
                            self.trigger(document, 'workspaceSaved', self.attr.data);
                            d.resolve({ workspace: self.attr.data });
                        })
                        .catch(function() {
                            self.attr.data = revert;
                            self.trigger(document, 'workspaceSaved', revert);
                            d.reject();
                        })
                }

            if (immediate) {
                save();
            } else {
                clearTimeout(timeout);
                timeout = setTimeout(function() {
                    save();
                }, 1000);
            }

            return d;
        };

        this.loadUserPermissionsList = function() {
            var self = this,
                workspace = this.attr.data,
                workspaceUsers = workspace.users || (workspace.users = []),
                userIds = _.pluck(workspaceUsers, 'userId'),
                html = $();

            userIds = _.without(userIds, lumifyData.currentUser.id);

            (userIds.length ?
                this.dataRequest('user', 'search', { userIds: userIds }) :
                Promise.resolve([]))
                .done(function(users) {
                    var usersById = _.indexBy(users, 'id');
                    self.currentUsers = usersById;

                    _.sortBy(workspaceUsers, function(userPermission) {
                        var user = usersById[userPermission.userId];
                        return user && user.displayName || 1;
                    }).forEach(function(userPermission) {
                        if (userPermission.userId != lumifyData.currentUser.id) {
                            var data = self.shareRowDataForPermission(userPermission);
                            if (data) {
                                html = html.add(shareRowTemplate(data));
                            }
                        }
                    });
                    self.select('shareHeader').after(html).find('.loading').remove();
                    if (self.editable) {
                        self.select('shareFormSelector').show();
                        self.updatePopovers();
                    }

                })
        };

{'fraction_non_alphanumeric': 0.11853155242741742, 'fraction_numerical': 0.0012796928737103094, 'mean_word_length': 1.2846701991595104, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '129866', 'n_tokens_mistral': 3035, 'n_tokens_neox': 2866, 'n_words': 464}