views_controller.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 App.ajax.send({
  41. name: 'views.info',
  42. sender: this,
  43. success: 'loadAmbariViewsSuccess',
  44. error: 'loadAmbariViewsError'
  45. });
  46. }
  47. },
  48. loadAmbariViewsSuccess: function (data, opt, params) {
  49. if (data.items.length) {
  50. App.ajax.send({
  51. name: 'views.instances',
  52. sender: this,
  53. success: 'loadViewInstancesSuccess',
  54. error: 'loadViewInstancesError'
  55. });
  56. } else {
  57. this.set('ambariViews', []);
  58. this.set('isDataLoaded', true);
  59. }
  60. },
  61. loadAmbariViewsError: function () {
  62. this.set('ambariViews', []);
  63. this.set('isDataLoaded', true);
  64. },
  65. loadViewInstancesSuccess: function (data, opt, params) {
  66. this.set('ambariViews', []);
  67. var instances = [];
  68. data.items.forEach(function (view) {
  69. view.versions.forEach(function (version) {
  70. version.instances.forEach(function (instance) {
  71. var current_instance = Em.Object.create({
  72. iconPath: instance.ViewInstanceInfo.icon_path || "/img/ambari-view-default.png",
  73. label: instance.ViewInstanceInfo.label || version.ViewVersionInfo.label || instance.ViewInstanceInfo.view_name,
  74. visible: instance.ViewInstanceInfo.visible || false,
  75. version: instance.ViewInstanceInfo.version,
  76. description: instance.ViewInstanceInfo.description || Em.I18n.t('views.main.instance.noDescription'),
  77. viewName: instance.ViewInstanceInfo.view_name,
  78. instanceName: instance.ViewInstanceInfo.instance_name,
  79. href: instance.ViewInstanceInfo.context_path + "/"
  80. });
  81. if (current_instance.visible) {
  82. instances.push(current_instance);
  83. }
  84. }, this);
  85. }, this);
  86. }, this);
  87. this.get('ambariViews').pushObjects(instances);
  88. this.set('isDataLoaded', true);
  89. },
  90. loadViewInstancesError: function () {
  91. this.set('ambariViews', []);
  92. this.set('isDataLoaded', true);
  93. },
  94. setView: function (event) {
  95. if (event.context) {
  96. App.router.route('main/views/' + event.context.viewName + '/' + event.context.version + '/' + event.context.instanceName);
  97. }
  98. }
  99. });