{"size": 4774, "ext": "js", "max_stars_repo_path": "Part-03-Understanding-Software-Crafting-Your-Own-Tools/models/edx-platform/lms/static/js/edxnotes/views/notes_visibility_factory.js", "max_stars_repo_name": "osoco/better-ways-of-thinking-about-software", "max_stars_repo_stars_event_min_datetime": "2021-12-15T04:58:18.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-06T12:15:37.000Z", "max_issues_repo_path": "Part-03-Understanding-Software-Crafting-Your-Own-Tools/models/edx-platform/lms/static/js/edxnotes/views/notes_visibility_factory.js", "max_issues_repo_name": "osoco/better-ways-of-thinking-about-software", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "Part-03-Understanding-Software-Crafting-Your-Own-Tools/models/edx-platform/lms/static/js/edxnotes/views/notes_visibility_factory.js", "max_forks_repo_name": "osoco/better-ways-of-thinking-about-software", "max_forks_repo_forks_event_min_datetime": "2019-01-02T14:38:50.000Z", "max_forks_repo_forks_event_max_datetime": "2019-01-02T14:38:50.000Z"}
{"max_stars_count": 3, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 38.192, "max_line_length": 144, "alphanum_fraction": 0.4987431923}
(function(define, undefined) {
    'use strict';
    define([
        'jquery', 'underscore', 'backbone', 'gettext',
        'annotator_1.2.9', 'js/edxnotes/views/visibility_decorator', 'js/utils/animation'
    ], function($, _, Backbone, gettext, Annotator, VisibilityDecorator) {
        var ToggleVisibilityView = Backbone.View.extend({
            events: {
                'click .action-toggle-notes': 'toggleHandler'
            },

            errorMessage: gettext('An error has occurred. Make sure that you are connected to the Internet, and then try refreshing the page.'),

            initialize: function(options) {
                _.bindAll(this, 'onSuccess', 'onError', 'keyDownToggleHandler', 'receiveMessage');
                this.visibility = options.visibility;
                this.visibilityUrl = options.visibilityUrl;

                this.label = this.$('.utility-control-label');
                this.actionLink = this.$('.action-toggle-notes');
                this.actionLink.removeClass('is-disabled');
                this.actionToggleMessage = this.$('.action-toggle-message');
                if (options.hideUI) {
                    $(window).on('message', this.receiveMessage);
                }
                this.notification = new Annotator.Notification();
                $(document).on('keydown.edxnotes:togglenotes', this.keyDownToggleHandler);
            },

            remove: function() {
                $(document).off('keydown.edxnotes:togglenotes');
                Backbone.View.prototype.remove.call(this);
            },

            receiveMessage: function(event) {
                var data = event.originalEvent.data;
                if (data === 'tools.toggleNotes') {
                    event.preventDefault();
                    this.visibility = !this.visibility;
                    if (this.visibility) {
                        this.enableNotes();
                    } else {
                        this.disableNotes();
                    }
                }
            },
            toggleHandler: function(event) {
                event.preventDefault();
                this.visibility = !this.visibility;
                AnimationUtil.triggerAnimation(this.actionToggleMessage);
                this.toggleNotes(this.visibility);
            },

            keyDownToggleHandler: function(event) {
            // Character '[' has keyCode 219
                if (event.keyCode === 219 && event.ctrlKey && event.shiftKey) {
                    this.toggleHandler(event);
                }
            },

            toggleNotes: function(visibility) {
                if (visibility) {
                    this.enableNotes();
                } else {
                    this.disableNotes();
                }
                this.sendRequest();
            },

            enableNotes: function() {
                _.each($('.edx-notes-wrapper'), VisibilityDecorator.enableNote);
                this.actionLink.addClass('is-active');
                this.label.text(gettext('Hide notes'));
                this.actionToggleMessage.text(gettext('Notes visible'));
            },

            disableNotes: function() {
                VisibilityDecorator.disableNotes();
                this.actionLink.removeClass('is-active');
                this.label.text(gettext('Show notes'));
                this.actionToggleMessage.text(gettext('Notes hidden'));
            },

            hideErrorMessage: function() {
                this.notification.hide();
            },

            showErrorMessage: function(message) {
                this.notification.show(message, Annotator.Notification.ERROR);
            },

            sendRequest: function() {
                return $.ajax({
                    type: 'PUT',
                    url: this.visibilityUrl,
                    dataType: 'json',
                    data: JSON.stringify({visibility: this.visibility}),
                    success: this.onSuccess,
                    error: this.onError
                });
            },

            onSuccess: function() {
                this.hideErrorMessage();
            },

            onError: function() {
                this.showErrorMessage(this.errorMessage);
            }
        });

        return {
            ToggleVisibilityView: function(visibility, visibilityUrl, hideUI) {
                return new ToggleVisibilityView({
                    el: $('.edx-notes-visibility').get(0),
                    visibility: visibility,
                    visibilityUrl: visibilityUrl,
                    hideUI: hideUI
                });
            },
            VisibilityDecorator: VisibilityDecorator
        };
    });
}).call(this, define || RequireJS.define);
