<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.
 */

/*global , opensocial, Config, window, alert, $ */

var sakai = sakai || {};

/**
 * @name sakai.changepic
 *
 * @class changepic
 *
 * @description
 * Changepic widget
 *
 * @version 0.0.1
 * @param {String} tuid Unique id of the widget
 * @param {Boolean} showSettings Show the settings of the widget or not
 */
sakai.changepic = function(tuid, showSettings){


    //////////////////////
    // Config Variables //
    //////////////////////

    var realw = 0;
    var realh = 0;
    var picture = false;
    var ratio = 1;
    var userSelection = null; // The object returned by imgAreaSelect that contains the user his choice.
    var me = null;
    var imageareaobject;

    // These values are just in case there are no css values specified.
    // If you want to change the size of a thumbnail please do this in the CSS.
    var thumbnailWidth = 100;
    var thumbnailHeight = 100;


    //////////////
    // CSS IDS  //
    //////////////

    var containerTrigger = '#changepic_container_trigger'; // This is the id that will trigger this widget.

    // tabs
    var tabSelect = "#changepic_select";
    var tabUpload = "#changepic_upload";
    var tabSelectContent = "#changepic_selectpicture";
    var tabUploadContent = "#changepic_uploadnew";
    var tabActiveClass = "fl-tabs-active";
    var tabSearchSelected = "search_tab_selected";

    // others
    var container = "#changepic_container";
    var picForm = "#changepic_form";
    var picInput = "#profilepicture";
    var picInputError = "#changepic_nofile_error";
    var uploadProcessing = "#changepic_processing_msg";
    var uploadNewButtons = "#changepic_uploadnew_buttons";
    var pictureMeasurer = "#picture_measurer";
    var pictureMeasurerImage = "#picture_measurer_image";
    var saveNewSelection = "#save_new_selection";
    var fullPicture = '#changepic_fullpicture';
    var thumbnail = "#thumbnail";
    var thumbnailContainer = "#thumbnail_container";

    // An array with selectors pointing to images that need to be changed.
    var imagesToChange = ["#picture_holder img", "#entity_profile_picture", "#myprofile_pic", "#chat_available_me .chat_available_image img", "#profile_userinfo_picture"];


    ///////////////////
    // TAB FUNCTIONS //
    ///////////////////

    /**
     * Will set the upload a new picture as the viewable tab.
     * The other tab will be hidden.
     */
    var showNewTab = function(){

        $(fullPicture).imgAreaSelect({
            hide: true,
            disable: true
        });

        $(tabSelect).removeClass(tabActiveClass);
        $(tabSelect).removeClass(tabSearchSelected);

        $(tabUpload).addClass(tabSearchSelected);
        $(tabUpload).addClass(tabActiveClass);

        $(tabSelectContent).hide();
        $(tabUploadContent).show();
    };

    /**
     * Will show the tab where the user can cut out a square.
     * The upload-a-pic tab will be hidden.
     */
    var showSelectTab = function(){

        $(tabSelect).addClass(tabActiveClass);
        $(tabSelect).addClass(tabSearchSelected);

        $(tabUpload).removeClass(tabSearchSelected);
        $(tabUpload).removeClass(tabActiveClass);

        $(tabSelectContent).show();
        $(tabUploadContent).hide();
    };

    /**
     * Clicked on the upload tab
     */
    $(tabUpload).click(function(){
        showNewTab();
    });

    /**
     * Clicked on the select tab
     */
    $(tabSelect).click(function(){
        sakai.changepic.doInit();
    });

    /**
     * When the user has drawn a square this function will be called by imgAreaSelect.
     * This will draw the thumbnail by modifying it's css values.
     * @param {Object} img    The thumbnail
     * @param {Object} selection The selection object from imgAreaSelect
     */
    function preview(img, selection){
        // Save the user his selection in a global variable.
        userSelection = selection;

        // How much has the user scaled down the image?
        var scaleX = thumbnailWidth / (selection.width || 1);
        var scaleY = thumbnailHeight / (selection.height || 1);

        // Change the thumbnail according to the user his selection via CSS.
        $(thumbnail).css({
            width: Math.round(scaleX * img.width) + 'px',
            height: Math.round(scaleY * img.height) + 'px',
            marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
            marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
        });
    }

     /**
     * Empty upload field by resetting the form
     */
    var resetUploadField = function(){
        $(picInput).val("");
        $(picInputError).hide();
        $(uploadProcessing).hide();
        $(uploadNewButtons).show();
    };

    // Add click event to all cancel buttons in the overlay
    // Since file upload form is reset every time overlay closes do this in init function
    $("#changepic_container .jqmClose").click(function(){
        resetUploadField();
    });

    /**
     * On changepic form submit, check that a file has been selected
     * and submit the form.
     */
    $(picForm).submit(function () {
        // validate args
        if($(picInput).val()) {
            $(picInputError).hide();
            $(uploadNewButtons).hide();
            $(uploadProcessing).show();
            return AIM.submit(this, {
                'onStart' : sakai.changepic.startCallback,
                'onComplete' : sakai.changepic.completeCallback
            });
        } else {
            // no input, show error
            $(picInputError).show();
            return false;
        }<commit_msg>SAKIII-641 Restrict file format for the profile picture to png,jpg,jpeg and gif<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.
 */

/*global , opensocial, Config, window, alert, $ */

var sakai = sakai || {};

/**
 * @name sakai.changepic
 *
 * @class changepic
 *
 * @description
 * Changepic widget
 *
 * @version 0.0.1
 * @param {String} tuid Unique id of the widget
 * @param {Boolean} showSettings Show the settings of the widget or not
 */
sakai.changepic = function(tuid, showSettings){


    //////////////////////
    // Config Variables //
    //////////////////////

    var realw = 0;
    var realh = 0;
    var picture = false;
    var ratio = 1;
    var userSelection = null; // The object returned by imgAreaSelect that contains the user his choice.
    var me = null;
    var imageareaobject;

    // These values are just in case there are no css values specified.
    // If you want to change the size of a thumbnail please do this in the CSS.
    var thumbnailWidth = 100;
    var thumbnailHeight = 100;


    //////////////
    // CSS IDS  //
    //////////////

    var containerTrigger = '#changepic_container_trigger'; // This is the id that will trigger this widget.

    // tabs
    var tabSelect = "#changepic_select";
    var tabUpload = "#changepic_upload";
    var tabSelectContent = "#changepic_selectpicture";
    var tabUploadContent = "#changepic_uploadnew";
    var tabActiveClass = "fl-tabs-active";
    var tabSearchSelected = "search_tab_selected";

    // others
    var container = "#changepic_container";
    var picForm = "#changepic_form";
    var picInput = "#profilepicture";
    var picInputError = "#changepic_nofile_error";
    var uploadProcessing = "#changepic_processing_msg";
    var uploadNewButtons = "#changepic_uploadnew_buttons";
    var pictureMeasurer = "#picture_measurer";
    var pictureMeasurerImage = "#picture_measurer_image";
    var saveNewSelection = "#save_new_selection";
    var fullPicture = '#changepic_fullpicture';
    var thumbnail = "#thumbnail";
    var thumbnailContainer = "#thumbnail_container";

    // An array with selectors pointing to images that need to be changed.
    var imagesToChange = ["#picture_holder img", "#entity_profile_picture", "#myprofile_pic", "#chat_available_me .chat_available_image img", "#profile_userinfo_picture"];


    ///////////////////
    // TAB FUNCTIONS //
    ///////////////////

    /**
     * Will set the upload a new picture as the viewable tab.
     * The other tab will be hidden.
     */
    var showNewTab = function(){

        $(fullPicture).imgAreaSelect({
            hide: true,
            disable: true
        });

        $(tabSelect).removeClass(tabActiveClass);
        $(tabSelect).removeClass(tabSearchSelected);

        $(tabUpload).addClass(tabSearchSelected);
        $(tabUpload).addClass(tabActiveClass);

        $(tabSelectContent).hide();
        $(tabUploadContent).show();
    };

    /**
     * Will show the tab where the user can cut out a square.
     * The upload-a-pic tab will be hidden.
     */
    var showSelectTab = function(){

        $(tabSelect).addClass(tabActiveClass);
        $(tabSelect).addClass(tabSearchSelected);

        $(tabUpload).removeClass(tabSearchSelected);
        $(tabUpload).removeClass(tabActiveClass);

        $(tabSelectContent).show();
        $(tabUploadContent).hide();
    };

    /**
     * Clicked on the upload tab
     */
    $(tabUpload).click(function(){
        showNewTab();
    });

    /**
     * Clicked on the select tab
     */
    $(tabSelect).click(function(){
        sakai.changepic.doInit();
    });

    /**
     * When the user has drawn a square this function will be called by imgAreaSelect.
     * This will draw the thumbnail by modifying it's css values.
     * @param {Object} img    The thumbnail
     * @param {Object} selection The selection object from imgAreaSelect
     */
    function preview(img, selection){
        // Save the user his selection in a global variable.
        userSelection = selection;

        // How much has the user scaled down the image?
        var scaleX = thumbnailWidth / (selection.width || 1);
        var scaleY = thumbnailHeight / (selection.height || 1);

        // Change the thumbnail according to the user his selection via CSS.
        $(thumbnail).css({
            width: Math.round(scaleX * img.width) + 'px',
            height: Math.round(scaleY * img.height) + 'px',
            marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
            marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
        });
    }

     /**
     * Empty upload field by resetting the form
     */
    var resetUploadField = function(){
        $(picInput).val("");
        $(picInputError).hide();
        $(uploadProcessing).hide();
        $(uploadNewButtons).show();
    };

    // Add click event to all cancel buttons in the overlay
    // Since file upload form is reset every time overlay closes do this in init function
    $("#changepic_container .jqmClose").click(function(){
        resetUploadField();
    });

    /**
     * On changepic form submit, check that a file has been selected
     * and submit the form.
     */
    $(picForm).submit(function () {
        // validate args
        // file extension allow for image
        var extensionArray = new Array(".png", ".jpg", ".jpeg",".gif");
        // get file name
        var fileName = $(picInput).val();
        // get extension from the file name.
        var extension = fileName.slice(fileName.indexOf(".")).toLowerCase();
        var allowSubmit = false;

        for (var i = 0; i < extensionArray.length; i++) {
             // extension is acceptable image format
             if (extensionArray[i] === extension) {
                allowSubmit = true;
                break;
             }
        }
        // if image format is acceptable
        if(allowSubmit) {
            $(picInputError).hide();
            $(uploadNewButtons).hide();
            $(uploadProcessing).show();
            return AIM.submit(this, {
                'onStart' : sakai.changepic.startCallback,
                'onComplete' : sakai.changepic.completeCallback
            });
        } else {
            // no input, show error
            $(picInputError).show();
            return false;
        }
{'fraction_non_alphanumeric': 0.12374953686550574, 'fraction_numerical': 0.003260466839570211, 'mean_word_length': 2.375687843921961, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, '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': '7140310', 'n_tokens_mistral': 3637, 'n_tokens_neox': 3523, 'n_words': 1183}