application.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.ApplicationController = Em.Controller.extend({
  20. name: 'applicationController',
  21. clusterName: function () {
  22. return (App.router.get('clusterController.clusterName') || 'My Cluster');
  23. }.property('App.router.clusterController.clusterName'),
  24. ambariVersion: function () {
  25. return (App.router.get('clusterController.ambariVersion') || Em.I18n.t('common.notAvailable'));
  26. }.property('App.router.clusterController.ambariVersion'),
  27. clusterDisplayName: function () {
  28. var name = this.get('clusterName');
  29. return name.length > 13 ? name.substr(0, 10) + "..." : name;
  30. }.property('clusterName'),
  31. isClusterDataLoaded: function() {
  32. return App.router.get('clusterController.isLoaded') && App.router.get('loggedIn');
  33. }.property('App.router.clusterController.isLoaded','App.router.loggedIn'),
  34. init: function(){
  35. this._super();
  36. },
  37. dataLoading: function () {
  38. var dfd = $.Deferred();
  39. var self = this;
  40. this.getUserPref(this.persistKey()).done(function () {
  41. var curPref = self.get('currentPrefObject');
  42. self.set('currentPrefObject', null);
  43. dfd.resolve(curPref);
  44. });
  45. return dfd.promise();
  46. },
  47. persistKey: function (loginName) {
  48. if (App.get('testMode')) {
  49. return 'admin_settings_show_bg';
  50. }
  51. if (!loginName)
  52. loginName = App.router.get('loginName');
  53. return 'admin-settings-show-bg-' + loginName;
  54. },
  55. currentPrefObject: null,
  56. /**
  57. * get persist value from server with persistKey
  58. */
  59. getUserPref: function(key){
  60. return App.ajax.send({
  61. name: 'settings.get.user_pref',
  62. sender: this,
  63. data: {
  64. key: key
  65. },
  66. success: 'getUserPrefSuccessCallback',
  67. error: 'getUserPrefErrorCallback'
  68. });
  69. },
  70. getUserPrefSuccessCallback: function (response, request, data) {
  71. if (response != null) {
  72. console.log('Got persist value from server with key ' + data.key + '. Value is: ' + response);
  73. this.set('currentPrefObject', response);
  74. return response;
  75. }
  76. },
  77. getUserPrefErrorCallback: function (request, ajaxOptions, error) {
  78. // this user is first time login
  79. if (request.status == 404) {
  80. console.log('Persist did NOT find the key');
  81. this.set('currentPrefObject', true);
  82. this.postUserPref(this.persistKey(), true);
  83. return true;
  84. }
  85. },
  86. /**
  87. * post persist key/value to server, value is object
  88. */
  89. postUserPref: function (key, value) {
  90. var keyValuePair = {};
  91. keyValuePair[key] = JSON.stringify(value);
  92. App.ajax.send({
  93. 'name': 'settings.post.user_pref',
  94. 'sender': this,
  95. 'beforeSend': 'postUserPrefBeforeSend',
  96. 'data': {
  97. 'keyValuePair': keyValuePair
  98. }
  99. });
  100. },
  101. postUserPrefBeforeSend: function(request, ajaxOptions, data){
  102. console.log('BeforeSend to persist: persistKeyValues', data.keyValuePair);
  103. },
  104. showSettingsPopup: function() {
  105. // Settings only for admins
  106. if (!App.get('isAdmin')) return;
  107. var self = this;
  108. var curValue = null;
  109. this.dataLoading().done(function (initValue) {
  110. App.ModalPopup.show({
  111. header: Em.I18n.t('common.userSettings'),
  112. bodyClass: Em.View.extend({
  113. templateName: require('templates/common/settings'),
  114. isNotShowBgChecked: !initValue,
  115. updateValue: function () {
  116. curValue = !this.get('isNotShowBgChecked');
  117. }.observes('isNotShowBgChecked')
  118. }),
  119. primary: Em.I18n.t('common.save'),
  120. onPrimary: function() {
  121. if (curValue == null) {
  122. curValue = initValue;
  123. }
  124. var key = self.persistKey();
  125. if (!App.testMode) {
  126. self.postUserPref(key, curValue);
  127. }
  128. this.hide();
  129. }
  130. })
  131. });
  132. },
  133. showAboutPopup: function() {
  134. var self = this;
  135. App.ModalPopup.show({
  136. header: Em.I18n.t('common.aboutAmbari'),
  137. secondary: false,
  138. bodyClass: Em.View.extend({
  139. templateName: require('templates/common/about'),
  140. ambariVersion: this.get('ambariVersion')
  141. })
  142. });
  143. }
  144. });