views.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. module.exports = Em.Route.extend({
  20. route: '/views',
  21. enter: function (router) {
  22. router.get('mainViewsController').loadAmbariViews();
  23. },
  24. index: Em.Route.extend({
  25. route: '/',
  26. connectOutlets: function (router) {
  27. router.get('mainViewsController').dataLoading().done(function() {
  28. router.get('mainController').connectOutlet('mainViews');
  29. });
  30. }
  31. }),
  32. viewDetails: Em.Route.extend({
  33. route: '/:viewName/:version/:instanceName',
  34. connectOutlets: function (router, params) {
  35. // find and set content for `mainViewsDetails` and associated controller
  36. var href = ['/views', params.viewName, params.version, params.instanceName + "/"].join('/');
  37. var viewPath = this.parseViewPath(window.location.href.slice(window.location.href.indexOf('?')));
  38. if (viewPath) {
  39. var slicedInstanceName = this._getSlicedInstanceName(params.instanceName);
  40. if (slicedInstanceName === params.instanceName) {
  41. viewPath = '';
  42. }
  43. href = ['/views', params.viewName, params.version, slicedInstanceName + "/"].join('/');
  44. //remove slash from viewPath since href already contains it at the end
  45. if (viewPath.charAt(0) === '/') viewPath = viewPath.slice(1);
  46. }
  47. router.get('mainViewsController').dataLoading().done(function() {
  48. var content = App.router.get('mainViewsController.ambariViews').filter(function(i) {
  49. return Em.get(i, 'href').endsWith(href);
  50. })[0];
  51. if (content) content.set('viewPath', viewPath);
  52. router.get('mainController').connectOutlet('mainViewsDetails', content);
  53. });
  54. },
  55. /**
  56. * parse the instance name and slice if needed
  57. *
  58. * @param {string}
  59. * @returns {string}
  60. * @private
  61. */
  62. _getSlicedInstanceName: function (instanceName) {
  63. if (instanceName.lastIndexOf('?') > -1) {
  64. return instanceName.slice(0, instanceName.lastIndexOf('?'));
  65. }
  66. return instanceName;
  67. },
  68. /**
  69. * parse internal view path
  70. * "viewPath" - used as a key of additional path
  71. * Example:
  72. * origin URL: viewName?viewPath=%2Fuser%2Fadmin%2Faddress&foo=bar&count=1
  73. * should be translated to
  74. * view path: /user/admin/address?foo=bar&count=1
  75. *
  76. * @param {string} instanceName
  77. * @returns {string}
  78. */
  79. parseViewPath: function (instanceName) {
  80. var path = '';
  81. if (instanceName.contains('?')) {
  82. path = instanceName.slice(instanceName.indexOf('?'));
  83. if (path.contains('viewPath')) {
  84. path = decodeURIComponent(path.slice((path.lastIndexOf('?viewPath=') + 10))).replace('&', '?');
  85. }
  86. }
  87. return path;
  88. }
  89. })
  90. });