stack_versions_controller.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.MainStackVersionsController = Em.ArrayController.extend({
  20. name: 'mainStackVersionsController',
  21. content: App.StackVersion.find(),
  22. timeoutRef: null,
  23. isPolling: false,
  24. dataIsLoaded: false,
  25. /**
  26. * path to the mock json
  27. * @type {String}
  28. */
  29. mockUrl: '/data/stack_versions/stack_version_all.json',
  30. /**
  31. * api to get ClusterStackVersions with repository_versions (use to init data load)
  32. * @type {String}
  33. */
  34. realUrl: function () {
  35. return App.apiPrefix + '/clusters/' + App.get('clusterName') + '/stack_versions?fields=*,repository_versions/*,repository_versions/operatingSystems/repositories/*';
  36. }.property('App.clusterName'),
  37. /**
  38. * api to get ClusterStackVersions without repository_versions (use to update data)
  39. * @type {String}
  40. */
  41. realUpdateUrl: function () {
  42. return App.apiPrefix + '/clusters/' + App.get('clusterName') + '/stack_versions?fields=ClusterStackVersions/*';
  43. }.property('App.clusterName'),
  44. /**
  45. * request latest data from server and update content
  46. * @method doPolling
  47. */
  48. doPolling: function () {
  49. var self = this;
  50. this.set('timeoutRef', setTimeout(function () {
  51. if (self.get('isPolling')) {
  52. self.loadStackVersionsToModel(self.get('dataIsLoaded')).done(function () {
  53. self.doPolling();
  54. })
  55. }
  56. }, App.componentsUpdateInterval));
  57. },
  58. /**
  59. * load all data components required by stack version table
  60. * @return {*}
  61. * @method load
  62. */
  63. load: function () {
  64. var dfd = $.Deferred();
  65. var self = this;
  66. this.loadStackVersionsToModel().done(function () {
  67. self.set('dataIsLoaded', true);
  68. dfd.resolve();
  69. });
  70. return dfd.promise();
  71. },
  72. /**
  73. * get stack versions from server and push it to model
  74. * @return {*}
  75. * @method loadStackVersionsToModel
  76. */
  77. loadStackVersionsToModel: function (isUpdate) {
  78. var dfd = $.Deferred();
  79. App.HttpClient.get(this.getUrl(isUpdate), App.stackVersionMapper, {
  80. complete: function () {
  81. dfd.resolve();
  82. }
  83. });
  84. return dfd.promise();
  85. },
  86. /**
  87. * returns api url to get clusteStackVersion wirh repositoryVersion
  88. * or just clustrerStackVersion if only updates are requested
  89. * or mock json if testmode is on
  90. * @param isUpdate true if data needs to be updated
  91. * @returns {String}
  92. * @method getUrl
  93. */
  94. getUrl: function (isUpdate) {
  95. return App.get('testMode') ? this.get('mockUrl') :
  96. isUpdate ? this.get('realUpdateUrl') : this.get('realUrl');
  97. },
  98. /**
  99. * goes to the hosts page with content filtered by repo_version_name and repo_version_state
  100. * @param version
  101. * @param state
  102. * @method filterHostsByStack
  103. */
  104. filterHostsByStack: function (version, state) {
  105. if (!version || !state)
  106. return;
  107. App.router.get('mainHostController').filterByStack(version, state);
  108. App.router.get('mainHostController').set('showFilterConditionsFirstLoad', true);
  109. App.router.transitionTo('hosts.index');
  110. },
  111. /**
  112. * shows popup with listed hosts wich has current state of hostStackVersion
  113. * @param event
  114. * @returns {*|void}
  115. * @method showHosts
  116. */
  117. showHosts: function(event) {
  118. var self = this;
  119. var status = event.currentTarget.title.toCapital();
  120. var version = event.contexts[0];
  121. var hosts = event.contexts[1];
  122. if (hosts.length) {
  123. return App.ModalPopup.show({
  124. bodyClass: Ember.View.extend({
  125. title: Em.I18n.t('admin.stackVersions.hosts.popup.title').format(version, status, hosts.length),
  126. template: Em.Handlebars.compile('<h4>{{view.title}}</h4><span class="limited-height-2">'+ hosts.join('<br/>') + '</span>')
  127. }),
  128. header: Em.I18n.t('admin.stackVersions.hosts.popup.header').format(status),
  129. primary: Em.I18n.t('admin.stackVersions.hosts.popup.primary'),
  130. secondary: Em.I18n.t('common.close'),
  131. onPrimary: function() {
  132. this.hide();
  133. self.filterHostsByStack(version, status);
  134. }
  135. });
  136. }
  137. }
  138. });