<commit_before>/*
 * Wegas
 * http://wegas.albasim.ch
 *
 * Copyright (c) 2013, 2014, 2015 School of Business and Engineering Vaud, Comem
 * Licensed under the MIT License
 */
/**
 * @fileoverview
 * @author <NAME> <cyril.junod at gmail.com>
 */
YUI.add('wegas-presence', function(Y) {
    "use strict";
    var CONTENTBOX = 'contentBox', Chat, pagePresence,
        updateState = function(node, status) {
            var text = "Chat: ";
            switch (status) {
                case "disconnected":
                    text += "Error while connecting";
                    break;
                case "unavailable":
                    text += "Connection lost";
                    break;
                case "initialized":
                case "connecting":
                    text += "Connecting ...";
                    break;
                case "failed":
                    text += "Unsupported browser";
                    break;
                case undefined:
                    text += "Missing pusher";
                    break;
                default:
                    text = "";
            }
            node.set("text", text);
            if (text) {
                node.show();
            } else {
                node.hide();
            }
        };
    /**
     * @name Y.Wegas.EditorChat
     * @class
     */
    Chat = Y.Base.create("wegas-editorchat", Y.Widget, [Y.WidgetChild], {
        BOUNDING_TEMPLATE: "<div><div class='conversation'>" +
            "<div class='msgs'></div><ul class='users'></ul><div style='clear: both'></div><textarea  rows='3' class='input' placeholder='Your message'></textarea></div>" +
            "<div class='editorchat-footer'><i class='chat-icon fa fa-comments'></i> <span class='pusher-status'></span><span class='count'></span><span class='user-list'></span></div></div>",
        CONTENT_TEMPLATE: null,
        initializer: function() {
            this.closed = true;
            this._handlers = [];
            if (Y.Wegas.Facade.Pusher.get("status")) {
                this._initConnection();
            } else {
                Y.Wegas.Facade.Pusher.once("statusChange", this._initConnection, this);
            }
        },
        _initConnection: function() {
            var gmID = Y.Wegas.Facade.GameModel.cache.getCurrentGameModel().get("id");

            pagePresence = Y.Wegas.Facade.Pusher.subscribe("presence-gm" + gmID);
            pagePresence.bind("client-message", this.onMessage, this);
            pagePresence.bind('pusher:subscription_succeeded', this.onConnected, this);
            pagePresence.bind("pusher:member_removed", this.onRemoved, this);
            pagePresence.bind("pusher:member_added", this.onAdded, this);
        //pagePresence.bind_all(Y.bind(this.onEvent, this));
        },
        renderUI: function() {
            var cb = this.get(CONTENTBOX);

            this.field = cb.one(".input");
            this.send = new Y.Wegas.Button({
                label: "Send",
                cssClass: "chat-send"
            }).render(cb.one(".conversation"));
            this.footer = cb.one(".editorchat-footer");
            this.get(CONTENTBOX).one(".conversation").toggleClass("closed", this.closed);
            updateState(this.get(CONTENTBOX).one(".pusher-status"), Y.Wegas.Facade.Pusher.get("status"));
        },
        bindUI: function() {
            this._handlers.push(this.send.on("click", this.sendInput, this));
            this._handlers.push(this.get(CONTENTBOX).on("clickoutside", function() {
                this.closed = true;
                this.get(CONTENTBOX).one(".conversation").toggleClass("closed", this.closed);
            }, this));
            this._handlers.push(Y.Wegas.Facade.Pusher.on("statusChange", function(e) {
                updateState(this.get(CONTENTBOX).one(".pusher-status"), e.newVal);
            }, this));
            this._handlers.push(this.field.on("key", function(e) {
                if (!(e.shiftKey || e.ctrlKey || e.altKey)) {
                    e.halt(true);
                    this.sendInput();
                }
            }, "enter", this));
            this._handlers.push(this.footer.on("click", function() {
                this.closed = !this.closed;
                this.get(CONTENTBOX).one(".conversation").toggleClass("closed", this.closed);
                this.get(CONTENTBOX).removeClass("new-message");
                this.get(CONTENTBOX).all("span").removeClass("new-message");
            }, this));
        },
        sendInput: function() {
            var val = this.field.get("value");
            if (val && pagePresence.subscribed) {
                pagePresence.trigger("client-message", {
                    data: val,
                    sender: pagePresence.members.me.id
                });
                this.onMessage({
                    sender: pagePresence.members.me.id,
                    data: val
                });
            }
            this.field.set("value", "");
        },
        onConnected: function() {
            this.get(CONTENTBOX).one('.users').empty();
            pagePresence.members.each(Y.bind(function(m) {
                if (+m.id !== +pagePresence.members.me.id) {
                    this.onAdded(m);
                } else {
                    this.get(CONTENTBOX).one('.users').append("<li class='me'>me</li>");
                    this.updateCount();
                }
            }, this));
        },
        //onEvent: function(event) {
        //    console.log(event);
        //},
        onRemoved: function(event) {
            this.get(CONTENTBOX).one('.users').one("#user" + event.id).remove(true);
            this.updateCount();
            this.notification(event.info.name + " left");
        },
        onAdded: function(event) {
            this.get(CONTENTBOX).one('.users').append("<li id='user" + event.id + "' class='user'>" + event.info.name +
                "</li>");
            this.updateCount();
            this.notification(event.info.name + " joined");
        },
        updateCount: function() {
            this.get(CONTENTBOX).one('.count').setHTML("viewer" +
                (pagePresence.members.count > 2 ? "s: " : ": ") +
                (pagePresence.members.count - 1)); //minus self
            this.updateSmallUserList();
        },
        updateSmallUserList: function() {
            var uList = this.get(CONTENTBOX).one(".user-list"),
                initials,
                count = 0,
                LIMIT = 6;
            uList.empty();
            pagePresence.members.each(function(m) {
                if (count < LIMIT && +m.id !== +pagePresence.members.me.id) {
                    initials = m.info.name.match(/\b\w/g).join("");
                    uList.append("<span class='u" + m.id + "'>" + initials + "</span>");
                    count += 1;
                }
            });
            if (count === LIMIT) {
                uList.append("<span>...</span>");
            }
        },
        addToChat: function(html) {
            var msgBox = this.get(CONTENTBOX).one('.msgs'),
                node = (html instanceof Y.Node) ? html :
                    Y.Node.create(html);
            msgBox.append(node);
            this.lastNode = node;
            msgBox.getDOMNode().scrollTop = msgBox.getDOMNode().scrollHeight;
            if (this.closed) {
                this.get(CONTENTBOX).addClass("new-message");
            }
        },
        onMessage: function(data) {
            var sender = ((pagePresence.members.me.id === data.sender) ? "me" : pagePresence.members.get(data.sender).info.name);
            if (this.lastNode && this.lastNode.one(".sender") &&
                this.lastNode.one(".sender").get("text") === sender) {<commit_msg>Editor-chat: smarter initials update<commit_after>/*
 * Wegas
 * http://wegas.albasim.ch
 *
 * Copyright (c) 2013, 2014, 2015 School of Business and Engineering Vaud, Comem
 * Licensed under the MIT License
 */
/**
 * @fileoverview
 * @author <NAME> <cyril.junod at gmail.com>
 */
YUI.add('wegas-presence', function(Y) {
    "use strict";
    var CONTENTBOX = 'contentBox', Chat, pagePresence,
        INITIALS_LIMIT_COUNT = 6,
        updateState = function(node, status) {
            var text = "Chat: ";
            switch (status) {
                case "disconnected":
                    text += "Error while connecting";
                    break;
                case "unavailable":
                    text += "Connection lost";
                    break;
                case "initialized":
                case "connecting":
                    text += "Connecting ...";
                    break;
                case "failed":
                    text += "Unsupported browser";
                    break;
                case undefined:
                    text += "Missing pusher";
                    break;
                default:
                    text = "";
            }
            node.set("text", text);
            if (text) {
                node.show();
            } else {
                node.hide();
            }
        };
    /**
     * @name Y.Wegas.EditorChat
     * @class
     */
    Chat = Y.Base.create("wegas-editorchat", Y.Widget, [Y.WidgetChild], {
        BOUNDING_TEMPLATE: "<div><div class='conversation'>" +
            "<div class='msgs'></div><ul class='users'></ul><div style='clear: both'></div><textarea  rows='3' class='input' placeholder='Your message'></textarea></div>" +
            "<div class='editorchat-footer'><i class='chat-icon fa fa-comments'></i> <span class='pusher-status'></span><span class='count'></span><span class='user-list'></span></div></div>",
        CONTENT_TEMPLATE: null,
        initializer: function() {
            this.closed = true;
            this._handlers = [];
            if (Y.Wegas.Facade.Pusher.get("status")) {
                this._initConnection();
            } else {
                Y.Wegas.Facade.Pusher.once("statusChange", this._initConnection, this);
            }
        },
        _initConnection: function() {
            var gmID = Y.Wegas.Facade.GameModel.cache.getCurrentGameModel().get("id");

            pagePresence = Y.Wegas.Facade.Pusher.subscribe("presence-gm" + gmID);
            pagePresence.bind("client-message", this.onMessage, this);
            pagePresence.bind('pusher:subscription_succeeded', this.onConnected, this);
            pagePresence.bind("pusher:member_removed", this.onRemoved, this);
            pagePresence.bind("pusher:member_added", this.onAdded, this);
        //pagePresence.bind_all(Y.bind(this.onEvent, this));
        },
        renderUI: function() {
            var cb = this.get(CONTENTBOX);

            this.field = cb.one(".input");
            this.send = new Y.Wegas.Button({
                label: "Send",
                cssClass: "chat-send"
            }).render(cb.one(".conversation"));
            this.footer = cb.one(".editorchat-footer");
            this.get(CONTENTBOX).one(".conversation").toggleClass("closed", this.closed);
            updateState(this.get(CONTENTBOX).one(".pusher-status"), Y.Wegas.Facade.Pusher.get("status"));
        },
        bindUI: function() {
            this._handlers.push(this.send.on("click", this.sendInput, this));
            this._handlers.push(this.get(CONTENTBOX).on("clickoutside", function() {
                this.closed = true;
                this.get(CONTENTBOX).one(".conversation").toggleClass("closed", this.closed);
            }, this));
            this._handlers.push(Y.Wegas.Facade.Pusher.on("statusChange", function(e) {
                updateState(this.get(CONTENTBOX).one(".pusher-status"), e.newVal);
            }, this));
            this._handlers.push(this.field.on("key", function(e) {
                if (!(e.shiftKey || e.ctrlKey || e.altKey)) {
                    e.halt(true);
                    this.sendInput();
                }
            }, "enter", this));
            this._handlers.push(this.footer.on("click", function() {
                this.closed = !this.closed;
                this.get(CONTENTBOX).one(".conversation").toggleClass("closed", this.closed);
                this.get(CONTENTBOX).removeClass("new-message");
                this.get(CONTENTBOX).all("span").removeClass("new-message");
            }, this));
        },
        sendInput: function() {
            var val = this.field.get("value");
            if (val && pagePresence.subscribed) {
                pagePresence.trigger("client-message", {
                    data: val,
                    sender: pagePresence.members.me.id
                });
                this.onMessage({
                    sender: pagePresence.members.me.id,
                    data: val
                });
            }
            this.field.set("value", "");
        },
        onConnected: function() {
            var users = this.get(CONTENTBOX).one('.users');
            users.empty();
            pagePresence.members.each(Y.bind(function(m) {

                if (+m.id !== +pagePresence.members.me.id) {
                    this.onAdded(m);
                } else {
                    users.append("<li class='me'>me</li>");
                    this.updateCount();
                }
            }, this));
        },
        onRemoved: function(event) {
            this.get(CONTENTBOX).one('.users').one("#user" + event.id).remove(true);
            this.updateCount();
            this.notification(event.info.name + " left");
            this.get(CONTENTBOX).one(".user-list").all(".u" + event.id).remove(true);
        },
        onAdded: function(event) {
            var uList = this.get(CONTENTBOX).one(".user-list"), initials;
            this.get(CONTENTBOX).one('.users').append("<li id='user" + event.id + "' class='user'>" + event.info.name +
                "</li>");
            this.updateCount();
            this.notification(event.info.name + " joined");
            if (uList.get("children").size() < INITIALS_LIMIT_COUNT) {
                initials = event.info.name.match(/\b\w/g).join("");
                uList.append("<span class='u" + event.id + "'>" + initials + "</span>");
            }
        },
        updateCount: function() {
            this.get(CONTENTBOX).one('.count').setHTML("viewer" +
                (pagePresence.members.count > 2 ? "s: " : ": ") +
                (pagePresence.members.count - 1)); //minus self
        },
        addToChat: function(html) {
            var msgBox = this.get(CONTENTBOX).one('.msgs'),
                node = (html instanceof Y.Node) ? html :
                    Y.Node.create(html);
            msgBox.append(node);
            this.lastNode = node;
            msgBox.getDOMNode().scrollTop = msgBox.getDOMNode().scrollHeight;
            if (this.closed) {
                this.get(CONTENTBOX).addClass("new-message");
            }
        },
        onMessage: function(data) {
            var sender = ((pagePresence.members.me.id === data.sender) ? "me" : pagePresence.members.get(data.sender).info.name);
            if (this.lastNode && this.lastNode.one(".sender") &&
                this.lastNode.one(".sender").get("text") === sender) {
{'fraction_non_alphanumeric': 0.15822286389289933, 'fraction_numerical': 0.0022312639453996586, 'mean_word_length': 1.6369614120089981, 'pattern_counts': {'":': 12, '<': 67, '<?xml version=': 0, '>': 67, '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': '3109639', 'n_tokens_mistral': 4372, 'n_tokens_neox': 4237, 'n_words': 652}