angular.module("app").directive("preloadTemplates", function($rootScope, $compile, $http, SQEvents) {
    return {
        restrict: 'E',
        scope: {},
        link: function (scope, element, attrs) {
        
            function downloadAndUpdate(){
                $http.get('/common/templates.html').then(function(response) {
                    $compile(response.data)(scope);

                    window.parent.preloadedTemplates = response.data;
                });
            }

            if (window.parent.preloadedTemplates) {
                $compile(window.parent.preloadedTemplates)(scope);
            } 
            else {
                downloadAndUpdate();
            }
        }
    };
});