<commit_before>/*
 * Licensed to the Sakai Foundation (SF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The SF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

require(["jquery", "sakai/sakai.api.core"], function($, sakai) {

    sakai_global.sakaidocs = function (tuid, showSettings) {

        var currentPageShown = false;
        var bookmark = false;

        var AUTOSAVE_INTERVAL = 15000, // 15 seconds
            CONCURRENT_EDITING_TIMEOUT = 10000, // 10 seconds
            CONCURRENT_EDITING_INTERVAL = 5000; // 5 seconds

        var isEditingPage = false,
            autosaveInterval = false,
            editInterval = false,
            lastAutosave = "",
            autosaveDialogShown = false;

        var $rootel = $("#"+tuid);

        ///////////////
        // Autosave //
        //////////////

        $('#autosave_dialog').jqm({
            modal: true,
            overlay: 20,
            toTop: true
        });

        var clearIntervals = function() {
            clearInterval(editInterval);
            clearInterval(autosaveInterval);
        };

        var setAutosaveInterval = function() {
            autosaveInterval = setInterval(autosave, AUTOSAVE_INTERVAL);
        };

        var setEditInterval = function() {
            editInterval = setInterval(editing, CONCURRENT_EDITING_INTERVAL);
        };

        var revertAutosave = function() {
            autosaveDialogShown = false;
            if ($rootel.is(":visible")) {
                setAutosaveInterval();
                tinyMCE.get("elm1").setContent(currentPageShown.autosave.page, {format : 'raw'});
                $('#autosave_dialog').jqmHide();
            }
        };

        var keepAutosave = function() {
            autosaveDialogShown = false;
            setAutosaveInterval();
            $('#autosave_dialog').jqmHide();
        };

        var checkAutosave = function(callback) {
            var pageSavePath = currentPageShown.pageSavePath + "/" + currentPageShown.saveRef;
            sakai.api.Server.loadJSON(pageSavePath + ".infinity.json", function(success, data) {
                if (success) {
                    // update the cached copy of autosave
                    currentPageShown.autosave = data.autosave;
                    currentPageShown.content = data.page;
                    // if there is an editing flag and it is less than CONCURRENT_EDITING_TIMEOUT ago, and you aren't the most recent editor, then
                    // someone else is editing the page right now.
                    if (data.editing && sakai.api.Util.Datetime.getCurrentGMTTime() - data.editing.time < CONCURRENT_EDITING_TIMEOUT && data.editing._lastModifiedBy !== sakai.api.User.data.me.user.userid) {
                        if ($.isFunction(callback)) {
                            callback(false);
                            return;
                        }
                    } else if (data.autosave && data.page && data.autosave._lastModified > data._lastModified) {
                        $('#autosave_dialog').jqmShow();
                        autosaveDialogShown = true;
                        if ($.isFunction(callback)) {
                            callback(true);
                            return;
                        }
                    }
                }
                if ($.isFunction(callback)) {
                    callback(true);
                    return;
                }
            });
        };

        var editing = function() {
            if (isEditingPage) {<commit_msg>SAKIII-4271 Check for presence of property instead of relying on it having a non-empty-string value<commit_after>/*
 * Licensed to the Sakai Foundation (SF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The SF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

require(["jquery", "sakai/sakai.api.core"], function($, sakai) {

    sakai_global.sakaidocs = function (tuid, showSettings) {

        var currentPageShown = false;
        var bookmark = false;

        var AUTOSAVE_INTERVAL = 15000, // 15 seconds
            CONCURRENT_EDITING_TIMEOUT = 10000, // 10 seconds
            CONCURRENT_EDITING_INTERVAL = 5000; // 5 seconds

        var isEditingPage = false,
            autosaveInterval = false,
            editInterval = false,
            lastAutosave = "",
            autosaveDialogShown = false;

        var $rootel = $("#"+tuid);

        ///////////////
        // Autosave //
        //////////////

        $('#autosave_dialog').jqm({
            modal: true,
            overlay: 20,
            toTop: true
        });

        var clearIntervals = function() {
            clearInterval(editInterval);
            clearInterval(autosaveInterval);
        };

        var setAutosaveInterval = function() {
            autosaveInterval = setInterval(autosave, AUTOSAVE_INTERVAL);
        };

        var setEditInterval = function() {
            editInterval = setInterval(editing, CONCURRENT_EDITING_INTERVAL);
        };

        var revertAutosave = function() {
            autosaveDialogShown = false;
            if ($rootel.is(":visible")) {
                setAutosaveInterval();
                tinyMCE.get("elm1").setContent(currentPageShown.autosave.page, {format : 'raw'});
                $('#autosave_dialog').jqmHide();
            }
        };

        var keepAutosave = function() {
            autosaveDialogShown = false;
            setAutosaveInterval();
            $('#autosave_dialog').jqmHide();
        };

        var checkAutosave = function(callback) {
            var pageSavePath = currentPageShown.pageSavePath + "/" + currentPageShown.saveRef;
            sakai.api.Server.loadJSON(pageSavePath + ".infinity.json", function(success, data) {
                if (success) {
                    // update the cached copy of autosave
                    currentPageShown.autosave = data.autosave;
                    currentPageShown.content = data.page;
                    // if there is an editing flag and it is less than CONCURRENT_EDITING_TIMEOUT ago, and you aren't the most recent editor, then
                    // someone else is editing the page right now.
                    if (data.editing && sakai.api.Util.Datetime.getCurrentGMTTime() - data.editing.time < CONCURRENT_EDITING_TIMEOUT && data.editing._lastModifiedBy !== sakai.api.User.data.me.user.userid) {
                        if ($.isFunction(callback)) {
                            callback(false);
                            return;
                        }
                    } else if (data.autosave && data.hasOwnProperty("page") && data.autosave._lastModified > data._lastModified) {
                        $('#autosave_dialog').jqmShow();
                        autosaveDialogShown = true;
                        if ($.isFunction(callback)) {
                            callback(true);
                            return;
                        }
                    }
                }
                if ($.isFunction(callback)) {
                    callback(true);
                    return;
                }
            });
        };

        var editing = function() {
            if (isEditingPage) {
{'fraction_non_alphanumeric': 0.0992231638418079, 'fraction_numerical': 0.006591337099811676, 'mean_word_length': 1.7112316528398213, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, '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': '6709908', 'n_tokens_mistral': 2239, 'n_tokens_neox': 2114, 'n_words': 576}