<commit_before>###############################################################################
#
# SageMathCloud: A collaborative web-based interface to Sage, IPython, LaTeX and the Terminal.
#
#    Copyright (C) 2014 -- 2016, SageMath, Inc.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Gener@al Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################

$        = window.$
misc     = require('smc-util/misc')
{defaults, required} = misc

misc_page = require('./misc_page')

message  = require('smc-util/message')
markdown = require('./markdown')

# Define interact jQuery plugins - used only by sage worksheets
require('./interact')

{salvus_client} = require('./salvus_client')
{alert_message} = require('./alerts')

async = require('async')

templates = $("#salvus-editor-templates")

account = require('./account')

{redux} = require('./smc-react')

{EventEmitter} = require('events')

side_chat = require('./side_chat')

{IS_MOBILE} = require('./feature')

class AbstractSynchronizedDoc extends EventEmitter
    file_path: () =>
        if not @_file_path?
            @_file_path = misc.path_split(@filename).head
        return @_file_path

synchronized_string = (opts) ->
    new SynchronizedString(opts)

exports.synchronized_string = synchronized_string

class SynchronizedDocument extends AbstractSynchronizedDoc
    codemirrors: () =>
        v = [@codemirror]
        if @editor._split_view
            v.push(@codemirror1)
        return v

    focused_codemirror: () =>
        @editor.focused_codemirror()

    # For testing.  To use on 'foo' do this in some consoles:   smc.editors['foo'].syncdoc.testbot()
    testbot: (opts) =>
        @focused_codemirror().testbot
            n     : opts?.n
            delay : opts?.delay
            f     : @sync

    ###
    The rest of this class is chat functionality.  This will, of course, be factored out.
    ###
    init_chat: () =>
        chat = @element.find(".salvus-editor-codemirror-chat")
        input = chat.find(".salvus-editor-codemirror-chat-input")

        # # send chat message
        # input.keydown (evt) =>
        #     if evt.which == 13 # enter
        #         content = $.trim(input.val())
        #         if content != ""
        #             input.val("")
        #             @write_chat_message
        #                 event_type : "chat"
        #                 payload    : {content : content}
        #         return false

        #@chat_session.on('sync', (=>@render_chat_log()))

        #@render_chat_log()  # first time
        @init_chat_toggle()
        @init_video_toggle()


    # This is an interface that allows messages to be passed between connected
    # clients via a log in the filesystem. These messages are handled on all
    # clients through the render_chat_log() method, which listens to the 'sync'
    # event emitted by the chat_session object.

    init_chat_toggle: () =>
        title = @element.find(".salvus-editor-chat-title-text")
        title.click () =>
            if @editor._chat_is_hidden? and @editor._chat_is_hidden
                @show_chat_window()
            else
                @hide_chat_window()
        if @editor._chat_is_hidden
            @hide_chat_window()
        else
            @show_chat_window()

    show_chat_window: () =>
        # SHOW the chat window
        @editor._chat_is_hidden = false
        @editor.local_storage("chat_is_hidden", false)
        @element.find(".salvus-editor-chat-show").hide()
        @element.find(".salvus-editor-chat-hide").show()
        #@element.find(".salvus-editor-codemirror-input-box").removeClass('col-sm-12').addClass('col-sm-9')
        @element.find(".salvus-editor-codemirror-chat-column").show()
        # see http://stackoverflow.com/questions/4819518/jquery-ui-resizable-does-not-support-position-fixed-any-recommendations
        # if you want to try to make this resizable
        @editor.show()  # updates editor width
        @editor.emit 'show-chat'
        #@render_chat_log()
        chat_height = @element.height() + 2 - @element.find(".salvus-editor-codemirror-button-row").height() - 70

        @_chat_redux_name = side_chat.render(@editor.project_id, @editor.chat_filename, @editor.chat_elt[0], redux, chat_height)

        height_resize = () =>
            chat_height = $($(".salvus-editor-codemirror")[1]).css("height").split("px")[0] - 140
            side_chat.render(@editor.project_id, @editor.chat_filename, @editor.chat_elt[0], redux, chat_height)
        $(window).on("resize", height_resize)

    hide_chat_window: () =>
        # HIDE the chat window
        @editor._chat_is_hidden = true
        @editor.local_storage("chat_is_hidden", true)
        @element.find(".salvus-editor-chat-hide").hide()
        @element.find(".salvus-editor-chat-show").show()
        #@element.find(".salvus-editor-codemirror-input-box").removeClass('col-sm-9').addClass('col-sm-12')
        @element.find(".salvus-editor-codemirror-chat-column").hide()
        @editor.show()  # update size/display of editor (especially the width)
        @editor.emit('hide-chat')

    init_video_toggle: () =>
        video_on_button  = @element.find(".salvus-editor-chat-video-is-off")
        video_off_button = @element.find(".salvus-editor-chat-video-is-on")

        video_on_button.click () =>
            actions = redux.getActions(@_chat_redux_name)
            actions.open_video_chat_window()
            @element.find(".salvus-editor-chat-video-is-off").hide()
            @element.find(".salvus-editor-chat-video-is-on").show()
            actions._video_window.addEventListener "unload", () =>
                @element.find(".salvus-editor-chat-video-is-off").show()
                @element.find(".salvus-editor-chat-video-is-on").hide()

        video_off_button.click () =>
            actions = redux.getActions(@_chat_redux_name)
            actions.close_video_chat_window()
            @element.find(".salvus-editor-chat-video-is-off").show()
            @element.find(".salvus-editor-chat-video-is-on").hide()<commit_msg>revert code from https://github.com/sagemathinc/smc/commit/ec25e7a5f02c6fe7a2e375dab728114ecd4366b5 that installs a broken window resize handler<commit_after>###############################################################################
#
# SageMathCloud: A collaborative web-based interface to Sage, IPython, LaTeX and the Terminal.
#
#    Copyright (C) 2014 -- 2016, SageMath, Inc.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Gener@al Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################

$        = window.$
misc     = require('smc-util/misc')
{defaults, required} = misc

misc_page = require('./misc_page')

message  = require('smc-util/message')
markdown = require('./markdown')

# Define interact jQuery plugins - used only by sage worksheets
require('./interact')

{salvus_client} = require('./salvus_client')
{alert_message} = require('./alerts')

async = require('async')

templates = $("#salvus-editor-templates")

account = require('./account')

{redux} = require('./smc-react')

{EventEmitter} = require('events')

side_chat = require('./side_chat')

{IS_MOBILE} = require('./feature')

class AbstractSynchronizedDoc extends EventEmitter
    file_path: () =>
        if not @_file_path?
            @_file_path = misc.path_split(@filename).head
        return @_file_path

synchronized_string = (opts) ->
    new SynchronizedString(opts)

exports.synchronized_string = synchronized_string

class SynchronizedDocument extends AbstractSynchronizedDoc
    codemirrors: () =>
        v = [@codemirror]
        if @editor._split_view
            v.push(@codemirror1)
        return v

    focused_codemirror: () =>
        @editor.focused_codemirror()

    # For testing.  To use on 'foo' do this in some consoles:   smc.editors['foo'].syncdoc.testbot()
    testbot: (opts) =>
        @focused_codemirror().testbot
            n     : opts?.n
            delay : opts?.delay
            f     : @sync

    ###
    The rest of this class is chat functionality.  This will, of course, be factored out.
    ###
    init_chat: () =>
        chat = @element.find(".salvus-editor-codemirror-chat")
        input = chat.find(".salvus-editor-codemirror-chat-input")

        # # send chat message
        # input.keydown (evt) =>
        #     if evt.which == 13 # enter
        #         content = $.trim(input.val())
        #         if content != ""
        #             input.val("")
        #             @write_chat_message
        #                 event_type : "chat"
        #                 payload    : {content : content}
        #         return false

        #@chat_session.on('sync', (=>@render_chat_log()))

        #@render_chat_log()  # first time
        @init_chat_toggle()
        @init_video_toggle()


    # This is an interface that allows messages to be passed between connected
    # clients via a log in the filesystem. These messages are handled on all
    # clients through the render_chat_log() method, which listens to the 'sync'
    # event emitted by the chat_session object.

    init_chat_toggle: () =>
        title = @element.find(".salvus-editor-chat-title-text")
        title.click () =>
            if @editor._chat_is_hidden? and @editor._chat_is_hidden
                @show_chat_window()
            else
                @hide_chat_window()
        if @editor._chat_is_hidden
            @hide_chat_window()
        else
            @show_chat_window()

    show_chat_window: () =>
        # SHOW the chat window
        @editor._chat_is_hidden = false
        @editor.local_storage("chat_is_hidden", false)
        @element.find(".salvus-editor-chat-show").hide()
        @element.find(".salvus-editor-chat-hide").show()
        #@element.find(".salvus-editor-codemirror-input-box").removeClass('col-sm-12').addClass('col-sm-9')
        @element.find(".salvus-editor-codemirror-chat-column").show()
        # see http://stackoverflow.com/questions/4819518/jquery-ui-resizable-does-not-support-position-fixed-any-recommendations
        # if you want to try to make this resizable
        @editor.show()  # updates editor width
        @editor.emit 'show-chat'
        #@render_chat_log()
        chat_height = @element.height() + 2 - @element.find(".salvus-editor-codemirror-button-row").height() - 70

        @_chat_redux_name = side_chat.render(@editor.project_id, @editor.chat_filename, @editor.chat_elt[0], redux, chat_height)

    hide_chat_window: () =>
        # HIDE the chat window
        @editor._chat_is_hidden = true
        @editor.local_storage("chat_is_hidden", true)
        @element.find(".salvus-editor-chat-hide").hide()
        @element.find(".salvus-editor-chat-show").show()
        #@element.find(".salvus-editor-codemirror-input-box").removeClass('col-sm-9').addClass('col-sm-12')
        @element.find(".salvus-editor-codemirror-chat-column").hide()
        @editor.show()  # update size/display of editor (especially the width)
        @editor.emit('hide-chat')

    init_video_toggle: () =>
        video_on_button  = @element.find(".salvus-editor-chat-video-is-off")
        video_off_button = @element.find(".salvus-editor-chat-video-is-on")

        video_on_button.click () =>
            actions = redux.getActions(@_chat_redux_name)
            actions.open_video_chat_window()
            @element.find(".salvus-editor-chat-video-is-off").hide()
            @element.find(".salvus-editor-chat-video-is-on").show()
            actions._video_window.addEventListener "unload", () =>
                @element.find(".salvus-editor-chat-video-is-off").show()
                @element.find(".salvus-editor-chat-video-is-on").hide()

        video_off_button.click () =>
            actions = redux.getActions(@_chat_redux_name)
            actions.close_video_chat_window()
            @element.find(".salvus-editor-chat-video-is-off").show()
            @element.find(".salvus-editor-chat-video-is-on").hide()
{'fraction_non_alphanumeric': 0.1524846834581348, 'fraction_numerical': 0.006580440208758793, 'mean_word_length': 2.695360536612633, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 38, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, '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': '4202235', 'n_tokens_mistral': 4045, 'n_tokens_neox': 3917, 'n_words': 824}