'use strict'; // // declare modules // var app = angular.module('ExternalEmergencyWaitTimes', [ 'ngRoute', //'ngCookies', //'ngAnimate', 'ngResource', //'ngSanitize', ]); app.controller('HomeController', ['$scope', '$rootScope', function ($scope, $rootScope ) { // // Turn off the spinning that the route change handler turned on. // $rootScope.stopSpin(); } ]); app.factory('HTTPErrorInterceptor', ['$rootScope', '$q', '$location', function ( $rootScope, $q, $location ) { return { request: function (request) { return request; }, response: function (response) { return response; }, responseError: function (response) { return $q.reject(response); } } }]); app.config(['$routeProvider', '$controllerProvider', '$locationProvider', '$httpProvider', function ($routeProvider, $controllerProvider, $locationProvider, $httpProvider ) { /*Creating a more synthesized form of service of $ controllerProvider.register*/ app.registerCtrl = $controllerProvider.register; function loadScript(path) { var result = $.Deferred(), script = document.createElement("script"); script.async = "async"; script.type = "text/javascript"; script.src = path; script.onload = script.onreadystatechange = function (_, isAbort) { if (!script.readyState || /loaded|complete/.test(script.readyState)) { if (isAbort) result.reject(); else result.resolve(); } }; script.onerror = function () { result.reject(); }; document.querySelector("head").appendChild(script); return result.promise(); } function loader(arrayName){ return { load: function($q){ var deferred = $q.defer(), map = arrayName.map(function(name) { return loadScript(name); }); $q.all(map).then(function(r){ deferred.resolve(); }); return deferred.promise; } }; } $httpProvider.interceptors.push('HTTPErrorInterceptor'); // use the HTML5 History API $locationProvider.html5Mode(true); var dc = document.getElementById("DataContainer"); let randomNumber = dc.getAttribute("data-random-number"); $routeProvider // // Root handler goes home. // .when('/', { controller: 'HomeController', resolve: loader(['Templates/Home/HomeController.js?random=' + randomNumber ]), templateUrl: 'Templates/Home/Home.html' }) // // C# auto generated templates and controllers. Angular routing handles listing and details, but MVC handles the Reports // .otherwise( { redirectTo: '/' } ); }]); /** * AngularJS default filter with the following expression: * "person in people | filter: {name: $select.search, age: $select.search}" * performs a AND between 'name: $select.search' and 'age: $select.search'. * We want to perform a OR. */ app.filter('propsFilter', function () { return function (items, props) { var out = []; if (angular.isArray(items)) { items.forEach(function (item) { var itemMatches = false; var keys = Object.keys(props); for (var i = 0; i < keys.length; i++) { var prop = keys[i]; var text = props[prop].toLowerCase(); if (item[prop].toString().toLowerCase().indexOf(text) !== -1) { itemMatches = true; break; } } if (itemMatches) { out.push(item); } }); } else { // Let the output be the input untouched out = items; } return out; } }); app.run(['$rootScope', '$location', '$http', '$resource', '$q', function ($rootScope, $location, $http, $resource, $q) { // // Watch the location for particular URLs that we want to send back to the server, rather than deal with in this app. // $rootScope.$watch(function() { return $location.path(); }, function(newValue, oldValue) { }); /* The spinner functions depend on these divs being in the body - they're in the layout view/
*/ // start by displaying the title bar, as most screens in this app will be using it. $rootScope.titleBar = { visible: true }; $rootScope.appTitle = "ExternalEmergencyWaitTimesUserInterface"; $rootScope.userDisplayName = ""; $rootScope.userPrincipalName = ""; $rootScope.spinning = false; $rootScope.startSpin = function () { try { if ($rootScope.spinning == false) { $rootScope.spinning = true; $('#spinnerModal').fadeIn('medium'); $('#spinnerFade').fadeIn('medium'); } } catch (err) { console.log(err); } } $rootScope.stopSpin = function () { try { if ($rootScope.spinning == true) { $('#spinnerModal').fadeOut('medium'); $('#spinnerFade').fadeOut('medium'); $rootScope.spinning = false; } } catch (err) { console.log(err); } } $rootScope.b64toBlob = function(b64Data, contentType, sliceSize) { contentType = contentType || ''; sliceSize = sliceSize || 512; var byteCharacters = atob(b64Data); var byteArrays = []; for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) { var slice = byteCharacters.slice(offset, offset + sliceSize); var byteNumbers = new Array(slice.length); for (var i = 0; i < slice.length; i++) { byteNumbers[i] = slice.charCodeAt(i); } var byteArray = new Uint8Array(byteNumbers); byteArrays.push(byteArray); } var blob = new Blob(byteArrays, {type: contentType}); return blob; } $rootScope.$on("$routeChangeStart", function (event, next, current) { // // Do this on each route change. It's up to each controller to stop it when they're ready. // $rootScope.startSpin(); }); $rootScope.loadScript = function(path) { var result = $.Deferred(), script = document.createElement("script"); script.async = "async"; script.type = "text/javascript"; script.src = path; script.onload = script.onreadystatechange = function (_, isAbort) { if (!script.readyState || /loaded|complete/.test(script.readyState)) { if (isAbort) result.reject(); else result.resolve(); } }; script.onerror = function () { result.reject(); }; document.querySelector("head").appendChild(script); return result.promise(); } $rootScope.loadController = function(arrayName){ return { load: function(){ var deferred = $q.defer(), map = arrayName.map(function(name) { return $rootScope.loadScript(name); }); $q.all(map).then(function(r){ deferred.resolve(); }); return deferred.promise; } }; } }]);