--- /dev/null
+(function() {
+ "use strict";
+
+ angular
+ .module("nero.app.external-services")
+ .directive("connectorDirective", connectorDirective);
+
+ function connectorDirective() {
+ return {
+ restrict: "E",
+ scope: {
+ forceNewTab: "=",
+ page: "@?"
+ },
+ templateUrl: Global.Theme.getURLPortal() + Liferay.ThemeDisplay.getPathJavaScript() + "/angularjs/app-external-services/connector/connector.html?t="+Liferay.extTimestamp,
+ controller: ConnectorController,
+ controllerAs: "ConnectorCtrl",
+ bindToController: true
+ };
+ }
+
+ ConnectorController.$inject = ["$window", "$sce", "toastr", "connectorService"];
+
+ function ConnectorController($window, $sce, toastr, connectorService) {
+ var vm = this;
+
+ vm.contextPath = contextPath;
+ vm.urls = [];
+ vm.serviceName = "";
+ vm.serviceKey = "";
+ vm.isHttps = isHttps;
+ vm.getIframeUrl = getIframeUrl;
+ vm.onChangeUrl = onChangeUrl;
+ vm.openInNewTab = openInNewTab;
+
+ var serviceWindow = null;
+
+ // Load current service configuration
+ connectorService.loadConnectorConfig().then(function(config) {
+ vm.serviceSchoolUrls = config.serviceSchoolUrls;
+ if (vm.serviceSchoolUrls.length > 0) {
+ vm.selectedUrl = vm.serviceSchoolUrls[0].serviceUrl;
+ if (vm.page != undefined && vm.page.length > 0) {
+ vm.selectedUrl += "?page="+vm.page;
+ }
+ onChangeUrl();
+ }
+ vm.serviceName = config.serviceName;
+ vm.serviceKey = config.serviceKey;
+ })
+ ["catch"](function(message) {
+ toastr.error(message);
+ });
+
+ /**
+ Return url to display in iframe (need to check it before display)
+ */
+ function getIframeUrl() {
+ return $sce.trustAsResourceUrl(vm.selectedUrl);
+ }
+
+ /**
+ Return true if the current URL is secure
+ */
+ function isHttps() {
+ // Opening new tab
+ if (vm.forceNewTab) {
+ return false;
+ }
+ // Check if url start with https
+ if (vm.selectedUrl.lastIndexOf("https", 0) != 0) {
+ return false;
+ }
+ else if (vm.selectedUrl.indexOf("login?service=") != -1) {
+ return (vm.selectedUrl.indexOf("service=https") != -1);
+ }
+ else {
+ return true;
+ }
+ }
+
+ /**
+ If the current URL is not secure open it in new tab
+ */
+ function onChangeUrl() {
+ if (!isHttps()) {
+ openInNewTab();
+ }
+ }
+
+ /**
+ Open current URL in new tab
+ */
+ function openInNewTab() {
+ if(serviceWindow == null || serviceWindow.closed) {
+ serviceWindow = $window.open(vm.selectedUrl, "_blank");
+ }
+ else {
+ serviceWindow.location = vm.selectedUrl;
+ }
+ }
+
+ }
+
+})();