<commit_before>(function() {
'use strict';

angular.module('superdesk.editor2.embed', []).controller('SdAddEmbedController', SdAddEmbedController);

SdAddEmbedController.$inject = ['embedService', '$element', '$timeout', '$q', 'lodash', 'EMBED_PROVIDERS', '$scope', 'editor', '$http'];
function SdAddEmbedController (embedService, $element, $timeout, $q, _, EMBED_PROVIDERS, $scope, editor, $http) {
    var vm = this;
    angular.extend(vm, {
        editorCtrl: undefined,  // defined in link method
        previewLoading: false,
        toggle: function(close) {
            // use parameter or toggle
            vm.extended = angular.isDefined(close) ? !close : !vm.extended;
        },
        /**
         * Return html code to represent an embedded link
         *
         * @param {string} url
         * @param {string} title
         * @param {string} description
         * @param {string} illustration
         * @return {string} html
         */
        linkToHtml: function(url, title, description, illustration) {
            var html = [
                '<div class="embed--link">',
                angular.isDefined(illustration) ?
                '  <img src="' + illustration + '" class="embed--link__illustration"/>' : '',
                '  <div class="embed--link__title">',
                '      <a href="' + url + '" target="_blank">' + title + '</a>',
                '  </div>',
                '  <div class="embed--link__description">' + description + '</div>',
                '</div>'];
            return html.join('\n');
        },
        retrieveEmbed:function() {
            function retrieveEmbedFromUrl() {
                return embedService.get(vm.input).then(function(data) {
                    var embed = data.html;
                    if (!angular.isDefined(embed)) {
                        if (data.type === 'link') {
                            embed = vm.linkToHtml(data.url, data.title, data.description, data.thumbnail_url);
                        } else {
                            embed = editor.generateImageTag({url: data.url, altText: data.description});
                        }
                    }
                    return $q.when(embed).then(function(embed) {
                        return {
                            body: embed,
                            provider: data.provider_name || EMBED_PROVIDERS.custom
                        };
                    });
                });
            }
            function parseRawEmbedCode() {
                var waitFor = [];
                var embedBlock = {
                    body: vm.input,
                    provider: EMBED_PROVIDERS.custom
                };
                var providersKnown = [
                    {
                        pattern: /twitter\.com\/widgets\.js/g,
                        name: EMBED_PROVIDERS.twitter
                    },
                    {
                        pattern: /www\.youtube\.com/g,
                        name: EMBED_PROVIDERS.youtube
                    },
                    {
                        pattern: /src="(.*vidible\.tv.*pid=.*.js)/g,
                        name: EMBED_PROVIDERS.vidible,
                        callback: function(match) {
                            var url = 'https://' + match[1];
                            // $http raise a Same Origin Policy error, so we use jquery here
                            waitFor.push(
                                $q(function(resolve, reject) {
                                    $.ajax({
                                        url: url,
                                        type: 'GET',
                                        dataType: 'text',
                                        success: function(data) {
                                            data = /({.*})/.exec(data)[1];
                                            data = JSON.parse(data);
                                            // set association
                                            embedBlock.association = {
                                                uri: data.bid.id,
                                                title: data.bid.videos[0].name,
                                                type: 'video',
                                                thumbnail: data.bid.videos[0].thumbnail,
                                                url: data.bid.videos[0].videoUrls[0],
                                                // size: ,
                                                // creationDate: ,
                                                company: data.bid.videos[0].studioName,
                                                duration: data.bid.videos[0].metadata.duration,
                                            };
                                            resolve(embedBlock);
                                        }
                                    });
                                })
                            );
                        }
                    }
                ];
                // try to guess the provider of the custom embed
                for (var i = 0; i < providersKnown.length; i++) {
                    var provider = providersKnown[i];
                    var match = provider.pattern.exec(vm.input);
                    if (match) {
                        embedBlock.provider = provider.name;
                        if (provider.callback) {
                            provider.callback(match);
                        }
                        break;
                    }
                }
                return $q.all(waitFor).then(function() {
                    return embedBlock;
                });
            }
            var embedCode;
            // if it's an url, use embedService to retrieve the embed code
            if (_.startsWith(vm.input, 'http')) {
                embedCode = retrieveEmbedFromUrl(vm.input);
            // otherwise we use the content of the field directly
            } else {
                embedCode = parseRawEmbedCode(vm.input);
            }
            return $q.when(embedCode);
        },
        updatePreview: function() {
            vm.previewLoading = true;
            vm.retrieveEmbed().then(function(embed) {
                angular.element($element).find('.preview').html(embed.body.replace('\\n', ''));
                vm.previewLoading = false;
            });
        },
        createFigureBlock: function(data) {
            // create a new block containing the embed
            data.blockType = 'embed';
            return vm.editorCtrl.insertNewBlock(vm.addToPosition, data);
        },
        createBlockFromEmbed: function() {
            vm.retrieveEmbed().then(function(embed) {
                vm.createFigureBlock({
                    embedType: embed.provider,
                    body: embed.body,
                    association: embed.association
                });
                // close the addEmbed form
                vm.toggle(true);
            });
        }
    });

    // toggle when the `extended` directive attribute changes
    $scope.$watch(function() {
        return vm.extended;
    }, function(extended) {
        // on enter, focus on input
        if (angular.isDefined(extended)) {
            if (extended) {
                $timeout(function() {
                    angular.element($element).find('input').focus();
                }, 500, false); // positive timeout because of a chrome issue
            // on leave, clear field
            } else {<commit_msg>Editor2 embed field: Trigger onClose only if it was open before<commit_after>(function() {
'use strict';

angular.module('superdesk.editor2.embed', []).controller('SdAddEmbedController', SdAddEmbedController);

SdAddEmbedController.$inject = ['embedService', '$element', '$timeout', '$q', 'lodash', 'EMBED_PROVIDERS', '$scope', 'editor', '$http'];
function SdAddEmbedController (embedService, $element, $timeout, $q, _, EMBED_PROVIDERS, $scope, editor, $http) {
    var vm = this;
    angular.extend(vm, {
        editorCtrl: undefined,  // defined in link method
        previewLoading: false,
        toggle: function(close) {
            // use parameter or toggle
            vm.extended = angular.isDefined(close) ? !close : !vm.extended;
        },
        /**
         * Return html code to represent an embedded link
         *
         * @param {string} url
         * @param {string} title
         * @param {string} description
         * @param {string} illustration
         * @return {string} html
         */
        linkToHtml: function(url, title, description, illustration) {
            var html = [
                '<div class="embed--link">',
                angular.isDefined(illustration) ?
                '  <img src="' + illustration + '" class="embed--link__illustration"/>' : '',
                '  <div class="embed--link__title">',
                '      <a href="' + url + '" target="_blank">' + title + '</a>',
                '  </div>',
                '  <div class="embed--link__description">' + description + '</div>',
                '</div>'];
            return html.join('\n');
        },
        retrieveEmbed:function() {
            function retrieveEmbedFromUrl() {
                return embedService.get(vm.input).then(function(data) {
                    var embed = data.html;
                    if (!angular.isDefined(embed)) {
                        if (data.type === 'link') {
                            embed = vm.linkToHtml(data.url, data.title, data.description, data.thumbnail_url);
                        } else {
                            embed = editor.generateImageTag({url: data.url, altText: data.description});
                        }
                    }
                    return $q.when(embed).then(function(embed) {
                        return {
                            body: embed,
                            provider: data.provider_name || EMBED_PROVIDERS.custom
                        };
                    });
                });
            }
            function parseRawEmbedCode() {
                var waitFor = [];
                var embedBlock = {
                    body: vm.input,
                    provider: EMBED_PROVIDERS.custom
                };
                var providersKnown = [
                    {
                        pattern: /twitter\.com\/widgets\.js/g,
                        name: EMBED_PROVIDERS.twitter
                    },
                    {
                        pattern: /www\.youtube\.com/g,
                        name: EMBED_PROVIDERS.youtube
                    },
                    {
                        pattern: /src="(.*vidible\.tv.*pid=.*.js)/g,
                        name: EMBED_PROVIDERS.vidible,
                        callback: function(match) {
                            var url = 'https://' + match[1];
                            // $http raise a Same Origin Policy error, so we use jquery here
                            waitFor.push(
                                $q(function(resolve, reject) {
                                    $.ajax({
                                        url: url,
                                        type: 'GET',
                                        dataType: 'text',
                                        success: function(data) {
                                            data = /({.*})/.exec(data)[1];
                                            data = JSON.parse(data);
                                            // set association
                                            embedBlock.association = {
                                                uri: data.bid.id,
                                                title: data.bid.videos[0].name,
                                                type: 'video',
                                                thumbnail: data.bid.videos[0].thumbnail,
                                                url: data.bid.videos[0].videoUrls[0],
                                                // size: ,
                                                // creationDate: ,
                                                company: data.bid.videos[0].studioName,
                                                duration: data.bid.videos[0].metadata.duration,
                                            };
                                            resolve(embedBlock);
                                        }
                                    });
                                })
                            );
                        }
                    }
                ];
                // try to guess the provider of the custom embed
                for (var i = 0; i < providersKnown.length; i++) {
                    var provider = providersKnown[i];
                    var match = provider.pattern.exec(vm.input);
                    if (match) {
                        embedBlock.provider = provider.name;
                        if (provider.callback) {
                            provider.callback(match);
                        }
                        break;
                    }
                }
                return $q.all(waitFor).then(function() {
                    return embedBlock;
                });
            }
            var embedCode;
            // if it's an url, use embedService to retrieve the embed code
            if (_.startsWith(vm.input, 'http')) {
                embedCode = retrieveEmbedFromUrl(vm.input);
            // otherwise we use the content of the field directly
            } else {
                embedCode = parseRawEmbedCode(vm.input);
            }
            return $q.when(embedCode);
        },
        updatePreview: function() {
            vm.previewLoading = true;
            vm.retrieveEmbed().then(function(embed) {
                angular.element($element).find('.preview').html(embed.body.replace('\\n', ''));
                vm.previewLoading = false;
            });
        },
        createFigureBlock: function(data) {
            // create a new block containing the embed
            data.blockType = 'embed';
            return vm.editorCtrl.insertNewBlock(vm.addToPosition, data);
        },
        createBlockFromEmbed: function() {
            vm.retrieveEmbed().then(function(embed) {
                vm.createFigureBlock({
                    embedType: embed.provider,
                    body: embed.body,
                    association: embed.association
                });
                // close the addEmbed form
                vm.toggle(true);
            });
        }
    });

    // toggle when the `extended` directive attribute changes
    $scope.$watch(function() {
        return vm.extended;
    }, function(extended, wasExtended) {
        // on enter, focus on input
        if (angular.isDefined(extended)) {
            if (extended) {
                $timeout(function() {
                    angular.element($element).find('input').focus();
                }, 500, false); // positive timeout because of a chrome issue
            // on leave, clear field
            } else if (wasExtended) {
{'fraction_non_alphanumeric': 0.10631055250065462, 'fraction_numerical': 0.001767478397486253, 'mean_word_length': 0.9778612118073537, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 78, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6976461', 'n_tokens_mistral': 3495, 'n_tokens_neox': 3262, 'n_words': 713}