<commit_before>// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

var UserStore = require('../stores/user_store.jsx');
var PostStore = require('../stores/post_store.jsx');
var AppDispatcher = require('../dispatcher/app_dispatcher.jsx');
var Mention = require('./mention.jsx');

var Constants = require('../utils/constants.jsx');
var ActionTypes = Constants.ActionTypes;

var MAX_HEIGHT_LIST = 292;
var MAX_ITEMS_IN_LIST = 25;
var ITEM_HEIGHT = 36;

module.exports = React.createClass({
    displayName: "MentionList",
    componentDidMount: function() {
        PostStore.addMentionDataChangeListener(this._onChange);
        var self = this;

        $('body').on('keydown.mentionlist', '#'+this.props.id,
            function(e) {
                if (!self.isEmpty() && self.state.mentionText != '-1' && (e.which === 13 || e.which === 9)) {
                    e.stopPropagation();
                    e.preventDefault();
                    self.addCurrentMention();
                }
                else if (!self.isEmpty() && self.state.mentionText != '-1' && (e.which === 38 || e.which === 40)) {
                    e.stopPropagation();
                    e.preventDefault();

                    if (!self.getSelection(self.state.selectedMention)) {
                        //self.setState({ selectedMention: 0, selectedUsername: self.refs.mention0.props.username });
                        //self.refs['mention' + self.state.selectedMention].deselect();
                        var tempSelectedMention = -1
                        while (self.getSelection(++tempSelectedMention)) {
                            if (self.state.selectedUsername === self.refs['mention' + tempSelectedMention].props.username) {
                                this.refs['mention' + tempSelectedMention].select();
                                this.setState({ selectedMention: tempSelectedMention });
                                break;
                            }
                        }
                    }
                    else {
                        self.refs['mention' + self.state.selectedMention].deselect();
                        if (e.which === 38) {    
                            if (self.getSelection(self.state.selectedMention - 1))
                                self.setState({ selectedMention: self.state.selectedMention - 1, selectedUsername: self.refs['mention' + (self.state.selectedMention - 1)].props.username });
                            else {
                                var tempSelectedMention = -1;
                                while (self.getSelection(++tempSelectedMention))
                                    ;
                                self.setState({ selectedMention: tempSelectedMention - 1, selectedUsername: self.refs['mention' + (tempSelectedMention - 1)].props.username });
                            }
                        }
                        else {
                            if (self.getSelection(self.state.selectedMention + 1))
                                self.setState({ selectedMention: self.state.selectedMention + 1, selectedUsername: self.refs['mention' + (self.state.selectedMention + 1)].props.username });
                            else
                                self.setState({ selectedMention: 0, selectedUsername: self.refs.mention0.props.username });
                        }
                    }
                    self.refs['mention' + self.state.selectedMention].select();

                    //self.checkIfInView($('#'+self.props.id));
                    //self.checkIfInView($('#'+self.refs['mention' + self.state.selectedMention].props.id));
                }
            }
        );
        $(document).click(function(e) {
            if (!($('#'+self.props.id).is(e.target) || $('#'+self.props.id).has(e.target).length ||
                ('mentionlist' in self.refs && $(self.refs['mentionlist'].getDOMNode()).has(e.target).length))) {
                self.setState({mentionText: "-1"})
            }
        });
    },
    componentWillUnmount: function() {
        PostStore.removeMentionDataChangeListener(this._onChange);
        $('body').off('keydown.mentionlist', '#'+this.props.id);
    },
    componentWillUpdate: function() {
        if (this.state.mentionText != "-1" && this.getSelection(this.state.selectedMention)) {
            this.refs['mention' + this.state.selectedMention].deselect();
        }
    },
    componentDidUpdate: function() {
        /*if (this.state.mentionText != "-1" && !this.getSelection(this.state.selectedMention)) {
        }*/
        if (this.state.mentionText != "-1" /*&& this.getSelection(this.state.selectedMention)*/) {
            if (this.state.selectedUsername !== "" && (!this.getSelection(this.state.selectedMention) || this.state.selectedUsername !== this.refs['mention' + this.state.selectedMention].props.username)) {
                var tempSelectedMention = -1;
                var foundMatch = false;
                while (this.getSelection(++tempSelectedMention)) {
                    if (this.state.selectedUsername === this.refs['mention' + tempSelectedMention].props.username) {
                        this.refs['mention' + tempSelectedMention].select();
                        this.setState({ selectedMention: tempSelectedMention });
                        foundMatch = true;
                        break;
                    }
                }
                if (!foundMatch) {
                    this.refs.mention0.select();
                    this.setState({ selectedMention: 0, selectedUsername: this.refs.mention0.props.username });
                }
            }
            else {
                this.refs['mention' + this.state.selectedMention].select();
            }
        }
    },
    _onChange: function(id, mentionText, excludeList) {
        if (id !== this.props.id) return;

        var newState = this.state;
        if (mentionText != null) newState.mentionText = mentionText;
        if (excludeList != null) newState.excludeUsers = excludeList;
        this.setState(newState);
    },
    handleClick: function(name) {
        AppDispatcher.handleViewAction({
            type: ActionTypes.RECIEVED_ADD_MENTION,
            id: this.props.id,
            username: name
        });

        this.setState({ mentionText: '-1' });
    },
    handleMouseEnter: function(listId) {
        if (this.getSelection(this.state.selectedMention)) {
            this.refs['mention' + this.state.selectedMention].deselect();
        }
        this.setState({ selectedMention: listId, selectedUsername: this.refs['mention' + listId].props.username });
        this.refs['mention' + listId].select();
    },
    getSelection: function(listId) {
        var mention = this.refs['mention' + listId];
        if (!mention) 
            return false;
        else
            return true;
    },
    addCurrentMention: function() {
        if (!this.refs['mention' + this.state.selectedMention]) this.addFirstMention();
        this.refs['mention' + this.state.selectedMention].handleClick();
    },
    addFirstMention: function() {
        if (!this.refs.mention0) return;
        this.refs.mention0.handleClick();
    },
    isEmpty: function() {
        return (!this.refs.mention0);
    },
    checkIfInView: function(element) {
        var offset = element.offset().top - $(window).scrollTop(); //$(this.props.id) ??
        if(offset > window.innerHeight){
            // Not in view so scroll to it
            $('body').animate({scrollTop: offset}, 1000); //$(this.props.id) ??
            return false;
        }
       return true;
    },
    alreadyMentioned: function(username) {
        var excludeUsers = this.state.excludeUsers;
        for (var i = 0; i < excludeUsers.length; i++) {
            if (excludeUsers[i] === username) {
                return true;
            }
        }
        return false;
    },
    getInitialState: function() {
        return { excludeUsers: [], mentionText: "-1", selectedMention: 0, selectedUsername: "" };
    },
    render: function() {
        var mentionText = this.state.mentionText;
        if (mentionText === '-1') return null;

        var profiles = UserStore.getActiveOnlyProfiles();
        var users = [];
        for (var id in profiles) {
            users.push(profiles[id]);
        }

        var all = {};
        all.username = "all";<commit_msg>Minor changes and tweaks trying to get scrolling to work<commit_after>// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

var UserStore = require('../stores/user_store.jsx');
var PostStore = require('../stores/post_store.jsx');
var AppDispatcher = require('../dispatcher/app_dispatcher.jsx');
var Mention = require('./mention.jsx');

var Constants = require('../utils/constants.jsx');
var ActionTypes = Constants.ActionTypes;

var MAX_HEIGHT_LIST = 292;
var MAX_ITEMS_IN_LIST = 25;
var ITEM_HEIGHT = 36;

module.exports = React.createClass({
    displayName: "MentionList",
    componentDidMount: function() {
        PostStore.addMentionDataChangeListener(this._onChange);
        var self = this;

        $('body').on('keydown.mentionlist', '#'+this.props.id,
            function(e) {
                if (!self.isEmpty() && self.state.mentionText != '-1' && (e.which === 13 || e.which === 9)) {
                    e.stopPropagation();
                    e.preventDefault();
                    self.addCurrentMention();
                }
                else if (!self.isEmpty() && self.state.mentionText != '-1' && (e.which === 38 || e.which === 40)) {
                    e.stopPropagation();
                    e.preventDefault();

                    if (!self.getSelection(self.state.selectedMention)) {
                        //self.setState({ selectedMention: 0, selectedUsername: self.refs.mention0.props.username });
                        //self.refs['mention' + self.state.selectedMention].deselect();
                        var tempSelectedMention = -1
                        while (self.getSelection(++tempSelectedMention)) {
                            if (self.state.selectedUsername === self.refs['mention' + tempSelectedMention].props.username) {
                                this.refs['mention' + tempSelectedMention].select();
                                this.setState({ selectedMention: tempSelectedMention });
                                break;
                            }
                        }
                    }
                    else {
                        self.refs['mention' + self.state.selectedMention].deselect();
                        if (e.which === 38) {    
                            if (self.getSelection(self.state.selectedMention - 1))
                                self.setState({ selectedMention: self.state.selectedMention - 1, selectedUsername: self.refs['mention' + (self.state.selectedMention - 1)].props.username });
                            else {
                                var tempSelectedMention = -1;
                                while (self.getSelection(++tempSelectedMention))
                                    ; //Need to find the top of the list
                                self.setState({ selectedMention: tempSelectedMention - 1, selectedUsername: self.refs['mention' + (tempSelectedMention - 1)].props.username });
                            }
                        }
                        else {
                            if (self.getSelection(self.state.selectedMention + 1))
                                self.setState({ selectedMention: self.state.selectedMention + 1, selectedUsername: self.refs['mention' + (self.state.selectedMention + 1)].props.username });
                            else
                                self.setState({ selectedMention: 0, selectedUsername: self.refs.mention0.props.username });
                        }
                    }
                    self.refs['mention' + self.state.selectedMention].select();

                    //self.checkIfInView($('#'+self.props.id));
                    self.checkIfInView();
                    //console.log('#'+self.refs['mention' + self.state.selectedMention].props.id);
                    //console.log($('#'+self.refs['mention' + self.state.selectedMention].props.id));
                    //console.log($('#'+self.props.id));
                }
            }
        );
        $(document).click(function(e) {
            if (!($('#'+self.props.id).is(e.target) || $('#'+self.props.id).has(e.target).length ||
                ('mentionlist' in self.refs && $(self.refs['mentionlist'].getDOMNode()).has(e.target).length))) {
                self.setState({mentionText: "-1"})
            }
        });
    },
    componentWillUnmount: function() {
        PostStore.removeMentionDataChangeListener(this._onChange);
        $('body').off('keydown.mentionlist', '#'+this.props.id);
    },
    componentWillUpdate: function() {
        if (this.state.mentionText != "-1" && this.getSelection(this.state.selectedMention)) {
            this.refs['mention' + this.state.selectedMention].deselect();
        }
    },
    componentDidUpdate: function() {
        if (this.state.mentionText != "-1") {
            if (this.state.selectedUsername !== "" && (!this.getSelection(this.state.selectedMention) || this.state.selectedUsername !== this.refs['mention' + this.state.selectedMention].props.username)) {
                var tempSelectedMention = -1;
                var foundMatch = false;
                while (this.getSelection(++tempSelectedMention)) {
                    if (this.state.selectedUsername === this.refs['mention' + tempSelectedMention].props.username) {
                        this.refs['mention' + tempSelectedMention].select();
                        this.setState({ selectedMention: tempSelectedMention });
                        foundMatch = true;
                        break;
                    }
                }
                if (!foundMatch) {
                    this.refs.mention0.select();
                    this.setState({ selectedMention: 0, selectedUsername: this.refs.mention0.props.username });
                }
            }
            else {
                this.refs['mention' + this.state.selectedMention].select();
            }
        }
    },
    _onChange: function(id, mentionText, excludeList) {
        if (id !== this.props.id) return;

        var newState = this.state;
        if (mentionText != null) newState.mentionText = mentionText;
        if (excludeList != null) newState.excludeUsers = excludeList;
        this.setState(newState);
    },
    handleClick: function(name) {
        AppDispatcher.handleViewAction({
            type: ActionTypes.RECIEVED_ADD_MENTION,
            id: this.props.id,
            username: name
        });

        this.setState({ mentionText: '-1' });
    },
    handleMouseEnter: function(listId) {
        if (this.getSelection(this.state.selectedMention)) {
            this.refs['mention' + this.state.selectedMention].deselect();
        }
        this.setState({ selectedMention: listId, selectedUsername: this.refs['mention' + listId].props.username });
        this.refs['mention' + listId].select();
    },
    getSelection: function(listId) {
        var mention = this.refs['mention' + listId];
        if (!mention) 
            return false;
        else
            return true;
    },
    addCurrentMention: function() {
        if (!this.refs['mention' + this.state.selectedMention]) 
            this.addFirstMention();
        else
            this.refs['mention' + this.state.selectedMention].handleClick();
    },
    addFirstMention: function() {
        if (!this.refs.mention0) return;
        this.refs.mention0.handleClick();
    },
    isEmpty: function() {
        return (!this.refs.mention0);
    },
    checkIfInView: function() {
        var element = $('#'+this.refs['mention' + this.state.selectedMention].props.id);
        //var top = $('.mentions-box').position().bottom;
        //$(".mentions-box").css("bottom", top+303);
        //$("#"+this.props.id).scrollTop($("#"+this.props.id).scrollTop() + $("div.mentions-name.mentions-focused").position().top);
        console.log(element.length);
        console.log(element + " " + element.offset());
        var offset = element.offset().bottom - $('#'+this.props.id).scrollTop(); //$(this.props.id) ??
        if(offset > $('#'+this.props.id).innerHeight){
            // Not in view so scroll to it
            $('body').animate({scrollTop: offset}, 1000); //$(this.props.id) ??
            return false;
        }
        return true;
    },
    alreadyMentioned: function(username) {
        var excludeUsers = this.state.excludeUsers;
        for (var i = 0; i < excludeUsers.length; i++) {
            if (excludeUsers[i] === username) {
                return true;
            }
        }
        return false;
    },
    getInitialState: function() {
        return { excludeUsers: [], mentionText: "-1", selectedMention: 0, selectedUsername: "" };
    },
    render: function() {
        var mentionText = this.state.mentionText;
        if (mentionText === '-1') return null;

        var profiles = UserStore.getActiveOnlyProfiles();
        var users = [];
        for (var id in profiles) {
            users.push(profiles[id]);
        }

        var all = {};
        all.username = "all";
{'fraction_non_alphanumeric': 0.14024077138129742, 'fraction_numerical': 0.006504250584812004, 'mean_word_length': 1.7738566228833677, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6597764', 'n_tokens_mistral': 4874, 'n_tokens_neox': 4612, 'n_words': 790}