Source: directives/webview/webview.js

/**
 * @ngdoc directives
 * @name Webview
 * @module s4c.directives.webview.Webview
 *
 * @description
 * `Webview` Controller do módulo de Webview
 * 
 * 
 */
(function () {
    'use strict';

    webviewController.$inject = [
        '$scope',
        'Preset',
        '$q',
        '$sce',
        '$timeout'
    ];


    function webviewController(
        $scope,
        Preset,
        $q,
        $sce, $timeout) {

            

        $scope.res = $scope.$root.res;
        $scope.id = Math.random().toString(36).substring(7);
        $scope.webviewId = $scope.id + '1';
        $scope.divWebviewId = $scope.id + '2';

        if (!Preset.webviewIds) {
            Preset.webviewIds = [];
        }

        $scope.link;
        $scope.address;
        /**
         * @method ok
         */
        $scope.ok = function () {

            if (!$scope.address) {
                return;
            }

            updateLink($scope.address);
            $scope.hasLink = true;

            Preset.webviewIds.splice(Preset.webviewIds.indexOf($scope.webviewObject), 1);
            $scope.webviewObject = { id: $scope.id, isVisible: $scope.hasLink };
            Preset.webviewIds.push($scope.webviewObject);
        };

        /**
         * @method updateLink
         * @param {*} link 
         * @param {*} noUpdate 
         */
        function updateLink(link, noUpdate) {

            if(!link){
                return;
            }

            if (link.indexOf('http://') == -1 && link.indexOf('https://') == -1) {
                link = 'http://' + link;
            }

            $scope.modulo.extras = { url: link };
            if (!noUpdate) {
                $scope.link = $sce.trustAsResourceUrl(link);
            }

            Preset.salvar();
            $scope.hasLink = true;
        }

        var modulos = Preset.obter().PresetModulos;
        $scope.presetId = Preset.obter().id;

        $timeout(function () {
            for (var index in modulos) {
                if ((modulos[index].Face && modulos[index].Face.template == 'app/components/modulos/modulo-webview.html') && !modulos[index].usado) {

                    modulos[index].usado = true;
                    $scope.modulo = modulos[index];

                    if (!modulos[index].Face.endereco && (!$scope.modulo.extras || !$scope.modulo.extras.url)) {
                        $scope.hasLink = false;

                        //Exibe a barra de navegação
                        $('#' + modulos[index].id + ' .barraModulo').addClass('mostraBarra');
                        $('#' + modulos[index].id + ' .barraModulo .md-toolbar-tools span').addClass('menuFonte');
                        $('#' + modulos[index].id + ' .barraModulo').css('position', 'absolute');
                        $('#' + modulos[index].id + ' .btn-action').removeClass('show');
                        $('#' + modulos[index].id + ' .wrapper').removeClass('topClass');
                        $('#' + modulos[index].id + ' .webViewNav').parent().next().addClass('mt-5');
                    } else {
                        $scope.hasLink = true;

                        if (!$scope.modulo.extras) {
                            $scope.modulo.extras = {};
                        }
                        updateLink($scope.modulo.extras.url || modulos[index].Face.endereco, false);
                    }

                    $scope.webviewObject = { id: $scope.id, isVisible: $scope.hasLink };
                    Preset.webviewIds.push($scope.webviewObject);

                    addWebViewListeners();
                    break;
                }
            }
        }, 300);

        /**
         * @method addWebViewListeners
         */
        function addWebViewListeners(){

            var document_ = document.getElementById($scope.modulo.id);
            var webView = document_.getElementsByTagName("webview")[0];
            webView.addEventListener('new-window', function (e) {

                if (e.url == webView.getURL()) {
                    return;
                }

                webView.src = e.url;
                updateLink(e.url);
                $('#' + $scope.modulo.id + ' .addressClass').val(webView.getURL());
            });

            webView.addEventListener('did-stop-loading', function () {
                updateLink(webView.getURL(), true);
                $('#' + $scope.modulo.id + ' .addressClass').val(webView.getURL());
                $('#' + $scope.modulo.id + ' .addressClass').blur();

                Preset.salvar();
            });
        }

        angular.extend($scope, {

        });
    }

    function s4cWebview() {
        return {
            restrict: 'EA',
            templateUrl: 'app/directives/webview/webview.html',
            scope: {},
            controller: webviewController
        };
    }

    angular.module('s4c.components.webview', [])
        .directive('s4cWebview', s4cWebview);
})();