<commit_before>/**
 * OpenEyes
 *
 * (C) OpenEyes Foundation, 2016
 * This file is part of OpenEyes.
 * OpenEyes 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.
 * OpenEyes 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 OpenEyes in a file titled COPYING. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package OpenEyes
 * @link http://www.openeyes.org.uk
 * @author OpenEyes <<EMAIL>>
 * @copyright Copyright (c) 2016, OpenEyes Foundation
 * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0
 */

(function(exports) {
    /**
     * PatientSidebar constructor. The PatientSidebar ......
     *
     * @param options
     * @constructor
     */
    function PatientSidebar(element, options) {
        this.$element = $(element);
        this.$element.data('patient-sidebar', this);
        this.options = $.extend(true, {}, PatientSidebar._defaultOptions, options);
        this.create();
    }

    /**
     * _defaultOptions
     *
     *  patient_sidebar_json        json string     JSON string of the sidebar elements
     *  element_container_selector  string
     *  tree_id                     string          CSS CLass ID of where the built sidebar will go
     */
    PatientSidebar._defaultOptions = {
        patient_sidebar_json: {},
        element_container_selector: '.js-active-elements',
        tree_id: '',
        scroll_selector: 'div.oe-scroll-wrapper'
    };

    /**
     * Simple error message wrapper
     * @param msg
     */
    PatientSidebar.prototype.error = function(msg)
    {
        console.log('PatientSidebar ERROR: ' + msg);
    };

    /**
     *  Main code that builds and updates the tree
     */
    PatientSidebar.prototype.create = function() {
        var self = this;

        var $scrollElement = self.$element.find(self.options.scroll_selector);
        if ($scrollElement.length) {
            // if the scrollbar controller is in place, then when we have changed the content
            // we want to trigger the resize event, as the element list may be longer than the
            // original contents.
            this.sidebarController = $scrollElement.data('sidebar');
        }

        var $realContainer = self.$element.find('.all-panels');

        var $children = $realContainer.children();
        var $newContent = $('<div class="oe-event-sidebar-edit"><ul class="oe-element-list"></ul></div>');
        $realContainer.attr('id', self.options.tree_id);
        self.$element = $newContent.find('ul');

        self.openElements();

        self.errorElements();

        self.parseJSON();

        self.buildTree();

        $realContainer.children().remove();
        $realContainer.append($newContent);

        if (this.sidebarController) {
            this.sidebarController.checkSideNavHeight();
        }

        self.$elementContainer = $(document).find(self.options.element_container_selector);

        // couple of hooks to keep the menu in sync with the elements on the page.
        self.$elementContainer.on('click', '.js-remove-element', function(e) {
            self.removeElememt(e.target);
        });

        self.$elementContainer.on('click', '.js-remove-child-element', function(e) {
            self.removeElememt(e.target);
        });

        // expand or collapse the menu for the given menu item
        self.$element.on('click', 'a .icon', function(e) {
            e.preventDefault();
            e.stopPropagation();

            var li = $(this).parent().parent();
            if($(this).hasClass('expand')){
                li.removeClass('collapsed').addClass('expanded');
                li.children('ul.children').slideDown(100);
                $(this).removeClass('expand').addClass('collapse');
            } else {
                li.removeClass('expanded').addClass('collapsed');
                li.children('ul.children').slideUp(100);
                $(this).removeClass('collapse').addClass('expand');
            }
        });

        // if the clicked element is a child, ensures parent loaded first. if the element is already
        // loaded, then just move the view port appropriately.
        self.$element.on('click', 'a', function(e) {
            e.preventDefault();
            self.loadClickedItem($(e.target));
        }.bind(self));
    };

    /**
     * Calls the function that will set the view port to the given element for the menu item.
     */
    PatientSidebar.prototype.moveTo = function($item) {
        var elementTypeClass = $item.parents('li:first').data('element-type-class');
        moveToElement($('section[data-element-type-class="' + elementTypeClass + '"]'));
    };

    /**
     * Determines whether clicked item is a child or not, whether it or its parent are currently visible,
     * and thereby what loading actions to call (including calling itself if necessary.
     *<commit_msg>[OE-6796] auto expansion of sidebar element list when adding a parent element<commit_after>/**
 * OpenEyes
 *
 * (C) OpenEyes Foundation, 2016
 * This file is part of OpenEyes.
 * OpenEyes 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.
 * OpenEyes 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 OpenEyes in a file titled COPYING. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package OpenEyes
 * @link http://www.openeyes.org.uk
 * @author OpenEyes <<EMAIL>>
 * @copyright Copyright (c) 2016, OpenEyes Foundation
 * @license http://www.gnu.org/licenses/gpl-3.0.html The GNU General Public License V3.0
 */

(function(exports) {
    /**
     * PatientSidebar constructor. The PatientSidebar ......
     *
     * @param options
     * @constructor
     */
    function PatientSidebar(element, options) {
        this.$element = $(element);
        this.$element.data('patient-sidebar', this);
        this.options = $.extend(true, {}, PatientSidebar._defaultOptions, options);
        this.create();
    }

    /**
     * _defaultOptions
     *
     *  patient_sidebar_json        json string     JSON string of the sidebar elements
     *  element_container_selector  string
     *  tree_id                     string          CSS CLass ID of where the built sidebar will go
     */
    PatientSidebar._defaultOptions = {
        patient_sidebar_json: {},
        element_container_selector: '.js-active-elements',
        tree_id: '',
        scroll_selector: 'div.oe-scroll-wrapper'
    };

    /**
     * Simple error message wrapper
     * @param msg
     */
    PatientSidebar.prototype.error = function(msg)
    {
        console.log('PatientSidebar ERROR: ' + msg);
    };

    /**
     *  Main code that builds and updates the tree
     */
    PatientSidebar.prototype.create = function() {
        var self = this;

        var $scrollElement = self.$element.find(self.options.scroll_selector);
        if ($scrollElement.length) {
            // if the scrollbar controller is in place, then when we have changed the content
            // we want to trigger the resize event, as the element list may be longer than the
            // original contents.
            this.sidebarController = $scrollElement.data('sidebar');
        }

        var $realContainer = self.$element.find('.all-panels');

        var $children = $realContainer.children();
        var $newContent = $('<div class="oe-event-sidebar-edit"><ul class="oe-element-list"></ul></div>');
        $realContainer.attr('id', self.options.tree_id);
        self.$element = $newContent.find('ul');

        self.openElements();

        self.errorElements();

        self.parseJSON();

        self.buildTree();

        $realContainer.children().remove();
        $realContainer.append($newContent);

        if (this.sidebarController) {
            this.sidebarController.checkSideNavHeight();
        }

        self.$elementContainer = $(document).find(self.options.element_container_selector);

        // couple of hooks to keep the menu in sync with the elements on the page.
        self.$elementContainer.on('click', '.js-remove-element', function(e) {
            self.removeElememt(e.target);
        });

        self.$elementContainer.on('click', '.js-remove-child-element', function(e) {
            self.removeElememt(e.target);
        });

        // expand or collapse the menu for the given menu item
        self.$element.on('click', 'a .icon', function(e) {
            e.preventDefault();
            e.stopPropagation();

            var li = $(this).parent().parent();
            if($(this).hasClass('expand')){
                li.removeClass('collapsed').addClass('expanded');
                li.children('ul.children').slideDown(100);
                $(this).removeClass('expand').addClass('collapse');
            } else {
                li.removeClass('expanded').addClass('collapsed');
                li.children('ul.children').slideUp(100);
                $(this).removeClass('collapse').addClass('expand');
            }
        });

        // if the clicked element is a child, ensures parent loaded first. if the element is already
        // loaded, then just move the view port appropriately.
        self.$element.on('click', 'a', function(e) {
            e.preventDefault();
            self.loadClickedItem($(e.target));
            var li = $(e.target).parent();
            if (li.hasClass('has-children') && li.hasClass('collapsed')) {
                $(e.target).find('.icon').trigger('click');
            }
        }.bind(self));
    };

    /**
     * Calls the function that will set the view port to the given element for the menu item.
     */
    PatientSidebar.prototype.moveTo = function($item) {
        var elementTypeClass = $item.parents('li:first').data('element-type-class');
        moveToElement($('section[data-element-type-class="' + elementTypeClass + '"]'));
    };

    /**
     * Determines whether clicked item is a child or not, whether it or its parent are currently visible,
     * and thereby what loading actions to call (including calling itself if necessary.
     *
{'fraction_non_alphanumeric': 0.12258242770307608, 'fraction_numerical': 0.0038681156750782833, 'mean_word_length': 2.433133101485931, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '7480687', 'n_tokens_mistral': 2907, 'n_tokens_neox': 2798, 'n_words': 832}