application.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. clusterDisplayName: function () {
  25. var name = this.get('clusterName');
  26. var displayName = name.length > 13 ? name.substr(0, 10) + "..." : name;
  27. return displayName;
  28. }.property('clusterName'),
  29. isClusterDataLoaded: function() {
  30. return App.router.get('clusterController.isLoaded') && App.router.get('loggedIn');
  31. }.property('App.router.clusterController.isLoaded','App.router.loggedIn'),
  32. init: function(){
  33. this._super();
  34. },
  35. loadShowBgChecked: function () {
  36. if (App.testMode) {
  37. return true;
  38. } else {
  39. this.getUserPref(this.persistKey());
  40. var currentPrefObject = this.get('currentPrefObject');
  41. if (currentPrefObject != null) {
  42. return currentPrefObject;
  43. } else {
  44. // post persist
  45. this.postUserPref(this.persistKey(), true);
  46. return true;
  47. }
  48. }
  49. },
  50. persistKey: function () {
  51. var loginName = App.router.get('loginName');
  52. return 'admin-settings-show-bg-' + loginName;
  53. },
  54. currentPrefObject: null,
  55. /**
  56. * get persist value from server with persistKey
  57. */
  58. getUserPref: function (key) {
  59. var self = this;
  60. var url = App.apiPrefix + '/persist/' + key;
  61. jQuery.ajax(
  62. {
  63. url: url,
  64. context: this,
  65. async: false,
  66. success: function (response) {
  67. if (response) {
  68. var value = jQuery.parseJSON(response);
  69. console.log('Got persist value from server with key: ' + key + '. Value is: ' + response);
  70. self.set('currentPrefObject', value);
  71. return value;
  72. }
  73. },
  74. error: function (xhr) {
  75. // this user is first time login
  76. if (xhr.status == 404) {
  77. console.log('Persist did NOT find the key: '+ key);
  78. self.set('currentPrefObject', null);
  79. return null;
  80. }
  81. },
  82. statusCode: require('data/statusCodes')
  83. }
  84. );
  85. },
  86. /**
  87. * post persist key/value to server, value is object
  88. */
  89. postUserPref: function (key, value) {
  90. var url = App.apiPrefix + '/persist/';
  91. var keyValuePair = {};
  92. keyValuePair[key] = JSON.stringify(value);
  93. jQuery.ajax({
  94. async: false,
  95. context: this,
  96. type: "POST",
  97. url: url,
  98. data: JSON.stringify(keyValuePair),
  99. beforeSend: function () {
  100. console.log('BeforeSend to persist: persistKeyValues', keyValuePair);
  101. }
  102. });
  103. },
  104. showSettingsPopup: function() {
  105. var self = this;
  106. var initValue = this.loadShowBgChecked();
  107. var curValue = null;
  108. App.ModalPopup.show({
  109. header: Em.I18n.t('common.userSettings'),
  110. bodyClass: Em.View.extend({
  111. templateName: require('templates/common/settings'),
  112. isShowBgChecked: initValue,
  113. updateValue: function () {
  114. curValue = this.get('isShowBgChecked');
  115. }.observes('isShowBgChecked')
  116. }),
  117. primary: Em.I18n.t('common.save'),
  118. onPrimary: function() {
  119. if (curValue == null) {
  120. curValue = initValue;
  121. }
  122. var key = self.persistKey();
  123. if (!App.testMode) {
  124. self.postUserPref(key, curValue);
  125. }
  126. this.hide();
  127. },
  128. onSecondary: function() {
  129. this.hide();
  130. }
  131. })
  132. }
  133. });