/*$(document).ready(function () { loadAngular(); }); */ $platform.angularLoaded = 0; $platform.angular = -1; function setAngularApp() { $platform.angularApp = $platform.angularDirectives = angular.module("civicoreAngularApp", []).config(function ($sceDelegateProvider, $compileProvider) { $platform.angularDirectives = $compileProvider; $sceDelegateProvider.resourceUrlWhitelist([ // Allow same origin resource loads. "self", // Allow loading from our assets domain. Notice the difference between * and **. "https://s3-us-west-1.amazonaws.com/framework-front-end**", "https://civicore-fw-common.s3-us-west-2.amazonaws.com/lib/js/civicoreApplication**", "http://civicore-fw-common.s3-us-west-2.amazonaws.com/lib/js/civicoreApplication**", "https://ddb9l06w3jzip.cloudfront.net/lib/js/civicoreApplication**", "http://ddb9l06w3jzip.cloudfront.net/lib/js/civicoreApplication**", "https://ddb9l06w3jzip.cloudfront.net/lib**", "http://ddb9l06w3jzip.cloudfront.net/lib**" ]) }) .factory('FrameworkFunctions', ['$q', '$injector', function ($q, $injector) { return { getTemplateURL: function (directive, template) { if (typeof template == "undefined" || typeof template == "" || template == "" || template == "undefined") { if (typeof $platform.defaultTemplates[directive] == "undefined") { template = directive; } else { template = $platform.defaultTemplates[directive]; } } return $platform.angularLibBaseURL + this.directiveToDirectory(directive) + "/" + template + ".html"; }, directiveToDirectory: function (directive) { var directiveParts = directive.split("-"); directiveParts.shift(); // Remove ca from the front var folder = directiveParts.join("/"); return folder; }, loadDirective: function (directive, requiredDirectives) { var deferred = $q.defer(); // If we are undefined, than we didn't pass in required directives, make it an array if (typeof requiredDirectives == "undefined") { requiredDirectives = []; } // If we are a string, we only have 1 required directive. Turn it into an array if (typeof requiredDirectives == "string") { requiredDirectives = [requiredDirectives]; } // Add our directive to the array requiredDirectives.push(directive); // Load directives var loadedDirectives = 0; for (var i in requiredDirectives) { this.loadDirective_helper(requiredDirectives[i]).then(function () { // Increment count of directives loaded loadedDirectives += 1; // If our count matches our length, than resolve promise if (loadedDirectives == requiredDirectives.length) { deferred.resolve(); } }); } return deferred.promise; }, loadDirective_helper: function (directive) { var deferred = $q.defer(); if (!$injector.has($.camelCase(directive) + "Directive")) { var directiveFile = this.directiveToDirectory(directive) + "/" + directive + ".js" // download the javascript file var script = document.createElement('script'); script.src = $platform.angularLibBaseURL + directiveFile; script.onload = function () { deferred.resolve(); }; document.getElementsByTagName('head')[0].appendChild(script); } else { deferred.resolve(); } return deferred.promise; } } } ]) .controller("civicoreAngularController", ["$scope", "$compile", "$injector", "$q", "$http", "FrameworkFunctions", function ($scope, $compile, $injector, $q, $http, FrameworkFunctions) { $platform.angular = $scope; $scope.modules = []; $scope.fields = []; var modulePrototype = function (module) { var $this = Object.assign(module, this); $this.call = function (functionRequest, variables) { postData = variables; postData["ajaxFunction"] = functionRequest; postData = variables; var request = $http({ url: $platform.url + "?ajaxRequest=" + this.module_id, data: postData, method: 'POST', headers: {'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'}, transformRequest: function (obj) { return $.param(obj); }, withCredentials: true, crossDomain: true }); return request; } return $this; } $scope.compileElm = function (elm) { $compile(elm)($scope); } $scope.createModule = function (moduleNumber, angularOptions, module, location) { $scope.fields[moduleNumber] = []; $("body").append(module.oldModuleObject); $scope.modules[moduleNumber] = new modulePrototype(module); $scope.modules[moduleNumber].module_id = moduleNumber; FrameworkFunctions.loadDirective(angularOptions.directive, angularOptions.requiredDirectives).then(function () { if (typeof angularOptions.dialog != "undefined") { var dialog = document.createElement("ca-ui-dialog"); var elm = document.createElement(angularOptions.directive); elm.setAttribute("module", "modules[" + moduleNumber + "]"); elm.setAttribute("template", angularOptions.template); $(dialog).append(elm); $compile(dialog)($scope); $("body").append(dialog); } else { var elm = document.createElement(angularOptions.directive); elm.setAttribute("module", "modules[" + moduleNumber + "]"); elm.setAttribute("template", angularOptions.template); $compile(elm)($scope); $(location).html(elm); } }); } $scope.setLoginDetails = function (user) { $scope.user = user; } $scope.logout = function (callback) { $platform.logout(callback); } setTimeout(function () { $platform.angularAppLoaded(); }, 0); $('[civicore=loginDetails]').each(function () { $compile(this)($scope); }).show(); }]) .filter("to_trusted", ["$sce", function ($sce) { return function (text) { if (typeof text != "string") return ""; return $sce.trustAsHtml(text); }; }]) .filter("orderObjectBy", function () { return function (items, field, reverse) { var filtered = []; angular.forEach(items, function (item) { filtered.push(item); }); filtered.sort(function (a, b) { return (a[field] > b[field] ? 1 : -1); }); if (reverse) filtered.reverse(); return filtered; }; }) .filter("dump", function () { return function (variable) { console.log(variable); return ""; }; }) .directive('script', function () { return { restrict: 'E', scope: false, link: function (scope, elem, attr) { if (attr.type === 'text/javascript-lazy') { var code = elem.text(); var f = new Function(code); f(); } } }; }); } function loadAngular() { $platform.loader.loadJS('https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular.min.js').then(function () { angularLoaded(); }); } function angularLoaded() { setAngularApp(); if ($('[ng-app=civicoreAngularApp]').length == 0) { // Load angular $('body').append('
'); } if ($platform.angularLoaded == 0) { angular.element(document).ready(function () { angular.bootstrap(document, ['civicoreAngularApp']); }); } } $platform.defaultTemplates = {}; $platform.runWhenAngularLoads = []; $platform.angularAppLoaded = function () { if ($platform.angularLoaded == 1) { return; } $platform.angularLoaded = 1; for (i in $platform.runWhenAngularLoads) { $platform.runWhenAngularLoads[i](); } } $platform.loadModule = function (json) { if ($platform.angularLoaded) { $platform.angular.createModule(json.module_id, json.angular, json.module, json.location); } else { loadAngular(); $platform.runWhenAngularLoads.push(function () { $platform.angular.createModule(json.module_id, json.angular, json.module, json.location); }); } } // Function for loading an action via template. $platform.loadAction = function (url) { $.ajax($platform.url + url + "&renderMethod=template", { type: "GET", success: function (data, status, xhr) { var ret = JSON.parse(data); // Load login details $platform.angular.setLoginDetails(ret.user); // Check status switch (ret["status"]) { case "success": $platform.loadModule(ret["module"]); break; default: alert("Can't load action for some reason"); } }, xhrFields: { withCredentials: true }, crossDomain: true }); } $platform.logout = function (callback) { $.ajax($platform.url + "?logout=1", { type: "GET", success: function (data, status, xhr) { $platform.angular.user.displayName = ""; $platform.angular.user.loggedIn = 0; if (typeof callback != "undefined") { callback(); } else { window.location = window.location; } }, xhrFields: { withCredentials: true }, crossDomain: true }); }