views_controller.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. App.MainViewsController = Em.Controller.extend({
  20. name:'mainViewsController',
  21. isDataLoaded: false,
  22. ambariViews: [],
  23. dataLoading: function () {
  24. var viewsController = this;
  25. var dfd = $.Deferred();
  26. if (this.get('isDataLoaded')) {
  27. dfd.resolve(this.get('ambariViews'));
  28. } else {
  29. var interval = setInterval(function () {
  30. if (viewsController.get('isDataLoaded')) {
  31. dfd.resolve(viewsController.get('ambariViews'));
  32. clearInterval(interval);
  33. }
  34. }, 50);
  35. }
  36. return dfd.promise();
  37. },
  38. loadAmbariViews: function () {
  39. if (!App.router.get('loggedIn')) {
  40. return;
  41. }
  42. App.ajax.send({
  43. name: 'views.info',
  44. sender: this,
  45. success: 'loadAmbariViewsSuccess',
  46. error: 'loadAmbariViewsError'
  47. });
  48. },
  49. loadAmbariViewsSuccess: function (data, opt, params) {
  50. if (data.items.length) {
  51. App.ajax.send({
  52. name: 'views.instances',
  53. sender: this,
  54. success: 'loadViewInstancesSuccess',
  55. error: 'loadViewInstancesError'
  56. });
  57. } else {
  58. this.set('ambariViews', []);
  59. this.set('isDataLoaded', true);
  60. }
  61. },
  62. loadAmbariViewsError: function () {
  63. this.set('ambariViews', []);
  64. this.set('isDataLoaded', true);
  65. },
  66. loadViewInstancesSuccess: function (data, opt, params) {
  67. this.set('ambariViews', []);
  68. var instances = [];
  69. data.items.forEach(function (view) {
  70. view.versions.forEach(function (version) {
  71. version.instances.forEach(function (instance) {
  72. var current_instance = Em.Object.create({
  73. iconPath: instance.ViewInstanceInfo.icon_path || "/img/ambari-view-default.png",
  74. label: instance.ViewInstanceInfo.label || version.ViewVersionInfo.label || instance.ViewInstanceInfo.view_name,
  75. visible: instance.ViewInstanceInfo.visible || false,
  76. version: instance.ViewInstanceInfo.version,
  77. description: instance.ViewInstanceInfo.description || Em.I18n.t('views.main.instance.noDescription'),
  78. viewName: instance.ViewInstanceInfo.view_name,
  79. instanceName: instance.ViewInstanceInfo.instance_name,
  80. href: instance.ViewInstanceInfo.context_path
  81. });
  82. if( current_instance.visible ){
  83. instances.push(current_instance);
  84. }
  85. }, this);
  86. }, this);
  87. }, this);
  88. this.get('ambariViews').pushObjects(instances);
  89. this.set('isDataLoaded', true);
  90. },
  91. loadViewInstancesError: function () {
  92. this.set('ambariViews', []);
  93. this.set('isDataLoaded', true);
  94. },
  95. setView: function(event) {
  96. if(event.context){
  97. App.router.route('main/views/' + event.context.viewName + '/' + event.context.version + '/' + event.context.instanceName);
  98. }
  99. }
  100. });