|
| 1 | +// Make sure to include the `ui.router` module as a dependency |
| 2 | +angular.module('uiRouterSample', [ |
| 3 | + 'uiRouterSample.contacts', |
| 4 | + 'uiRouterSample.contacts.service', |
| 5 | + 'uiRouterSample.utils.service', |
| 6 | + 'ui.router', |
| 7 | + 'ngAnimate' |
| 8 | +]) |
| 9 | + |
| 10 | +.run( ['$rootScope', '$state', '$stateParams', '$timeout', '$location', |
| 11 | + function ($rootScope, $state, $stateParams, $timeout, $location) { |
| 12 | + $rootScope.$state = $state; |
| 13 | + $rootScope.$stateParams = $stateParams; |
| 14 | + |
| 15 | + var steps = [ |
| 16 | + { state: 'about', params: { } }, |
| 17 | + { state: 'contacts.list', params: { } }, |
| 18 | + { state: 'contacts.detail', params: { contactId: "1" } }, |
| 19 | + { state: 'contacts.detail', params: { contactId: "42" } }, |
| 20 | + { state: 'contacts.detail', params: { contactId: "123" } }, |
| 21 | + { state: 'contacts.detail', params: { contactId: "1" } }, |
| 22 | + { state: 'contacts.detail', params: { contactId: "42" } }, |
| 23 | + { state: 'contacts.detail', params: { contactId: "123" } }, |
| 24 | + { state: 'about', params: { } }, |
| 25 | + { state: 'contacts.list', params: { } }, |
| 26 | + { state: 'contacts.detail', params: { contactId: "1" } }, |
| 27 | + { state: 'contacts.list', params: { } }, |
| 28 | + { state: 'contacts.detail', params: { contactId: "42" } }, |
| 29 | + { state: 'contacts.list', params: { } }, |
| 30 | + { state: 'contacts.detail', params: { contactId: "123" } }, |
| 31 | + { state: 'contacts.detail.item', params: { contactId: "123", itemId: 'a' } }, |
| 32 | + { state: 'contacts.detail.item', params: { contactId: "123", itemId: 'b' } }, |
| 33 | + { state: 'contacts.detail.item', params: { contactId: "123", itemId: 'a' } }, |
| 34 | + { state: 'contacts.detail.item', params: { contactId: "123", itemId: 'b' } }, |
| 35 | + { state: 'contacts.detail.item.edit', params: { contactId: "123", itemId: 'b' } }, |
| 36 | + { state: 'contacts.detail.item', params: { contactId: "123", itemId: 'b' } }, |
| 37 | + { state: 'contacts.detail.item.edit', params: { contactId: "123", itemId: 'b' } }, |
| 38 | + { state: 'contacts.detail.item', params: { contactId: "123", itemId: 'b' } }, |
| 39 | + { state: 'contacts.detail.item.edit', params: { contactId: "123", itemId: 'b' } }, |
| 40 | + ]; |
| 41 | + |
| 42 | + $rootScope.totalTransitions = 0; |
| 43 | + $rootScope.running = false; |
| 44 | + |
| 45 | + var idx = 0; |
| 46 | + var tick = function() { |
| 47 | + if (!$rootScope.running) return; |
| 48 | + idx = (idx + 1) % steps.length; |
| 49 | + const go = () => |
| 50 | + $rootScope.running && $state.go(steps[idx].state, steps[idx].params) |
| 51 | + .then(() => $rootScope.totalTransitions++); |
| 52 | + |
| 53 | + Promise.resolve($state.transition) |
| 54 | + .then(() => new Promise(resolve => setTimeout(resolve, 50))) |
| 55 | + .then(go) |
| 56 | + .then(tick); |
| 57 | + } |
| 58 | + |
| 59 | + let scheduledStartTime; |
| 60 | + $rootScope.startStop = function() { |
| 61 | + if ($rootScope.running) { |
| 62 | + stop(); |
| 63 | + } else { |
| 64 | + start(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + function start() { |
| 69 | + scheduledStartTime = Date.now() + 2500; |
| 70 | + |
| 71 | + const countdown = () => { |
| 72 | + $rootScope.countdown = Math.max(0, scheduledStartTime - Date.now()); |
| 73 | + if ($rootScope.countdown === 0) { |
| 74 | + $rootScope.running = true; |
| 75 | + tick(); |
| 76 | + } else { |
| 77 | + $timeout(countdown, 50); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + $timeout(countdown, 50); |
| 82 | + } |
| 83 | + |
| 84 | + function stop() { |
| 85 | + $rootScope.running = false; |
| 86 | + } |
| 87 | + |
| 88 | + $rootScope.ngVersions = [ |
| 89 | + // "1.2.0", "1.2.1", "1.2.2", "1.2.3", "1.2.4", "1.2.5", "1.2.6", "1.2.7", "1.2.8", "1.2.9", "1.2.10", |
| 90 | + // "1.2.11", "1.2.12", "1.2.13", "1.2.14", "1.2.15", "1.2.16", "1.2.17", "1.2.18", "1.2.19", "1.2.20", |
| 91 | + // "1.2.21", "1.2.22", "1.2.23", |
| 92 | + "1.2.27", "1.2.28", "1.2.29", "1.2.30", "1.2.31", "1.2.32", |
| 93 | + "1.3.0", "1.3.1", "1.3.2", "1.3.3", "1.3.4", "1.3.5", "1.3.6", "1.3.7", "1.3.8", "1.3.9", "1.3.10", |
| 94 | + "1.3.11", "1.3.12", "1.3.13", "1.3.14", "1.3.15", "1.3.16", "1.3.17", "1.3.18", "1.3.19", "1.3.20", "1.3.36", |
| 95 | + "1.4.0", "1.4.1", "1.4.2", "1.4.3", "1.4.4", "1.4.5", "1.4.6", "1.4.7", "1.4.8", "1.4.9", "1.4.10", |
| 96 | + "1.4.11", "1.4.12", "1.4.13", "1.4.14", |
| 97 | + "1.5.0", "1.5.1", "1.5.2", "1.5.3", "1.5.5", "1.5.6", "1.5.7", "1.5.8", "1.5.9", "1.5.10", "1.5.11", |
| 98 | + "1.6.0", "1.6.1", |
| 99 | + ] |
| 100 | + |
| 101 | + $rootScope.uirVersions = [ |
| 102 | + '0.2.8', '0.2.10', '0.2.11', '0.2.13', '0.2.14', '0.2.15', '0.2.16', '0.2.17', '0.2.18', |
| 103 | + '0.3.0', '0.3.1', '0.3.2', |
| 104 | + '0.4.0', '0.4.1', '0.4.2', |
| 105 | + '1.0.0-alpha.1', '1.0.0-alpha.2', '1.0.0-alpha.3', '1.0.0-alpha.4', '1.0.0-alpha.5', '1.0.0-alpha0', |
| 106 | + '1.0.0-beta.1', '1.0.0-beta.2', '1.0.0-beta.3', '1.0.0-rc.1', |
| 107 | + ] |
| 108 | + |
| 109 | + $rootScope.ngVersion = localStorage.getItem('ngVersion'); |
| 110 | + $rootScope.uirVersion = localStorage.getItem('uirVersion'); |
| 111 | + |
| 112 | + $rootScope.changeNgVersion = () => { |
| 113 | + let newVersion = $rootScope.ngVersion; |
| 114 | + localStorage.setItem('ngVersion', newVersion); |
| 115 | + setTimeout(() => document.location.reload(true), 100); |
| 116 | + } |
| 117 | + |
| 118 | + $rootScope.changeUIRVersion = () => { |
| 119 | + let newVersion = $rootScope.uirVersion; |
| 120 | + localStorage.setItem('uirVersion', newVersion); |
| 121 | + setTimeout(() => document.location.reload(true), 100); |
| 122 | + } |
| 123 | + } |
| 124 | + ] |
| 125 | +) |
| 126 | + |
| 127 | +.config( |
| 128 | + [ '$stateProvider', '$urlRouterProvider', |
| 129 | + function ($stateProvider, $urlRouterProvider) { |
| 130 | + |
| 131 | + ///////////////////////////// |
| 132 | + // Redirects and Otherwise // |
| 133 | + ///////////////////////////// |
| 134 | + |
| 135 | + // Use $urlRouterProvider to configure any redirects (when) and invalid urls (otherwise). |
| 136 | + $urlRouterProvider |
| 137 | + |
| 138 | + // The `when` method says if the url is ever the 1st param, then redirect to the 2nd param |
| 139 | + // Here we are just setting up some convenience urls. |
| 140 | + .when('/c?id', '/contacts/:id') |
| 141 | + .when('/user/:id', '/contacts/:id') |
| 142 | + |
| 143 | + // If the url is ever invalid, e.g. '/asdf', then redirect to '/' aka the home state |
| 144 | + .otherwise('/'); |
| 145 | + |
| 146 | + |
| 147 | + ////////////////////////// |
| 148 | + // State Configurations // |
| 149 | + ////////////////////////// |
| 150 | + |
| 151 | + // Use $stateProvider to configure your states. |
| 152 | + $stateProvider |
| 153 | + |
| 154 | + ////////// |
| 155 | + // Home // |
| 156 | + ////////// |
| 157 | + |
| 158 | + .state("home", { |
| 159 | + |
| 160 | + // Use a url of "/" to set a state as the "index". |
| 161 | + url: "/", |
| 162 | + |
| 163 | + // Example of an inline template string. By default, templates |
| 164 | + // will populate the ui-view within the parent state's template. |
| 165 | + // For top level states, like this one, the parent template is |
| 166 | + // the index.html file. So this template will be inserted into the |
| 167 | + // ui-view within index.html. |
| 168 | + template: '<p class="lead">Welcome to the UI-Router Demo</p>' + |
| 169 | + '<p>Use the menu above to navigate. ' + |
| 170 | + 'Pay attention to the <code>$state</code> and <code>$stateParams</code> values below.</p>' + |
| 171 | + '<p>Click these links—<a href="#/c?id=1">Alice</a> or ' + |
| 172 | + '<a href="#/user/42">Bob</a>—to see a url redirect in action.</p>' |
| 173 | + |
| 174 | + }) |
| 175 | + |
| 176 | + /////////// |
| 177 | + // About // |
| 178 | + /////////// |
| 179 | + |
| 180 | + .state('about', { |
| 181 | + url: '/about', |
| 182 | + |
| 183 | + // Showing off how you could return a promise from templateProvider |
| 184 | + templateProvider: ['$timeout', |
| 185 | + function ( $timeout) { |
| 186 | + return $timeout(function () { |
| 187 | + return '<p class="lead">UI-Router Resources</p><ul>' + |
| 188 | + '<li><a href="https://github.com/angular-ui/ui-router/tree/gh-pages/sample">Source for this Sample</a></li>' + |
| 189 | + '<li><a href="https://github.com/angular-ui/ui-router">GitHub Main Page</a></li>' + |
| 190 | + '<li><a href="https://github.com/angular-ui/ui-router#quick-start">Quick Start</a></li>' + |
| 191 | + '<li><a href="https://github.com/angular-ui/ui-router/wiki">In-Depth Guide</a></li>' + |
| 192 | + '<li><a href="https://github.com/angular-ui/ui-router/wiki/Quick-Reference">API Reference</a></li>' + |
| 193 | + '</ul>'; |
| 194 | + }, 100); |
| 195 | + }] |
| 196 | + }) |
| 197 | + |
| 198 | + .state('instructions', { |
| 199 | + url: '/leaktest-instructions', |
| 200 | + |
| 201 | + // Showing off how you could return a promise from templateProvider |
| 202 | + templateProvider: ['$timeout', |
| 203 | + function ( $timeout) { |
| 204 | + return $timeout(function () { |
| 205 | + return '<p class="lead">UI-Router Resources</p><ul>' + |
| 206 | + '<li><a href="https://github.com/angular-ui/ui-router/tree/gh-pages/sample">Source for this Sample</a></li>' + |
| 207 | + '<li><a href="https://github.com/angular-ui/ui-router">GitHub Main Page</a></li>' + |
| 208 | + '<li><a href="https://github.com/angular-ui/ui-router#quick-start">Quick Start</a></li>' + |
| 209 | + '<li><a href="https://github.com/angular-ui/ui-router/wiki">In-Depth Guide</a></li>' + |
| 210 | + '<li><a href="https://github.com/angular-ui/ui-router/wiki/Quick-Reference">API Reference</a></li>' + |
| 211 | + '</ul>'; |
| 212 | + }, 100); |
| 213 | + }] |
| 214 | + }) |
| 215 | + } |
| 216 | + ] |
| 217 | +); |
0 commit comments