<commit_before>##############################################################################
#
# SageMathCloud: A collaborative web-based interface to Sage, IPython, LaTeX and the Terminal.
#
#    Copyright (C) 2015 -- 2016, SageMath, Inc.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General 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/>.
#
###############################################################################
# Viewer for history of changes to a document
###############################################################################

$ = window.$

misc = require('smc-util/misc')

{salvus_client} = require('./salvus_client')
{redux} = require('./smc-react')
{FileEditor, codemirror_session_editor} = require('./editor')

sagews  = require('./sagews')
jupyter = require('./editor_jupyter')
tasks   = require('./tasks')

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

underscore = require('underscore')

class exports.HistoryEditor extends FileEditor
    constructor: (@project_id, @filename, content, opts) ->
        super(@project_id, @filename)
        @init_paths()
        @init_view_doc opts, (err) =>
            if not err
                @init_syncstring()
                @init_slider()
            else
                # FUTURE: need a better way to report this
                console.warn("FAILED to configure view_doc")

    init_paths: =>
        #   @filename = "path/to/.file.sage-history"
        s = misc.path_split(@filename)
        @_path = s.tail.slice(1, s.tail.length - ".sage-history".length)
        @_open_file_path = @_path
        if s.head
            @_open_file_path = s.head + '/' + @_path
        else
            @_open_file_path = @_path
        @ext = misc.filename_extension(@_path)
        if @ext == 'ipynb'
            @_path = '.' + @_path + require('./editor_jupyter').IPYTHON_SYNCFILE_EXTENSION
        if s.head
            @_path = s.head + '/' + @_path

    init_syncstring: =>
        @syncstring = salvus_client.sync_string
            project_id : @project_id
            path       : @_path
        @syncstring.once 'connected', =>
            @render_slider()
            @render_diff_slider()
            @syncstring.on 'change', =>
                if @_diff_mode
                    @resize_diff_slider()
                else
                    @resize_slider()

            if @syncstring.has_full_history()
                @load_all.hide()
            else
                @load_all.show()

            # only show button for reverting if not read only
            @syncstring.wait_until_read_only_known (err) =>
                if not @syncstring.get_read_only()
                    @element.find("a[href=\"#revert\"]").show()

    close: () =>
        @remove()

    remove: () =>
        @syncstring?.close()
        @view_doc?.remove()

    disconnect_from_session: =>
        @close()

    init_view_doc: (opts, cb) =>
        opts.mode = ''
        opts.read_only = true
        @element  = templates.find(".salvus-editor-history").clone()
        switch @ext
            when 'ipynb'
                @view_doc = jupyter.jupyter_notebook(@, @_open_file_path, opts).data("jupyter_notebook")
                @element.find("a[href=\"#show-diff\"]").hide()
            when 'tasks'
                @view_doc = tasks.task_list(undefined, undefined, {viewer:true}).data('task_list')
                @element.find("a[href=\"#show-diff\"]").hide()
            else
                @view_doc = codemirror_session_editor(@project_id, @filename, opts)

        if @ext in ['course', 'sage-chat']
            @element.find(".salvus-editor-history-no-viewer").show()
            @top_elt = @element.find(".salvus-editor-history-no-viewer")
        else
            @top_elt = @element.find(".salvus-editor-history-sliders")

        @element.find(".salvus-editor-history-history_editor").append(@view_doc.element)

        if @ext == "sagews"
            opts0 =
                allow_javascript_eval : false<commit_msg>fix a history editor issue<commit_after>##############################################################################
#
# SageMathCloud: A collaborative web-based interface to Sage, IPython, LaTeX and the Terminal.
#
#    Copyright (C) 2015 -- 2016, SageMath, Inc.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General 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/>.
#
###############################################################################
# Viewer for history of changes to a document
###############################################################################

$ = window.$

misc = require('smc-util/misc')

{salvus_client} = require('./salvus_client')
{redux} = require('./smc-react')
{FileEditor, codemirror_session_editor} = require('./editor')

sagews  = require('./sagews')
jupyter = require('./editor_jupyter')
tasks   = require('./tasks')

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

underscore = require('underscore')

class exports.HistoryEditor extends FileEditor
    constructor: (@project_id, @filename, content, opts) ->
        super(@project_id, @filename)
        @init_paths()
        @init_view_doc opts, (err) =>
            if not err
                @init_syncstring()
                @init_slider()
            else
                # FUTURE: need a better way to report this
                console.warn("FAILED to configure view_doc")

    init_paths: =>
        #   @filename = "path/to/.file.sage-history"
        s = misc.path_split(@filename)
        @_path = s.tail.slice(1, s.tail.length - ".sage-history".length)
        @_open_file_path = @_path
        if s.head
            @_open_file_path = s.head + '/' + @_path
        else
            @_open_file_path = @_path
        @ext = misc.filename_extension(@_path)
        if @ext == 'ipynb'
            @_path = '.' + @_path + require('./editor_jupyter').IPYTHON_SYNCFILE_EXTENSION
        if s.head
            @_path = s.head + '/' + @_path

    init_syncstring: =>
        @syncstring = salvus_client.sync_string
            project_id : @project_id
            path       : @_path
        @syncstring.once 'connected', =>
            @render_slider()
            @render_diff_slider()
            @syncstring.on 'change', =>
                if @_diff_mode
                    @resize_diff_slider()
                else
                    @resize_slider()

            if @syncstring.has_full_history()
                @load_all.hide()
            else
                @load_all.show()

            # only show button for reverting if not read only
            @syncstring.wait_until_read_only_known (err) =>
                if not @syncstring.get_read_only()
                    @element.find("a[href=\"#revert\"]").show()

    close: () =>
        @remove()

    remove: () =>
        @syncstring?.close()
        @view_doc?.remove?()  # might not have a remove method!

    disconnect_from_session: =>
        @close()

    init_view_doc: (opts, cb) =>
        opts.mode = ''
        opts.read_only = true
        @element  = templates.find(".salvus-editor-history").clone()
        switch @ext
            when 'ipynb'
                @view_doc = jupyter.jupyter_notebook(@, @_open_file_path, opts).data("jupyter_notebook")
                @element.find("a[href=\"#show-diff\"]").hide()
            when 'tasks'
                @view_doc = tasks.task_list(undefined, undefined, {viewer:true}).data('task_list')
                @element.find("a[href=\"#show-diff\"]").hide()
            else
                @view_doc = codemirror_session_editor(@project_id, @filename, opts)

        if @ext in ['course', 'sage-chat']
            @element.find(".salvus-editor-history-no-viewer").show()
            @top_elt = @element.find(".salvus-editor-history-no-viewer")
        else
            @top_elt = @element.find(".salvus-editor-history-sliders")

        @element.find(".salvus-editor-history-history_editor").append(@view_doc.element)

        if @ext == "sagews"
            opts0 =
                allow_javascript_eval : false
{'fraction_non_alphanumeric': 0.1686891877118181, 'fraction_numerical': 0.0024051601618016835, 'mean_word_length': 2.310893955845096, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, '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': '2632046', 'n_tokens_mistral': 2693, 'n_tokens_neox': 2588, 'n_words': 615}