typescript/ionic/angular front end project

ionicUIRouter.js 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. function checkState(check, state){
  2. var setTo = check;
  3. var states = state.get();
  4. var foundAlternative = false;
  5. var currentState = state.current;
  6. if (!currentState.views){
  7. return setTo;
  8. }
  9. var checkView = setTo+'_'+Object.keys(currentState.views)[0];
  10. for (var i=0;i<states.length;i++){
  11. if (states[i].name == setTo){
  12. return setTo;
  13. }
  14. if (states[i].name == checkView){
  15. foundAlternative = true;
  16. }
  17. }
  18. if (foundAlternative){
  19. return checkView;
  20. }else{
  21. return setTo;
  22. }
  23. }
  24. angular.module('ionicUIRouter', [])
  25. .config([
  26. '$provide', '$stateProvider',
  27. function($provide, $stateProvider) {
  28. function $StateDecorator($state) {
  29. $state.__go = $state.go;
  30. $state.go = function(to, params, options){
  31. to = checkState(to, $state);
  32. return $state.__go(to, params, options);
  33. }
  34. return $state;
  35. }
  36. $provide.decorator('$state', ['$delegate', $StateDecorator]);
  37. $stateProvider.__state = $stateProvider.state;
  38. $stateProvider.state = function(statename, data){
  39. if (data.views){
  40. var numKeys = 0;
  41. for (var key in data.views){
  42. numKeys++;
  43. if (numKeys > 1){
  44. for (var key in data.views){
  45. var copy = {};
  46. angular.copy(data, copy);
  47. copy.views = {};
  48. copy.views[key] = data.views[key];
  49. copy.url = '/'+key+data.url;
  50. $stateProvider.__state(statename+'_'+key, copy);
  51. }
  52. return $stateProvider;
  53. }
  54. }
  55. }
  56. $stateProvider.__state(statename, data);
  57. return $stateProvider;
  58. }
  59. }])
  60. .directive('uiSref', function($state){
  61. return {
  62. restrict: 'A',
  63. priority: -1,
  64. link: function(scope, element, attrs){
  65. attrs.uiSref = checkState(attrs.uiSref, $state);
  66. }
  67. }
  68. })
  69. ;