application.js 4.3 KB

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