PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/webapp/bower_components/angular-ui-router/src/common.js

https://gitlab.com/ndkhoiits/hello-jhipster
JavaScript | 292 lines | 136 code | 30 blank | 126 comment | 27 complexity | 4ffcaf758362623545ada4ffb34eef81 MD5 | raw file
  1. /*jshint globalstrict:true*/
  2. /*global angular:false*/
  3. 'use strict';
  4. var isDefined = angular.isDefined,
  5. isFunction = angular.isFunction,
  6. isString = angular.isString,
  7. isObject = angular.isObject,
  8. isArray = angular.isArray,
  9. forEach = angular.forEach,
  10. extend = angular.extend,
  11. copy = angular.copy;
  12. function inherit(parent, extra) {
  13. return extend(new (extend(function() {}, { prototype: parent }))(), extra);
  14. }
  15. function merge(dst) {
  16. forEach(arguments, function(obj) {
  17. if (obj !== dst) {
  18. forEach(obj, function(value, key) {
  19. if (!dst.hasOwnProperty(key)) dst[key] = value;
  20. });
  21. }
  22. });
  23. return dst;
  24. }
  25. /**
  26. * Finds the common ancestor path between two states.
  27. *
  28. * @param {Object} first The first state.
  29. * @param {Object} second The second state.
  30. * @return {Array} Returns an array of state names in descending order, not including the root.
  31. */
  32. function ancestors(first, second) {
  33. var path = [];
  34. for (var n in first.path) {
  35. if (first.path[n] !== second.path[n]) break;
  36. path.push(first.path[n]);
  37. }
  38. return path;
  39. }
  40. /**
  41. * IE8-safe wrapper for `Object.keys()`.
  42. *
  43. * @param {Object} object A JavaScript object.
  44. * @return {Array} Returns the keys of the object as an array.
  45. */
  46. function objectKeys(object) {
  47. if (Object.keys) {
  48. return Object.keys(object);
  49. }
  50. var result = [];
  51. forEach(object, function(val, key) {
  52. result.push(key);
  53. });
  54. return result;
  55. }
  56. /**
  57. * IE8-safe wrapper for `Array.prototype.indexOf()`.
  58. *
  59. * @param {Array} array A JavaScript array.
  60. * @param {*} value A value to search the array for.
  61. * @return {Number} Returns the array index value of `value`, or `-1` if not present.
  62. */
  63. function indexOf(array, value) {
  64. if (Array.prototype.indexOf) {
  65. return array.indexOf(value, Number(arguments[2]) || 0);
  66. }
  67. var len = array.length >>> 0, from = Number(arguments[2]) || 0;
  68. from = (from < 0) ? Math.ceil(from) : Math.floor(from);
  69. if (from < 0) from += len;
  70. for (; from < len; from++) {
  71. if (from in array && array[from] === value) return from;
  72. }
  73. return -1;
  74. }
  75. /**
  76. * Merges a set of parameters with all parameters inherited between the common parents of the
  77. * current state and a given destination state.
  78. *
  79. * @param {Object} currentParams The value of the current state parameters ($stateParams).
  80. * @param {Object} newParams The set of parameters which will be composited with inherited params.
  81. * @param {Object} $current Internal definition of object representing the current state.
  82. * @param {Object} $to Internal definition of object representing state to transition to.
  83. */
  84. function inheritParams(currentParams, newParams, $current, $to) {
  85. var parents = ancestors($current, $to), parentParams, inherited = {}, inheritList = [];
  86. for (var i in parents) {
  87. if (!parents[i].params) continue;
  88. parentParams = objectKeys(parents[i].params);
  89. if (!parentParams.length) continue;
  90. for (var j in parentParams) {
  91. if (indexOf(inheritList, parentParams[j]) >= 0) continue;
  92. inheritList.push(parentParams[j]);
  93. inherited[parentParams[j]] = currentParams[parentParams[j]];
  94. }
  95. }
  96. return extend({}, inherited, newParams);
  97. }
  98. /**
  99. * Performs a non-strict comparison of the subset of two objects, defined by a list of keys.
  100. *
  101. * @param {Object} a The first object.
  102. * @param {Object} b The second object.
  103. * @param {Array} keys The list of keys within each object to compare. If the list is empty or not specified,
  104. * it defaults to the list of keys in `a`.
  105. * @return {Boolean} Returns `true` if the keys match, otherwise `false`.
  106. */
  107. function equalForKeys(a, b, keys) {
  108. if (!keys) {
  109. keys = [];
  110. for (var n in a) keys.push(n); // Used instead of Object.keys() for IE8 compatibility
  111. }
  112. for (var i=0; i<keys.length; i++) {
  113. var k = keys[i];
  114. if (a[k] != b[k]) return false; // Not '===', values aren't necessarily normalized
  115. }
  116. return true;
  117. }
  118. /**
  119. * Returns the subset of an object, based on a list of keys.
  120. *
  121. * @param {Array} keys
  122. * @param {Object} values
  123. * @return {Boolean} Returns a subset of `values`.
  124. */
  125. function filterByKeys(keys, values) {
  126. var filtered = {};
  127. forEach(keys, function (name) {
  128. filtered[name] = values[name];
  129. });
  130. return filtered;
  131. }
  132. // like _.indexBy
  133. // when you know that your index values will be unique, or you want last-one-in to win
  134. function indexBy(array, propName) {
  135. var result = {};
  136. forEach(array, function(item) {
  137. result[item[propName]] = item;
  138. });
  139. return result;
  140. }
  141. // extracted from underscore.js
  142. // Return a copy of the object only containing the whitelisted properties.
  143. function pick(obj) {
  144. var copy = {};
  145. var keys = Array.prototype.concat.apply(Array.prototype, Array.prototype.slice.call(arguments, 1));
  146. forEach(keys, function(key) {
  147. if (key in obj) copy[key] = obj[key];
  148. });
  149. return copy;
  150. }
  151. // extracted from underscore.js
  152. // Return a copy of the object omitting the blacklisted properties.
  153. function omit(obj) {
  154. var copy = {};
  155. var keys = Array.prototype.concat.apply(Array.prototype, Array.prototype.slice.call(arguments, 1));
  156. for (var key in obj) {
  157. if (indexOf(keys, key) == -1) copy[key] = obj[key];
  158. }
  159. return copy;
  160. }
  161. function pluck(collection, key) {
  162. var result = isArray(collection) ? [] : {};
  163. forEach(collection, function(val, i) {
  164. result[i] = isFunction(key) ? key(val) : val[key];
  165. });
  166. return result;
  167. }
  168. function filter(collection, callback) {
  169. var array = isArray(collection);
  170. var result = array ? [] : {};
  171. forEach(collection, function(val, i) {
  172. if (callback(val, i)) {
  173. result[array ? result.length : i] = val;
  174. }
  175. });
  176. return result;
  177. }
  178. function map(collection, callback) {
  179. var result = isArray(collection) ? [] : {};
  180. forEach(collection, function(val, i) {
  181. result[i] = callback(val, i);
  182. });
  183. return result;
  184. }
  185. /**
  186. * @ngdoc overview
  187. * @name ui.router.util
  188. *
  189. * @description
  190. * # ui.router.util sub-module
  191. *
  192. * This module is a dependency of other sub-modules. Do not include this module as a dependency
  193. * in your angular app (use {@link ui.router} module instead).
  194. *
  195. */
  196. angular.module('ui.router.util', ['ng']);
  197. /**
  198. * @ngdoc overview
  199. * @name ui.router.router
  200. *
  201. * @requires ui.router.util
  202. *
  203. * @description
  204. * # ui.router.router sub-module
  205. *
  206. * This module is a dependency of other sub-modules. Do not include this module as a dependency
  207. * in your angular app (use {@link ui.router} module instead).
  208. */
  209. angular.module('ui.router.router', ['ui.router.util']);
  210. /**
  211. * @ngdoc overview
  212. * @name ui.router.state
  213. *
  214. * @requires ui.router.router
  215. * @requires ui.router.util
  216. *
  217. * @description
  218. * # ui.router.state sub-module
  219. *
  220. * This module is a dependency of the main ui.router module. Do not include this module as a dependency
  221. * in your angular app (use {@link ui.router} module instead).
  222. *
  223. */
  224. angular.module('ui.router.state', ['ui.router.router', 'ui.router.util']);
  225. /**
  226. * @ngdoc overview
  227. * @name ui.router
  228. *
  229. * @requires ui.router.state
  230. *
  231. * @description
  232. * # ui.router
  233. *
  234. * ## The main module for ui.router
  235. * There are several sub-modules included with the ui.router module, however only this module is needed
  236. * as a dependency within your angular app. The other modules are for organization purposes.
  237. *
  238. * The modules are:
  239. * * ui.router - the main "umbrella" module
  240. * * ui.router.router -
  241. *
  242. * *You'll need to include **only** this module as the dependency within your angular app.*
  243. *
  244. * <pre>
  245. * <!doctype html>
  246. * <html ng-app="myApp">
  247. * <head>
  248. * <script src="js/angular.js"></script>
  249. * <!-- Include the ui-router script -->
  250. * <script src="js/angular-ui-router.min.js"></script>
  251. * <script>
  252. * // ...and add 'ui.router' as a dependency
  253. * var myApp = angular.module('myApp', ['ui.router']);
  254. * </script>
  255. * </head>
  256. * <body>
  257. * </body>
  258. * </html>
  259. * </pre>
  260. */
  261. angular.module('ui.router', ['ui.router.state']);
  262. angular.module('ui.router.compat', ['ui.router']);