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