<commit_before>import {get} from 'lodash';

/**
 * @ngdoc controller
 * @module superdesk.apps.authoring
 * @name ChangeImageController
 *
 * @requires $scope
 * @requires gettext
 * @requires notify
 * @requires modal
 * @requires lodash
 * @requires api
 * @requires $rootScope
 * @requires deployConfig
 *
 * @description Controller is responsible for cropping pictures and setting Point of Interest for an image.
 */

export function validateMediaFieldsThrows(validator, metadata) {
    for (let key in validator) {
        const value = metadata[key];
        const regex = new RegExp('^\<*br\/*\>*$', 'i');

        if (validator[key].required && (!value || value.match(regex))) {
            throw gettext('Required field(s) missing');
        }
    }
}

ChangeImageController.$inject = ['$scope', 'gettext', 'notify', 'modal', 'lodash', 'api', '$rootScope',
    'deployConfig', '$q', 'config'];
export function ChangeImageController($scope, gettext, notify, modal, _, api, $rootScope, deployConfig, $q, config) {
    $scope.data = $scope.locals.data;
    $scope.data.cropData = {};
    $scope.validator = deployConfig.getSync('validator_media_metadata');
    const sizes = {};

    const DEFAULT_CONTROLS = {
        brightness: 1,
        contrast: 1,
        saturation: 1,
        rotate: 0,
        fliph: 0,
        flipv: 0,
        isDirty: false,
    };

    const EDITABLE_METADATA = [
        'subject', // required for "usage terms" and other fields based on vocabularies
        'headline',
        'description_text',
        'archive_description',
        'alt_text',
        'byline',
        'copyrightholder',
        'usageterms',
        'copyrightnotice',
        'place',
        'subject',
        'keywords',
    ].concat(Object.keys(get(deployConfig.getSync('schema'), 'picture', {})));

    $scope.controls = angular.copy(DEFAULT_CONTROLS);

    $scope.showMetadata = $scope.data.showMetadata;
    $scope.nav = $scope.data.defaultTab || 'view';
    $scope.hideTabs = $scope.data.hideTabs || [];

    $scope.metadata = {
        isDirty: false
    };

    $scope.crops = {
        isDirty: false
    };

    $scope.data.renditions.forEach((rendition) => {
        const original = $scope.data.item.renditions.original;
        // only extend the item renditions if the original image can fit the rendition dimensions
        // otherwise we will get an error saving

        if (original.height >= rendition.height && original.width >= rendition.width) {
            sizes[rendition.name] = {width: rendition.width, height: rendition.height};
            $scope.data.cropData[rendition.name] = angular.extend({}, $scope.data.item.renditions[rendition.name]);
        }
    });

    $scope.data.isDirty = false;
    $scope.isNew = $scope.data.isNew === true;
    // should show the metadata form in the view
    $scope.data.showMetadataEditor = $scope.data.showMetadataEditor === true;
    // initialize metadata from `item`
    $scope.data.metadata = angular.copy($scope.data.item);
    $scope.selectedRendition = null;
    $scope.selectRendition = function(rendition) {
        if (!rendition) {
            $scope.selectedRendition = null;
        } else if ($scope.selectedRendition === null || $scope.selectedRendition.name !== rendition.name) {
            $scope.selectedRendition = rendition;
        }
    };

    $scope.handleNoImageCrops = function(_origCropsData) {
        if (Object.keys(_origCropsData).length === 0) {
            $scope.data.isDirty = true;
        }
    };

    const _origCropsData = angular.copy($scope.data.cropData);

    $scope.handleNoImageCrops(_origCropsData);

    /**
     * @ngdoc method
     * @name ChangeImageController#isDoneEnabled
     * @public
     * @description if dirty or is new picture item.
     * @returns {Boolean}
     */
    $scope.isDoneEnabled = function() {
        return !$scope.metadata.isDirty &&
            !$scope.controls.isDirty &&
            !$scope.crops.isDirty &&
            !$scope.isAoISelectionModeEnabled;
    };

    /**
    * @ngdoc method
    * @name ChangeImageController#saveCrops
    * @public
    * @description Validate new crop-coordinates and resolve the promise and return
    * modified crop information, point of interest and metadata changes.
    */
    $scope.saveCrops = function() {
        /* Throw an exception if PoI is outside of a crop */
        function poiIsInsideEachCrop() {
            const originalImage = $scope.data.metadata.renditions.original;
<commit_msg>rplaced function call with content<commit_after>import {get} from 'lodash';

/**
 * @ngdoc controller
 * @module superdesk.apps.authoring
 * @name ChangeImageController
 *
 * @requires $scope
 * @requires gettext
 * @requires notify
 * @requires modal
 * @requires lodash
 * @requires api
 * @requires $rootScope
 * @requires deployConfig
 *
 * @description Controller is responsible for cropping pictures and setting Point of Interest for an image.
 */

export function validateMediaFieldsThrows(validator, metadata) {
    for (let key in validator) {
        const value = metadata[key];
        const regex = new RegExp('^\<*br\/*\>*$', 'i');

        if (validator[key].required && (!value || value.match(regex))) {
            throw gettext('Required field(s) missing');
        }
    }
}

ChangeImageController.$inject = ['$scope', 'gettext', 'notify', 'modal', 'lodash', 'api', '$rootScope',
    'deployConfig', '$q', 'config'];
export function ChangeImageController($scope, gettext, notify, modal, _, api, $rootScope, deployConfig, $q, config) {
    $scope.data = $scope.locals.data;
    $scope.data.cropData = {};
    $scope.validator = deployConfig.getSync('validator_media_metadata');
    const sizes = {};

    const DEFAULT_CONTROLS = {
        brightness: 1,
        contrast: 1,
        saturation: 1,
        rotate: 0,
        fliph: 0,
        flipv: 0,
        isDirty: false,
    };

    const EDITABLE_METADATA = [
        'subject', // required for "usage terms" and other fields based on vocabularies
        'headline',
        'description_text',
        'archive_description',
        'alt_text',
        'byline',
        'copyrightholder',
        'usageterms',
        'copyrightnotice',
        'place',
        'subject',
        'keywords',
    ].concat(Object.keys(get(deployConfig.getSync('schema'), 'picture', {})));

    $scope.controls = angular.copy(DEFAULT_CONTROLS);

    $scope.showMetadata = $scope.data.showMetadata;
    $scope.nav = $scope.data.defaultTab || 'view';
    $scope.hideTabs = $scope.data.hideTabs || [];

    $scope.metadata = {
        isDirty: false
    };

    $scope.crops = {
        isDirty: false
    };

    $scope.data.renditions.forEach((rendition) => {
        const original = $scope.data.item.renditions.original;
        // only extend the item renditions if the original image can fit the rendition dimensions
        // otherwise we will get an error saving

        if (original.height >= rendition.height && original.width >= rendition.width) {
            sizes[rendition.name] = {width: rendition.width, height: rendition.height};
            $scope.data.cropData[rendition.name] = angular.extend({}, $scope.data.item.renditions[rendition.name]);
        }
    });

    $scope.data.isDirty = false;
    $scope.isNew = $scope.data.isNew === true;
    // should show the metadata form in the view
    $scope.data.showMetadataEditor = $scope.data.showMetadataEditor === true;
    // initialize metadata from `item`
    $scope.data.metadata = angular.copy($scope.data.item);
    $scope.selectedRendition = null;
    $scope.selectRendition = function(rendition) {
        if (!rendition) {
            $scope.selectedRendition = null;
        } else if ($scope.selectedRendition === null || $scope.selectedRendition.name !== rendition.name) {
            $scope.selectedRendition = rendition;
        }
    };

    const _origCropsData = angular.copy($scope.data.cropData);

    if (_origCropsData && Object.keys(_origCropsData).length === 0) {
        $scope.data.isDirty = true;
    }

    /**
     * @ngdoc method
     * @name ChangeImageController#isDoneEnabled
     * @public
     * @description if dirty or is new picture item.
     * @returns {Boolean}
     */
    $scope.isDoneEnabled = function() {
        return !$scope.metadata.isDirty &&
            !$scope.controls.isDirty &&
            !$scope.crops.isDirty &&
            !$scope.isAoISelectionModeEnabled;
    };

    /**
    * @ngdoc method
    * @name ChangeImageController#saveCrops
    * @public
    * @description Validate new crop-coordinates and resolve the promise and return
    * modified crop information, point of interest and metadata changes.
    */
    $scope.saveCrops = function() {
        /* Throw an exception if PoI is outside of a crop */
        function poiIsInsideEachCrop() {
            const originalImage = $scope.data.metadata.renditions.original;

{'fraction_non_alphanumeric': 0.13165266106442577, 'fraction_numerical': 0.001568627450980392, 'mean_word_length': 2.7222685571309424, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3110454', 'n_tokens_mistral': 2676, 'n_tokens_neox': 2602, 'n_words': 606}